Annotation of loncom/interface/domainprefs.pm, revision 1.267

1.1       raeburn     1: # The LearningOnline Network with CAPA
                      2: # Handler to set domain-wide configuration settings
                      3: #
1.267   ! raeburn     4: # $Id: domainprefs.pm,v 1.266 2015/06/15 20:11:56 raeburn Exp $
1.2       albertel    5: #
1.1       raeburn     6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA#
                     23: # /home/httpd/html/adm/gpl.txt
                     24: #
                     25: # http://www.lon-capa.org/
                     26: #
                     27: #
                     28: ###############################################################
                     29: ##############################################################
                     30: 
1.101     raeburn    31: =pod
                     32: 
                     33: =head1 NAME
                     34: 
                     35: Apache::domainprefs.pm
                     36: 
                     37: =head1 SYNOPSIS
                     38: 
                     39: Handles configuration of a LON-CAPA domain.  
                     40: 
                     41: This is part of the LearningOnline Network with CAPA project
                     42: described at http://www.lon-capa.org.
                     43: 
                     44: 
                     45: =head1 OVERVIEW
                     46: 
                     47: Each institution using LON-CAPA will typically have a single domain designated 
1.183     bisitz     48: for use by individuals affiliated with the institution.  Accordingly, each domain
1.101     raeburn    49: may define a default set of logos and a color scheme which can be used to "brand"
                     50: the LON-CAPA instance. In addition, an institution will typically have a language
                     51: and timezone which are used for the majority of courses.
                     52: 
                     53: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
                     54: host of other domain-wide settings which determine the types of functionality
                     55: available to users and courses in the domain.
                     56: 
                     57: There is also a mechanism to configure cataloging of courses in the domain, and
                     58: controls on the operation of automated processes which govern such things as
                     59: roster updates, user directory updates and processing of course requests.
                     60: 
                     61: The domain coordination manual which is built dynamically on install/update of 
                     62: LON-CAPA from the relevant help items provides more information about domain 
                     63: configuration.
                     64: 
                     65: Most of the domain settings are stored in the configuration.db GDBM file which is
                     66: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
                     67: where $dom is the domain.  The configuration.db stores settings in a number of 
                     68: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
                     69: the domain as files (e.g., image files for logos etc., or plain text files for
                     70: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
                     71: session hosted on the primary library server in the domain, as these files are 
                     72: stored in author space belonging to a special $dom-domainconfig user.   
                     73: 
                     74: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
                     75: the current settings, and provides an interface to make modifications.
                     76: 
                     77: =head1 SUBROUTINES
                     78: 
                     79: =over
                     80: 
                     81: =item print_quotas()
                     82: 
                     83: Inputs: 4 
                     84: 
                     85: $dom,$settings,$rowtotal,$action.
                     86: 
                     87: $dom is the domain, $settings is a reference to a hash of current settings for
                     88: the current context, $rowtotal is a reference to the scalar used to record the 
1.210     raeburn    89: number of rows displayed on the page, and $action is the context (quotas, 
1.163     raeburn    90: requestcourses or requestauthor).
1.101     raeburn    91: 
                     92: The print_quotas routine was orginally created to display/store information
                     93: about default quota sizes for portfolio spaces for the different types of 
                     94: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
                     95: but is now also used to manage availability of user tools: 
                     96: i.e., blogs, aboutme page, and portfolios, and the course request tool,
1.197     raeburn    97: used by course owners to request creation of a course, and to display/store
1.223     bisitz     98: default quota sizes for Authoring Spaces.
1.101     raeburn    99: 
                    100: Outputs: 1
                    101: 
                    102: $datatable  - HTML containing form elements which allow settings to be changed. 
                    103: 
                    104: In the case of course requests, radio buttons are displayed for each institutional
                    105: affiliate type (and also default, and _LC_adv) for each of the course types 
1.216     raeburn   106: (official, unofficial, community, and textbook).  In each case the radio buttons 
                    107: allow the selection of one of four values:
1.101     raeburn   108: 
1.104     raeburn   109: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
1.101     raeburn   110: which have the following effects:
                    111: 
                    112: 0
                    113: 
                    114: =over
                    115: 
                    116: - course requests are not allowed for this course types/affiliation
                    117: 
                    118: =back
                    119: 
1.104     raeburn   120: approval 
1.101     raeburn   121: 
                    122: =over 
                    123: 
                    124: - course requests must be approved by a Doman Coordinator in the 
                    125: course's domain
                    126: 
                    127: =back
                    128: 
                    129: validate 
                    130: 
                    131: =over
                    132: 
                    133: - an institutional validation (e.g., check requestor is instructor
                    134: of record) needs to be passed before the course will be created.  The required
                    135: validation is in localenroll.pm on the primary library server for the course 
                    136: domain.
                    137: 
                    138: =back
                    139: 
                    140: autolimit 
                    141: 
                    142: =over
                    143:  
1.143     raeburn   144: - course requests will be processed automatically up to a limit of
1.101     raeburn   145: N requests for the course type for the particular requestor.
                    146: If N is undefined, there is no limit to the number of course requests
                    147: which a course owner may submit and have processed automatically. 
                    148: 
                    149: =back
                    150: 
                    151: =item modify_quotas() 
                    152: 
                    153: =back
                    154: 
                    155: =cut
                    156: 
1.1       raeburn   157: package Apache::domainprefs;
                    158: 
                    159: use strict;
                    160: use Apache::Constants qw(:common :http);
                    161: use Apache::lonnet;
                    162: use Apache::loncommon();
                    163: use Apache::lonhtmlcommon();
                    164: use Apache::lonlocal;
1.43      raeburn   165: use Apache::lonmsg();
1.91      raeburn   166: use Apache::lonconfigsettings;
1.232     raeburn   167: use Apache::lonuserutils();
1.235     raeburn   168: use Apache::loncoursequeueadmin();
1.69      raeburn   169: use LONCAPA qw(:DEFAULT :match);
1.6       raeburn   170: use LONCAPA::Enrollment;
1.81      raeburn   171: use LONCAPA::lonauthcgi();
1.9       raeburn   172: use File::Copy;
1.43      raeburn   173: use Locale::Language;
1.62      raeburn   174: use DateTime::TimeZone;
1.68      raeburn   175: use DateTime::Locale;
1.267   ! raeburn   176: use Time::HiRes qw( sleep );
1.1       raeburn   177: 
1.155     raeburn   178: my $registered_cleanup;
                    179: my $modified_urls;
                    180: 
1.1       raeburn   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: 
1.91      raeburn   189:     my $context = 'domain';
1.1       raeburn   190:     my $dom = $env{'request.role.domain'};
1.5       albertel  191:     my $domdesc = &Apache::lonnet::domain($dom,'description');
1.1       raeburn   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:     }
1.155     raeburn   200: 
                    201:     $registered_cleanup=0;
                    202:     @{$modified_urls}=();
                    203: 
1.1       raeburn   204:     &Apache::lonhtmlcommon::clear_breadcrumbs();
                    205:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.58      raeburn   206:                                             ['phase','actions']);
1.30      raeburn   207:     my $phase = 'pickactions';
1.3       raeburn   208:     if ( exists($env{'form.phase'}) ) {
                    209:         $phase = $env{'form.phase'};
                    210:     }
1.150     raeburn   211:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
1.3       raeburn   212:     my %domconfig =
1.6       raeburn   213:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
1.125     raeburn   214:                 'quotas','autoenroll','autoupdate','autocreate',
                    215:                 'directorysrch','usercreation','usermodification',
                    216:                 'contacts','defaults','scantron','coursecategories',
                    217:                 'serverstatuses','requestcourses','helpsettings',
1.163     raeburn   218:                 'coursedefaults','usersessions','loadbalancing',
1.267   ! raeburn   219:                 'requestauthor','selfenrollment','inststatus',
        !           220:                 'ltitools'],$dom);
1.43      raeburn   221:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
1.125     raeburn   222:                        'autoupdate','autocreate','directorysrch','contacts',
1.224     raeburn   223:                        'usercreation','selfcreation','usermodification','scantron',
1.163     raeburn   224:                        'requestcourses','requestauthor','coursecategories',
1.267   ! raeburn   225:                        'serverstatuses','helpsettings','coursedefaults',
        !           226:                        'ltitools','selfenrollment','usersessions');
1.171     raeburn   227:     my %existing;
                    228:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
                    229:         %existing = %{$domconfig{'loadbalancing'}};
                    230:     }
                    231:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
1.150     raeburn   232:         push(@prefs_order,'loadbalancing');
                    233:     }
1.30      raeburn   234:     my %prefs = (
                    235:         'rolecolors' =>
                    236:                    { text => 'Default color schemes',
1.67      raeburn   237:                      help => 'Domain_Configuration_Color_Schemes',
1.30      raeburn   238:                      header => [{col1 => 'Student Settings',
                    239:                                  col2 => '',},
                    240:                                 {col1 => 'Coordinator Settings',
                    241:                                  col2 => '',},
                    242:                                 {col1 => 'Author Settings',
                    243:                                  col2 => '',},
                    244:                                 {col1 => 'Administrator Settings',
                    245:                                  col2 => '',}],
1.230     raeburn   246:                       print => \&print_rolecolors,
                    247:                       modify => \&modify_rolecolors,
1.30      raeburn   248:                     },
1.110     raeburn   249:         'login' =>
1.30      raeburn   250:                     { text => 'Log-in page options',
1.67      raeburn   251:                       help => 'Domain_Configuration_Login_Page',
1.168     raeburn   252:                       header => [{col1 => 'Log-in Page Items',
                    253:                                   col2 => '',},
                    254:                                  {col1 => 'Log-in Help',
1.256     raeburn   255:                                   col2 => 'Value'},
                    256:                                  {col1 => 'Custom HTML in document head',
1.168     raeburn   257:                                   col2 => 'Value'}],
1.230     raeburn   258:                       print => \&print_login,
                    259:                       modify => \&modify_login,
1.30      raeburn   260:                     },
1.43      raeburn   261:         'defaults' => 
1.236     raeburn   262:                     { text => 'Default authentication/language/timezone/portal/types',
1.67      raeburn   263:                       help => 'Domain_Configuration_LangTZAuth',
1.43      raeburn   264:                       header => [{col1 => 'Setting',
1.236     raeburn   265:                                   col2 => 'Value'},
                    266:                                  {col1 => 'Institutional user types',
                    267:                                   col2 => 'Assignable to e-mail usernames'}],
1.230     raeburn   268:                       print => \&print_defaults,
                    269:                       modify => \&modify_defaults,
1.43      raeburn   270:                     },
1.30      raeburn   271:         'quotas' => 
1.197     raeburn   272:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
1.67      raeburn   273:                       help => 'Domain_Configuration_Quotas',
1.77      raeburn   274:                       header => [{col1 => 'User affiliation',
1.72      raeburn   275:                                   col2 => 'Available tools',
1.213     raeburn   276:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
1.230     raeburn   277:                       print => \&print_quotas,
                    278:                       modify => \&modify_quotas,
1.30      raeburn   279:                     },
                    280:         'autoenroll' =>
                    281:                    { text => 'Auto-enrollment settings',
1.67      raeburn   282:                      help => 'Domain_Configuration_Auto_Enrollment',
1.30      raeburn   283:                      header => [{col1 => 'Configuration setting',
                    284:                                  col2 => 'Value(s)'}],
1.230     raeburn   285:                      print => \&print_autoenroll,
                    286:                      modify => \&modify_autoenroll,
1.30      raeburn   287:                    },
                    288:         'autoupdate' => 
                    289:                    { text => 'Auto-update settings',
1.67      raeburn   290:                      help => 'Domain_Configuration_Auto_Updates',
1.30      raeburn   291:                      header => [{col1 => 'Setting',
                    292:                                  col2 => 'Value',},
1.131     raeburn   293:                                 {col1 => 'Setting',
                    294:                                  col2 => 'Affiliation'},
1.43      raeburn   295:                                 {col1 => 'User population',
1.227     bisitz    296:                                  col2 => 'Updatable user data'}],
1.230     raeburn   297:                      print => \&print_autoupdate,
                    298:                      modify => \&modify_autoupdate,
1.30      raeburn   299:                   },
1.125     raeburn   300:         'autocreate' => 
                    301:                   { text => 'Auto-course creation settings',
                    302:                      help => 'Domain_Configuration_Auto_Creation',
                    303:                      header => [{col1 => 'Configuration Setting',
                    304:                                  col2 => 'Value',}],
1.230     raeburn   305:                      print => \&print_autocreate,
                    306:                      modify => \&modify_autocreate,
1.125     raeburn   307:                   },
1.30      raeburn   308:         'directorysrch' => 
                    309:                   { text => 'Institutional directory searches',
1.67      raeburn   310:                     help => 'Domain_Configuration_InstDirectory_Search',
1.30      raeburn   311:                     header => [{col1 => 'Setting',
                    312:                                 col2 => 'Value',}],
1.230     raeburn   313:                     print => \&print_directorysrch,
                    314:                     modify => \&modify_directorysrch,
1.30      raeburn   315:                   },
                    316:         'contacts' =>
                    317:                   { text => 'Contact Information',
1.67      raeburn   318:                     help => 'Domain_Configuration_Contact_Info',
1.30      raeburn   319:                     header => [{col1 => 'Setting',
                    320:                                 col2 => 'Value',}],
1.230     raeburn   321:                     print => \&print_contacts,
                    322:                     modify => \&modify_contacts,
1.30      raeburn   323:                   },
                    324:         'usercreation' => 
                    325:                   { text => 'User creation',
1.67      raeburn   326:                     help => 'Domain_Configuration_User_Creation',
1.43      raeburn   327:                     header => [{col1 => 'Format rule type',
                    328:                                 col2 => 'Format rules in force'},
1.34      raeburn   329:                                {col1 => 'User account creation',
                    330:                                 col2 => 'Usernames which may be created',},
1.30      raeburn   331:                                {col1 => 'Context',
1.43      raeburn   332:                                 col2 => 'Assignable authentication types'}],
1.230     raeburn   333:                     print => \&print_usercreation,
                    334:                     modify => \&modify_usercreation,
1.30      raeburn   335:                   },
1.224     raeburn   336:         'selfcreation' => 
                    337:                   { text => 'Users self-creating accounts',
                    338:                     help => 'Domain_Configuration_Self_Creation', 
                    339:                     header => [{col1 => 'Self-creation with institutional username',
                    340:                                 col2 => 'Enabled?'},
                    341:                                {col1 => 'Institutional user type (login/SSO self-creation)',
                    342:                                 col2 => 'Information user can enter'},
                    343:                                {col1 => 'Self-creation with e-mail as username',
                    344:                                 col2 => 'Settings'}],
1.230     raeburn   345:                     print => \&print_selfcreation,
                    346:                     modify => \&modify_selfcreation,
1.224     raeburn   347:                   },
1.69      raeburn   348:         'usermodification' =>
1.33      raeburn   349:                   { text => 'User modification',
1.67      raeburn   350:                     help => 'Domain_Configuration_User_Modification',
1.33      raeburn   351:                     header => [{col1 => 'Target user has role',
1.227     bisitz    352:                                 col2 => 'User information updatable in author context'},
1.33      raeburn   353:                                {col1 => 'Target user has role',
1.227     bisitz    354:                                 col2 => 'User information updatable in course context'}],
1.230     raeburn   355:                     print => \&print_usermodification,
                    356:                     modify => \&modify_usermodification,
1.33      raeburn   357:                   },
1.69      raeburn   358:         'scantron' =>
1.95      www       359:                   { text => 'Bubblesheet format file',
1.67      raeburn   360:                     help => 'Domain_Configuration_Scantron_Format',
1.46      raeburn   361:                     header => [ {col1 => 'Item',
                    362:                                  col2 => '',
                    363:                               }],
1.230     raeburn   364:                     print => \&print_scantron,
                    365:                     modify => \&modify_scantron,
1.46      raeburn   366:                   },
1.86      raeburn   367:         'requestcourses' => 
                    368:                  {text => 'Request creation of courses',
                    369:                   help => 'Domain_Configuration_Request_Courses',
                    370:                   header => [{col1 => 'User affiliation',
1.102     raeburn   371:                               col2 => 'Availability/Processing of requests',},
                    372:                              {col1 => 'Setting',
1.216     raeburn   373:                               col2 => 'Value'},
                    374:                              {col1 => 'Available textbooks',
1.235     raeburn   375:                               col2 => ''},
1.242     raeburn   376:                              {col1 => 'Available templates',
                    377:                               col2 => ''},
1.235     raeburn   378:                              {col1 => 'Validation (not official courses)',
                    379:                               col2 => 'Value'},],
1.230     raeburn   380:                   print => \&print_quotas,
                    381:                   modify => \&modify_quotas,
1.86      raeburn   382:                  },
1.163     raeburn   383:         'requestauthor' =>
1.223     bisitz    384:                  {text => 'Request Authoring Space',
1.163     raeburn   385:                   help => 'Domain_Configuration_Request_Author',
                    386:                   header => [{col1 => 'User affiliation',
                    387:                               col2 => 'Availability/Processing of requests',},
                    388:                              {col1 => 'Setting',
                    389:                               col2 => 'Value'}],
1.230     raeburn   390:                   print => \&print_quotas,
                    391:                   modify => \&modify_quotas,
1.163     raeburn   392:                  },
1.69      raeburn   393:         'coursecategories' =>
1.120     raeburn   394:                   { text => 'Cataloging of courses/communities',
1.67      raeburn   395:                     help => 'Domain_Configuration_Cataloging_Courses',
1.238     raeburn   396:                     header => [{col1 => 'Catalog type/availability',
                    397:                                 col2 => '',},
                    398:                                {col1 => 'Category settings for standard catalog',
1.57      raeburn   399:                                 col2 => '',},
                    400:                                {col1 => 'Categories',
                    401:                                 col2 => '',
                    402:                                }],
1.230     raeburn   403:                     print => \&print_coursecategories,
                    404:                     modify => \&modify_coursecategories,
1.69      raeburn   405:                   },
                    406:         'serverstatuses' =>
1.77      raeburn   407:                  {text   => 'Access to server status pages',
1.69      raeburn   408:                   help   => 'Domain_Configuration_Server_Status',
                    409:                   header => [{col1 => 'Status Page',
                    410:                               col2 => 'Other named users',
                    411:                               col3 => 'Specific IPs',
                    412:                             }],
1.230     raeburn   413:                   print => \&print_serverstatuses,
                    414:                   modify => \&modify_serverstatuses,
1.69      raeburn   415:                  },
1.118     jms       416:         'helpsettings' =>
                    417:                  {text   => 'Help page settings',
                    418:                   help   => 'Domain_Configuration_Help_Settings',
1.166     raeburn   419:                   header => [{col1 => 'Help Settings (logged-in users)',
                    420:                               col2 => 'Value'}],
1.230     raeburn   421:                   print  => \&print_helpsettings,
                    422:                   modify => \&modify_helpsettings,
1.118     jms       423:                  },
1.121     raeburn   424:         'coursedefaults' => 
                    425:                  {text => 'Course/Community defaults',
                    426:                   help => 'Domain_Configuration_Course_Defaults',
1.139     raeburn   427:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
                    428:                               col2 => 'Value',},
                    429:                              {col1 => 'Defaults which can be overridden for each course by a DC',
                    430:                               col2 => 'Value',},],
1.230     raeburn   431:                   print => \&print_coursedefaults,
                    432:                   modify => \&modify_coursedefaults,
1.121     raeburn   433:                  },
1.231     raeburn   434:         'selfenrollment' => 
                    435:                  {text   => 'Self-enrollment in Course/Community',
                    436:                   help   => 'Domain_Configuration_Selfenrollment',
                    437:                   header => [{col1 => 'Configuration Rights',
                    438:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
                    439:                              {col1 => 'Defaults',
                    440:                               col2 => 'Value'},
                    441:                              {col1 => 'Self-enrollment validation (optional)',
                    442:                               col2 => 'Value'},],
                    443:                   print => \&print_selfenrollment,
                    444:                   modify => \&modify_selfenrollment,
                    445:                  },
1.120     raeburn   446:         'privacy' => 
                    447:                  {text   => 'User Privacy',
                    448:                   help   => 'Domain_Configuration_User_Privacy',
                    449:                   header => [{col1 => 'Setting',
                    450:                               col2 => 'Value',}],
1.230     raeburn   451:                   print => \&print_privacy,
                    452:                   modify => \&modify_privacy,
1.120     raeburn   453:                  },
1.141     raeburn   454:         'usersessions' =>
1.145     raeburn   455:                  {text  => 'User session hosting/offloading',
1.137     raeburn   456:                   help  => 'Domain_Configuration_User_Sessions',
1.145     raeburn   457:                   header => [{col1 => 'Domain server',
                    458:                               col2 => 'Servers to offload sessions to when busy'},
                    459:                              {col1 => 'Hosting of users from other domains',
1.137     raeburn   460:                               col2 => 'Rules'},
                    461:                              {col1 => "Hosting domain's own users elsewhere",
                    462:                               col2 => 'Rules'}],
1.230     raeburn   463:                   print => \&print_usersessions,
                    464:                   modify => \&modify_usersessions,
1.137     raeburn   465:                  },
1.150     raeburn   466:          'loadbalancing' =>
1.185     raeburn   467:                  {text  => 'Dedicated Load Balancer(s)',
1.150     raeburn   468:                   help  => 'Domain_Configuration_Load_Balancing',
1.171     raeburn   469:                   header => [{col1 => 'Balancers',
1.150     raeburn   470:                               col2 => 'Default destinations',
1.183     bisitz    471:                               col3 => 'User affiliation',
1.150     raeburn   472:                               col4 => 'Overrides'},
                    473:                             ],
1.230     raeburn   474:                   print => \&print_loadbalancing,
                    475:                   modify => \&modify_loadbalancing,
1.150     raeburn   476:                  },
1.267   ! raeburn   477:          'ltitools' => 
        !           478:                  {text => 'External Tools (LTI)',
        !           479:                   help => 'Domain_configuration_LTI_Tools',
        !           480:                   header => [{col1 => 'Setting',
        !           481:                               col2 => 'Value',}],
        !           482:                   print => \&print_ltitools,
        !           483:                   modify => \&modify_ltitools,
        !           484:                  },
        !           485:  
1.3       raeburn   486:     );
1.110     raeburn   487:     if (keys(%servers) > 1) {
                    488:         $prefs{'login'}  = { text   => 'Log-in page options',
                    489:                              help   => 'Domain_Configuration_Login_Page',
                    490:                             header => [{col1 => 'Log-in Service',
                    491:                                         col2 => 'Server Setting',},
                    492:                                        {col1 => 'Log-in Page Items',
1.168     raeburn   493:                                         col2 => ''},
                    494:                                        {col1 => 'Log-in Help',
1.256     raeburn   495:                                         col2 => 'Value'},
                    496:                                        {col1 => 'Custom HTML in document head',
1.168     raeburn   497:                                         col2 => 'Value'}],
1.230     raeburn   498:                             print => \&print_login,
                    499:                             modify => \&modify_login,
1.110     raeburn   500:                            };
                    501:     }
1.174     foxr      502: 
1.6       raeburn   503:     my @roles = ('student','coordinator','author','admin');
1.30      raeburn   504:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
1.3       raeburn   505:     &Apache::lonhtmlcommon::add_breadcrumb
1.30      raeburn   506:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
1.133     raeburn   507:       text=>"Settings to display/modify"});
1.9       raeburn   508:     my $confname = $dom.'-domainconfig';
1.174     foxr      509: 
1.3       raeburn   510:     if ($phase eq 'process') {
1.212     raeburn   511:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
                    512:                                                               \%prefs,\%domconfig,$confname,\@roles);
1.224     raeburn   513:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
1.205     raeburn   514:             $r->rflush();
1.212     raeburn   515:             &devalidate_remote_domconfs($dom,$result);
1.205     raeburn   516:         }
1.30      raeburn   517:     } elsif ($phase eq 'display') {
1.192     raeburn   518:         my $js = &recaptcha_js().
1.236     raeburn   519:                  &toggle_display_js();
1.171     raeburn   520:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
1.152     raeburn   521:             my ($othertitle,$usertypes,$types) =
                    522:                 &Apache::loncommon::sorted_inst_types($dom);
1.171     raeburn   523:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
                    524:                                           $domconfig{'loadbalancing'}).
1.170     raeburn   525:                    &new_spares_js().
                    526:                    &common_domprefs_js().
                    527:                    &Apache::loncommon::javascript_array_indexof();
1.152     raeburn   528:         }
1.216     raeburn   529:         if (grep(/^requestcourses$/,@actions)) {
                    530:             my $javascript_validations;
                    531:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
                    532:             $js .= <<END;
                    533: <script type="text/javascript">
                    534: $javascript_validations
                    535: </script>
                    536: $coursebrowserjs
                    537: END
                    538:         }
1.150     raeburn   539:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
1.1       raeburn   540:     } else {
1.180     raeburn   541: # check if domconfig user exists for the domain.
                    542:         my $servadm = $r->dir_config('lonAdmEMail');
                    543:         my ($configuserok,$author_ok,$switchserver) =
                    544:             &config_check($dom,$confname,$servadm);
                    545:         unless ($configuserok eq 'ok') {
1.181     raeburn   546:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
                    547:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
1.210     raeburn   548:                           $confname).
1.181     raeburn   549:                       '<br />'
                    550:             );
1.180     raeburn   551:             if ($switchserver) {
1.181     raeburn   552:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
                    553:                           '<br />'.
                    554:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
                    555:                           '<br />'.
                    556:                           &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).
                    557:                           '<br />'.
                    558:                           &mt('To do that now, use the following link: [_1]',$switchserver)
                    559:                 );
                    560:             } else {
                    561:                 $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.').
                    562:                           '<br />'.
                    563:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
                    564:                 );
1.180     raeburn   565:             }
                    566:             $r->print(&Apache::loncommon::end_page());
                    567:             return OK;
                    568:         }
1.21      raeburn   569:         if (keys(%domconfig) == 0) {
                    570:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
1.29      raeburn   571:             my @ids=&Apache::lonnet::current_machine_ids();
                    572:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
1.21      raeburn   573:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
1.41      raeburn   574:                 my @loginimages = ('img','logo','domlogo','login');
1.21      raeburn   575:                 my $custom_img_count = 0;
                    576:                 foreach my $img (@loginimages) {
                    577:                     if ($designhash{$dom.'.login.'.$img} ne '') {
                    578:                         $custom_img_count ++;
                    579:                     }
                    580:                 }
                    581:                 foreach my $role (@roles) {
                    582:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
                    583:                         $custom_img_count ++;
                    584:                     }
                    585:                 }
                    586:                 if ($custom_img_count > 0) {
1.94      raeburn   587:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
1.21      raeburn   588:                     my $switch_server = &check_switchserver($dom,$confname);
1.29      raeburn   589:                     $r->print(
                    590:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
                    591:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
                    592:     &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 />'.
                    593:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
                    594:                     if ($switch_server) {
1.30      raeburn   595:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
1.29      raeburn   596:                     }
1.91      raeburn   597:                     $r->print(&Apache::loncommon::end_page());
1.21      raeburn   598:                     return OK;
                    599:                 }
                    600:             }
                    601:         }
1.91      raeburn   602:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
1.3       raeburn   603:     }
                    604:     return OK;
                    605: }
                    606: 
                    607: sub process_changes {
1.205     raeburn   608:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
1.92      raeburn   609:     my %domconfig;
                    610:     if (ref($values) eq 'HASH') {
                    611:         %domconfig = %{$values};
                    612:     }
1.3       raeburn   613:     my $output;
                    614:     if ($action eq 'login') {
1.205     raeburn   615:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
1.6       raeburn   616:     } elsif ($action eq 'rolecolors') {
1.9       raeburn   617:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
1.205     raeburn   618:                                      $lastactref,%domconfig);
1.3       raeburn   619:     } elsif ($action eq 'quotas') {
1.216     raeburn   620:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
1.3       raeburn   621:     } elsif ($action eq 'autoenroll') {
1.205     raeburn   622:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
1.3       raeburn   623:     } elsif ($action eq 'autoupdate') {
                    624:         $output = &modify_autoupdate($dom,%domconfig);
1.125     raeburn   625:     } elsif ($action eq 'autocreate') {
                    626:         $output = &modify_autocreate($dom,%domconfig);
1.23      raeburn   627:     } elsif ($action eq 'directorysrch') {
                    628:         $output = &modify_directorysrch($dom,%domconfig);
1.27      raeburn   629:     } elsif ($action eq 'usercreation') {
1.28      raeburn   630:         $output = &modify_usercreation($dom,%domconfig);
1.224     raeburn   631:     } elsif ($action eq 'selfcreation') {
                    632:         $output = &modify_selfcreation($dom,%domconfig);
1.33      raeburn   633:     } elsif ($action eq 'usermodification') {
                    634:         $output = &modify_usermodification($dom,%domconfig);
1.28      raeburn   635:     } elsif ($action eq 'contacts') {
1.205     raeburn   636:         $output = &modify_contacts($dom,$lastactref,%domconfig);
1.43      raeburn   637:     } elsif ($action eq 'defaults') {
1.212     raeburn   638:         $output = &modify_defaults($dom,$lastactref,%domconfig);
1.46      raeburn   639:     } elsif ($action eq 'scantron') {
1.205     raeburn   640:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
1.48      raeburn   641:     } elsif ($action eq 'coursecategories') {
1.239     raeburn   642:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
1.69      raeburn   643:     } elsif ($action eq 'serverstatuses') {
                    644:         $output = &modify_serverstatuses($dom,%domconfig);
1.86      raeburn   645:     } elsif ($action eq 'requestcourses') {
1.216     raeburn   646:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
1.163     raeburn   647:     } elsif ($action eq 'requestauthor') {
1.216     raeburn   648:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
1.118     jms       649:     } elsif ($action eq 'helpsettings') {
1.122     jms       650:         $output = &modify_helpsettings($r,$dom,$confname,%domconfig);
1.121     raeburn   651:     } elsif ($action eq 'coursedefaults') {
1.212     raeburn   652:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
1.231     raeburn   653:     } elsif ($action eq 'selfenrollment') {
                    654:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
1.137     raeburn   655:     } elsif ($action eq 'usersessions') {
1.212     raeburn   656:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
1.150     raeburn   657:     } elsif ($action eq 'loadbalancing') {
                    658:         $output = &modify_loadbalancing($dom,%domconfig);
1.267   ! raeburn   659:     } elsif ($action eq 'ltitools') {
        !           660:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
1.3       raeburn   661:     }
                    662:     return $output;
                    663: }
                    664: 
                    665: sub print_config_box {
1.9       raeburn   666:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
1.30      raeburn   667:     my $rowtotal = 0;
1.49      raeburn   668:     my $output;
                    669:     if ($action eq 'coursecategories') {
                    670:         $output = &coursecategories_javascript($settings);
1.236     raeburn   671:     } elsif ($action eq 'defaults') {
                    672:         $output = &defaults_javascript($settings); 
1.91      raeburn   673:     }
1.236     raeburn   674:     $output .=
1.30      raeburn   675:          '<table class="LC_nested_outer">
1.3       raeburn   676:           <tr>
1.66      raeburn   677:            <th align="left" valign="middle"><span class="LC_nobreak">'.
                    678:            &mt($item->{text}).'&nbsp;'.
                    679:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
                    680:           '</tr>';
1.30      raeburn   681:     $rowtotal ++;
1.110     raeburn   682:     my $numheaders = 1;
                    683:     if (ref($item->{'header'}) eq 'ARRAY') {
                    684:         $numheaders = scalar(@{$item->{'header'}});
                    685:     }
                    686:     if ($numheaders > 1) {
1.64      raeburn   687:         my $colspan = '';
1.145     raeburn   688:         my $rightcolspan = '';
1.238     raeburn   689:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
1.256     raeburn   690:             (($action eq 'login') && ($numheaders < 4))) {
1.64      raeburn   691:             $colspan = ' colspan="2"';
                    692:         }
1.145     raeburn   693:         if ($action eq 'usersessions') {
                    694:             $rightcolspan = ' colspan="3"'; 
                    695:         }
1.30      raeburn   696:         $output .= '
1.3       raeburn   697:           <tr>
                    698:            <td>
                    699:             <table class="LC_nested">
                    700:              <tr class="LC_info_row">
1.59      bisitz    701:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
1.145     raeburn   702:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
1.30      raeburn   703:              </tr>';
1.69      raeburn   704:         $rowtotal ++;
1.230     raeburn   705:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
1.236     raeburn   706:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
1.230     raeburn   707:             ($action eq 'selfenrollment') || ($action eq 'usersessions')) {
                    708:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
1.57      raeburn   709:         } elsif ($action eq 'coursecategories') {
1.230     raeburn   710:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
1.110     raeburn   711:         } elsif ($action eq 'login') {
1.256     raeburn   712:             if ($numheaders == 4) {
1.168     raeburn   713:                 $colspan = ' colspan="2"';
                    714:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
                    715:             } else {
                    716:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
                    717:             }
1.230     raeburn   718:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
1.163     raeburn   719:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
1.122     jms       720:         } elsif ($action eq 'rolecolors') {
1.30      raeburn   721:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
1.6       raeburn   722:         }
1.30      raeburn   723:         $output .= '
1.6       raeburn   724:            </table>
                    725:           </td>
                    726:          </tr>
                    727:          <tr>
                    728:            <td>
                    729:             <table class="LC_nested">
                    730:              <tr class="LC_info_row">
1.230     raeburn   731:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
1.59      bisitz    732:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
1.30      raeburn   733:              </tr>';
                    734:             $rowtotal ++;
1.230     raeburn   735:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
                    736:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
1.238     raeburn   737:             ($action eq 'usersessions') || ($action eq 'coursecategories')) {
                    738:             if ($action eq 'coursecategories') {
                    739:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
                    740:                 $colspan = ' colspan="2"';
                    741:             } else {
                    742:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
                    743:             }
                    744:             $output .= '
1.63      raeburn   745:            </table>
                    746:           </td>
                    747:          </tr>
                    748:          <tr>
                    749:            <td>
                    750:             <table class="LC_nested">
                    751:              <tr class="LC_info_row">
                    752:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
1.224     raeburn   753:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
1.238     raeburn   754:              </tr>'."\n";
                    755:             if ($action eq 'coursecategories') {
                    756:                 $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
                    757:             } else {
                    758:                 $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
                    759:             }
1.63      raeburn   760:             $rowtotal ++;
1.236     raeburn   761:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
                    762:                   ($action eq 'defaults')) {
1.230     raeburn   763:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
1.110     raeburn   764:         } elsif ($action eq 'login') {
1.256     raeburn   765:             if ($numheaders == 4) {
1.168     raeburn   766:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
                    767:            </table>
                    768:           </td>
                    769:          </tr>
                    770:          <tr>
                    771:            <td>
                    772:             <table class="LC_nested">
                    773:              <tr class="LC_info_row">
                    774:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
1.216     raeburn   775:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
1.168     raeburn   776:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
                    777:                 $rowtotal ++;
                    778:             } else {
                    779:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
                    780:             }
1.256     raeburn   781:             $output .= '
                    782:            </table>
                    783:           </td>
                    784:          </tr>
                    785:          <tr>
                    786:            <td>
                    787:             <table class="LC_nested">
                    788:              <tr class="LC_info_row">';
                    789:             if ($numheaders == 4) {
                    790:                 $output .= '
                    791:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
                    792:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
                    793:              </tr>';
                    794:             } else {
                    795:                 $output .= '
                    796:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
                    797:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
                    798:              </tr>';
                    799:             }
                    800:             $rowtotal ++;
                    801:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
1.102     raeburn   802:         } elsif ($action eq 'requestcourses') {
1.247     raeburn   803:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
                    804:             $rowtotal ++;
                    805:             $output .= &print_studentcode($settings,\$rowtotal).'
1.216     raeburn   806:            </table>
                    807:           </td>
                    808:          </tr>
                    809:          <tr>
                    810:            <td>
                    811:             <table class="LC_nested">
                    812:              <tr class="LC_info_row">
                    813:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
                    814:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
1.242     raeburn   815:                        &textbookcourses_javascript($settings).
                    816:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
                    817:             </table>
                    818:            </td>
                    819:           </tr>
                    820:          <tr>
                    821:            <td>
                    822:             <table class="LC_nested">
                    823:              <tr class="LC_info_row">
                    824:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
                    825:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
                    826:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
1.235     raeburn   827:             </table>
                    828:            </td>
                    829:           </tr>
                    830:           <tr>
                    831:            <td>
                    832:             <table class="LC_nested">
                    833:              <tr class="LC_info_row">
1.242     raeburn   834:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
                    835:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
1.235     raeburn   836:              </tr>'.
                    837:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
1.163     raeburn   838:         } elsif ($action eq 'requestauthor') {
                    839:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
1.247     raeburn   840:             $rowtotal ++;
1.122     jms       841:         } elsif ($action eq 'rolecolors') {
1.30      raeburn   842:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
1.6       raeburn   843:            </table>
                    844:           </td>
                    845:          </tr>
                    846:          <tr>
                    847:            <td>
                    848:             <table class="LC_nested">
                    849:              <tr class="LC_info_row">
1.69      raeburn   850:               <td class="LC_left_item"'.$colspan.' valign="top">'.
                    851:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
                    852:               <td class="LC_right_item" valign="top">'.
                    853:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
1.3       raeburn   854:              </tr>'.
1.30      raeburn   855:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
1.3       raeburn   856:            </table>
                    857:           </td>
                    858:          </tr>
                    859:          <tr>
                    860:            <td>
                    861:             <table class="LC_nested">
                    862:              <tr class="LC_info_row">
1.59      bisitz    863:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
                    864:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
1.3       raeburn   865:              </tr>'.
1.30      raeburn   866:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
                    867:             $rowtotal += 2;
1.6       raeburn   868:         }
1.3       raeburn   869:     } else {
1.30      raeburn   870:         $output .= '
1.3       raeburn   871:           <tr>
                    872:            <td>
                    873:             <table class="LC_nested">
1.30      raeburn   874:              <tr class="LC_info_row">';
1.24      raeburn   875:         if (($action eq 'login') || ($action eq 'directorysrch')) {
1.30      raeburn   876:             $output .= '  
1.59      bisitz    877:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
1.69      raeburn   878:         } elsif ($action eq 'serverstatuses') {
                    879:             $output .= '
                    880:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
                    881:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
                    882: 
1.6       raeburn   883:         } else {
1.30      raeburn   884:             $output .= '
1.69      raeburn   885:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
                    886:         }
1.72      raeburn   887:         if (defined($item->{'header'}->[0]->{'col3'})) {
                    888:             $output .= '<td class="LC_left_item" valign="top">'.
                    889:                        &mt($item->{'header'}->[0]->{'col2'});
                    890:             if ($action eq 'serverstatuses') {
                    891:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
                    892:             } 
1.69      raeburn   893:         } else {
                    894:             $output .= '<td class="LC_right_item" valign="top">'.
                    895:                        &mt($item->{'header'}->[0]->{'col2'});
                    896:         }
                    897:         $output .= '</td>';
                    898:         if ($item->{'header'}->[0]->{'col3'}) {
1.150     raeburn   899:             if (defined($item->{'header'}->[0]->{'col4'})) {
                    900:                 $output .= '<td class="LC_left_item" valign="top">'.
                    901:                             &mt($item->{'header'}->[0]->{'col3'});
                    902:             } else {
                    903:                 $output .= '<td class="LC_right_item" valign="top">'.
                    904:                            &mt($item->{'header'}->[0]->{'col3'});
                    905:             }
1.69      raeburn   906:             if ($action eq 'serverstatuses') {
                    907:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
                    908:             }
                    909:             $output .= '</td>';
1.6       raeburn   910:         }
1.150     raeburn   911:         if ($item->{'header'}->[0]->{'col4'}) {
                    912:             $output .= '<td class="LC_right_item" valign="top">'.
                    913:                        &mt($item->{'header'}->[0]->{'col4'});
                    914:         }
1.69      raeburn   915:         $output .= '</tr>';
1.48      raeburn   916:         $rowtotal ++;
1.168     raeburn   917:         if ($action eq 'quotas') {
1.86      raeburn   918:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
1.230     raeburn   919:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || ($action eq 'directorysrch') ||
1.267   ! raeburn   920:                  ($action eq 'contacts') || ($action eq 'serverstatuses') || ($action eq 'loadbalancing') ||
        !           921:                  ($action eq 'ltitools')) {
1.230     raeburn   922:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
1.46      raeburn   923:         } elsif ($action eq 'scantron') {
                    924:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
1.118     jms       925:         } elsif ($action eq 'helpsettings') {
1.168     raeburn   926:             $output .= &print_helpsettings($dom,$confname,$settings,\$rowtotal);
1.121     raeburn   927:         }
1.3       raeburn   928:     }
1.30      raeburn   929:     $output .= '
1.3       raeburn   930:    </table>
                    931:   </td>
                    932:  </tr>
1.30      raeburn   933: </table><br />';
                    934:     return ($output,$rowtotal);
1.1       raeburn   935: }
                    936: 
1.3       raeburn   937: sub print_login {
1.168     raeburn   938:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
1.110     raeburn   939:     my ($css_class,$datatable);
1.6       raeburn   940:     my %choices = &login_choices();
1.110     raeburn   941: 
1.168     raeburn   942:     if ($caller eq 'service') {
1.149     raeburn   943:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
1.110     raeburn   944:         my $choice = $choices{'disallowlogin'};
                    945:         $css_class = ' class="LC_odd_row"';
1.128     raeburn   946:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
1.110     raeburn   947:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
1.128     raeburn   948:                       '<th>'.$choices{'server'}.'</th>'.
                    949:                       '<th>'.$choices{'serverpath'}.'</th>'.
                    950:                       '<th>'.$choices{'custompath'}.'</th>'.
                    951:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
1.110     raeburn   952:         my %disallowed;
                    953:         if (ref($settings) eq 'HASH') {
                    954:             if (ref($settings->{'loginvia'}) eq 'HASH') {
                    955:                %disallowed = %{$settings->{'loginvia'}};
                    956:             }
                    957:         }
                    958:         foreach my $lonhost (sort(keys(%servers))) {
                    959:             my $direct = 'selected="selected"';
1.128     raeburn   960:             if (ref($disallowed{$lonhost}) eq 'HASH') {
                    961:                 if ($disallowed{$lonhost}{'server'} ne '') {
                    962:                     $direct = '';
                    963:                 }
1.110     raeburn   964:             }
1.115     raeburn   965:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
1.128     raeburn   966:                           '<td><select name="'.$lonhost.'_server">'.
1.110     raeburn   967:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
                    968:                           '</option>';
1.184     raeburn   969:             foreach my $hostid (sort(keys(%servers))) {
1.115     raeburn   970:                 next if ($servers{$hostid} eq $servers{$lonhost});
1.110     raeburn   971:                 my $selected = '';
1.128     raeburn   972:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
                    973:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
                    974:                         $selected = 'selected="selected"';
                    975:                     }
1.110     raeburn   976:                 }
                    977:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
                    978:                               $servers{$hostid}.'</option>';
                    979:             }
1.128     raeburn   980:             $datatable .= '</select></td>'.
                    981:                           '<td><select name="'.$lonhost.'_serverpath">';
                    982:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
                    983:                 my $pathname = $path;
                    984:                 if ($path eq 'custom') {
                    985:                     $pathname = &mt('Custom Path').' ->';
                    986:                 }
                    987:                 my $selected = '';
                    988:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
                    989:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
                    990:                         $selected = 'selected="selected"';
                    991:                     }
                    992:                 } elsif ($path eq '') {
                    993:                     $selected = 'selected="selected"';
                    994:                 }
                    995:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
                    996:             }
                    997:             $datatable .= '</select></td>';
                    998:             my ($custom,$exempt);
                    999:             if (ref($disallowed{$lonhost}) eq 'HASH') {
                   1000:                 $custom = $disallowed{$lonhost}{'custompath'};
                   1001:                 $exempt = $disallowed{$lonhost}{'exempt'};
                   1002:             }
                   1003:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
                   1004:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
                   1005:                           '</tr>';
1.110     raeburn  1006:         }
                   1007:         $datatable .= '</table></td></tr>';
                   1008:         return $datatable;
1.168     raeburn  1009:     } elsif ($caller eq 'page') {
                   1010:         my %defaultchecked = ( 
                   1011:                                'coursecatalog' => 'on',
1.188     raeburn  1012:                                'helpdesk'      => 'on',
1.168     raeburn  1013:                                'adminmail'     => 'off',
                   1014:                                'newuser'       => 'off',
                   1015:                              );
1.188     raeburn  1016:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
1.168     raeburn  1017:         my (%checkedon,%checkedoff);
1.42      raeburn  1018:         foreach my $item (@toggles) {
1.168     raeburn  1019:             if ($defaultchecked{$item} eq 'on') { 
                   1020:                 $checkedon{$item} = ' checked="checked" ';
1.42      raeburn  1021:                 $checkedoff{$item} = ' ';
1.168     raeburn  1022:             } elsif ($defaultchecked{$item} eq 'off') {
                   1023:                 $checkedoff{$item} = ' checked="checked" ';
1.42      raeburn  1024:                 $checkedon{$item} = ' ';
                   1025:             }
1.1       raeburn  1026:         }
1.168     raeburn  1027:         my @images = ('img','logo','domlogo','login');
                   1028:         my @logintext = ('textcol','bgcol');
                   1029:         my @bgs = ('pgbg','mainbg','sidebg');
                   1030:         my @links = ('link','alink','vlink');
                   1031:         my %designhash = &Apache::loncommon::get_domainconf($dom);
                   1032:         my %defaultdesign = %Apache::loncommon::defaultdesign;
                   1033:         my (%is_custom,%designs);
                   1034:         my %defaults = (
                   1035:                        font => $defaultdesign{'login.font'},
                   1036:                        );
1.6       raeburn  1037:         foreach my $item (@images) {
1.168     raeburn  1038:             $defaults{$item} = $defaultdesign{'login.'.$item};
                   1039:             $defaults{'showlogo'}{$item} = 1;
                   1040:         }
                   1041:         foreach my $item (@bgs) {
                   1042:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
1.6       raeburn  1043:         }
1.41      raeburn  1044:         foreach my $item (@logintext) {
1.168     raeburn  1045:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
1.41      raeburn  1046:         }
1.168     raeburn  1047:         foreach my $item (@links) {
                   1048:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
1.6       raeburn  1049:         }
1.168     raeburn  1050:         if (ref($settings) eq 'HASH') {
                   1051:             foreach my $item (@toggles) {
                   1052:                 if ($settings->{$item} eq '1') {
                   1053:                     $checkedon{$item} =  ' checked="checked" ';
                   1054:                     $checkedoff{$item} = ' ';
                   1055:                 } elsif ($settings->{$item} eq '0') {
                   1056:                     $checkedoff{$item} =  ' checked="checked" ';
                   1057:                     $checkedon{$item} = ' ';
                   1058:                 }
                   1059:             }
                   1060:             foreach my $item (@images) {
                   1061:                 if (defined($settings->{$item})) {
                   1062:                     $designs{$item} = $settings->{$item};
                   1063:                     $is_custom{$item} = 1;
                   1064:                 }
                   1065:                 if (defined($settings->{'showlogo'}{$item})) {
                   1066:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
                   1067:                 }
                   1068:             }
                   1069:             foreach my $item (@logintext) {
                   1070:                 if ($settings->{$item} ne '') {
                   1071:                     $designs{'logintext'}{$item} = $settings->{$item};
                   1072:                     $is_custom{$item} = 1;
                   1073:                 }
                   1074:             }
                   1075:             if ($settings->{'font'} ne '') {
                   1076:                 $designs{'font'} = $settings->{'font'};
                   1077:                 $is_custom{'font'} = 1;
                   1078:             }
                   1079:             foreach my $item (@bgs) {
                   1080:                 if ($settings->{$item} ne '') {
                   1081:                     $designs{'bgs'}{$item} = $settings->{$item};
                   1082:                     $is_custom{$item} = 1;
                   1083:                 }
                   1084:             }
                   1085:             foreach my $item (@links) {
                   1086:                 if ($settings->{$item} ne '') {
                   1087:                     $designs{'links'}{$item} = $settings->{$item};
                   1088:                     $is_custom{$item} = 1;
                   1089:                 }
                   1090:             }
                   1091:         } else {
                   1092:             if ($designhash{$dom.'.login.font'} ne '') {
                   1093:                 $designs{'font'} = $designhash{$dom.'.login.font'};
                   1094:                 $is_custom{'font'} = 1;
                   1095:             }
                   1096:             foreach my $item (@images) {
                   1097:                 if ($designhash{$dom.'.login.'.$item} ne '') {
                   1098:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
                   1099:                     $is_custom{$item} = 1;
                   1100:                 }
                   1101:             }
                   1102:             foreach my $item (@bgs) {
                   1103:                 if ($designhash{$dom.'.login.'.$item} ne '') {
                   1104:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
                   1105:                     $is_custom{$item} = 1;
                   1106:                 }
1.6       raeburn  1107:             }
1.168     raeburn  1108:             foreach my $item (@links) {
                   1109:                 if ($designhash{$dom.'.login.'.$item} ne '') {
                   1110:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
                   1111:                     $is_custom{$item} = 1;
                   1112:                 }
1.6       raeburn  1113:             }
                   1114:         }
1.168     raeburn  1115:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
                   1116:                                                       logo => 'Institution Logo',
                   1117:                                                       domlogo => 'Domain Logo',
                   1118:                                                       login => 'Login box');
                   1119:         my $itemcount = 1;
                   1120:         foreach my $item (@toggles) {
                   1121:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
                   1122:             $datatable .=  
                   1123:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
                   1124:                 '</td><td>'.
                   1125:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
                   1126:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
                   1127:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
                   1128:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
                   1129:                 '</tr>';
                   1130:             $itemcount ++;
1.6       raeburn  1131:         }
1.168     raeburn  1132:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
                   1133:         $datatable .= '</tr></table></td></tr>';
                   1134:     } elsif ($caller eq 'help') {
                   1135:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
                   1136:         my $switchserver = &check_switchserver($dom,$confname);
                   1137:         my $itemcount = 1;
                   1138:         $defaulturl = '/adm/loginproblems.html';
                   1139:         $defaulttype = 'default';
                   1140:         %lt = &Apache::lonlocal::texthash (
                   1141:                      del     => 'Delete?',
                   1142:                      rep     => 'Replace:',
                   1143:                      upl     => 'Upload:',
                   1144:                      default => 'Default',
                   1145:                      custom  => 'Custom',
                   1146:                                              );
                   1147:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
                   1148:         my @currlangs;
                   1149:         if (ref($settings) eq 'HASH') {
                   1150:             if (ref($settings->{'helpurl'}) eq 'HASH') {
                   1151:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
                   1152:                     next if ($settings->{'helpurl'}{$key} eq '');
                   1153:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
                   1154:                     $type{$key} = 'custom';
                   1155:                     unless ($key eq 'nolang') {
                   1156:                         push(@currlangs,$key);
                   1157:                     }
                   1158:                 }
                   1159:             } elsif ($settings->{'helpurl'} ne '') {
                   1160:                 $type{'nolang'} = 'custom';
                   1161:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
1.8       raeburn  1162:             }
                   1163:         }
1.168     raeburn  1164:         foreach my $lang ('nolang',sort(@currlangs)) {
                   1165:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
                   1166:             $datatable .= '<tr'.$css_class.'>';
                   1167:             if ($url{$lang} eq '') {
                   1168:                 $url{$lang} = $defaulturl;
                   1169:             }
                   1170:             if ($type{$lang} eq '') {
                   1171:                 $type{$lang} = $defaulttype;
                   1172:             }
                   1173:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
                   1174:             if ($lang eq 'nolang') {
                   1175:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
                   1176:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
                   1177:             } else {
                   1178:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
                   1179:                                   $langchoices{$lang},
                   1180:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
                   1181:             }
                   1182:             $datatable .= '</span></td>'."\n".
                   1183:                           '<td class="LC_left_item">';
                   1184:             if ($type{$lang} eq 'custom') {
                   1185:                 $datatable .= '<span class="LC_nobreak"><label>'.
                   1186:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
                   1187:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
                   1188:             } else {
                   1189:                 $datatable .= $lt{'upl'};
                   1190:             }
                   1191:             $datatable .='<br />';
                   1192:             if ($switchserver) {
                   1193:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
                   1194:             } else {
                   1195:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
1.6       raeburn  1196:             }
1.168     raeburn  1197:             $datatable .= '</td></tr>';
                   1198:             $itemcount ++;
1.6       raeburn  1199:         }
1.168     raeburn  1200:         my @addlangs;
                   1201:         foreach my $lang (sort(keys(%langchoices))) {
                   1202:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
                   1203:             push(@addlangs,$lang);
                   1204:         }
                   1205:         if (@addlangs > 0) {
                   1206:             my %toadd;
                   1207:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
                   1208:             $toadd{''} = &mt('Select');
                   1209:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
                   1210:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
                   1211:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
                   1212:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
                   1213:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
                   1214:             if ($switchserver) {
                   1215:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
                   1216:             } else {
                   1217:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
1.6       raeburn  1218:             }
1.168     raeburn  1219:             $datatable .= '</td></tr>';
1.169     raeburn  1220:             $itemcount ++;
1.6       raeburn  1221:         }
1.169     raeburn  1222:         $datatable .= &captcha_choice('login',$settings,$itemcount);
1.256     raeburn  1223:     } elsif ($caller eq 'headtag') {
                   1224:         my %domservers = &Apache::lonnet::get_servers($dom);
                   1225:         my $choice = $choices{'headtag'};
                   1226:         $css_class = ' class="LC_odd_row"';
                   1227:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
                   1228:                       '<td align="left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
                   1229:                       '<th>'.$choices{'current'}.'</th>'.
                   1230:                       '<th>'.$choices{'action'}.'</th>'.
                   1231:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
                   1232:         my (%currurls,%currexempt);
                   1233:         if (ref($settings) eq 'HASH') {
                   1234:             if (ref($settings->{'headtag'}) eq 'HASH') {
                   1235:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
                   1236:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
                   1237:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
                   1238:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
                   1239:                     }
                   1240:                 }
                   1241:             }
                   1242:         }
                   1243:         my %lt = &Apache::lonlocal::texthash(
                   1244:                                                del  => 'Delete?',
                   1245:                                                rep  => 'Replace:',
                   1246:                                                upl  => 'Upload:',
                   1247:                                                curr => 'View contents',
                   1248:                                                none => 'None',
                   1249:         );
                   1250:         my $switchserver = &check_switchserver($dom,$confname);
                   1251:         foreach my $lonhost (sort(keys(%domservers))) {
                   1252:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
                   1253:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
                   1254:             if ($currurls{$lonhost}) {
                   1255:                 $datatable .= '<td class="LC_right_item"><a href="'.
                   1256:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
                   1257:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
                   1258:                               '">'.$lt{'curr'}.'</a></td>'.
                   1259:                               '<td><span class="LC_nobreak"><label>'.
                   1260:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
                   1261:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
                   1262:             } else {
                   1263:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
                   1264:             }
                   1265:             $datatable .='<br />';
                   1266:             if ($switchserver) {
                   1267:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
                   1268:             } else {
                   1269:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
                   1270:             }
                   1271:             $datatable .= '</td><td><input type="textbox" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
                   1272:         }
                   1273:         $datatable .= '</table></td></tr>';
1.1       raeburn  1274:     }
1.6       raeburn  1275:     return $datatable;
                   1276: }
                   1277: 
                   1278: sub login_choices {
                   1279:     my %choices =
                   1280:         &Apache::lonlocal::texthash (
1.116     bisitz   1281:             coursecatalog => 'Display Course/Community Catalog link?',
1.110     raeburn  1282:             adminmail     => "Display Administrator's E-mail Address?",
1.188     raeburn  1283:             helpdesk      => 'Display "Contact Helpdesk" link',
1.110     raeburn  1284:             disallowlogin => "Login page requests redirected",
                   1285:             hostid        => "Server",
1.128     raeburn  1286:             server        => "Redirect to:",
                   1287:             serverpath    => "Path",
                   1288:             custompath    => "Custom", 
                   1289:             exempt        => "Exempt IP(s)",
1.110     raeburn  1290:             directlogin   => "No redirect",
                   1291:             newuser       => "Link to create a user account",
                   1292:             img           => "Header",
                   1293:             logo          => "Main Logo",
                   1294:             domlogo       => "Domain Logo",
                   1295:             login         => "Log-in Header", 
                   1296:             textcol       => "Text color",
                   1297:             bgcol         => "Box color",
                   1298:             bgs           => "Background colors",
                   1299:             links         => "Link colors",
                   1300:             font          => "Font color",
                   1301:             pgbg          => "Header",
                   1302:             mainbg        => "Page",
                   1303:             sidebg        => "Login box",
                   1304:             link          => "Link",
                   1305:             alink         => "Active link",
                   1306:             vlink         => "Visited link",
1.256     raeburn  1307:             headtag       => "Custom markup",
                   1308:             action        => "Action",
                   1309:             current       => "Current",
1.6       raeburn  1310:         );
                   1311:     return %choices;
                   1312: }
                   1313: 
                   1314: sub print_rolecolors {
1.30      raeburn  1315:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
1.6       raeburn  1316:     my %choices = &color_font_choices();
                   1317:     my @bgs = ('pgbg','tabbg','sidebg');
                   1318:     my @links = ('link','alink','vlink');
                   1319:     my @images = ('img');
                   1320:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
1.7       albertel 1321:     my %designhash = &Apache::loncommon::get_domainconf($dom);
1.6       raeburn  1322:     my %defaultdesign = %Apache::loncommon::defaultdesign;
                   1323:     my (%is_custom,%designs);
1.200     raeburn  1324:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
1.6       raeburn  1325:     if (ref($settings) eq 'HASH') {
                   1326:         if (ref($settings->{$role}) eq 'HASH') {
                   1327:             if ($settings->{$role}->{'img'} ne '') {
                   1328:                 $designs{'img'} = $settings->{$role}->{'img'};
                   1329:                 $is_custom{'img'} = 1;
                   1330:             }
                   1331:             if ($settings->{$role}->{'font'} ne '') {
                   1332:                 $designs{'font'} = $settings->{$role}->{'font'};
                   1333:                 $is_custom{'font'} = 1;
                   1334:             }
1.97      tempelho 1335:             if ($settings->{$role}->{'fontmenu'} ne '') {
                   1336:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
                   1337:                 $is_custom{'fontmenu'} = 1;
                   1338:             }
1.6       raeburn  1339:             foreach my $item (@bgs) {
                   1340:                 if ($settings->{$role}->{$item} ne '') {
                   1341:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
                   1342:                     $is_custom{$item} = 1;
                   1343:                 }
                   1344:             }
                   1345:             foreach my $item (@links) {
                   1346:                 if ($settings->{$role}->{$item} ne '') {
                   1347:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
                   1348:                     $is_custom{$item} = 1;
                   1349:                 }
                   1350:             }
                   1351:         }
                   1352:     } else {
                   1353:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
                   1354:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
                   1355:             $is_custom{'img'} = 1;
                   1356:         }
1.97      tempelho 1357:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
                   1358:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
                   1359:             $is_custom{'fontmenu'} = 1; 
                   1360:         }
1.6       raeburn  1361:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
                   1362:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
                   1363:             $is_custom{'font'} = 1;
                   1364:         }
                   1365:         foreach my $item (@bgs) {
                   1366:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
                   1367:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
                   1368:                 $is_custom{$item} = 1;
                   1369:             
                   1370:             }
                   1371:         }
                   1372:         foreach my $item (@links) {
                   1373:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
                   1374:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
                   1375:                 $is_custom{$item} = 1;
                   1376:             }
                   1377:         }
                   1378:     }
                   1379:     my $itemcount = 1;
1.30      raeburn  1380:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
1.6       raeburn  1381:     $datatable .= '</tr></table></td></tr>';
                   1382:     return $datatable;
                   1383: }
                   1384: 
1.200     raeburn  1385: sub role_defaults {
                   1386:     my ($role,$bgs,$links,$images,$logintext) = @_;
1.202     raeburn  1387:     my %defaults;
                   1388:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
1.200     raeburn  1389:         return %defaults;
                   1390:     }
                   1391:     my %defaultdesign = %Apache::loncommon::defaultdesign;
                   1392:     if ($role eq 'login') {
                   1393:         %defaults = (
                   1394:                        font => $defaultdesign{$role.'.font'},
                   1395:                     );
                   1396:         if (ref($logintext) eq 'ARRAY') {
                   1397:             foreach my $item (@{$logintext}) {
                   1398:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
                   1399:             }
                   1400:         }
                   1401:         foreach my $item (@{$images}) {
                   1402:             $defaults{'showlogo'}{$item} = 1;
                   1403:         }
                   1404:     } else {
                   1405:         %defaults = (
                   1406:                        img => $defaultdesign{$role.'.img'},
                   1407:                        font => $defaultdesign{$role.'.font'},
                   1408:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
                   1409:                     );
                   1410:     }
                   1411:     foreach my $item (@{$bgs}) {
                   1412:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
                   1413:     }
                   1414:     foreach my $item (@{$links}) {
                   1415:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
                   1416:     }
                   1417:     foreach my $item (@{$images}) {
                   1418:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
                   1419:     }
                   1420:     return %defaults;
                   1421: }
                   1422: 
1.6       raeburn  1423: sub display_color_options {
1.9       raeburn  1424:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
1.135     bisitz   1425:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
1.159     raeburn  1426:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
1.6       raeburn  1427:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
1.176     raeburn  1428:     my $datatable = '<tr'.$css_class.'>'.
1.6       raeburn  1429:         '<td>'.$choices->{'font'}.'</td>';
                   1430:     if (!$is_custom->{'font'}) {
1.30      raeburn  1431:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
1.6       raeburn  1432:     } else {
                   1433:         $datatable .= '<td>&nbsp;</td>';
                   1434:     }
1.174     foxr     1435:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
                   1436: 
1.8       raeburn  1437:     $datatable .= '<td><span class="LC_nobreak">'.
1.174     foxr     1438:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
1.202     raeburn  1439:                   ' value="'.$current_color.'" />&nbsp;'.
1.174     foxr     1440:                   '&nbsp;</td></tr>';
1.107     raeburn  1441:     unless ($role eq 'login') { 
                   1442:         $datatable .= '<tr'.$css_class.'>'.
                   1443:                       '<td>'.$choices->{'fontmenu'}.'</td>';
                   1444:         if (!$is_custom->{'fontmenu'}) {
                   1445:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
                   1446:         } else {
                   1447:             $datatable .= '<td>&nbsp;</td>';
                   1448:         }
1.202     raeburn  1449: 	$current_color = $designs->{'fontmenu'} ?
1.174     foxr     1450: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
1.107     raeburn  1451:         $datatable .= '<td><span class="LC_nobreak">'.
1.174     foxr     1452:                       '<input class="colorchooser" type="text" size="10" name="'
                   1453: 		      .$role.'_fontmenu"'.
                   1454:                       ' value="'.$current_color.'" />&nbsp;'.
                   1455:                       '&nbsp;</td></tr>';
1.97      tempelho 1456:     }
1.9       raeburn  1457:     my $switchserver = &check_switchserver($dom,$confname);
1.6       raeburn  1458:     foreach my $img (@{$images}) {
1.18      albertel 1459: 	$itemcount ++;
1.6       raeburn  1460:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
1.8       raeburn  1461:         $datatable .= '<tr'.$css_class.'>'.
1.70      raeburn  1462:                       '<td>'.$choices->{$img};
1.41      raeburn  1463:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
1.70      raeburn  1464:         if ($role eq 'login') {
                   1465:             if ($img eq 'login') {
                   1466:                 $login_hdr_pick =
1.135     bisitz   1467:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
1.70      raeburn  1468:                 $logincolors =
                   1469:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
1.201     raeburn  1470:                                        $designs,$defaults);
1.70      raeburn  1471:             } elsif ($img ne 'domlogo') {
                   1472:                 $datatable.= &logo_display_options($img,$defaults,$designs);
                   1473:             }
                   1474:         }
                   1475:         $datatable .= '</td>';
1.6       raeburn  1476:         if ($designs->{$img} ne '') {
                   1477:             $imgfile = $designs->{$img};
1.18      albertel 1478: 	    $img_import = ($imgfile =~ m{^/adm/});
1.6       raeburn  1479:         } else {
                   1480:             $imgfile = $defaults->{$img};
                   1481:         }
                   1482:         if ($imgfile) {
1.9       raeburn  1483:             my ($showfile,$fullsize);
                   1484:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
1.6       raeburn  1485:                 my $urldir = $1;
                   1486:                 my $filename = $2;
                   1487:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
                   1488:                 if (@info) {
                   1489:                     my $thumbfile = 'tn-'.$filename;
                   1490:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
                   1491:                     if (@thumb) {
                   1492:                         $showfile = $urldir.'/'.$thumbfile;
                   1493:                     } else {
                   1494:                         $showfile = $imgfile;
                   1495:                     }
                   1496:                 } else {
                   1497:                     $showfile = '';
                   1498:                 }
                   1499:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
1.16      raeburn  1500:                 $showfile = $imgfile;
1.6       raeburn  1501:                 my $imgdir = $1;
                   1502:                 my $filename = $2;
1.159     raeburn  1503:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
1.6       raeburn  1504:                     $showfile = "/$imgdir/tn-".$filename;
                   1505:                 } else {
1.159     raeburn  1506:                     my $input = $londocroot.$imgfile;
                   1507:                     my $output = "$londocroot/$imgdir/tn-".$filename;
1.6       raeburn  1508:                     if (!-e $output) {
1.9       raeburn  1509:                         my ($width,$height) = &thumb_dimensions();
1.16      raeburn  1510:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
                   1511:                         if ($fullwidth ne '' && $fullheight ne '') {
                   1512:                             if ($fullwidth > $width && $fullheight > $height) { 
                   1513:                                 my $size = $width.'x'.$height;
                   1514:                                 system("convert -sample $size $input $output");
1.159     raeburn  1515:                                 $showfile = "/$imgdir/tn-".$filename;
1.16      raeburn  1516:                             }
                   1517:                         }
1.6       raeburn  1518:                     }
                   1519:                 }
1.16      raeburn  1520:             }
1.6       raeburn  1521:             if ($showfile) {
1.40      raeburn  1522:                 if ($showfile =~ m{^/(adm|res)/}) {
                   1523:                     if ($showfile =~ m{^/res/}) {
                   1524:                         my $local_showfile =
                   1525:                             &Apache::lonnet::filelocation('',$showfile);
                   1526:                         &Apache::lonnet::repcopy($local_showfile);
                   1527:                     }
                   1528:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
                   1529:                 }
                   1530:                 if ($imgfile) {
                   1531:                     if ($imgfile  =~ m{^/(adm|res)/}) {
                   1532:                         if ($imgfile =~ m{^/res/}) {
                   1533:                             my $local_imgfile =
                   1534:                                 &Apache::lonnet::filelocation('',$imgfile);
                   1535:                             &Apache::lonnet::repcopy($local_imgfile);
                   1536:                         }
                   1537:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
                   1538:                     } else {
                   1539:                         $fullsize = $imgfile;
                   1540:                     }
                   1541:                 }
1.41      raeburn  1542:                 $datatable .= '<td>';
                   1543:                 if ($img eq 'login') {
1.135     bisitz   1544:                     $datatable .= $login_hdr_pick;
                   1545:                 } 
1.41      raeburn  1546:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
                   1547:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
1.6       raeburn  1548:             } else {
1.201     raeburn  1549:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
                   1550:                               &mt('Upload:').'<br />';
1.6       raeburn  1551:             }
                   1552:         } else {
1.201     raeburn  1553:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
                   1554:                           &mt('Upload:').'<br />';
1.6       raeburn  1555:         }
1.9       raeburn  1556:         if ($switchserver) {
                   1557:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
                   1558:         } else {
1.135     bisitz   1559:             if ($img ne 'login') { # suppress file selection for Log-in header
                   1560:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
                   1561:             }
1.9       raeburn  1562:         }
                   1563:         $datatable .= '</td></tr>';
1.6       raeburn  1564:     }
                   1565:     $itemcount ++;
                   1566:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
                   1567:     $datatable .= '<tr'.$css_class.'>'.
                   1568:                   '<td>'.$choices->{'bgs'}.'</td>';
                   1569:     my $bgs_def;
                   1570:     foreach my $item (@{$bgs}) {
                   1571:         if (!$is_custom->{$item}) {
1.70      raeburn  1572:             $bgs_def .= '<td><span class="LC_nobreak">'.$choices->{$item}.'</span>&nbsp;<span id="css_default_'.$role.'_'.$item.'" style="background-color: '.$defaults->{'bgs'}{$item}.';">&nbsp;&nbsp;&nbsp;</span><br />'.$defaults->{'bgs'}{$item}.'</td>';
1.6       raeburn  1573:         }
                   1574:     }
                   1575:     if ($bgs_def) {
1.8       raeburn  1576:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
1.6       raeburn  1577:     } else {
                   1578:         $datatable .= '<td>&nbsp;</td>';
                   1579:     }
                   1580:     $datatable .= '<td class="LC_right_item">'.
                   1581:                   '<table border="0"><tr>';
1.174     foxr     1582: 
1.6       raeburn  1583:     foreach my $item (@{$bgs}) {
1.201     raeburn  1584:         $datatable .= '<td align="center">'.$choices->{$item};
1.174     foxr     1585: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
1.6       raeburn  1586:         if ($designs->{'bgs'}{$item}) {
1.174     foxr     1587:             $datatable .= '&nbsp;';
1.6       raeburn  1588:         }
1.174     foxr     1589:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
1.41      raeburn  1590:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
1.6       raeburn  1591:     }
                   1592:     $datatable .= '</tr></table></td></tr>';
                   1593:     $itemcount ++;
                   1594:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
                   1595:     $datatable .= '<tr'.$css_class.'>'.
                   1596:                   '<td>'.$choices->{'links'}.'</td>';
                   1597:     my $links_def;
                   1598:     foreach my $item (@{$links}) {
                   1599:         if (!$is_custom->{$item}) {
1.30      raeburn  1600:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
1.6       raeburn  1601:         }
                   1602:     }
                   1603:     if ($links_def) {
1.8       raeburn  1604:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
1.6       raeburn  1605:     } else {
                   1606:         $datatable .= '<td>&nbsp;</td>';
                   1607:     }
                   1608:     $datatable .= '<td class="LC_right_item">'.
                   1609:                   '<table border="0"><tr>';
                   1610:     foreach my $item (@{$links}) {
1.234     raeburn  1611: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
1.201     raeburn  1612:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
1.6       raeburn  1613:         if ($designs->{'links'}{$item}) {
1.174     foxr     1614:             $datatable.='&nbsp;';
1.6       raeburn  1615:         }
1.174     foxr     1616:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
1.6       raeburn  1617:                       '" /></td>';
                   1618:     }
1.30      raeburn  1619:     $$rowtotal += $itemcount;
1.3       raeburn  1620:     return $datatable;
                   1621: }
                   1622: 
1.70      raeburn  1623: sub logo_display_options {
                   1624:     my ($img,$defaults,$designs) = @_;
                   1625:     my $checkedon;
                   1626:     if (ref($defaults) eq 'HASH') {
                   1627:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
                   1628:             if ($defaults->{'showlogo'}{$img}) {
                   1629:                 $checkedon = 'checked="checked" ';     
                   1630:             }
                   1631:         } 
                   1632:     }
                   1633:     if (ref($designs) eq 'HASH') {
                   1634:         if (ref($designs->{'showlogo'}) eq 'HASH') {
                   1635:             if (defined($designs->{'showlogo'}{$img})) {
                   1636:                 if ($designs->{'showlogo'}{$img} == 0) {
                   1637:                     $checkedon = '';
                   1638:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
                   1639:                     $checkedon = 'checked="checked" ';
                   1640:                 }
                   1641:             }
                   1642:         }
                   1643:     }
                   1644:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
                   1645:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
                   1646:            &mt('show').'</label>'."\n";
                   1647: }
                   1648: 
1.41      raeburn  1649: sub login_header_options  {
1.135     bisitz   1650:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
                   1651:     my $output = '';
1.41      raeburn  1652:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
1.135     bisitz   1653:         $output .= &mt('Text default(s):').'<br />';
1.41      raeburn  1654:         if (!$is_custom->{'textcol'}) {
                   1655:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
                   1656:                        '&nbsp;&nbsp;&nbsp;';
                   1657:         }
                   1658:         if (!$is_custom->{'bgcol'}) {
                   1659:             $output .= $choices->{'bgcol'}.':&nbsp;'.
                   1660:                        '<span id="css_'.$role.'_font" style="background-color: '.
                   1661:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
                   1662:         }
                   1663:         $output .= '<br />';
                   1664:     }
                   1665:     $output .='<br />';
                   1666:     return $output;
                   1667: }
                   1668: 
                   1669: sub login_text_colors {
1.201     raeburn  1670:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
1.41      raeburn  1671:     my $color_menu = '<table border="0"><tr>';
                   1672:     foreach my $item (@{$logintext}) {
1.201     raeburn  1673:         $color_menu .= '<td align="center">'.$choices->{$item};
                   1674:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
                   1675:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
                   1676:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
1.41      raeburn  1677:     }
                   1678:     $color_menu .= '</tr></table><br />';
                   1679:     return $color_menu;
                   1680: }
                   1681: 
                   1682: sub image_changes {
                   1683:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
                   1684:     my $output;
1.135     bisitz   1685:     if ($img eq 'login') {
                   1686:             # suppress image for Log-in header
                   1687:     } elsif (!$is_custom) {
1.70      raeburn  1688:         if ($img ne 'domlogo') {
1.41      raeburn  1689:             $output .= &mt('Default image:').'<br />';
                   1690:         } else {
                   1691:             $output .= &mt('Default in use:').'<br />';
                   1692:         }
                   1693:     }
1.135     bisitz   1694:     if ($img eq 'login') { # suppress image for Log-in header
                   1695:         $output .= '<td>'.$logincolors;
1.41      raeburn  1696:     } else {
1.135     bisitz   1697:         if ($img_import) {
                   1698:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
                   1699:         }
                   1700:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
                   1701:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
                   1702:         if ($is_custom) {
                   1703:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
                   1704:                        '<input type="checkbox" name="'.
                   1705:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
                   1706:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
                   1707:         } else {
1.201     raeburn  1708:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
1.135     bisitz   1709:         }
1.41      raeburn  1710:     }
                   1711:     return $output;
                   1712: }
                   1713: 
1.3       raeburn  1714: sub print_quotas {
1.86      raeburn  1715:     my ($dom,$settings,$rowtotal,$action) = @_;
                   1716:     my $context;
                   1717:     if ($action eq 'quotas') {
                   1718:         $context = 'tools';
                   1719:     } else {
                   1720:         $context = $action;
                   1721:     }
1.197     raeburn  1722:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
1.44      raeburn  1723:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
1.3       raeburn  1724:     my $typecount = 0;
1.101     raeburn  1725:     my ($css_class,%titles);
1.86      raeburn  1726:     if ($context eq 'requestcourses') {
1.216     raeburn  1727:         @usertools = ('official','unofficial','community','textbook');
1.106     raeburn  1728:         @options =('norequest','approval','validate','autolimit');
1.101     raeburn  1729:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
                   1730:         %titles = &courserequest_titles();
1.163     raeburn  1731:     } elsif ($context eq 'requestauthor') {
                   1732:         @usertools = ('author');
                   1733:         @options = ('norequest','approval','automatic');
1.210     raeburn  1734:         %titles = &authorrequest_titles();
1.86      raeburn  1735:     } else {
1.162     raeburn  1736:         @usertools = ('aboutme','blog','webdav','portfolio');
1.101     raeburn  1737:         %titles = &tool_titles();
1.86      raeburn  1738:     }
1.26      raeburn  1739:     if (ref($types) eq 'ARRAY') {
1.23      raeburn  1740:         foreach my $type (@{$types}) {
1.197     raeburn  1741:             my ($currdefquota,$currauthorquota);
1.163     raeburn  1742:             unless (($context eq 'requestcourses') ||
                   1743:                     ($context eq 'requestauthor')) {
1.86      raeburn  1744:                 if (ref($settings) eq 'HASH') {
                   1745:                     if (ref($settings->{defaultquota}) eq 'HASH') {
1.197     raeburn  1746:                         $currdefquota = $settings->{defaultquota}->{$type};
1.86      raeburn  1747:                     } else {
                   1748:                         $currdefquota = $settings->{$type};
                   1749:                     }
1.197     raeburn  1750:                     if (ref($settings->{authorquota}) eq 'HASH') {
                   1751:                         $currauthorquota = $settings->{authorquota}->{$type};
                   1752:                     }
1.78      raeburn  1753:                 }
1.72      raeburn  1754:             }
1.3       raeburn  1755:             if (defined($usertypes->{$type})) {
                   1756:                 $typecount ++;
                   1757:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
1.72      raeburn  1758:                 $datatable .= '<tr'.$css_class.'>'.
1.3       raeburn  1759:                               '<td>'.$usertypes->{$type}.'</td>'.
1.72      raeburn  1760:                               '<td class="LC_left_item">';
1.101     raeburn  1761:                 if ($context eq 'requestcourses') {
                   1762:                     $datatable .= '<table><tr>';
                   1763:                 }
                   1764:                 my %cell;  
1.72      raeburn  1765:                 foreach my $item (@usertools) {
1.101     raeburn  1766:                     if ($context eq 'requestcourses') {
                   1767:                         my ($curroption,$currlimit);
                   1768:                         if (ref($settings) eq 'HASH') {
                   1769:                             if (ref($settings->{$item}) eq 'HASH') {
                   1770:                                 $curroption = $settings->{$item}->{$type};
                   1771:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
                   1772:                                     $currlimit = $1; 
                   1773:                                 }
                   1774:                             }
                   1775:                         }
                   1776:                         if (!$curroption) {
                   1777:                             $curroption = 'norequest';
                   1778:                         }
                   1779:                         $datatable .= '<th>'.$titles{$item}.'</th>';
                   1780:                         foreach my $option (@options) {
                   1781:                             my $val = $option;
                   1782:                             if ($option eq 'norequest') {
                   1783:                                 $val = 0;  
                   1784:                             }
                   1785:                             if ($option eq 'validate') {
                   1786:                                 my $canvalidate = 0;
                   1787:                                 if (ref($validations{$item}) eq 'HASH') { 
                   1788:                                     if ($validations{$item}{$type}) {
                   1789:                                         $canvalidate = 1;
                   1790:                                     }
                   1791:                                 }
                   1792:                                 next if (!$canvalidate);
                   1793:                             }
                   1794:                             my $checked = '';
                   1795:                             if ($option eq $curroption) {
                   1796:                                 $checked = ' checked="checked"';
                   1797:                             } elsif ($option eq 'autolimit') {
                   1798:                                 if ($curroption =~ /^autolimit/) {
                   1799:                                     $checked = ' checked="checked"';
                   1800:                                 }                       
                   1801:                             } 
                   1802:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
                   1803:                                   '<input type="radio" name="crsreq_'.$item.
                   1804:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
1.127     raeburn  1805:                                   $titles{$option}.'</label>';
1.101     raeburn  1806:                             if ($option eq 'autolimit') {
1.127     raeburn  1807:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
1.101     raeburn  1808:                                                 $item.'_limit_'.$type.'" size="1" '.
1.103     raeburn  1809:                                                 'value="'.$currlimit.'" />';
1.101     raeburn  1810:                             }
1.127     raeburn  1811:                             $cell{$item} .= '</span> ';
1.103     raeburn  1812:                             if ($option eq 'autolimit') {
1.127     raeburn  1813:                                 $cell{$item} .= $titles{'unlimited'};
1.103     raeburn  1814:                             }
1.101     raeburn  1815:                         }
1.163     raeburn  1816:                     } elsif ($context eq 'requestauthor') {
                   1817:                         my $curroption;
                   1818:                         if (ref($settings) eq 'HASH') {
                   1819:                             $curroption = $settings->{$type};
                   1820:                         }
                   1821:                         if (!$curroption) {
                   1822:                             $curroption = 'norequest';
                   1823:                         }
                   1824:                         foreach my $option (@options) {
                   1825:                             my $val = $option;
                   1826:                             if ($option eq 'norequest') {
                   1827:                                 $val = 0;
                   1828:                             }
                   1829:                             my $checked = '';
                   1830:                             if ($option eq $curroption) {
                   1831:                                 $checked = ' checked="checked"';
                   1832:                             }
                   1833:                             $datatable .= '<span class="LC_nobreak"><label>'.
                   1834:                                   '<input type="radio" name="authorreq_'.$type.
                   1835:                                   '" value="'.$val.'"'.$checked.' />'.
                   1836:                                   $titles{$option}.'</label></span>&nbsp; ';
                   1837:                         }
1.101     raeburn  1838:                     } else {
                   1839:                         my $checked = 'checked="checked" ';
                   1840:                         if (ref($settings) eq 'HASH') {
                   1841:                             if (ref($settings->{$item}) eq 'HASH') {
                   1842:                                 if ($settings->{$item}->{$type} == 0) {
                   1843:                                     $checked = '';
                   1844:                                 } elsif ($settings->{$item}->{$type} == 1) {
                   1845:                                     $checked =  'checked="checked" ';
                   1846:                                 }
1.78      raeburn  1847:                             }
1.72      raeburn  1848:                         }
1.101     raeburn  1849:                         $datatable .= '<span class="LC_nobreak"><label>'.
                   1850:                                       '<input type="checkbox" name="'.$context.'_'.$item.
                   1851:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
                   1852:                                       '</label></span>&nbsp; ';
1.72      raeburn  1853:                     }
1.101     raeburn  1854:                 }
                   1855:                 if ($context eq 'requestcourses') {
                   1856:                     $datatable .= '</tr><tr>';
                   1857:                     foreach my $item (@usertools) {
1.106     raeburn  1858:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
1.101     raeburn  1859:                     }
                   1860:                     $datatable .= '</tr></table>';
1.72      raeburn  1861:                 }
1.86      raeburn  1862:                 $datatable .= '</td>';
1.163     raeburn  1863:                 unless (($context eq 'requestcourses') ||
                   1864:                         ($context eq 'requestauthor')) {
1.86      raeburn  1865:                     $datatable .= 
1.197     raeburn  1866:                               '<td class="LC_right_item">'.
                   1867:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
1.3       raeburn  1868:                               '<input type="text" name="quota_'.$type.
1.72      raeburn  1869:                               '" value="'.$currdefquota.
1.197     raeburn  1870:                               '" size="5" /></span>'.('&nbsp;' x 2).
                   1871:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
                   1872:                               '<input type="text" name="authorquota_'.$type.
                   1873:                               '" value="'.$currauthorquota.
                   1874:                               '" size="5" /></span></td>';
1.86      raeburn  1875:                 }
                   1876:                 $datatable .= '</tr>';
1.3       raeburn  1877:             }
                   1878:         }
                   1879:     }
1.163     raeburn  1880:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
1.86      raeburn  1881:         $defaultquota = '20';
1.197     raeburn  1882:         $authorquota = '500';
1.86      raeburn  1883:         if (ref($settings) eq 'HASH') {
                   1884:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
                   1885:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
                   1886:             } elsif (defined($settings->{'default'})) {
                   1887:                 $defaultquota = $settings->{'default'};
                   1888:             }
1.197     raeburn  1889:             if (ref($settings->{'authorquota'}) eq 'HASH') {
                   1890:                 $authorquota = $settings->{'authorquota'}->{'default'};
                   1891:             }
1.3       raeburn  1892:         }
                   1893:     }
                   1894:     $typecount ++;
                   1895:     $css_class = $typecount%2?' class="LC_odd_row"':'';
                   1896:     $datatable .= '<tr'.$css_class.'>'.
1.26      raeburn  1897:                   '<td>'.$othertitle.'</td>'.
1.72      raeburn  1898:                   '<td class="LC_left_item">';
1.101     raeburn  1899:     if ($context eq 'requestcourses') {
                   1900:         $datatable .= '<table><tr>';
                   1901:     }
                   1902:     my %defcell;
1.72      raeburn  1903:     foreach my $item (@usertools) {
1.101     raeburn  1904:         if ($context eq 'requestcourses') {
                   1905:             my ($curroption,$currlimit);
                   1906:             if (ref($settings) eq 'HASH') {
                   1907:                 if (ref($settings->{$item}) eq 'HASH') {
                   1908:                     $curroption = $settings->{$item}->{'default'};
                   1909:                     if ($curroption =~ /^autolimit=(\d*)$/) {
                   1910:                         $currlimit = $1;
                   1911:                     }
                   1912:                 }
                   1913:             }
                   1914:             if (!$curroption) {
                   1915:                 $curroption = 'norequest';
                   1916:             }
                   1917:             $datatable .= '<th>'.$titles{$item}.'</th>';
                   1918:             foreach my $option (@options) {
                   1919:                 my $val = $option;
                   1920:                 if ($option eq 'norequest') {
                   1921:                     $val = 0;
                   1922:                 }
                   1923:                 if ($option eq 'validate') {
                   1924:                     my $canvalidate = 0;
                   1925:                     if (ref($validations{$item}) eq 'HASH') {
                   1926:                         if ($validations{$item}{'default'}) {
                   1927:                             $canvalidate = 1;
                   1928:                         }
                   1929:                     }
                   1930:                     next if (!$canvalidate);
                   1931:                 }
                   1932:                 my $checked = '';
                   1933:                 if ($option eq $curroption) {
                   1934:                     $checked = ' checked="checked"';
                   1935:                 } elsif ($option eq 'autolimit') {
                   1936:                     if ($curroption =~ /^autolimit/) {
                   1937:                         $checked = ' checked="checked"';
                   1938:                     }
                   1939:                 }
                   1940:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
                   1941:                                   '<input type="radio" name="crsreq_'.$item.
                   1942:                                   '_default" value="'.$val.'"'.$checked.' />'.
                   1943:                                   $titles{$option}.'</label>';
                   1944:                 if ($option eq 'autolimit') {
1.127     raeburn  1945:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
1.101     raeburn  1946:                                        $item.'_limit_default" size="1" '.
                   1947:                                        'value="'.$currlimit.'" />';
                   1948:                 }
1.127     raeburn  1949:                 $defcell{$item} .= '</span> ';
1.104     raeburn  1950:                 if ($option eq 'autolimit') {
1.127     raeburn  1951:                     $defcell{$item} .= $titles{'unlimited'};
1.104     raeburn  1952:                 }
1.101     raeburn  1953:             }
1.163     raeburn  1954:         } elsif ($context eq 'requestauthor') {
                   1955:             my $curroption;
                   1956:             if (ref($settings) eq 'HASH') {
1.172     raeburn  1957:                 $curroption = $settings->{'default'};
1.163     raeburn  1958:             }
                   1959:             if (!$curroption) {
                   1960:                 $curroption = 'norequest';
                   1961:             }
                   1962:             foreach my $option (@options) {
                   1963:                 my $val = $option;
                   1964:                 if ($option eq 'norequest') {
                   1965:                     $val = 0;
                   1966:                 }
                   1967:                 my $checked = '';
                   1968:                 if ($option eq $curroption) {
                   1969:                     $checked = ' checked="checked"';
                   1970:                 }
                   1971:                 $datatable .= '<span class="LC_nobreak"><label>'.
                   1972:                               '<input type="radio" name="authorreq_default"'.
                   1973:                               ' value="'.$val.'"'.$checked.' />'.
                   1974:                               $titles{$option}.'</label></span>&nbsp; ';
                   1975:             }
1.101     raeburn  1976:         } else {
                   1977:             my $checked = 'checked="checked" ';
                   1978:             if (ref($settings) eq 'HASH') {
                   1979:                 if (ref($settings->{$item}) eq 'HASH') {
                   1980:                     if ($settings->{$item}->{'default'} == 0) {
                   1981:                         $checked = '';
                   1982:                     } elsif ($settings->{$item}->{'default'} == 1) {
                   1983:                         $checked = 'checked="checked" ';
                   1984:                     }
1.78      raeburn  1985:                 }
1.72      raeburn  1986:             }
1.101     raeburn  1987:             $datatable .= '<span class="LC_nobreak"><label>'.
                   1988:                           '<input type="checkbox" name="'.$context.'_'.$item.
                   1989:                           '" value="default" '.$checked.'/>'.$titles{$item}.
                   1990:                           '</label></span>&nbsp; ';
                   1991:         }
                   1992:     }
                   1993:     if ($context eq 'requestcourses') {
                   1994:         $datatable .= '</tr><tr>';
                   1995:         foreach my $item (@usertools) {
1.106     raeburn  1996:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
1.72      raeburn  1997:         }
1.101     raeburn  1998:         $datatable .= '</tr></table>';
1.72      raeburn  1999:     }
1.86      raeburn  2000:     $datatable .= '</td>';
1.163     raeburn  2001:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
1.197     raeburn  2002:         $datatable .= '<td class="LC_right_item">'.
                   2003:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
1.86      raeburn  2004:                       '<input type="text" name="defaultquota" value="'.
1.197     raeburn  2005:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
                   2006:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
                   2007:                       '<input type="text" name="authorquota" value="'.
                   2008:                       $authorquota.'" size="5" /></span></td>';
1.86      raeburn  2009:     }
                   2010:     $datatable .= '</tr>';
1.72      raeburn  2011:     $typecount ++;
                   2012:     $css_class = $typecount%2?' class="LC_odd_row"':'';
                   2013:     $datatable .= '<tr'.$css_class.'>'.
1.197     raeburn  2014:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
1.104     raeburn  2015:     if ($context eq 'requestcourses') {
1.109     raeburn  2016:         $datatable .= &mt('(overrides affiliation, if set)').
                   2017:                       '</td>'.
                   2018:                       '<td class="LC_left_item">'.
                   2019:                       '<table><tr>';
1.101     raeburn  2020:     } else {
1.109     raeburn  2021:         $datatable .= &mt('(overrides affiliation, if checked)').
                   2022:                       '</td>'.
                   2023:                       '<td class="LC_left_item" colspan="2">'.
                   2024:                       '<br />';
1.101     raeburn  2025:     }
                   2026:     my %advcell;
1.72      raeburn  2027:     foreach my $item (@usertools) {
1.101     raeburn  2028:         if ($context eq 'requestcourses') {
                   2029:             my ($curroption,$currlimit);
                   2030:             if (ref($settings) eq 'HASH') {
                   2031:                 if (ref($settings->{$item}) eq 'HASH') {
                   2032:                     $curroption = $settings->{$item}->{'_LC_adv'};
                   2033:                     if ($curroption =~ /^autolimit=(\d*)$/) {
                   2034:                         $currlimit = $1;
                   2035:                     }
                   2036:                 }
                   2037:             }
                   2038:             $datatable .= '<th>'.$titles{$item}.'</th>';
1.104     raeburn  2039:             my $checked = '';
                   2040:             if ($curroption eq '') {
                   2041:                 $checked = ' checked="checked"';
                   2042:             }
                   2043:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
                   2044:                                '<input type="radio" name="crsreq_'.$item.
                   2045:                                '__LC_adv" value=""'.$checked.' />'.
                   2046:                                &mt('No override set').'</label></span>&nbsp; ';
1.101     raeburn  2047:             foreach my $option (@options) {
                   2048:                 my $val = $option;
                   2049:                 if ($option eq 'norequest') {
                   2050:                     $val = 0;
                   2051:                 }
                   2052:                 if ($option eq 'validate') {
                   2053:                     my $canvalidate = 0;
                   2054:                     if (ref($validations{$item}) eq 'HASH') {
                   2055:                         if ($validations{$item}{'_LC_adv'}) {
                   2056:                             $canvalidate = 1;
                   2057:                         }
                   2058:                     }
                   2059:                     next if (!$canvalidate);
                   2060:                 }
                   2061:                 my $checked = '';
1.104     raeburn  2062:                 if ($val eq $curroption) {
1.101     raeburn  2063:                     $checked = ' checked="checked"';
                   2064:                 } elsif ($option eq 'autolimit') {
                   2065:                     if ($curroption =~ /^autolimit/) {
                   2066:                         $checked = ' checked="checked"';
                   2067:                     }
                   2068:                 }
                   2069:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
                   2070:                                   '<input type="radio" name="crsreq_'.$item.
                   2071:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
                   2072:                                   $titles{$option}.'</label>';
                   2073:                 if ($option eq 'autolimit') {
1.127     raeburn  2074:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
1.101     raeburn  2075:                                        $item.'_limit__LC_adv" size="1" '.
                   2076:                                        'value="'.$currlimit.'" />';
                   2077:                 }
1.127     raeburn  2078:                 $advcell{$item} .= '</span> ';
1.104     raeburn  2079:                 if ($option eq 'autolimit') {
1.127     raeburn  2080:                     $advcell{$item} .= $titles{'unlimited'};
1.104     raeburn  2081:                 }
1.101     raeburn  2082:             }
1.163     raeburn  2083:         } elsif ($context eq 'requestauthor') {
                   2084:             my $curroption;
                   2085:             if (ref($settings) eq 'HASH') {
                   2086:                 $curroption = $settings->{'_LC_adv'};
                   2087:             }
                   2088:             my $checked = '';
                   2089:             if ($curroption eq '') {
                   2090:                 $checked = ' checked="checked"';
                   2091:             }
                   2092:             $datatable .= '<span class="LC_nobreak"><label>'.
                   2093:                           '<input type="radio" name="authorreq__LC_adv"'.
                   2094:                           ' value=""'.$checked.' />'.
                   2095:                           &mt('No override set').'</label></span>&nbsp; ';
                   2096:             foreach my $option (@options) {
                   2097:                 my $val = $option;
                   2098:                 if ($option eq 'norequest') {
                   2099:                     $val = 0;
                   2100:                 }
                   2101:                 my $checked = '';
                   2102:                 if ($val eq $curroption) {
                   2103:                     $checked = ' checked="checked"';
                   2104:                 }
                   2105:                 $datatable .= '<span class="LC_nobreak"><label>'.
1.173     raeburn  2106:                               '<input type="radio" name="authorreq__LC_adv"'.
                   2107:                               ' value="'.$val.'"'.$checked.' />'.
1.163     raeburn  2108:                               $titles{$option}.'</label></span>&nbsp; ';
                   2109:             }
1.101     raeburn  2110:         } else {
                   2111:             my $checked = 'checked="checked" ';
                   2112:             if (ref($settings) eq 'HASH') {
                   2113:                 if (ref($settings->{$item}) eq 'HASH') {
                   2114:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
                   2115:                         $checked = '';
                   2116:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
                   2117:                         $checked = 'checked="checked" ';
                   2118:                     }
1.79      raeburn  2119:                 }
1.72      raeburn  2120:             }
1.101     raeburn  2121:             $datatable .= '<span class="LC_nobreak"><label>'.
                   2122:                           '<input type="checkbox" name="'.$context.'_'.$item.
                   2123:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
                   2124:                           '</label></span>&nbsp; ';
                   2125:         }
                   2126:     }
                   2127:     if ($context eq 'requestcourses') {
                   2128:         $datatable .= '</tr><tr>';
                   2129:         foreach my $item (@usertools) {
1.106     raeburn  2130:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
1.72      raeburn  2131:         }
1.101     raeburn  2132:         $datatable .= '</tr></table>';
1.72      raeburn  2133:     }
1.98      raeburn  2134:     $datatable .= '</td></tr>';
1.30      raeburn  2135:     $$rowtotal += $typecount;
1.3       raeburn  2136:     return $datatable;
                   2137: }
                   2138: 
1.163     raeburn  2139: sub print_requestmail {
                   2140:     my ($dom,$action,$settings,$rowtotal) = @_;
1.208     raeburn  2141:     my ($now,$datatable,%currapp);
1.102     raeburn  2142:     $now = time;
                   2143:     if (ref($settings) eq 'HASH') {
                   2144:         if (ref($settings->{'notify'}) eq 'HASH') {
                   2145:             if ($settings->{'notify'}{'approval'} ne '') {
1.224     raeburn  2146:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
1.102     raeburn  2147:             }
                   2148:         }
                   2149:     }
1.191     raeburn  2150:     my $numinrow = 2;
1.224     raeburn  2151:     my $css_class;
                   2152:     $css_class = ($$rowtotal%2? ' class="LC_odd_row"':'');
1.163     raeburn  2153:     my $text;
                   2154:     if ($action eq 'requestcourses') {
                   2155:         $text = &mt('Receive notification of course requests requiring approval');
1.224     raeburn  2156:     } elsif ($action eq 'requestauthor') {
                   2157:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
1.163     raeburn  2158:     } else {
1.224     raeburn  2159:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
1.163     raeburn  2160:     }
1.224     raeburn  2161:     $datatable = '<tr'.$css_class.'>'.
1.163     raeburn  2162:                  ' <td>'.$text.'</td>'.
1.102     raeburn  2163:                  ' <td class="LC_left_item">';
1.191     raeburn  2164:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
1.224     raeburn  2165:                                                  $action.'notifyapproval',%currapp);
1.191     raeburn  2166:     if ($numdc > 0) {
                   2167:         $datatable .= $table;
1.102     raeburn  2168:     } else {
                   2169:         $datatable .= &mt('There are no active Domain Coordinators');
                   2170:     }
                   2171:     $datatable .='</td></tr>';
                   2172:     return $datatable;
                   2173: }
                   2174: 
1.216     raeburn  2175: sub print_studentcode {
                   2176:     my ($settings,$rowtotal) = @_;
                   2177:     my $rownum = 0; 
1.218     raeburn  2178:     my ($output,%current);
                   2179:     my @crstypes = ('official','unofficial','community','textbook');
1.248     raeburn  2180:     if (ref($settings) eq 'HASH') {
                   2181:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
                   2182:             foreach my $type (@crstypes) {
                   2183:                 $current{$type} = $settings->{'uniquecode'}{$type};
                   2184:             }
1.218     raeburn  2185:         }
                   2186:     }
                   2187:     $output .= '<tr>'.
                   2188:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
                   2189:                '<td class="LC_left_item">';
                   2190:     foreach my $type (@crstypes) {
                   2191:         my $check = ' ';
                   2192:         if ($current{$type}) {
                   2193:             $check = ' checked="checked" ';
                   2194:         }
                   2195:         $output .= '<span class="LC_nobreak"><label>'.
                   2196:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
                   2197:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
                   2198:     }
                   2199:     $output .= '</td></tr>';
                   2200:     $$rowtotal ++;
                   2201:     return $output;
1.216     raeburn  2202: }
                   2203: 
                   2204: sub print_textbookcourses {
1.242     raeburn  2205:     my ($dom,$type,$settings,$rowtotal) = @_;
1.216     raeburn  2206:     my $rownum = 0;
                   2207:     my $css_class;
                   2208:     my $itemcount = 1;
                   2209:     my $maxnum = 0;
                   2210:     my $bookshash;
                   2211:     if (ref($settings) eq 'HASH') {
1.242     raeburn  2212:         $bookshash = $settings->{$type};
1.216     raeburn  2213:     }
                   2214:     my %ordered;
                   2215:     if (ref($bookshash) eq 'HASH') {
                   2216:         foreach my $item (keys(%{$bookshash})) {
                   2217:             if (ref($bookshash->{$item}) eq 'HASH') {
                   2218:                 my $num = $bookshash->{$item}{'order'};
                   2219:                 $ordered{$num} = $item;
                   2220:             }
                   2221:         }
                   2222:     }
                   2223:     my $confname = $dom.'-domainconfig';
                   2224:     my $switchserver = &check_switchserver($dom,$confname);
1.242     raeburn  2225:     my $maxnum = scalar(keys(%ordered));
                   2226:     my $datatable;
1.216     raeburn  2227:     if (keys(%ordered)) {
                   2228:         my @items = sort { $a <=> $b } keys(%ordered);
                   2229:         for (my $i=0; $i<@items; $i++) {
                   2230:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
                   2231:             my $key = $ordered{$items[$i]};
                   2232:             my %coursehash=&Apache::lonnet::coursedescription($key);
                   2233:             my $coursetitle = $coursehash{'description'};
1.243     raeburn  2234:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
1.216     raeburn  2235:             if (ref($bookshash->{$key}) eq 'HASH') {
                   2236:                 $subject = $bookshash->{$key}->{'subject'};
                   2237:                 $title = $bookshash->{$key}->{'title'};
1.242     raeburn  2238:                 if ($type eq 'textbooks') {
1.243     raeburn  2239:                     $publisher = $bookshash->{$key}->{'publisher'};
1.242     raeburn  2240:                     $author = $bookshash->{$key}->{'author'};
                   2241:                     $image = $bookshash->{$key}->{'image'};
                   2242:                     if ($image ne '') {
                   2243:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
                   2244:                         my $imagethumb = "$path/tn-".$imagefile;
                   2245:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
                   2246:                     }
1.216     raeburn  2247:                 }
                   2248:             }
1.242     raeburn  2249:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
1.216     raeburn  2250:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
1.242     raeburn  2251:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
1.216     raeburn  2252:             for (my $k=0; $k<=$maxnum; $k++) {
                   2253:                 my $vpos = $k+1;
                   2254:                 my $selstr;
                   2255:                 if ($k == $i) {
                   2256:                     $selstr = ' selected="selected" ';
                   2257:                 }
                   2258:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
                   2259:             }
                   2260:             $datatable .= '</select>'.('&nbsp;'x2).
1.242     raeburn  2261:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
1.216     raeburn  2262:                 &mt('Delete?').'</label></span></td>'.
                   2263:                 '<td colspan="2">'.
1.242     raeburn  2264:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
1.216     raeburn  2265:                 ('&nbsp;'x2).
1.242     raeburn  2266:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
                   2267:             if ($type eq 'textbooks') {
                   2268:                 $datatable .= ('&nbsp;'x2).
1.243     raeburn  2269:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
                   2270:                               ('&nbsp;'x2).
1.242     raeburn  2271:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
                   2272:                               ('&nbsp;'x2).
                   2273:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
                   2274:                 if ($image) {
1.267   ! raeburn  2275:                     $datatable .= $imgsrc.
1.242     raeburn  2276:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
                   2277:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
                   2278:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
                   2279:                 }
                   2280:                 if ($switchserver) {
                   2281:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
                   2282:                 } else {
                   2283:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
                   2284:                 }
1.216     raeburn  2285:             }
1.242     raeburn  2286:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
1.216     raeburn  2287:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
                   2288:                           $coursetitle.'</span></td></tr>'."\n";
                   2289:             $itemcount ++;
                   2290:         }
                   2291:     }
                   2292:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
1.242     raeburn  2293:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
1.216     raeburn  2294:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
1.242     raeburn  2295:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
                   2296:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
1.216     raeburn  2297:     for (my $k=0; $k<$maxnum+1; $k++) {
                   2298:         my $vpos = $k+1;
                   2299:         my $selstr;
                   2300:         if ($k == $maxnum) {
                   2301:             $selstr = ' selected="selected" ';
                   2302:         }
                   2303:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
                   2304:     }
                   2305:     $datatable .= '</select>&nbsp;'."\n".
1.242     raeburn  2306:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</td>'."\n".
1.216     raeburn  2307:                   '<td colspan="2">'.
1.242     raeburn  2308:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
1.216     raeburn  2309:                   ('&nbsp;'x2).
1.242     raeburn  2310:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
                   2311:                   ('&nbsp;'x2);
                   2312:     if ($type eq 'textbooks') {
1.243     raeburn  2313:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
                   2314:                       ('&nbsp;'x2).
                   2315:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
1.242     raeburn  2316:                       ('&nbsp;'x2).
                   2317:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
                   2318:         if ($switchserver) {
                   2319:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
                   2320:         } else {
                   2321:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
                   2322:         }
1.216     raeburn  2323:     }
                   2324:     $datatable .= '</span>'."\n".
                   2325:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
1.242     raeburn  2326:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
                   2327:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
1.216     raeburn  2328:                   &Apache::loncommon::selectcourse_link
1.242     raeburn  2329:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course');
1.216     raeburn  2330:                   '</span></td>'."\n".
                   2331:                   '</tr>'."\n";
                   2332:     $itemcount ++;
                   2333:     return $datatable;
                   2334: }
                   2335: 
1.217     raeburn  2336: sub textbookcourses_javascript {
1.242     raeburn  2337:     my ($settings) = @_;
                   2338:     return unless(ref($settings) eq 'HASH');
                   2339:     my (%ordered,%total,%jstext);
                   2340:     foreach my $type ('textbooks','templates') {
                   2341:         $total{$type} = 0;
                   2342:         if (ref($settings->{$type}) eq 'HASH') {
                   2343:             foreach my $item (keys(%{$settings->{$type}})) {
                   2344:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
                   2345:                     my $num = $settings->{$type}->{$item}{'order'};
                   2346:                     $ordered{$type}{$num} = $item;
                   2347:                 }
                   2348:             }
                   2349:             $total{$type} = scalar(keys(%{$settings->{$type}}));
                   2350:         }
                   2351:         my @jsarray = ();
                   2352:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
                   2353:             push(@jsarray,$ordered{$type}{$item});
                   2354:         }
                   2355:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
1.217     raeburn  2356:     }
                   2357:     return <<"ENDSCRIPT";
                   2358: <script type="text/javascript">
                   2359: // <![CDATA[
1.242     raeburn  2360: function reorderBooks(form,item,caller) {
1.217     raeburn  2361:     var changedVal;
1.242     raeburn  2362: $jstext{'textbooks'};
                   2363: $jstext{'templates'};
                   2364:     var newpos;
                   2365:     var maxh;
                   2366:     if (caller == 'textbooks') {  
                   2367:         newpos = 'textbooks_addbook_pos';
                   2368:         maxh = 1 + $total{'textbooks'};
                   2369:     } else {
                   2370:         newpos = 'templates_addbook_pos';
                   2371:         maxh = 1 + $total{'templates'};
                   2372:     }
1.217     raeburn  2373:     var current = new Array;
                   2374:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
                   2375:     if (item == newpos) {
                   2376:         changedVal = newitemVal;
                   2377:     } else {
                   2378:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
                   2379:         current[newitemVal] = newpos;
                   2380:     }
1.242     raeburn  2381:     if (caller == 'textbooks') {
                   2382:         for (var i=0; i<textbooks.length; i++) {
                   2383:             var elementName = 'textbooks_'+textbooks[i];
                   2384:             if (elementName != item) {
                   2385:                 if (form.elements[elementName]) {
                   2386:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
                   2387:                     current[currVal] = elementName;
                   2388:                 }
                   2389:             }
                   2390:         }
                   2391:     }
                   2392:     if (caller == 'templates') {
                   2393:         for (var i=0; i<templates.length; i++) {
                   2394:             var elementName = 'templates_'+templates[i];
                   2395:             if (elementName != item) {
                   2396:                 if (form.elements[elementName]) {
                   2397:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
                   2398:                     current[currVal] = elementName;
                   2399:                 }
1.217     raeburn  2400:             }
                   2401:         }
                   2402:     }
                   2403:     var oldVal;
                   2404:     for (var j=0; j<maxh; j++) {
                   2405:         if (current[j] == undefined) {
                   2406:             oldVal = j;
                   2407:         }
                   2408:     }
                   2409:     if (oldVal < changedVal) {
                   2410:         for (var k=oldVal+1; k<=changedVal ; k++) {
                   2411:            var elementName = current[k];
                   2412:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
                   2413:         }
                   2414:     } else {
                   2415:         for (var k=changedVal; k<oldVal; k++) {
                   2416:             var elementName = current[k];
                   2417:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
                   2418:         }
                   2419:     }
                   2420:     return;
                   2421: }
                   2422: 
                   2423: // ]]>
                   2424: </script>
                   2425: 
                   2426: ENDSCRIPT
                   2427: }
                   2428: 
1.267   ! raeburn  2429: sub ltitools_javascript {
        !          2430:     my ($settings) = @_;
        !          2431:     return unless(ref($settings) eq 'HASH');
        !          2432:     my (%ordered,$total,%jstext);
        !          2433:     $total = 0;
        !          2434:     foreach my $item (keys(%{$settings})) {
        !          2435:         if (ref($settings->{$item}) eq 'HASH') {
        !          2436:             my $num = $settings->{$item}{'order'};
        !          2437:             $ordered{$num} = $item;
        !          2438:         }
        !          2439:     }
        !          2440:     $total = scalar(keys(%{$settings}));
        !          2441:     my @jsarray = ();
        !          2442:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
        !          2443:         push(@jsarray,$ordered{$item});
        !          2444:     }
        !          2445:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
        !          2446:     return <<"ENDSCRIPT";
        !          2447: <script type="text/javascript">
        !          2448: // <![CDATA[
        !          2449: function reorderLTI(form,item) {
        !          2450:     var changedVal;
        !          2451: $jstext
        !          2452:     var newpos = 'ltitools_add_pos';
        !          2453:     var maxh = 1 + $total;
        !          2454:     var current = new Array;
        !          2455:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
        !          2456:     if (item == newpos) {
        !          2457:         changedVal = newitemVal;
        !          2458:     } else {
        !          2459:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
        !          2460:         current[newitemVal] = newpos;
        !          2461:     }
        !          2462:     for (var i=0; i<ltitools.length; i++) {
        !          2463:         var elementName = 'ltitools_'+ltitools[i];
        !          2464:         if (elementName != item) {
        !          2465:             if (form.elements[elementName]) {
        !          2466:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
        !          2467:                 current[currVal] = elementName;
        !          2468:             }
        !          2469:         }
        !          2470:     }
        !          2471:     var oldVal;
        !          2472:     for (var j=0; j<maxh; j++) {
        !          2473:         if (current[j] == undefined) {
        !          2474:             oldVal = j;
        !          2475:         }
        !          2476:     }
        !          2477:     if (oldVal < changedVal) {
        !          2478:         for (var k=oldVal+1; k<=changedVal ; k++) {
        !          2479:            var elementName = current[k];
        !          2480:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
        !          2481:         }
        !          2482:     } else {
        !          2483:         for (var k=changedVal; k<oldVal; k++) {
        !          2484:             var elementName = current[k];
        !          2485:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
        !          2486:         }
        !          2487:     }
        !          2488:     return;
        !          2489: }
        !          2490: 
        !          2491: // ]]>
        !          2492: </script>
        !          2493: 
        !          2494: ENDSCRIPT
        !          2495: }
        !          2496: 
1.3       raeburn  2497: sub print_autoenroll {
1.30      raeburn  2498:     my ($dom,$settings,$rowtotal) = @_;
1.3       raeburn  2499:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
1.129     raeburn  2500:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
1.3       raeburn  2501:     if (ref($settings) eq 'HASH') {
                   2502:         if (exists($settings->{'run'})) {
                   2503:             if ($settings->{'run'} eq '0') {
                   2504:                 $runoff = ' checked="checked" ';
                   2505:                 $runon = ' ';
                   2506:             } else {
                   2507:                 $runon = ' checked="checked" ';
                   2508:                 $runoff = ' ';
                   2509:             }
                   2510:         } else {
                   2511:             if ($autorun) {
                   2512:                 $runon = ' checked="checked" ';
                   2513:                 $runoff = ' ';
                   2514:             } else {
                   2515:                 $runoff = ' checked="checked" ';
                   2516:                 $runon = ' ';
                   2517:             }
                   2518:         }
1.129     raeburn  2519:         if (exists($settings->{'co-owners'})) {
                   2520:             if ($settings->{'co-owners'} eq '0') {
                   2521:                 $coownersoff = ' checked="checked" ';
                   2522:                 $coownerson = ' ';
                   2523:             } else {
                   2524:                 $coownerson = ' checked="checked" ';
                   2525:                 $coownersoff = ' ';
                   2526:             }
                   2527:         } else {
                   2528:             $coownersoff = ' checked="checked" ';
                   2529:             $coownerson = ' ';
                   2530:         }
1.3       raeburn  2531:         if (exists($settings->{'sender_domain'})) {
                   2532:             $defdom = $settings->{'sender_domain'};
                   2533:         }
1.14      raeburn  2534:     } else {
                   2535:         if ($autorun) {
                   2536:             $runon = ' checked="checked" ';
                   2537:             $runoff = ' ';
                   2538:         } else {
                   2539:             $runoff = ' checked="checked" ';
                   2540:             $runon = ' ';
                   2541:         }
1.3       raeburn  2542:     }
                   2543:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
1.39      raeburn  2544:     my $notif_sender;
                   2545:     if (ref($settings) eq 'HASH') {
                   2546:         $notif_sender = $settings->{'sender_uname'};
                   2547:     }
1.3       raeburn  2548:     my $datatable='<tr class="LC_odd_row">'.
                   2549:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
1.8       raeburn  2550:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
1.3       raeburn  2551:                   '<input type="radio" name="autoenroll_run"'.
1.8       raeburn  2552:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
                   2553:                   '<label><input type="radio" name="autoenroll_run"'.
1.14      raeburn  2554:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
1.3       raeburn  2555:                   '</tr><tr>'.
                   2556:                   '<td>'.&mt('Notification messages - sender').
1.8       raeburn  2557:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
1.3       raeburn  2558:                   &mt('username').':&nbsp;'.
                   2559:                   '<input type="text" name="sender_uname" value="'.
1.39      raeburn  2560:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
1.129     raeburn  2561:                   ':&nbsp;'.$domform.'</span></td></tr>'.
                   2562:                   '<tr class="LC_odd_row">'.
                   2563:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
                   2564:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
                   2565:                   '<input type="radio" name="autoassign_coowners"'.
                   2566:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
                   2567:                   '<label><input type="radio" name="autoassign_coowners"'.
                   2568:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
                   2569:                   '</tr>';
                   2570:     $$rowtotal += 3;
1.3       raeburn  2571:     return $datatable;
                   2572: }
                   2573: 
                   2574: sub print_autoupdate {
1.30      raeburn  2575:     my ($position,$dom,$settings,$rowtotal) = @_;
1.3       raeburn  2576:     my $datatable;
                   2577:     if ($position eq 'top') {
                   2578:         my $updateon = ' ';
                   2579:         my $updateoff = ' checked="checked" ';
                   2580:         my $classlistson = ' ';
                   2581:         my $classlistsoff = ' checked="checked" ';
                   2582:         if (ref($settings) eq 'HASH') {
                   2583:             if ($settings->{'run'} eq '1') {
                   2584:                 $updateon = $updateoff;
                   2585:                 $updateoff = ' ';
                   2586:             }
                   2587:             if ($settings->{'classlists'} eq '1') {
                   2588:                 $classlistson = $classlistsoff;
                   2589:                 $classlistsoff = ' ';
                   2590:             }
                   2591:         }
                   2592:         my %title = (
                   2593:                    run => 'Auto-update active?',
                   2594:                    classlists => 'Update information in classlists?',
                   2595:                     );
                   2596:         $datatable = '<tr class="LC_odd_row">'. 
                   2597:                   '<td>'.&mt($title{'run'}).'</td>'.
1.8       raeburn  2598:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
1.3       raeburn  2599:                   '<input type="radio" name="autoupdate_run"'.
1.8       raeburn  2600:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
                   2601:                   '<label><input type="radio" name="autoupdate_run"'.
                   2602:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
1.3       raeburn  2603:                   '</tr><tr>'.
                   2604:                   '<td>'.&mt($title{'classlists'}).'</td>'.
1.8       raeburn  2605:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
                   2606:                   '<label><input type="radio" name="classlists"'.
                   2607:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
                   2608:                   '<label><input type="radio" name="classlists"'.
                   2609:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
1.3       raeburn  2610:                   '</tr>';
1.30      raeburn  2611:         $$rowtotal += 2;
1.131     raeburn  2612:     } elsif ($position eq 'middle') {
                   2613:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
                   2614:         my $numinrow = 3;
                   2615:         my $locknamesettings;
                   2616:         $datatable .= &insttypes_row($settings,$types,$usertypes,
                   2617:                                      $dom,$numinrow,$othertitle,
                   2618:                                     'lockablenames');
                   2619:         $$rowtotal ++;
1.3       raeburn  2620:     } else {
1.44      raeburn  2621:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
1.132     raeburn  2622:         my @fields = ('lastname','firstname','middlename','generation',
1.20      raeburn  2623:                       'permanentemail','id');
1.33      raeburn  2624:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
1.3       raeburn  2625:         my $numrows = 0;
1.26      raeburn  2626:         if (ref($types) eq 'ARRAY') {
                   2627:             if (@{$types} > 0) {
                   2628:                 $datatable = 
                   2629:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
                   2630:                                          \@fields,$types,\$numrows);
1.30      raeburn  2631:                     $$rowtotal += @{$types}; 
1.26      raeburn  2632:             }
1.3       raeburn  2633:         }
                   2634:         $datatable .= 
                   2635:             &usertype_update_row($settings,{'default' => $othertitle},
                   2636:                                  \%fieldtitles,\@fields,['default'],
                   2637:                                  \$numrows);
1.30      raeburn  2638:         $$rowtotal ++;     
1.3       raeburn  2639:     }
                   2640:     return $datatable;
                   2641: }
                   2642: 
1.125     raeburn  2643: sub print_autocreate {
                   2644:     my ($dom,$settings,$rowtotal) = @_;
1.191     raeburn  2645:     my (%createon,%createoff,%currhash);
1.125     raeburn  2646:     my @types = ('xml','req');
                   2647:     if (ref($settings) eq 'HASH') {
                   2648:         foreach my $item (@types) {
                   2649:             $createoff{$item} = ' checked="checked" ';
                   2650:             $createon{$item} = ' ';
                   2651:             if (exists($settings->{$item})) {
                   2652:                 if ($settings->{$item}) {
                   2653:                     $createon{$item} = ' checked="checked" ';
                   2654:                     $createoff{$item} = ' ';
                   2655:                 }
                   2656:             }
                   2657:         }
1.210     raeburn  2658:         if ($settings->{'xmldc'} ne '') {
1.191     raeburn  2659:             $currhash{$settings->{'xmldc'}} = 1;
                   2660:         }
1.125     raeburn  2661:     } else {
                   2662:         foreach my $item (@types) {
                   2663:             $createoff{$item} = ' checked="checked" ';
                   2664:             $createon{$item} = ' ';
                   2665:         }
                   2666:     }
                   2667:     $$rowtotal += 2;
1.191     raeburn  2668:     my $numinrow = 2;
1.125     raeburn  2669:     my $datatable='<tr class="LC_odd_row">'.
                   2670:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
                   2671:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
                   2672:                   '<input type="radio" name="autocreate_xml"'.
                   2673:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
                   2674:                   '<label><input type="radio" name="autocreate_xml"'.
1.143     raeburn  2675:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
                   2676:                   '</td></tr><tr>'.
                   2677:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
                   2678:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
                   2679:                   '<input type="radio" name="autocreate_req"'.
                   2680:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
                   2681:                   '<label><input type="radio" name="autocreate_req"'.
                   2682:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
1.191     raeburn  2683:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
                   2684:                                                    'autocreate_xmldc',%currhash);
1.247     raeburn  2685:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
1.125     raeburn  2686:     if ($numdc > 1) {
1.247     raeburn  2687:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
                   2688:                       '</td><td class="LC_left_item">';
1.125     raeburn  2689:     } else {
1.247     raeburn  2690:         $datatable .= &mt('Course creation processed as:').
                   2691:                       '</td><td class="LC_right_item">';
1.125     raeburn  2692:     }
1.247     raeburn  2693:     $datatable .= $dctable.'</td></tr>';
1.191     raeburn  2694:     $$rowtotal += $rows;
1.125     raeburn  2695:     return $datatable;
                   2696: }
                   2697: 
1.23      raeburn  2698: sub print_directorysrch {
1.30      raeburn  2699:     my ($dom,$settings,$rowtotal) = @_;
1.23      raeburn  2700:     my $srchon = ' ';
                   2701:     my $srchoff = ' checked="checked" ';
1.25      raeburn  2702:     my ($exacton,$containson,$beginson);
1.24      raeburn  2703:     my $localon = ' ';
                   2704:     my $localoff = ' checked="checked" ';
1.23      raeburn  2705:     if (ref($settings) eq 'HASH') {
                   2706:         if ($settings->{'available'} eq '1') {
                   2707:             $srchon = $srchoff;
                   2708:             $srchoff = ' ';
                   2709:         }
1.24      raeburn  2710:         if ($settings->{'localonly'} eq '1') {
                   2711:             $localon = $localoff;
                   2712:             $localoff = ' ';
                   2713:         }
1.25      raeburn  2714:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
                   2715:             foreach my $type (@{$settings->{'searchtypes'}}) {
                   2716:                 if ($type eq 'exact') {
                   2717:                     $exacton = ' checked="checked" ';
                   2718:                 } elsif ($type eq 'contains') {
                   2719:                     $containson = ' checked="checked" ';
                   2720:                 } elsif ($type eq 'begins') {
                   2721:                     $beginson = ' checked="checked" ';
                   2722:                 }
                   2723:             }
                   2724:         } else {
                   2725:             if ($settings->{'searchtypes'} eq 'exact') {
                   2726:                 $exacton = ' checked="checked" ';
                   2727:             } elsif ($settings->{'searchtypes'} eq 'contains') {
                   2728:                 $containson = ' checked="checked" ';
                   2729:             } elsif ($settings->{'searchtypes'} eq 'specify') {
                   2730:                 $exacton = ' checked="checked" ';
                   2731:                 $containson = ' checked="checked" ';
                   2732:             }
1.23      raeburn  2733:         }
                   2734:     }
                   2735:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
1.45      raeburn  2736:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
1.23      raeburn  2737: 
                   2738:     my $numinrow = 4;
1.26      raeburn  2739:     my $cansrchrow = 0;
1.23      raeburn  2740:     my $datatable='<tr class="LC_odd_row">'.
1.30      raeburn  2741:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
1.23      raeburn  2742:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
                   2743:                   '<input type="radio" name="dirsrch_available"'.
                   2744:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
                   2745:                   '<label><input type="radio" name="dirsrch_available"'.
                   2746:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
                   2747:                   '</tr><tr>'.
1.30      raeburn  2748:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
1.24      raeburn  2749:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
                   2750:                   '<input type="radio" name="dirsrch_localonly"'.
                   2751:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
                   2752:                   '<label><input type="radio" name="dirsrch_localonly"'.
                   2753:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
1.25      raeburn  2754:                   '</tr>';
1.30      raeburn  2755:     $$rowtotal += 2;
1.26      raeburn  2756:     if (ref($usertypes) eq 'HASH') {
                   2757:         if (keys(%{$usertypes}) > 0) {
1.93      raeburn  2758:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
                   2759:                                          $numinrow,$othertitle,'cansearch');
1.26      raeburn  2760:             $cansrchrow = 1;
                   2761:         }
                   2762:     }
                   2763:     if ($cansrchrow) {
1.30      raeburn  2764:         $$rowtotal ++;
1.26      raeburn  2765:         $datatable .= '<tr>';
                   2766:     } else {
                   2767:         $datatable .= '<tr class="LC_odd_row">';
                   2768:     }
1.30      raeburn  2769:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
                   2770:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
1.25      raeburn  2771:     foreach my $title (@{$titleorder}) {
                   2772:         if (defined($searchtitles->{$title})) {
                   2773:             my $check = ' ';
1.93      raeburn  2774:             if (ref($settings) eq 'HASH') {
1.39      raeburn  2775:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
                   2776:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
                   2777:                         $check = ' checked="checked" ';
                   2778:                     }
1.25      raeburn  2779:                 }
                   2780:             }
                   2781:             $datatable .= '<td class="LC_left_item">'.
                   2782:                           '<span class="LC_nobreak"><label>'.
                   2783:                           '<input type="checkbox" name="searchby" '.
                   2784:                           'value="'.$title.'"'.$check.'/>'.
                   2785:                           $searchtitles->{$title}.'</label></span></td>';
                   2786:         }
                   2787:     }
1.26      raeburn  2788:     $datatable .= '</tr></table></td></tr>';
1.30      raeburn  2789:     $$rowtotal ++;
1.26      raeburn  2790:     if ($cansrchrow) {
                   2791:         $datatable .= '<tr class="LC_odd_row">';
                   2792:     } else {
                   2793:         $datatable .= '<tr>';
                   2794:     }
1.30      raeburn  2795:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
1.26      raeburn  2796:                   '<td class="LC_left_item" colspan="2">'.
1.25      raeburn  2797:                   '<span class="LC_nobreak"><label>'.
                   2798:                   '<input type="checkbox" name="searchtypes" '.
                   2799:                   $exacton.' value="exact" />'.&mt('Exact match').
                   2800:                   '</label>&nbsp;'.
                   2801:                   '<label><input type="checkbox" name="searchtypes" '.
                   2802:                   $beginson.' value="begins" />'.&mt('Begins with').
                   2803:                   '</label>&nbsp;'.
                   2804:                   '<label><input type="checkbox" name="searchtypes" '.
                   2805:                   $containson.' value="contains" />'.&mt('Contains').
                   2806:                   '</label></span></td></tr>';
1.30      raeburn  2807:     $$rowtotal ++;
1.25      raeburn  2808:     return $datatable;
                   2809: }
                   2810: 
1.28      raeburn  2811: sub print_contacts {
1.30      raeburn  2812:     my ($dom,$settings,$rowtotal) = @_;
1.28      raeburn  2813:     my $datatable;
                   2814:     my @contacts = ('adminemail','supportemail');
1.134     raeburn  2815:     my (%checked,%to,%otheremails,%bccemails);
1.102     raeburn  2816:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
1.203     raeburn  2817:                     'requestsmail','updatesmail','idconflictsmail');
1.28      raeburn  2818:     foreach my $type (@mailings) {
                   2819:         $otheremails{$type} = '';
                   2820:     }
1.134     raeburn  2821:     $bccemails{'helpdeskmail'} = '';
1.28      raeburn  2822:     if (ref($settings) eq 'HASH') {
                   2823:         foreach my $item (@contacts) {
                   2824:             if (exists($settings->{$item})) {
                   2825:                 $to{$item} = $settings->{$item};
                   2826:             }
                   2827:         }
                   2828:         foreach my $type (@mailings) {
                   2829:             if (exists($settings->{$type})) {
                   2830:                 if (ref($settings->{$type}) eq 'HASH') {
                   2831:                     foreach my $item (@contacts) {
                   2832:                         if ($settings->{$type}{$item}) {
                   2833:                             $checked{$type}{$item} = ' checked="checked" ';
                   2834:                         }
                   2835:                     }
                   2836:                     $otheremails{$type} = $settings->{$type}{'others'};
1.134     raeburn  2837:                     if ($type eq 'helpdeskmail') {
                   2838:                         $bccemails{$type} = $settings->{$type}{'bcc'};
                   2839:                     }
1.28      raeburn  2840:                 }
1.89      raeburn  2841:             } elsif ($type eq 'lonstatusmail') {
                   2842:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
1.28      raeburn  2843:             }
                   2844:         }
                   2845:     } else {
                   2846:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
                   2847:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
                   2848:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
                   2849:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
1.89      raeburn  2850:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
                   2851:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
1.102     raeburn  2852:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
1.190     raeburn  2853:         $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
1.203     raeburn  2854:         $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
1.28      raeburn  2855:     }
                   2856:     my ($titles,$short_titles) = &contact_titles();
                   2857:     my $rownum = 0;
                   2858:     my $css_class;
                   2859:     foreach my $item (@contacts) {
1.69      raeburn  2860:         $css_class = $rownum%2?' class="LC_odd_row"':'';
1.30      raeburn  2861:         $datatable .= '<tr'.$css_class.'>'. 
                   2862:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
                   2863:                   '</span></td><td class="LC_right_item">'.
1.28      raeburn  2864:                   '<input type="text" name="'.$item.'" value="'.
                   2865:                   $to{$item}.'" /></td></tr>';
1.203     raeburn  2866:         $rownum ++;
1.28      raeburn  2867:     }
                   2868:     foreach my $type (@mailings) {
1.69      raeburn  2869:         $css_class = $rownum%2?' class="LC_odd_row"':'';
1.28      raeburn  2870:         $datatable .= '<tr'.$css_class.'>'.
1.30      raeburn  2871:                       '<td><span class="LC_nobreak">'.
                   2872:                       $titles->{$type}.': </span></td>'.
1.28      raeburn  2873:                       '<td class="LC_left_item">'.
                   2874:                       '<span class="LC_nobreak">';
                   2875:         foreach my $item (@contacts) {
                   2876:             $datatable .= '<label>'.
                   2877:                           '<input type="checkbox" name="'.$type.'"'.
                   2878:                           $checked{$type}{$item}.
                   2879:                           ' value="'.$item.'" />'.$short_titles->{$item}.
                   2880:                           '</label>&nbsp;';
                   2881:         }
                   2882:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
                   2883:                       '<input type="text" name="'.$type.'_others" '.
1.134     raeburn  2884:                       'value="'.$otheremails{$type}.'"  />';
                   2885:         if ($type eq 'helpdeskmail') {
1.136     raeburn  2886:             $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
1.134     raeburn  2887:                           '<input type="text" name="'.$type.'_bcc" '.
                   2888:                           'value="'.$bccemails{$type}.'"  />';
                   2889:         }
                   2890:         $datatable .= '</td></tr>'."\n";
1.203     raeburn  2891:         $rownum ++;
1.28      raeburn  2892:     }
1.203     raeburn  2893:     my %choices;
                   2894:     $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
                   2895:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
                   2896:                                    &mt('LON-CAPA core group - MSU'),600,500));
                   2897:     $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
                   2898:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
                   2899:                                     &mt('LON-CAPA core group - MSU'),600,500));
                   2900:     my @toggles = ('reporterrors','reportupdates');
                   2901:     my %defaultchecked = ('reporterrors'  => 'on',
                   2902:                           'reportupdates' => 'on');
                   2903:     (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
                   2904:                                                \%choices,$rownum);
                   2905:     $datatable .= $reports;
1.30      raeburn  2906:     $$rowtotal += $rownum;
1.28      raeburn  2907:     return $datatable;
                   2908: }
                   2909: 
1.118     jms      2910: sub print_helpsettings {
1.168     raeburn  2911:     my ($dom,$confname,$settings,$rowtotal) = @_;
                   2912:     my ($datatable,$itemcount);
1.166     raeburn  2913:     $itemcount = 1;
1.168     raeburn  2914:     my (%choices,%defaultchecked,@toggles);
                   2915:     $choices{'submitbugs'} = &mt('Display link to: [_1]?',
                   2916:                                  &Apache::loncommon::modal_link('http://bugs.loncapa.org',
                   2917:                                  &mt('LON-CAPA bug tracker'),600,500));
                   2918:     %defaultchecked = ('submitbugs' => 'on');
                   2919:     @toggles = ('submitbugs',);
1.166     raeburn  2920: 
1.168     raeburn  2921:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
                   2922:                                                  \%choices,$itemcount);
1.166     raeburn  2923:     return $datatable;
1.121     raeburn  2924: }
                   2925: 
                   2926: sub radiobutton_prefs {
1.192     raeburn  2927:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
1.257     raeburn  2928:         $additional,$align) = @_;
1.121     raeburn  2929:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
                   2930:                    (ref($choices) eq 'HASH'));
                   2931: 
1.170     raeburn  2932:     my (%checkedon,%checkedoff,$datatable,$css_class);
1.121     raeburn  2933: 
                   2934:     foreach my $item (@{$toggles}) {
                   2935:         if ($defaultchecked->{$item} eq 'on') {
1.118     jms      2936:             $checkedon{$item} = ' checked="checked" ';
                   2937:             $checkedoff{$item} = ' ';
1.121     raeburn  2938:         } elsif ($defaultchecked->{$item} eq 'off') {
1.118     jms      2939:             $checkedoff{$item} = ' checked="checked" ';
                   2940:             $checkedon{$item} = ' ';
                   2941:         }
                   2942:     }
                   2943:     if (ref($settings) eq 'HASH') {
1.121     raeburn  2944:         foreach my $item (@{$toggles}) {
1.118     jms      2945:             if ($settings->{$item} eq '1') {
                   2946:                 $checkedon{$item} =  ' checked="checked" ';
                   2947:                 $checkedoff{$item} = ' ';
                   2948:             } elsif ($settings->{$item} eq '0') {
                   2949:                 $checkedoff{$item} =  ' checked="checked" ';
                   2950:                 $checkedon{$item} = ' ';
                   2951:             }
                   2952:         }
1.121     raeburn  2953:     }
1.192     raeburn  2954:     if ($onclick) {
                   2955:         $onclick = ' onclick="'.$onclick.'"';
                   2956:     }
1.121     raeburn  2957:     foreach my $item (@{$toggles}) {
1.118     jms      2958:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
1.121     raeburn  2959:         $datatable .=
1.192     raeburn  2960:             '<tr'.$css_class.'><td valign="top">'.
                   2961:             '<span class="LC_nobreak">'.$choices->{$item}.
1.257     raeburn  2962:             '</span></td>';
                   2963:         if ($align eq 'left') {
                   2964:             $datatable .= '<td class="LC_left_item">';
                   2965:         } else {
                   2966:             $datatable .= '<td class="LC_right_item">';
                   2967:         }
                   2968:         $datatable .= 
                   2969:             '<span class="LC_nobreak">'.
1.118     jms      2970:             '<label><input type="radio" name="'.
1.192     raeburn  2971:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
1.118     jms      2972:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
1.192     raeburn  2973:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
                   2974:             '</span>'.$additional.
                   2975:             '</td>'.
1.118     jms      2976:             '</tr>';
                   2977:         $itemcount ++;
1.121     raeburn  2978:     }
                   2979:     return ($datatable,$itemcount);
                   2980: }
                   2981: 
1.267   ! raeburn  2982: sub print_ltitools {
        !          2983:     my ($dom,$settings,$rowtotal) = @_;
        !          2984:     my $rownum = 0;
        !          2985:     my $css_class;
        !          2986:     my $itemcount = 1;
        !          2987:     my $maxnum = 0;
        !          2988:     my %ordered;
        !          2989:     if (ref($settings) eq 'HASH') {
        !          2990:         foreach my $item (keys(%{$settings})) {
        !          2991:             if (ref($settings->{$item}) eq 'HASH') {
        !          2992:                 my $num = $settings->{$item}{'order'};
        !          2993:                 $ordered{$num} = $item;
        !          2994:             }
        !          2995:         }
        !          2996:     }
        !          2997:     my $confname = $dom.'-domainconfig';
        !          2998:     my $switchserver = &check_switchserver($dom,$confname);
        !          2999:     my $maxnum = scalar(keys(%ordered));
        !          3000:     my $datatable = &ltitools_javascript($settings);
        !          3001:     my %lt = &ltitools_names();
        !          3002:     my @courseroles = ('cc','in','ta','ep','st');
        !          3003:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
        !          3004:     my @fields = ('fullname','firstname','lastname','email','user','roles');
        !          3005:     if (keys(%ordered)) {
        !          3006:         my @items = sort { $a <=> $b } keys(%ordered);
        !          3007:         for (my $i=0; $i<@items; $i++) {
        !          3008:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
        !          3009:             my $item = $ordered{$items[$i]};
        !          3010:             my ($title,$key,$secret,$url,$imgsrc,$version);
        !          3011:             if (ref($settings->{$item}) eq 'HASH') {
        !          3012:                 $title = $settings->{$item}->{'title'};
        !          3013:                 $url = $settings->{$item}->{'url'};
        !          3014:                 $key = $settings->{$item}->{'key'};
        !          3015:                 $secret = $settings->{$item}->{'secret'};
        !          3016:                 my $image = $settings->{$item}->{'image'};
        !          3017:                 if ($image ne '') {
        !          3018:                     $imgsrc = '<img src="'.$image.'" alt="'.&mt('Tool Provider icon').'" />';
        !          3019:                 }
        !          3020:             }
        !          3021:             my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_".$item."'".');"';
        !          3022:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
        !          3023:                          .'<select name="ltitools_'.$item.'"'.$chgstr.'>';
        !          3024:             for (my $k=0; $k<=$maxnum; $k++) {
        !          3025:                 my $vpos = $k+1;
        !          3026:                 my $selstr;
        !          3027:                 if ($k == $i) {
        !          3028:                     $selstr = ' selected="selected" ';
        !          3029:                 }
        !          3030:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
        !          3031:             }
        !          3032:             $datatable .= '</select>'.('&nbsp;'x2).
        !          3033:                 '<label><input type="checkbox" name="ltitools_del" value="'.$item.'" />'.
        !          3034:                 &mt('Delete?').'</label></span></td>'.
        !          3035:                 '<td colspan="2">'.
        !          3036:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
        !          3037:                 '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="30" name="ltitools_title_'.$i.'" value="'.$title.'" /></span> '.
        !          3038:                 ('&nbsp;'x2).
        !          3039:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_version_'.$i.'">'.
        !          3040:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
        !          3041:                 ('&nbsp;'x2).
        !          3042:                 '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_msgtype_'.$i.'">'.
        !          3043:                 '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
        !          3044:                 '<br /><br />'.
        !          3045:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="30" name="ltitools_url_'.$i.'"'.
        !          3046:                 ' value="'.$url.'" /></span>'.
        !          3047:                 ('&nbsp;'x2).
        !          3048:                 '<span class="LC_nobreak">'.$lt{'key'}.
        !          3049:                 '<input type="text" size="25" name="ltitools_key_'.$i.'" value="'.$key.'" /></span> '.
        !          3050:                 ('&nbsp;'x2).
        !          3051:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
        !          3052:                 '<input type="password" size="20" name="ltitools_secret_'.$i.'" value="'.$secret.'" />'.
        !          3053:                 '<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>'.
        !          3054:                 '<input type="hidden" name="ltitools_id_'.$i.'" value="'.$item.'" /></span>'.
        !          3055:                 '</fieldset>'.
        !          3056:                 '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
        !          3057:                 '<span class="LC_nobreak">'.&mt('Display target:');
        !          3058:             my %currdisp;
        !          3059:             if (ref($settings->{$item}->{'display'}) eq 'HASH') {
        !          3060:                 if ($settings->{$item}->{'display'}->{'target'} eq 'window') {
        !          3061:                     $currdisp{'window'} = ' checked="checked"';
        !          3062:                 } else {
        !          3063:                     $currdisp{'iframe'} = ' checked="checked"';
        !          3064:                 }
        !          3065:                 if ($settings->{$item}->{'display'}->{'width'} =~ /^(\d+)$/) {
        !          3066:                     $currdisp{'width'} = $1;
        !          3067:                 }
        !          3068:                 if ($settings->{$item}->{'display'}->{'height'} =~ /^(\d+)$/) {
        !          3069:                      $currdisp{'height'} = $1;
        !          3070:                 }
        !          3071:             } else {
        !          3072:                 $currdisp{'iframe'} = ' checked="checked"';
        !          3073:             }
        !          3074:             foreach my $disp ('iframe','window') {
        !          3075:                 $datatable .= '<label><input type="radio" name="ltitools_target_'.$i.'" value="'.$disp.'"'.$currdisp{$disp}.' />'.
        !          3076:                               $lt{$disp}.'</label>'.('&nbsp;'x2);
        !          3077:             }
        !          3078:             $datatable .= ('&nbsp;'x4);
        !          3079:             foreach my $dimen ('width','height') {
        !          3080:                 $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
        !          3081:                               '<input type="text" name="ltitools_'.$dimen.'_'.$i.'" size="5" value="'.$currdisp{$dimen}.'" /></label>'.
        !          3082:                               ('&nbsp;'x2);
        !          3083:             }
        !          3084:             $datatable .= '<br />';
        !          3085:             foreach my $extra ('passback','roster') {
        !          3086:                 my $checkedon = '';
        !          3087:                 my $checkedoff = ' checked="checked"';
        !          3088:                 if ($settings->{$item}->{$extra}) {
        !          3089:                     $checkedon = $checkedoff;
        !          3090:                     $checkedoff = '';
        !          3091:                 }
        !          3092:                 $datatable .= $lt{$extra}.'&nbsp;'.
        !          3093:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="1"'.$checkedon.' />'.
        !          3094:                               &mt('Yes').'</label>'.('&nbsp;'x2).
        !          3095:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="0"'.$checkedoff.' />'.
        !          3096:                               &mt('No').'</label>'.('&nbsp;'x4);
        !          3097:             }
        !          3098:             $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
        !          3099:             if ($imgsrc) {
        !          3100:                 $datatable .= $imgsrc.
        !          3101:                               '<label><input type="checkbox" name="ltitools_image_del"'.
        !          3102:                               ' value="'.$item.'" />'.&mt('Delete?').'</label></span> '.
        !          3103:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
        !          3104:             } else {
        !          3105:                 $datatable .= '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
        !          3106:             }
        !          3107:             if ($switchserver) {
        !          3108:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
        !          3109:             } else {
        !          3110:                 $datatable .= '<input type="file" name="ltitools_image_'.$i.'" value="" />';
        !          3111:             }
        !          3112:             $datatable .= '</span></fieldset>';
        !          3113:             my (%checkedfields,%rolemaps);
        !          3114:             if (ref($settings->{$item}) eq 'HASH') {
        !          3115:                 if (ref($settings->{$item}->{'fields'}) eq 'HASH') {
        !          3116:                     %checkedfields = %{$settings->{$item}->{'fields'}};
        !          3117:                 }
        !          3118:                 if (ref($settings->{$item}->{'roles'}) eq 'HASH') {
        !          3119:                     %rolemaps = %{$settings->{$item}->{'roles'}};
        !          3120:                     $checkedfields{'roles'} = 1;
        !          3121:                 }
        !          3122:             }
        !          3123:             $datatable .= '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
        !          3124:                           '<span class="LC_nobreak">';
        !          3125:             foreach my $field (@fields) {
        !          3126:                 my $checked;
        !          3127:                 if ($checkedfields{$field}) {
        !          3128:                     $checked = ' checked="checked"';
        !          3129:                 }
        !          3130:                 $datatable .= '<label>'.
        !          3131:                               '<input type="checkbox" name="ltitools_fields_'.$i.'" value="'.$field.'"'.$checked.' />'.
        !          3132:                               $lt{$field}.'</label>'.('&nbsp;' x2);
        !          3133:             }
        !          3134:             $datatable .= '</span></fieldset>'.
        !          3135:                           '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
        !          3136:             foreach my $role (@courseroles) {
        !          3137:                 my ($selected,$selectnone);
        !          3138:                 if (!$rolemaps{$role}) {
        !          3139:                     $selectnone = ' selected="selected"';
        !          3140:                 }
        !          3141:                 $datatable .= '<td align="center">'. 
        !          3142:                               &Apache::lonnet::plaintext($role,'Course').'<br />'.
        !          3143:                               '<select name="ltitools_roles_'.$role.'_'.$i.'">'.
        !          3144:                               '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
        !          3145:                 foreach my $ltirole (@ltiroles) {
        !          3146:                     unless ($selectnone) {
        !          3147:                         if ($rolemaps{$role} eq $ltirole) {
        !          3148:                             $selected = ' selected="selected"';
        !          3149:                         } else {
        !          3150:                             $selected = '';
        !          3151:                         }
        !          3152:                     }
        !          3153:                     $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
        !          3154:                 }
        !          3155:                 $datatable .= '</select></td>';
        !          3156:             }
        !          3157:             $datatable .= '</tr></table></fieldset>'.
        !          3158:                           '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
        !          3159:                           '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>';
        !          3160:             if (ref($settings->{$item}->{'custom'}) eq 'HASH') {
        !          3161:                 my %custom = %{$settings->{$item}->{'custom'}};
        !          3162:                 if (keys(%custom) > 0) {
        !          3163:                     foreach my $key (sort(keys(%custom))) {
        !          3164:                         $datatable .= '<tr><td><span class="LC_nobreak">'.
        !          3165:                                       '<label><input type="checkbox" name="ltitools_customdel_'.$i.'" value="'.
        !          3166:                                       $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
        !          3167:                                       '<td><input type="text" name="ltitools_customval_'.$key.'_'.$i.'"'.
        !          3168:                                       ' value="'.$custom{$key}.'" /></td></tr>';
        !          3169:                     }
        !          3170:                 }
        !          3171:             }
        !          3172:             $datatable .= '<tr><td><span class="LC_nobreak">'.
        !          3173:                           '<label><input type="checkbox" name="ltitools_customadd" value="'.$i.'" />'.
        !          3174:                           &mt('Add').'</label></span></td><td><input type="text" name="ltitools_custom_name_'.$i.'" />'.
        !          3175:                           '</td><td><input type="text" name="ltitools_custom_value_'.$i.'" /></td></tr>';
        !          3176:             $datatable .= '</table></fieldset></td></tr>'."\n";
        !          3177:             $itemcount ++;
        !          3178:         }
        !          3179:     }
        !          3180:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
        !          3181:     my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_add_pos'".');"';
        !          3182:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
        !          3183:                   '<input type="hidden" name="ltitools_maxnum" value="'.$maxnum.'" />'."\n".
        !          3184:                   '<select name="ltitools_add_pos"'.$chgstr.'>';
        !          3185:     for (my $k=0; $k<$maxnum+1; $k++) {
        !          3186:         my $vpos = $k+1;
        !          3187:         my $selstr;
        !          3188:         if ($k == $maxnum) {
        !          3189:             $selstr = ' selected="selected" ';
        !          3190:         }
        !          3191:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
        !          3192:     }
        !          3193:     $datatable .= '</select>&nbsp;'."\n".
        !          3194:                   '<input type="checkbox" name="ltitools_add" value="1" />'.&mt('Add').'</td>'."\n".
        !          3195:                   '<td colspan="2">'.
        !          3196:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
        !          3197:                   '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="30" name="ltitools_add_title" value="" /></span> '."\n".
        !          3198:                   ('&nbsp;'x2).
        !          3199:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_add_version">'.
        !          3200:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
        !          3201:                   ('&nbsp;'x2).
        !          3202:                   '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_add_msgtype">'.
        !          3203:                   '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
        !          3204:                   '<br />'.
        !          3205:                   '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="30" name="ltitools_add_url" value="" /></span> '."\n".
        !          3206:                   ('&nbsp;'x2).
        !          3207:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="ltitools_add_key" value="" /></span> '."\n".
        !          3208:                   ('&nbsp;'x2).
        !          3209:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="ltitools_add_secret" value="" />'.
        !          3210:                   '<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".
        !          3211:                   '</fieldset>'.
        !          3212:                   '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
        !          3213:                   '<span class="LC_nobreak">'.&mt('Display target:');
        !          3214:     my %defaultdisp;
        !          3215:     $defaultdisp{'iframe'} = ' checked="checked"';
        !          3216:     foreach my $disp ('iframe','window') {
        !          3217:         $datatable .= '<label><input type="radio" name="ltitools_add_target" value="'.$disp.'"'.$defaultdisp{$disp}.' />'.
        !          3218:                       $lt{$disp}.'</label>'.('&nbsp;'x2);
        !          3219:     }
        !          3220:     $datatable .= ('&nbsp;'x4);
        !          3221:     foreach my $dimen ('width','height') {
        !          3222:         $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
        !          3223:                       '<input type="text" name="ltitools_add_'.$dimen.'" size="5" /></label>'.
        !          3224:                       ('&nbsp;'x2);
        !          3225:     }
        !          3226:     $datatable .= '<br />';
        !          3227:     foreach my $extra ('passback','roster') {
        !          3228:         $datatable .= $lt{$extra}.'&nbsp;'.
        !          3229:                       '<label><input type="radio" name="ltitools_add_'.$extra.'" value="1" />'.
        !          3230:                       &mt('Yes').'</label>'.('&nbsp;'x2).
        !          3231:                       '<label><input type="radio" name="ltitools_add_'.$extra.'" value="0" checked="checked" />'.
        !          3232:                       &mt('No').'</label>'.('&nbsp;'x4);
        !          3233:     }
        !          3234:     $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;'.
        !          3235:                   '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
        !          3236:     if ($switchserver) {
        !          3237:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
        !          3238:     } else {
        !          3239:         $datatable .= '<input type="file" name="ltitools_add_image" value="" />';
        !          3240:     }
        !          3241:     $datatable .= '</span></fieldset>'.
        !          3242:                   '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
        !          3243:                   '<span class="LC_nobreak">';
        !          3244:     foreach my $field (@fields) {
        !          3245:         $datatable .= '<label>'.
        !          3246:                       '<input type="checkbox" name="ltitools_add_fields" value="'.$field.'" />'.
        !          3247:                       $lt{$field}.'</label>'.('&nbsp;' x2);
        !          3248:     }
        !          3249:     $datatable .= '</span></fieldset>'.
        !          3250:                   '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
        !          3251:     foreach my $role (@courseroles) {
        !          3252:         my ($checked,$checkednone);
        !          3253:         $datatable .= '<td align="center">'.
        !          3254:                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
        !          3255:                       '<select name="ltitools_add_roles_'.$role.'">'.
        !          3256:                       '<option value="" selected="selected">'.&mt('Select').'</option>';
        !          3257:         foreach my $ltirole (@ltiroles) {
        !          3258:             $datatable .= '<option value="'.$ltirole.'">'.$ltirole.'</option>';
        !          3259:         }
        !          3260:         $datatable .= '</select></td>';
        !          3261:     }
        !          3262:     $datatable .= '</tr></table></fieldset>'.
        !          3263:                   '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
        !          3264:                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
        !          3265:                   '<tr><td><span class="LC_nobreak">'.
        !          3266:                   '<label><input type="checkbox" name="ltitools_add_custom" value="1" />'.
        !          3267:                   &mt('Add').'</label></span></td><td><input type="text" name="ltitools_add_custom_name" />'.
        !          3268:                   '</td><td><input type="text" name="ltitools_add_custom_value" /></td></tr>'.
        !          3269:                   '</table></fieldset></td></tr>'."\n".
        !          3270:                   '</td>'."\n".
        !          3271:                   '</tr>'."\n";
        !          3272:     $itemcount ++;
        !          3273:     return $datatable;
        !          3274: }
        !          3275: 
        !          3276: sub ltitools_names {
        !          3277:     my %lt = &Apache::lonlocal::texthash(
        !          3278:                                           'title'     => 'Title',
        !          3279:                                           'version'   => 'Version',
        !          3280:                                           'msgtype'   => 'Message Type',
        !          3281:                                           'url'       => 'URL',
        !          3282:                                           'key'       => 'Key',
        !          3283:                                           'secret'    => 'Secret',
        !          3284:                                           'icon'      => 'Icon',   
        !          3285:                                           'user'      => 'Username:domain',
        !          3286:                                           'fullname'  => 'Full Name',
        !          3287:                                           'firstname' => 'First Name',
        !          3288:                                           'lastname'  => 'Last Name',
        !          3289:                                           'email'     => 'E-mail',
        !          3290:                                           'roles'     => 'Role',
        !          3291:                                           'window'    => 'Window/Tab',
        !          3292:                                           'iframe'    => 'iFrame',
        !          3293:                                           'height'    => 'Height',
        !          3294:                                           'width'     => 'Width',
        !          3295:                                           'passback'  => 'Tool can return grades:',
        !          3296:                                           'roster'    => 'Tool can retrieve roster:',
        !          3297:                                         );
        !          3298:     return %lt;
        !          3299: }
        !          3300: 
1.121     raeburn  3301: sub print_coursedefaults {
1.139     raeburn  3302:     my ($position,$dom,$settings,$rowtotal) = @_;
1.192     raeburn  3303:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
1.121     raeburn  3304:     my $itemcount = 1;
1.192     raeburn  3305:     my %choices =  &Apache::lonlocal::texthash (
                   3306:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
1.198     raeburn  3307:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
1.192     raeburn  3308:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
                   3309:         coursecredits        => 'Credits can be specified for courses',
1.257     raeburn  3310:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
                   3311:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
                   3312:         postsubmit           => 'Disable submit button/keypress following student submission',
1.264     raeburn  3313:         canclone             => "People who may clone a course (besides course's owner and coordinators)",     
1.192     raeburn  3314:     );
1.198     raeburn  3315:     my %staticdefaults = (
                   3316:                            anonsurvey_threshold => 10,
                   3317:                            uploadquota          => 500,
1.257     raeburn  3318:                            postsubmit           => 60,
1.198     raeburn  3319:                          );
1.139     raeburn  3320:     if ($position eq 'top') {
1.257     raeburn  3321:         %defaultchecked = (
                   3322:                             'canuse_pdfforms' => 'off',
                   3323:                             'uselcmath'       => 'on',
                   3324:                             'usejsme'         => 'on',
1.264     raeburn  3325:                             'canclone'        => 'none', 
1.257     raeburn  3326:                           );
                   3327:         @toggles = ('canuse_pdfforms','uselcmath','usejsme');
1.139     raeburn  3328:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
1.257     raeburn  3329:                                                      \%choices,$itemcount);
1.264     raeburn  3330:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
                   3331:         $datatable .=
                   3332:             '<tr'.$css_class.'><td valign="top">'.
                   3333:             '<span class="LC_nobreak">'.$choices{'canclone'}.
                   3334:             '</span></td><td class="LC_left_item">';
                   3335:         my $currcanclone = 'none';
                   3336:         my $onclick;
                   3337:         my @cloneoptions = ('none','domain');
                   3338:         my %clonetitles = (
                   3339:                              none     => 'No additional course requesters',
                   3340:                              domain   => "Any course requester in course's domain",
                   3341:                              instcode => 'Course requests for official courses ...',
                   3342:                           );
                   3343:         my (%codedefaults,@code_order,@posscodes);
                   3344:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
                   3345:                                                     \@code_order) eq 'ok') {
                   3346:             if (@code_order > 0) {
                   3347:                 push(@cloneoptions,'instcode');
                   3348:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
                   3349:             }
                   3350:         }
                   3351:         if (ref($settings) eq 'HASH') {
                   3352:             if ($settings->{'canclone'}) {
                   3353:                 if (ref($settings->{'canclone'}) eq 'HASH') {
                   3354:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
                   3355:                         if (@code_order > 0) {
                   3356:                             $currcanclone = 'instcode';
                   3357:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
                   3358:                         }
                   3359:                     }
                   3360:                 } elsif ($settings->{'canclone'} eq 'domain') {
                   3361:                     $currcanclone = $settings->{'canclone'};
                   3362:                 }
                   3363:             }
                   3364:         } 
                   3365:         foreach my $option (@cloneoptions) {
                   3366:             my ($checked,$additional);
                   3367:             if ($currcanclone eq $option) {
                   3368:                 $checked = ' checked="checked"';
                   3369:             }
                   3370:             if ($option eq 'instcode') {
                   3371:                 if (@code_order) {
                   3372:                     my $show = 'none';
                   3373:                     if ($checked) {
                   3374:                         $show = 'block';
                   3375:                     }
                   3376:                     $additional = '<div id="cloneinstcode" style="display:'.$show.'" />'.
                   3377:                                   &mt('Institutional codes for new and cloned course have identical:').
                   3378:                                   '<br />';
                   3379:                     foreach my $item (@code_order) {
                   3380:                         my $codechk;
                   3381:                         if ($checked) {
                   3382:                             if (grep(/^\Q$item\E$/,@posscodes)) {
                   3383:                                 $codechk = ' checked="checked"';
                   3384:                             }
                   3385:                         }
                   3386:                         $additional .= '<label>'.
                   3387:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
                   3388:                                        $item.'</label>';
                   3389:                     }
                   3390:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
                   3391:                 }
                   3392:             }
                   3393:             $datatable .=
                   3394:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
                   3395:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
                   3396:                 '</label>&nbsp;'.$additional.'</span><br />';
                   3397:         }
                   3398:         $datatable .= '</td>'.
                   3399:                       '</tr>';
                   3400:         $itemcount ++;
1.139     raeburn  3401:     } else {
                   3402:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
1.257     raeburn  3403:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout);
1.192     raeburn  3404:         my $currusecredits = 0;
1.257     raeburn  3405:         my $postsubmitclient = 1;
1.216     raeburn  3406:         my @types = ('official','unofficial','community','textbook');
1.139     raeburn  3407:         if (ref($settings) eq 'HASH') {
                   3408:             $currdefresponder = $settings->{'anonsurvey_threshold'};
1.198     raeburn  3409:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
                   3410:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
                   3411:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
                   3412:                 }
                   3413:             }
1.192     raeburn  3414:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
1.257     raeburn  3415:                 foreach my $type (@types) {
                   3416:                     next if ($type eq 'community');
                   3417:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
                   3418:                     if ($defcredits{$type} ne '') {
                   3419:                         $currusecredits = 1;
                   3420:                     }
                   3421:                 }
                   3422:             }
                   3423:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
                   3424:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
                   3425:                     $postsubmitclient = 0;
                   3426:                     foreach my $type (@types) {
                   3427:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
                   3428:                     }
                   3429:                 } else {
                   3430:                     foreach my $type (@types) {
                   3431:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
                   3432:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
                   3433:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type}; 
                   3434:                             } else {
                   3435:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
                   3436:                             }
                   3437:                         } else {
                   3438:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
                   3439:                         }
                   3440:                     }
                   3441:                 }
                   3442:             } else {
                   3443:                 foreach my $type (@types) {
                   3444:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
1.192     raeburn  3445:                 }
                   3446:             }
1.258     raeburn  3447:         } else {
                   3448:             foreach my $type (@types) {
                   3449:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
                   3450:             }
1.139     raeburn  3451:         }
                   3452:         if (!$currdefresponder) {
1.198     raeburn  3453:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
1.139     raeburn  3454:         } elsif ($currdefresponder < 1) {
                   3455:             $currdefresponder = 1;
                   3456:         }
1.198     raeburn  3457:         foreach my $type (@types) {
                   3458:             if ($curruploadquota{$type} eq '') {
                   3459:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
                   3460:             }
                   3461:         }
1.139     raeburn  3462:         $datatable .=
1.192     raeburn  3463:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
                   3464:                 $choices{'anonsurvey_threshold'}.
1.139     raeburn  3465:                 '</span></td>'.
                   3466:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
                   3467:                 '<input type="text" name="anonsurvey_threshold"'.
                   3468:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
1.230     raeburn  3469:                 '</td></tr>'."\n";
                   3470:         $itemcount ++;
                   3471:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
                   3472:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
                   3473:                       $choices{'uploadquota'}.
                   3474:                       '</span></td>'.
                   3475:                       '<td align="right" class="LC_right_item">'.
                   3476:                       '<table><tr>';
1.198     raeburn  3477:         foreach my $type (@types) {
                   3478:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
                   3479:                            '<input type="text" name="uploadquota_'.$type.'"'.
                   3480:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
                   3481:         }
                   3482:         $datatable .= '</tr></table></td></tr>'."\n";
1.230     raeburn  3483:         $itemcount ++;
1.236     raeburn  3484:         my $onclick = "toggleDisplay(this.form,'credits');";
1.210     raeburn  3485:         my $display = 'none';
1.192     raeburn  3486:         if ($currusecredits) {
                   3487:             $display = 'block';
                   3488:         }
                   3489:         my $additional = '<div id="credits" style="display: '.$display.'">'.
1.257     raeburn  3490:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
                   3491:         foreach my $type (@types) {
                   3492:             next if ($type eq 'community');
                   3493:             $additional .= '<td align="center">'.&mt($type).'<br />'.
                   3494:                            '<input type="text" name="'.$type.'_credits"'.
1.258     raeburn  3495:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
1.257     raeburn  3496:         }
                   3497:         $additional .= '</tr></table></div>'."\n";
1.192     raeburn  3498:         %defaultchecked = ('coursecredits' => 'off');
                   3499:         @toggles = ('coursecredits');
                   3500:         my $current = {
                   3501:                         'coursecredits' => $currusecredits,
                   3502:                       };
                   3503:         (my $table,$itemcount) =
                   3504:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
1.257     raeburn  3505:                                \%choices,$itemcount,$onclick,$additional,'left');
                   3506:         $datatable .= $table;
                   3507:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
                   3508:         my $display = 'none';
                   3509:         if ($postsubmitclient) {
                   3510:             $display = 'block';
                   3511:         }
                   3512:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
1.259     raeburn  3513:                       &mt('Number of seconds submit is disabled').'<br />'.
                   3514:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
                   3515:                       '<table><tr>';
1.257     raeburn  3516:         foreach my $type (@types) {
                   3517:             $additional .= '<td align="center">'.&mt($type).'<br />'.
                   3518:                            '<input type="text" name="'.$type.'_timeout" value="'.
                   3519:                            $deftimeout{$type}.'" size="5" /></td>';
                   3520:         }
                   3521:         $additional .= '</tr></table></div>'."\n";
                   3522:         %defaultchecked = ('postsubmit' => 'on');
                   3523:         @toggles = ('postsubmit');
                   3524:         my $current = {
                   3525:                         'postsubmit' => $postsubmitclient,
                   3526:                       };
                   3527:         ($table,$itemcount) =
                   3528:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
                   3529:                                \%choices,$itemcount,$onclick,$additional,'left');
1.192     raeburn  3530:         $datatable .= $table;
1.139     raeburn  3531:     }
1.192     raeburn  3532:     $$rowtotal += $itemcount;
1.121     raeburn  3533:     return $datatable;
1.118     jms      3534: }
                   3535: 
1.231     raeburn  3536: sub print_selfenrollment {
                   3537:     my ($position,$dom,$settings,$rowtotal) = @_;
                   3538:     my ($css_class,$datatable);
                   3539:     my $itemcount = 1;
                   3540:     my @types = ('official','unofficial','community','textbook');
                   3541:     if (($position eq 'top') || ($position eq 'middle')) {
1.232     raeburn  3542:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
                   3543:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
1.231     raeburn  3544:         my @rows;
                   3545:         my $key;
                   3546:         if ($position eq 'top') {
                   3547:             $key = 'admin'; 
                   3548:             if (ref($rowsref) eq 'ARRAY') {
                   3549:                 @rows = @{$rowsref};
                   3550:             }
                   3551:         } elsif ($position eq 'middle') {
                   3552:             $key = 'default';
                   3553:             @rows = ('types','registered','approval','limit');
                   3554:         }
                   3555:         foreach my $row (@rows) {
                   3556:             if (defined($titlesref->{$row})) {
                   3557:                 $itemcount ++;
                   3558:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
                   3559:                 $datatable .= '<tr'.$css_class.'>'.
                   3560:                               '<td>'.$titlesref->{$row}.'</td>'.
                   3561:                               '<td class="LC_left_item">'.
                   3562:                               '<table><tr>';
                   3563:                 my (%current,%currentcap);
                   3564:                 if (ref($settings) eq 'HASH') {
                   3565:                     if (ref($settings->{$key}) eq 'HASH') {
                   3566:                         foreach my $type (@types) {
                   3567:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
                   3568:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
                   3569:                             }
                   3570:                             if (($row eq 'limit') && ($key eq 'default')) {
                   3571:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
                   3572:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
                   3573:                                 }
                   3574:                             }
                   3575:                         }
                   3576:                     }
                   3577:                 }
                   3578:                 my %roles = (
                   3579:                              '0' => &Apache::lonnet::plaintext('dc'),
                   3580:                             ); 
                   3581:             
                   3582:                 foreach my $type (@types) {
                   3583:                     unless (($row eq 'registered') && ($key eq 'default')) {
                   3584:                         $datatable .= '<th>'.&mt($type).'</th>';
                   3585:                     }
                   3586:                 }
                   3587:                 unless (($row eq 'registered') && ($key eq 'default')) {
                   3588:                     $datatable .= '</tr><tr>';
                   3589:                 }
                   3590:                 foreach my $type (@types) {
                   3591:                     if ($type eq 'community') {
                   3592:                         $roles{'1'} = &mt('Community personnel');
                   3593:                     } else {
                   3594:                         $roles{'1'} = &mt('Course personnel');
                   3595:                     }
                   3596:                     $datatable .= '<td style="vertical-align: top">';
                   3597:                     if ($position eq 'top') {
                   3598:                         my %checked;
                   3599:                         if ($current{$type} eq '0') {
                   3600:                             $checked{'0'} = ' checked="checked"';
                   3601:                         } else {
                   3602:                             $checked{'1'} = ' checked="checked"';
                   3603:                         }
                   3604:                         foreach my $role ('1','0') {
                   3605:                             $datatable .= '<span class="LC_nobreak"><label>'.
                   3606:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
                   3607:                                           'value="'.$role.'"'.$checked{$role}.' />'.
                   3608:                                           $roles{$role}.'</label></span> ';
                   3609:                         }
                   3610:                     } else {
                   3611:                         if ($row eq 'types') {
                   3612:                             my %checked;
                   3613:                             if ($current{$type} =~ /^(all|dom)$/) {
                   3614:                                 $checked{$1} = ' checked="checked"';
                   3615:                             } else {
                   3616:                                 $checked{''} = ' checked="checked"';
                   3617:                             }
                   3618:                             foreach my $val ('','dom','all') {
                   3619:                                 $datatable .= '<span class="LC_nobreak"><label>'.
                   3620:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
                   3621:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
                   3622:                             }
                   3623:                         } elsif ($row eq 'registered') {
                   3624:                             my %checked;
                   3625:                             if ($current{$type} eq '1') {
                   3626:                                 $checked{'1'} = ' checked="checked"';
                   3627:                             } else {
                   3628:                                 $checked{'0'} = ' checked="checked"';
                   3629:                             }
                   3630:                             foreach my $val ('0','1') {
                   3631:                                 $datatable .= '<span class="LC_nobreak"><label>'.
                   3632:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
                   3633:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
                   3634:                             }
                   3635:                         } elsif ($row eq 'approval') {
                   3636:                             my %checked;
                   3637:                             if ($current{$type} =~ /^([12])$/) {
                   3638:                                 $checked{$1} = ' checked="checked"';
                   3639:                             } else {
                   3640:                                 $checked{'0'} = ' checked="checked"';
                   3641:                             }
                   3642:                             for my $val (0..2) {
                   3643:                                 $datatable .= '<span class="LC_nobreak"><label>'.
                   3644:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
                   3645:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
                   3646:                             }
                   3647:                         } elsif ($row eq 'limit') {
                   3648:                             my %checked;
                   3649:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
                   3650:                                 $checked{$1} = ' checked="checked"';
                   3651:                             } else {
                   3652:                                 $checked{'none'} = ' checked="checked"';
                   3653:                             }
                   3654:                             my $cap;
                   3655:                             if ($currentcap{$type} =~ /^\d+$/) {
                   3656:                                 $cap = $currentcap{$type};
                   3657:                             }
                   3658:                             foreach my $val ('none','allstudents','selfenrolled') {
                   3659:                                 $datatable .= '<span class="LC_nobreak"><label>'.
                   3660:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
                   3661:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
                   3662:                             }
                   3663:                             $datatable .= '<br />'.
                   3664:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
                   3665:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
                   3666:                                           '</span>'; 
                   3667:                         }
                   3668:                     }
                   3669:                     $datatable .= '</td>';
                   3670:                 }
                   3671:                 $datatable .= '</tr>';
                   3672:             }
                   3673:             $datatable .= '</table></td></tr>';
                   3674:         }
                   3675:     } elsif ($position eq 'bottom') {
1.235     raeburn  3676:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
                   3677:     }
                   3678:     $$rowtotal += $itemcount;
                   3679:     return $datatable;
                   3680: }
                   3681: 
                   3682: sub print_validation_rows {
                   3683:     my ($caller,$dom,$settings,$rowtotal) = @_;
                   3684:     my ($itemsref,$namesref,$fieldsref);
                   3685:     if ($caller eq 'selfenroll') { 
                   3686:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
                   3687:     } elsif ($caller eq 'requestcourses') {
                   3688:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
                   3689:     }
                   3690:     my %currvalidation;
                   3691:     if (ref($settings) eq 'HASH') {
                   3692:         if (ref($settings->{'validation'}) eq 'HASH') {
                   3693:             %currvalidation = %{$settings->{'validation'}};
1.231     raeburn  3694:         }
1.235     raeburn  3695:     }
                   3696:     my $datatable;
                   3697:     my $itemcount = 0;
                   3698:     foreach my $item (@{$itemsref}) {
                   3699:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
                   3700:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
                   3701:                       $namesref->{$item}.
                   3702:                       '</span></td>'.
                   3703:                       '<td class="LC_left_item">';
                   3704:         if (($item eq 'url') || ($item eq 'button')) {
                   3705:             $datatable .= '<span class="LC_nobreak">'.
                   3706:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
                   3707:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
                   3708:         } elsif ($item eq 'fields') {
                   3709:             my @currfields;
                   3710:             if (ref($currvalidation{$item}) eq 'ARRAY') {
                   3711:                 @currfields = @{$currvalidation{$item}};
                   3712:             }
                   3713:             foreach my $field (@{$fieldsref}) {
                   3714:                 my $check = '';
                   3715:                 if (grep(/^\Q$field\E$/,@currfields)) {
                   3716:                     $check = ' checked="checked"';
                   3717:                 }
                   3718:                 $datatable .= '<span class="LC_nobreak"><label>'.
                   3719:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
                   3720:                               ' value="'.$field.'"'.$check.' />'.$field.
                   3721:                               '</label></span> ';
                   3722:             }
                   3723:         } elsif ($item eq 'markup') {
                   3724:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
                   3725:                            $currvalidation{$item}.
1.231     raeburn  3726:                               '</textarea>';
1.235     raeburn  3727:         }
                   3728:         $datatable .= '</td></tr>'."\n";
                   3729:         if (ref($rowtotal)) {
1.231     raeburn  3730:             $itemcount ++;
                   3731:         }
                   3732:     }
1.235     raeburn  3733:     if ($caller eq 'requestcourses') {
                   3734:         my %currhash;
1.248     raeburn  3735:         if (ref($settings) eq 'HASH') {
                   3736:             if (ref($settings->{'validation'}) eq 'HASH') {
                   3737:                 if ($settings->{'validation'}{'dc'} ne '') {
                   3738:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
                   3739:                 }
1.235     raeburn  3740:             }
                   3741:         }
                   3742:         my $numinrow = 2;
                   3743:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
                   3744:                                                        'validationdc',%currhash);
1.247     raeburn  3745:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
                   3746:         $datatable .= '</td></tr><tr'.$css_class.'><td>';
1.235     raeburn  3747:         if ($numdc > 1) {
1.247     raeburn  3748:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
1.235     raeburn  3749:         } else {
1.247     raeburn  3750:             $datatable .=  &mt('Course creation processed as: ');
1.235     raeburn  3751:         }
1.247     raeburn  3752:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
1.235     raeburn  3753:         $itemcount ++;
                   3754:     }
                   3755:     if (ref($rowtotal)) {
                   3756:         $$rowtotal += $itemcount;
                   3757:     }
1.231     raeburn  3758:     return $datatable;
                   3759: }
                   3760: 
1.137     raeburn  3761: sub print_usersessions {
                   3762:     my ($position,$dom,$settings,$rowtotal) = @_;
                   3763:     my ($css_class,$datatable,%checked,%choices);
1.140     raeburn  3764:     my (%by_ip,%by_location,@intdoms);
                   3765:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
1.145     raeburn  3766: 
                   3767:     my @alldoms = &Apache::lonnet::all_domains();
1.152     raeburn  3768:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
1.149     raeburn  3769:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
1.152     raeburn  3770:     my %altids = &id_for_thisdom(%servers);
1.145     raeburn  3771:     my $itemcount = 1;
                   3772:     if ($position eq 'top') {
1.152     raeburn  3773:         if (keys(%serverhomes) > 1) {
1.145     raeburn  3774:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
1.261     raeburn  3775:             my $curroffloadnow;
                   3776:             if (ref($settings) eq 'HASH') {
                   3777:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
                   3778:                     $curroffloadnow = $settings->{'offloadnow'};
                   3779:                 }
                   3780:             }
                   3781:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$curroffloadnow,$rowtotal);
1.145     raeburn  3782:         } else {
1.140     raeburn  3783:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
1.150     raeburn  3784:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.');
1.140     raeburn  3785:         }
1.137     raeburn  3786:     } else {
1.145     raeburn  3787:         if (keys(%by_location) == 0) {
                   3788:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
1.150     raeburn  3789:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.');
1.145     raeburn  3790:         } else {
                   3791:             my %lt = &usersession_titles();
                   3792:             my $numinrow = 5;
                   3793:             my $prefix;
                   3794:             my @types;
                   3795:             if ($position eq 'bottom') {
                   3796:                 $prefix = 'remote';
                   3797:                 @types = ('version','excludedomain','includedomain');
                   3798:             } else {
                   3799:                 $prefix = 'hosted';
                   3800:                 @types = ('excludedomain','includedomain');
                   3801:             }
                   3802:             my (%current,%checkedon,%checkedoff);
                   3803:             my @lcversions = &Apache::lonnet::all_loncaparevs();
                   3804:             my @locations = sort(keys(%by_location));
                   3805:             foreach my $type (@types) {
                   3806:                 $checkedon{$type} = '';
                   3807:                 $checkedoff{$type} = ' checked="checked"';
                   3808:             }
                   3809:             if (ref($settings) eq 'HASH') {
                   3810:                 if (ref($settings->{$prefix}) eq 'HASH') {
                   3811:                     foreach my $key (keys(%{$settings->{$prefix}})) {
                   3812:                         $current{$key} = $settings->{$prefix}{$key};
                   3813:                         if ($key eq 'version') {
                   3814:                             if ($current{$key} ne '') {
                   3815:                                 $checkedon{$key} = ' checked="checked"';
                   3816:                                 $checkedoff{$key} = '';
                   3817:                             }
                   3818:                         } elsif (ref($current{$key}) eq 'ARRAY') {
                   3819:                             $checkedon{$key} = ' checked="checked"';
                   3820:                             $checkedoff{$key} = '';
                   3821:                         }
1.137     raeburn  3822:                     }
                   3823:                 }
                   3824:             }
1.145     raeburn  3825:             foreach my $type (@types) {
                   3826:                 next if ($type ne 'version' && !@locations);
                   3827:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
                   3828:                 $datatable .= '<tr'.$css_class.'>
                   3829:                                <td><span class="LC_nobreak">'.$lt{$type}.'</span><br />
                   3830:                                <span class="LC_nobreak">&nbsp;
                   3831:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
                   3832:                                <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
                   3833:                 if ($type eq 'version') {
                   3834:                     my $selector = '<select name="'.$prefix.'_version">';
                   3835:                     foreach my $version (@lcversions) {
                   3836:                         my $selected = '';
                   3837:                         if ($current{'version'} eq $version) {
                   3838:                             $selected = ' selected="selected"';
                   3839:                         }
                   3840:                         $selector .= ' <option value="'.$version.'"'.
                   3841:                                      $selected.'>'.$version.'</option>';
                   3842:                     }
                   3843:                     $selector .= '</select> ';
                   3844:                     $datatable .= &mt('remote server must be version: [_1] or later',$selector);
                   3845:                 } else {
                   3846:                     $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
                   3847:                                  'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
                   3848:                                  ' />'.('&nbsp;'x2).
                   3849:                                  '<input type="button" value="'.&mt('uncheck all').'" '.
                   3850:                                  'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
                   3851:                                  "\n".
                   3852:                                  '</div><div><table>';
                   3853:                     my $rem;
                   3854:                     for (my $i=0; $i<@locations; $i++) {
                   3855:                         my ($showloc,$value,$checkedtype);
                   3856:                         if (ref($by_location{$locations[$i]}) eq 'ARRAY') {
                   3857:                             my $ip = $by_location{$locations[$i]}->[0];
                   3858:                             if (ref($by_ip{$ip}) eq 'ARRAY') {
                   3859:                                  $value = join(':',@{$by_ip{$ip}});
                   3860:                                 $showloc = join(', ',@{$by_ip{$ip}});
                   3861:                                 if (ref($current{$type}) eq 'ARRAY') {
                   3862:                                     foreach my $loc (@{$by_ip{$ip}}) {  
                   3863:                                         if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
                   3864:                                             $checkedtype = ' checked="checked"';
                   3865:                                             last;
                   3866:                                         }
                   3867:                                     }
1.138     raeburn  3868:                                 }
                   3869:                             }
                   3870:                         }
1.145     raeburn  3871:                         $rem = $i%($numinrow);
                   3872:                         if ($rem == 0) {
                   3873:                             if ($i > 0) {
                   3874:                                 $datatable .= '</tr>';
                   3875:                             }
                   3876:                             $datatable .= '<tr>';
                   3877:                         }
                   3878:                         $datatable .= '<td class="LC_left_item">'.
                   3879:                                       '<span class="LC_nobreak"><label>'.
                   3880:                                       '<input type="checkbox" name="'.$prefix.'_'.$type.
                   3881:                                       '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
                   3882:                                       '</label></span></td>';
1.137     raeburn  3883:                     }
1.145     raeburn  3884:                     $rem = @locations%($numinrow);
                   3885:                     my $colsleft = $numinrow - $rem;
                   3886:                     if ($colsleft > 1 ) {
                   3887:                         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
                   3888:                                       '&nbsp;</td>';
                   3889:                     } elsif ($colsleft == 1) {
                   3890:                         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
1.137     raeburn  3891:                     }
1.145     raeburn  3892:                     $datatable .= '</tr></table>';
1.137     raeburn  3893:                 }
1.145     raeburn  3894:                 $datatable .= '</td></tr>';
                   3895:                 $itemcount ++;
1.137     raeburn  3896:             }
                   3897:         }
                   3898:     }
                   3899:     $$rowtotal += $itemcount;
                   3900:     return $datatable;
                   3901: }
                   3902: 
1.138     raeburn  3903: sub build_location_hashes {
                   3904:     my ($intdoms,$by_ip,$by_location) = @_;
                   3905:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
                   3906:                   (ref($by_location) eq 'HASH')); 
                   3907:     my %iphost = &Apache::lonnet::get_iphost();
                   3908:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
                   3909:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
                   3910:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
                   3911:         foreach my $id (@{$iphost{$primary_ip}}) {
                   3912:             my $intdom = &Apache::lonnet::internet_dom($id);
                   3913:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
                   3914:                 push(@{$intdoms},$intdom);
                   3915:             }
                   3916:         }
                   3917:     }
                   3918:     foreach my $ip (keys(%iphost)) {
                   3919:         if (ref($iphost{$ip}) eq 'ARRAY') {
                   3920:             foreach my $id (@{$iphost{$ip}}) {
                   3921:                 my $location = &Apache::lonnet::internet_dom($id);
                   3922:                 if ($location) {
                   3923:                     next if (grep(/^\Q$location\E$/,@{$intdoms}));
                   3924:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
                   3925:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
                   3926:                             push(@{$by_ip->{$ip}},$location);
                   3927:                         }
                   3928:                     } else {
                   3929:                         $by_ip->{$ip} = [$location];
                   3930:                     }
                   3931:                 }
                   3932:             }
                   3933:         }
                   3934:     }
                   3935:     foreach my $ip (sort(keys(%{$by_ip}))) {
                   3936:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
                   3937:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
                   3938:             my $first = $by_ip->{$ip}->[0];
                   3939:             if (ref($by_location->{$first}) eq 'ARRAY') {
                   3940:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
                   3941:                     push(@{$by_location->{$first}},$ip);
                   3942:                 }
                   3943:             } else {
                   3944:                 $by_location->{$first} = [$ip];
                   3945:             }
                   3946:         }
                   3947:     }
                   3948:     return;
                   3949: }
                   3950: 
1.145     raeburn  3951: sub current_offloads_to {
                   3952:     my ($dom,$settings,$servers) = @_;
                   3953:     my (%spareid,%otherdomconfigs);
1.152     raeburn  3954:     if (ref($servers) eq 'HASH') {
1.145     raeburn  3955:         foreach my $lonhost (sort(keys(%{$servers}))) {
                   3956:             my $gotspares;
1.152     raeburn  3957:             if (ref($settings) eq 'HASH') {
                   3958:                 if (ref($settings->{'spares'}) eq 'HASH') {
                   3959:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
                   3960:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
                   3961:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
                   3962:                         $gotspares = 1;
                   3963:                     }
1.145     raeburn  3964:                 }
                   3965:             }
                   3966:             unless ($gotspares) {
                   3967:                 my $gotspares;
                   3968:                 my $serverhomeID =
                   3969:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
                   3970:                 my $serverhomedom =
                   3971:                     &Apache::lonnet::host_domain($serverhomeID);
                   3972:                 if ($serverhomedom ne $dom) {
                   3973:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
                   3974:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
                   3975:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
                   3976:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
                   3977:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
                   3978:                                 $gotspares = 1;
                   3979:                             }
                   3980:                         }
                   3981:                     } else {
                   3982:                         $otherdomconfigs{$serverhomedom} =
                   3983:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
                   3984:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
                   3985:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
                   3986:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
                   3987:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
                   3988:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
                   3989:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
                   3990:                                         $gotspares = 1;
                   3991:                                     }
                   3992:                                 }
                   3993:                             }
                   3994:                         }
                   3995:                     }
                   3996:                 }
                   3997:             }
                   3998:             unless ($gotspares) {
                   3999:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
                   4000:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
                   4001:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
                   4002:                } else {
                   4003:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
                   4004:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
                   4005:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
                   4006:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
                   4007:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
                   4008:                     } else {
1.150     raeburn  4009:                         my %what = (
                   4010:                              spareid => 1,
                   4011:                         );
                   4012:                         my ($result,$returnhash) = 
                   4013:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
                   4014:                         if ($result eq 'ok') { 
                   4015:                             if (ref($returnhash) eq 'HASH') {
                   4016:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
                   4017:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
                   4018:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
                   4019:                                 }
                   4020:                             }
1.145     raeburn  4021:                         }
                   4022:                     }
                   4023:                 }
                   4024:             }
                   4025:         }
                   4026:     }
                   4027:     return %spareid;
                   4028: }
                   4029: 
                   4030: sub spares_row {
1.261     raeburn  4031:     my ($dom,$servers,$spareid,$serverhomes,$altids,$curroffloadnow,$rowtotal) = @_;
1.145     raeburn  4032:     my $css_class;
                   4033:     my $numinrow = 4;
                   4034:     my $itemcount = 1;
                   4035:     my $datatable;
1.152     raeburn  4036:     my %typetitles = &sparestype_titles();
                   4037:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
1.145     raeburn  4038:         foreach my $server (sort(keys(%{$servers}))) {
1.152     raeburn  4039:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
                   4040:             my ($othercontrol,$serverdom);
                   4041:             if ($serverhome ne $server) {
                   4042:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
                   4043:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
                   4044:             } else {
                   4045:                 $serverdom = &Apache::lonnet::host_domain($server);
                   4046:                 if ($serverdom ne $dom) {
                   4047:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
                   4048:                 }
                   4049:             }
                   4050:             next unless (ref($spareid->{$server}) eq 'HASH');
1.261     raeburn  4051:             my $checkednow;
                   4052:             if (ref($curroffloadnow) eq 'HASH') {
                   4053:                 if ($curroffloadnow->{$server}) {
                   4054:                     $checkednow = ' checked="checked"';
                   4055:                 }
                   4056:             }
1.145     raeburn  4057:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
                   4058:             $datatable .= '<tr'.$css_class.'>
                   4059:                            <td rowspan="2">
1.183     bisitz   4060:                             <span class="LC_nobreak">'.
                   4061:                           &mt('[_1] when busy, offloads to:'
1.261     raeburn  4062:                               ,'<b>'.$server.'</b>').'</span><br />'.
                   4063:                           '<span class="LC_nobreak">'."\n". 
                   4064:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
                   4065:                           '&nbsp;'.&mt('Switch active users on next access').'</label></span>'.
1.183     bisitz   4066:                           "\n";
1.145     raeburn  4067:             my (%current,%canselect);
1.152     raeburn  4068:             my @choices = 
                   4069:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
                   4070:             foreach my $type ('primary','default') {
                   4071:                 if (ref($spareid->{$server}) eq 'HASH') {
1.145     raeburn  4072:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
                   4073:                         my @spares = @{$spareid->{$server}{$type}};
                   4074:                         if (@spares > 0) {
1.152     raeburn  4075:                             if ($othercontrol) {
                   4076:                                 $current{$type} = join(', ',@spares);
                   4077:                             } else {
                   4078:                                 $current{$type} .= '<table>';
                   4079:                                 my $numspares = scalar(@spares);
                   4080:                                 for (my $i=0;  $i<@spares; $i++) {
                   4081:                                     my $rem = $i%($numinrow);
                   4082:                                     if ($rem == 0) {
                   4083:                                         if ($i > 0) {
                   4084:                                             $current{$type} .= '</tr>';
                   4085:                                         }
                   4086:                                         $current{$type} .= '<tr>';
1.145     raeburn  4087:                                     }
1.152     raeburn  4088:                                     $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;'.
                   4089:                                                        $spareid->{$server}{$type}[$i].
                   4090:                                                        '</label></td>'."\n";
                   4091:                                 }
                   4092:                                 my $rem = @spares%($numinrow);
                   4093:                                 my $colsleft = $numinrow - $rem;
                   4094:                                 if ($colsleft > 1 ) {
                   4095:                                     $current{$type} .= '<td colspan="'.$colsleft.
                   4096:                                                        '" class="LC_left_item">'.
                   4097:                                                        '&nbsp;</td>';
                   4098:                                 } elsif ($colsleft == 1) {
                   4099:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
1.145     raeburn  4100:                                 }
1.152     raeburn  4101:                                 $current{$type} .= '</tr></table>';
1.150     raeburn  4102:                             }
1.145     raeburn  4103:                         }
                   4104:                     }
                   4105:                     if ($current{$type} eq '') {
                   4106:                         $current{$type} = &mt('None specified');
                   4107:                     }
1.152     raeburn  4108:                     if ($othercontrol) {
                   4109:                         if ($type eq 'primary') {
                   4110:                             $canselect{$type} = $othercontrol;
                   4111:                         }
                   4112:                     } else {
                   4113:                         $canselect{$type} = 
                   4114:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
                   4115:                             '<select name="newspare_'.$type.'_'.$server.'" '.
                   4116:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
                   4117:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
                   4118:                         if (@choices > 0) {
                   4119:                             foreach my $lonhost (@choices) {
                   4120:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
                   4121:                             }
                   4122:                         }
                   4123:                         $canselect{$type} .= '</select>'."\n";
                   4124:                     }
                   4125:                 } else {
                   4126:                     $current{$type} = &mt('Could not be determined');
                   4127:                     if ($type eq 'primary') {
                   4128:                         $canselect{$type} =  $othercontrol;
                   4129:                     }
1.145     raeburn  4130:                 }
1.152     raeburn  4131:                 if ($type eq 'default') {
                   4132:                     $datatable .= '<tr'.$css_class.'>';
                   4133:                 }
                   4134:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
                   4135:                               '<td>'.$current{$type}.'</td>'."\n".
                   4136:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
1.145     raeburn  4137:             }
                   4138:             $itemcount ++;
                   4139:         }
                   4140:     }
                   4141:     $$rowtotal += $itemcount;
                   4142:     return $datatable;
                   4143: }
                   4144: 
1.152     raeburn  4145: sub possible_newspares {
                   4146:     my ($server,$currspares,$serverhomes,$altids) = @_;
                   4147:     my $serverhostname = &Apache::lonnet::hostname($server);
                   4148:     my %excluded;
                   4149:     if ($serverhostname ne '') {
                   4150:         %excluded = (
                   4151:                        $serverhostname => 1,
                   4152:                     );
                   4153:     }
                   4154:     if (ref($currspares) eq 'HASH') {
                   4155:         foreach my $type (keys(%{$currspares})) {
                   4156:             if (ref($currspares->{$type}) eq 'ARRAY') {
                   4157:                 if (@{$currspares->{$type}} > 0) {
                   4158:                     foreach my $curr (@{$currspares->{$type}}) {
                   4159:                         my $hostname = &Apache::lonnet::hostname($curr);
                   4160:                         $excluded{$hostname} = 1;
                   4161:                     }
                   4162:                 }
                   4163:             }
                   4164:         }
                   4165:     }
                   4166:     my @choices;
                   4167:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
                   4168:         if (keys(%{$serverhomes}) > 1) {
                   4169:             foreach my $name (sort(keys(%{$serverhomes}))) {
                   4170:                 unless ($excluded{$name}) {
                   4171:                     if (exists($altids->{$serverhomes->{$name}})) {
                   4172:                         push(@choices,$altids->{$serverhomes->{$name}});
                   4173:                     } else {
                   4174:                         push(@choices,$serverhomes->{$name});
1.145     raeburn  4175:                     }
                   4176:                 }
                   4177:             }
                   4178:         }
                   4179:     }
1.152     raeburn  4180:     return sort(@choices);
1.145     raeburn  4181: }
                   4182: 
1.150     raeburn  4183: sub print_loadbalancing {
                   4184:     my ($dom,$settings,$rowtotal) = @_;
                   4185:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
                   4186:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
                   4187:     my $numinrow = 1;
                   4188:     my $datatable;
                   4189:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
1.171     raeburn  4190:     my (%currbalancer,%currtargets,%currrules,%existing);
                   4191:     if (ref($settings) eq 'HASH') {
                   4192:         %existing = %{$settings};
                   4193:     }
                   4194:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
                   4195:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
                   4196:                                   \%currtargets,\%currrules);
1.150     raeburn  4197:     } else {
                   4198:         return;
                   4199:     }
                   4200:     my ($othertitle,$usertypes,$types) =
                   4201:         &Apache::loncommon::sorted_inst_types($dom);
1.209     raeburn  4202:     my $rownum = 8;
1.150     raeburn  4203:     if (ref($types) eq 'ARRAY') {
                   4204:         $rownum += scalar(@{$types});
                   4205:     }
1.171     raeburn  4206:     my @css_class = ('LC_odd_row','LC_even_row');
                   4207:     my $balnum = 0;
                   4208:     my $islast;
                   4209:     my (@toshow,$disabledtext);
                   4210:     if (keys(%currbalancer) > 0) {
                   4211:         @toshow = sort(keys(%currbalancer));
                   4212:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
                   4213:             push(@toshow,'');
                   4214:         }
                   4215:     } else {
                   4216:         @toshow = ('');
                   4217:         $disabledtext = &mt('No existing load balancer');
                   4218:     }
                   4219:     foreach my $lonhost (@toshow) {
                   4220:         if ($balnum == scalar(@toshow)-1) {
                   4221:             $islast = 1;
                   4222:         } else {
                   4223:             $islast = 0;
                   4224:         }
                   4225:         my $cssidx = $balnum%2;
                   4226:         my $targets_div_style = 'display: none';
                   4227:         my $disabled_div_style = 'display: block';
                   4228:         my $homedom_div_style = 'display: none';
                   4229:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
                   4230:                       '<td rowspan="'.$rownum.'" valign="top">'.
                   4231:                       '<p>';
                   4232:         if ($lonhost eq '') {
1.210     raeburn  4233:             $datatable .= '<span class="LC_nobreak">';
1.171     raeburn  4234:             if (keys(%currbalancer) > 0) {
                   4235:                 $datatable .= &mt('Add balancer:');
                   4236:             } else {
                   4237:                 $datatable .= &mt('Enable balancer:');
                   4238:             }
                   4239:             $datatable .= '&nbsp;'.
                   4240:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
                   4241:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
                   4242:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
                   4243:                           '<option value="" selected="selected">'.&mt('None').
                   4244:                           '</option>'."\n";
                   4245:             foreach my $server (sort(keys(%servers))) {
                   4246:                 next if ($currbalancer{$server});
                   4247:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
                   4248:             }
1.210     raeburn  4249:             $datatable .=
1.171     raeburn  4250:                 '</select>'."\n".
                   4251:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
                   4252:         } else {
                   4253:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
                   4254:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
                   4255:                            &mt('Stop balancing').'</label>'.
                   4256:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
                   4257:             $targets_div_style = 'display: block';
                   4258:             $disabled_div_style = 'display: none';
                   4259:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
                   4260:                 $homedom_div_style = 'display: block';
                   4261:             }
                   4262:         }
                   4263:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
                   4264:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
                   4265:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
                   4266:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
                   4267:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
                   4268:         my @sparestypes = ('primary','default');
                   4269:         my %typetitles = &sparestype_titles();
                   4270:         foreach my $sparetype (@sparestypes) {
                   4271:             my $targettable;
                   4272:             for (my $i=0; $i<$numspares; $i++) {
                   4273:                 my $checked;
                   4274:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
                   4275:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
                   4276:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
                   4277:                             $checked = ' checked="checked"';
                   4278:                         }
                   4279:                     }
                   4280:                 }
                   4281:                 my ($chkboxval,$disabled);
                   4282:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
                   4283:                     $chkboxval = $spares[$i];
                   4284:                 }
                   4285:                 if (exists($currbalancer{$spares[$i]})) {
                   4286:                     $disabled = ' disabled="disabled"';
                   4287:                 }
1.210     raeburn  4288:                 $targettable .=
1.253     raeburn  4289:                     '<td><span class="LC_nobreak"><label>'.
                   4290:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
1.171     raeburn  4291:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
1.253     raeburn  4292:                     '</span></label></span></td>';
1.171     raeburn  4293:                 my $rem = $i%($numinrow);
                   4294:                 if ($rem == 0) {
                   4295:                     if (($i > 0) && ($i < $numspares-1)) {
                   4296:                         $targettable .= '</tr>';
                   4297:                     }
                   4298:                     if ($i < $numspares-1) {
                   4299:                         $targettable .= '<tr>';
1.150     raeburn  4300:                     }
                   4301:                 }
                   4302:             }
1.171     raeburn  4303:             if ($targettable ne '') {
                   4304:                 my $rem = $numspares%($numinrow);
                   4305:                 my $colsleft = $numinrow - $rem;
                   4306:                 if ($colsleft > 1 ) {
                   4307:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
                   4308:                                     '&nbsp;</td>';
                   4309:                 } elsif ($colsleft == 1) {
                   4310:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
                   4311:                 }
                   4312:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
                   4313:                                '<table><tr>'.$targettable.'</tr></table><br />';
                   4314:             }
                   4315:         }
                   4316:         $datatable .= '</div></td></tr>'.
                   4317:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
                   4318:                                            $othertitle,$usertypes,$types,\%servers,
                   4319:                                            \%currbalancer,$lonhost,
                   4320:                                            $targets_div_style,$homedom_div_style,
                   4321:                                            $css_class[$cssidx],$balnum,$islast);
                   4322:         $$rowtotal += $rownum;
                   4323:         $balnum ++;
                   4324:     }
                   4325:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
                   4326:     return $datatable;
                   4327: }
                   4328: 
                   4329: sub get_loadbalancers_config {
                   4330:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
                   4331:     return unless ((ref($servers) eq 'HASH') &&
                   4332:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
                   4333:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
                   4334:     if (keys(%{$existing}) > 0) {
                   4335:         my $oldlonhost;
                   4336:         foreach my $key (sort(keys(%{$existing}))) {
                   4337:             if ($key eq 'lonhost') {
                   4338:                 $oldlonhost = $existing->{'lonhost'};
                   4339:                 $currbalancer->{$oldlonhost} = 1;
                   4340:             } elsif ($key eq 'targets') {
                   4341:                 if ($oldlonhost) {
                   4342:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
                   4343:                 }
                   4344:             } elsif ($key eq 'rules') {
                   4345:                 if ($oldlonhost) {
                   4346:                     $currrules->{$oldlonhost} = $existing->{'rules'};
                   4347:                 }
                   4348:             } elsif (ref($existing->{$key}) eq 'HASH') {
                   4349:                 $currbalancer->{$key} = 1;
                   4350:                 $currtargets->{$key} = $existing->{$key}{'targets'};
                   4351:                 $currrules->{$key} = $existing->{$key}{'rules'};
1.150     raeburn  4352:             }
                   4353:         }
1.171     raeburn  4354:     } else {
                   4355:         my ($balancerref,$targetsref) =
                   4356:                 &Apache::lonnet::get_lonbalancer_config($servers);
                   4357:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
                   4358:             foreach my $server (sort(keys(%{$balancerref}))) {
                   4359:                 $currbalancer->{$server} = 1;
                   4360:                 $currtargets->{$server} = $targetsref->{$server};
1.150     raeburn  4361:             }
                   4362:         }
                   4363:     }
1.171     raeburn  4364:     return;
1.150     raeburn  4365: }
                   4366: 
                   4367: sub loadbalancing_rules {
                   4368:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
1.171     raeburn  4369:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
                   4370:         $css_class,$balnum,$islast) = @_;
1.150     raeburn  4371:     my $output;
1.171     raeburn  4372:     my $num = 0;
1.210     raeburn  4373:     my ($alltypes,$othertypes,$titles) =
1.150     raeburn  4374:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
                   4375:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
                   4376:         foreach my $type (@{$alltypes}) {
1.171     raeburn  4377:             $num ++;
1.150     raeburn  4378:             my $current;
                   4379:             if (ref($currrules) eq 'HASH') {
                   4380:                 $current = $currrules->{$type};
                   4381:             }
1.253     raeburn  4382:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
1.171     raeburn  4383:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
1.150     raeburn  4384:                     $current = '';
                   4385:                 }
                   4386:             }
                   4387:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
1.171     raeburn  4388:                                              $servers,$currbalancer,$lonhost,$dom,
                   4389:                                              $targets_div_style,$homedom_div_style,
                   4390:                                              $css_class,$balnum,$num,$islast);
1.150     raeburn  4391:         }
                   4392:     }
                   4393:     return $output;
                   4394: }
                   4395: 
                   4396: sub loadbalancing_titles {
                   4397:     my ($dom,$intdom,$usertypes,$types) = @_;
                   4398:     my %othertypes = (
                   4399:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
                   4400:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
                   4401:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
                   4402:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
1.209     raeburn  4403:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
                   4404:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
1.150     raeburn  4405:                      );
1.209     raeburn  4406:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
1.150     raeburn  4407:     if (ref($types) eq 'ARRAY') {
                   4408:         unshift(@alltypes,@{$types},'default');
                   4409:     }
                   4410:     my %titles;
                   4411:     foreach my $type (@alltypes) {
                   4412:         if ($type =~ /^_LC_/) {
                   4413:             $titles{$type} = $othertypes{$type};
                   4414:         } elsif ($type eq 'default') {
                   4415:             $titles{$type} = &mt('All users from [_1]',$dom);
                   4416:             if (ref($types) eq 'ARRAY') {
                   4417:                 if (@{$types} > 0) {
                   4418:                     $titles{$type} = &mt('Other users from [_1]',$dom);
                   4419:                 }
                   4420:             }
                   4421:         } elsif (ref($usertypes) eq 'HASH') {
                   4422:             $titles{$type} = $usertypes->{$type};
                   4423:         }
                   4424:     }
                   4425:     return (\@alltypes,\%othertypes,\%titles);
                   4426: }
                   4427: 
                   4428: sub loadbalance_rule_row {
1.171     raeburn  4429:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
                   4430:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
1.209     raeburn  4431:     my @rulenames;
1.150     raeburn  4432:     my %ruletitles = &offloadtype_text();
1.209     raeburn  4433:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
1.254     raeburn  4434:         @rulenames = ('balancer','offloadedto','specific');
1.150     raeburn  4435:     } else {
1.209     raeburn  4436:         @rulenames = ('default','homeserver');
                   4437:         if ($type eq '_LC_external') {
                   4438:             push(@rulenames,'externalbalancer');
                   4439:         } else {
                   4440:             push(@rulenames,'specific');
                   4441:         }
                   4442:         push(@rulenames,'none');
1.150     raeburn  4443:     }
                   4444:     my $style = $targets_div_style;
1.253     raeburn  4445:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
1.150     raeburn  4446:         $style = $homedom_div_style;
                   4447:     }
1.171     raeburn  4448:     my $space;
                   4449:     if ($islast && $num == 1) {
                   4450:         $space = '<div display="inline-block">&nbsp;</div>';
                   4451:     }
1.210     raeburn  4452:     my $output =
1.171     raeburn  4453:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
                   4454:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
                   4455:         '<td valaign="top">'.$space.
                   4456:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
1.150     raeburn  4457:     for (my $i=0; $i<@rulenames; $i++) {
                   4458:         my $rule = $rulenames[$i];
                   4459:         my ($checked,$extra);
                   4460:         if ($rulenames[$i] eq 'default') {
                   4461:             $rule = '';
                   4462:         }
                   4463:         if ($rulenames[$i] eq 'specific') {
                   4464:             if (ref($servers) eq 'HASH') {
                   4465:                 my $default;
                   4466:                 if (($current ne '') && (exists($servers->{$current}))) {
                   4467:                     $checked = ' checked="checked"';
                   4468:                 }
                   4469:                 unless ($checked) {
                   4470:                     $default = ' selected="selected"';
                   4471:                 }
1.210     raeburn  4472:                 $extra =
1.171     raeburn  4473:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
                   4474:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
                   4475:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
                   4476:                     '<option value=""'.$default.'></option>'."\n";
                   4477:                 foreach my $server (sort(keys(%{$servers}))) {
                   4478:                     if (ref($currbalancer) eq 'HASH') {
                   4479:                         next if (exists($currbalancer->{$server}));
                   4480:                     }
1.150     raeburn  4481:                     my $selected;
1.171     raeburn  4482:                     if ($server eq $current) {
1.150     raeburn  4483:                         $selected = ' selected="selected"';
                   4484:                     }
1.171     raeburn  4485:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
1.150     raeburn  4486:                 }
                   4487:                 $extra .= '</select>';
                   4488:             }
                   4489:         } elsif ($rule eq $current) {
                   4490:             $checked = ' checked="checked"';
                   4491:         }
                   4492:         $output .= '<span class="LC_nobreak"><label>'.
1.171     raeburn  4493:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
                   4494:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
                   4495:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
1.254     raeburn  4496:                    ')"'.$checked.' />&nbsp;';
                   4497:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
                   4498:             $output .= $ruletitles{'particular'};
                   4499:         } else {
                   4500:             $output .= $ruletitles{$rulenames[$i]};
                   4501:         }
                   4502:         $output .= '</label>'.$extra.'</span><br />'."\n";
1.150     raeburn  4503:     }
                   4504:     $output .= '</div></td></tr>'."\n";
                   4505:     return $output;
                   4506: }
                   4507: 
                   4508: sub offloadtype_text {
                   4509:     my %ruletitles = &Apache::lonlocal::texthash (
                   4510:            'default'          => 'Offloads to default destinations',
                   4511:            'homeserver'       => "Offloads to user's home server",
                   4512:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
                   4513:            'specific'         => 'Offloads to specific server',
1.161     raeburn  4514:            'none'             => 'No offload',
1.209     raeburn  4515:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
                   4516:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
1.254     raeburn  4517:            'particular'       => 'Session hosted (after re-auth) on server:',
1.150     raeburn  4518:     );
                   4519:     return %ruletitles;
                   4520: }
                   4521: 
                   4522: sub sparestype_titles {
                   4523:     my %typestitles = &Apache::lonlocal::texthash (
                   4524:                           'primary' => 'primary',
                   4525:                           'default' => 'default',
                   4526:                       );
                   4527:     return %typestitles;
                   4528: }
                   4529: 
1.28      raeburn  4530: sub contact_titles {
                   4531:     my %titles = &Apache::lonlocal::texthash (
                   4532:                    'supportemail' => 'Support E-mail address',
1.69      raeburn  4533:                    'adminemail'   => 'Default Server Admin E-mail address',
1.28      raeburn  4534:                    'errormail'    => 'Error reports to be e-mailed to',
                   4535:                    'packagesmail' => 'Package update alerts to be e-mailed to',
1.89      raeburn  4536:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
                   4537:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
1.102     raeburn  4538:                    'requestsmail' => 'E-mail from course requests requiring approval',
1.190     raeburn  4539:                    'updatesmail'  => 'E-mail from nightly check of LON-CAPA module integrity/updates',
1.203     raeburn  4540:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
1.28      raeburn  4541:                  );
                   4542:     my %short_titles = &Apache::lonlocal::texthash (
                   4543:                            adminemail   => 'Admin E-mail address',
                   4544:                            supportemail => 'Support E-mail',
                   4545:                        );   
                   4546:     return (\%titles,\%short_titles);
                   4547: }
                   4548: 
1.72      raeburn  4549: sub tool_titles {
                   4550:     my %titles = &Apache::lonlocal::texthash (
1.162     raeburn  4551:                      aboutme    => 'Personal web page',
1.86      raeburn  4552:                      blog       => 'Blog',
1.162     raeburn  4553:                      webdav     => 'WebDAV',
1.86      raeburn  4554:                      portfolio  => 'Portfolio',
1.88      bisitz   4555:                      official   => 'Official courses (with institutional codes)',
                   4556:                      unofficial => 'Unofficial courses',
1.98      raeburn  4557:                      community  => 'Communities',
1.216     raeburn  4558:                      textbook   => 'Textbook courses',
1.86      raeburn  4559:                  );
1.72      raeburn  4560:     return %titles;
                   4561: }
                   4562: 
1.101     raeburn  4563: sub courserequest_titles {
                   4564:     my %titles = &Apache::lonlocal::texthash (
                   4565:                                    official   => 'Official',
                   4566:                                    unofficial => 'Unofficial',
                   4567:                                    community  => 'Communities',
1.216     raeburn  4568:                                    textbook   => 'Textbook',
1.101     raeburn  4569:                                    norequest  => 'Not allowed',
1.104     raeburn  4570:                                    approval   => 'Approval by Dom. Coord.',
1.101     raeburn  4571:                                    validate   => 'With validation',
                   4572:                                    autolimit  => 'Numerical limit',
1.103     raeburn  4573:                                    unlimited  => '(blank for unlimited)',
1.101     raeburn  4574:                  );
                   4575:     return %titles;
                   4576: }
                   4577: 
1.163     raeburn  4578: sub authorrequest_titles {
                   4579:     my %titles = &Apache::lonlocal::texthash (
                   4580:                                    norequest  => 'Not allowed',
                   4581:                                    approval   => 'Approval by Dom. Coord.',
                   4582:                                    automatic  => 'Automatic approval',
                   4583:                  );
                   4584:     return %titles;
1.210     raeburn  4585: }
1.163     raeburn  4586: 
1.101     raeburn  4587: sub courserequest_conditions {
                   4588:     my %conditions = &Apache::lonlocal::texthash (
1.104     raeburn  4589:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
1.193     bisitz   4590:        validate   => '(Processing of request subject to institutional validation).',
1.101     raeburn  4591:                  );
                   4592:     return %conditions;
                   4593: }
                   4594: 
                   4595: 
1.27      raeburn  4596: sub print_usercreation {
1.30      raeburn  4597:     my ($position,$dom,$settings,$rowtotal) = @_;
1.27      raeburn  4598:     my $numinrow = 4;
1.28      raeburn  4599:     my $datatable;
                   4600:     if ($position eq 'top') {
1.30      raeburn  4601:         $$rowtotal ++;
1.34      raeburn  4602:         my $rowcount = 0;
1.32      raeburn  4603:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
1.28      raeburn  4604:         if (ref($rules) eq 'HASH') {
                   4605:             if (keys(%{$rules}) > 0) {
1.32      raeburn  4606:                 $datatable .= &user_formats_row('username',$settings,$rules,
                   4607:                                                 $ruleorder,$numinrow,$rowcount);
1.30      raeburn  4608:                 $$rowtotal ++;
1.32      raeburn  4609:                 $rowcount ++;
                   4610:             }
                   4611:         }
                   4612:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
                   4613:         if (ref($idrules) eq 'HASH') {
                   4614:             if (keys(%{$idrules}) > 0) {
                   4615:                 $datatable .= &user_formats_row('id',$settings,$idrules,
                   4616:                                                 $idruleorder,$numinrow,$rowcount);
                   4617:                 $$rowtotal ++;
                   4618:                 $rowcount ++;
1.28      raeburn  4619:             }
                   4620:         }
1.39      raeburn  4621:         if ($rowcount == 0) {
                   4622:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
                   4623:             $$rowtotal ++;
                   4624:             $rowcount ++;
                   4625:         }
1.34      raeburn  4626:     } elsif ($position eq 'middle') {
1.224     raeburn  4627:         my @creators = ('author','course','requestcrs');
1.37      raeburn  4628:         my ($rules,$ruleorder) =
                   4629:             &Apache::lonnet::inst_userrules($dom,'username');
1.34      raeburn  4630:         my %lt = &usercreation_types();
                   4631:         my %checked;
                   4632:         if (ref($settings) eq 'HASH') {
                   4633:             if (ref($settings->{'cancreate'}) eq 'HASH') {
                   4634:                 foreach my $item (@creators) {
                   4635:                     $checked{$item} = $settings->{'cancreate'}{$item};
                   4636:                 }
                   4637:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
                   4638:                 foreach my $item (@creators) {
                   4639:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
                   4640:                         $checked{$item} = 'none';
                   4641:                     }
                   4642:                 }
                   4643:             }
                   4644:         }
                   4645:         my $rownum = 0;
                   4646:         foreach my $item (@creators) {
                   4647:             $rownum ++;
1.224     raeburn  4648:             if ($checked{$item} eq '') {
                   4649:                 $checked{$item} = 'any';
1.34      raeburn  4650:             }
                   4651:             my $css_class;
                   4652:             if ($rownum%2) {
                   4653:                 $css_class = '';
                   4654:             } else {
                   4655:                 $css_class = ' class="LC_odd_row" ';
                   4656:             }
                   4657:             $datatable .= '<tr'.$css_class.'>'.
                   4658:                          '<td><span class="LC_nobreak">'.$lt{$item}.
                   4659:                          '</span></td><td align="right">';
1.224     raeburn  4660:             my @options = ('any');
                   4661:             if (ref($rules) eq 'HASH') {
                   4662:                 if (keys(%{$rules}) > 0) {
                   4663:                     push(@options,('official','unofficial'));
1.37      raeburn  4664:                 }
                   4665:             }
1.224     raeburn  4666:             push(@options,'none');
1.37      raeburn  4667:             foreach my $option (@options) {
1.50      raeburn  4668:                 my $type = 'radio';
1.34      raeburn  4669:                 my $check = ' ';
1.224     raeburn  4670:                 if ($checked{$item} eq $option) {
                   4671:                     $check = ' checked="checked" ';
1.34      raeburn  4672:                 } 
                   4673:                 $datatable .= '<span class="LC_nobreak"><label>'.
1.50      raeburn  4674:                               '<input type="'.$type.'" name="can_createuser_'.
1.34      raeburn  4675:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
                   4676:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
                   4677:             }
                   4678:             $datatable .= '</td></tr>';
                   4679:         }
1.28      raeburn  4680:     } else {
                   4681:         my @contexts = ('author','course','domain');
                   4682:         my @authtypes = ('int','krb4','krb5','loc');
                   4683:         my %checked;
                   4684:         if (ref($settings) eq 'HASH') {
                   4685:             if (ref($settings->{'authtypes'}) eq 'HASH') {
                   4686:                 foreach my $item (@contexts) {
                   4687:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
                   4688:                         foreach my $auth (@authtypes) {
                   4689:                             if ($settings->{'authtypes'}{$item}{$auth}) {
                   4690:                                 $checked{$item}{$auth} = ' checked="checked" ';
                   4691:                             }
                   4692:                         }
                   4693:                     }
                   4694:                 }
1.27      raeburn  4695:             }
1.35      raeburn  4696:         } else {
                   4697:             foreach my $item (@contexts) {
1.36      raeburn  4698:                 foreach my $auth (@authtypes) {
1.35      raeburn  4699:                     $checked{$item}{$auth} = ' checked="checked" ';
                   4700:                 }
                   4701:             }
1.27      raeburn  4702:         }
1.28      raeburn  4703:         my %title = &context_names();
                   4704:         my %authname = &authtype_names();
                   4705:         my $rownum = 0;
                   4706:         my $css_class; 
                   4707:         foreach my $item (@contexts) {
                   4708:             if ($rownum%2) {
                   4709:                 $css_class = '';
                   4710:             } else {
                   4711:                 $css_class = ' class="LC_odd_row" ';
                   4712:             }
1.30      raeburn  4713:             $datatable .=   '<tr'.$css_class.'>'.
1.28      raeburn  4714:                             '<td>'.$title{$item}.
                   4715:                             '</td><td class="LC_left_item">'.
                   4716:                             '<span class="LC_nobreak">';
                   4717:             foreach my $auth (@authtypes) {
                   4718:                 $datatable .= '<label>'. 
                   4719:                               '<input type="checkbox" name="'.$item.'_auth" '.
                   4720:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
                   4721:                               $authname{$auth}.'</label>&nbsp;';
                   4722:             }
                   4723:             $datatable .= '</span></td></tr>';
                   4724:             $rownum ++;
1.27      raeburn  4725:         }
1.30      raeburn  4726:         $$rowtotal += $rownum;
1.27      raeburn  4727:     }
                   4728:     return $datatable;
                   4729: }
                   4730: 
1.224     raeburn  4731: sub print_selfcreation {
                   4732:     my ($position,$dom,$settings,$rowtotal) = @_;
1.236     raeburn  4733:     my (@selfcreate,$createsettings,$processing,$datatable);
1.224     raeburn  4734:     if (ref($settings) eq 'HASH') {
                   4735:         if (ref($settings->{'cancreate'}) eq 'HASH') {
                   4736:             $createsettings = $settings->{'cancreate'};
1.236     raeburn  4737:             if (ref($createsettings) eq 'HASH') {
                   4738:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
                   4739:                     @selfcreate = @{$createsettings->{'selfcreate'}};
                   4740:                 } elsif ($createsettings->{'selfcreate'} ne '') {
                   4741:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
                   4742:                         @selfcreate = ('email','login','sso');
                   4743:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
                   4744:                         @selfcreate = ($createsettings->{'selfcreate'});
                   4745:                     }
                   4746:                 }
                   4747:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
                   4748:                     $processing = $createsettings->{'selfcreateprocessing'};
1.224     raeburn  4749:                 }
                   4750:             }
                   4751:         }
                   4752:     }
                   4753:     my %radiohash;
                   4754:     my $numinrow = 4;
                   4755:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
                   4756:     if ($position eq 'top') {
                   4757:         my %choices = &Apache::lonlocal::texthash (
                   4758:                                                       cancreate_login      => 'Institutional Login',
                   4759:                                                       cancreate_sso        => 'Institutional Single Sign On',
                   4760:                                                   );
                   4761:         my @toggles = sort(keys(%choices));
                   4762:         my %defaultchecked = (
                   4763:                                'cancreate_login' => 'off',
                   4764:                                'cancreate_sso'   => 'off',
                   4765:                              );
1.228     raeburn  4766:         my ($onclick,$itemcount);
1.224     raeburn  4767:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
                   4768:                                                      \%choices,$itemcount,$onclick);
1.228     raeburn  4769:         $$rowtotal += $itemcount;
                   4770:         
1.224     raeburn  4771:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
                   4772: 
                   4773:         if (ref($usertypes) eq 'HASH') {
                   4774:             if (keys(%{$usertypes}) > 0) {
                   4775:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
                   4776:                                              $dom,$numinrow,$othertitle,
1.228     raeburn  4777:                                              'statustocreate',$$rowtotal);
1.224     raeburn  4778:                 $$rowtotal ++;
                   4779:             }
                   4780:         }
1.240     raeburn  4781:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
                   4782:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
                   4783:         $fieldtitles{'inststatus'} = &mt('Institutional status');
                   4784:         my $rem;
                   4785:         my $numperrow = 2;
                   4786:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
                   4787:         $datatable .= '<tr'.$css_class.'>'.
1.241     raeburn  4788:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
1.240     raeburn  4789:                      '<td class="LC_left_item">'."\n".
                   4790:                      '<table><tr><td>'."\n";
                   4791:         for (my $i=0; $i<@fields; $i++) {
                   4792:             $rem = $i%($numperrow);
                   4793:             if ($rem == 0) {
                   4794:                 if ($i > 0) {
                   4795:                     $datatable .= '</tr>';
                   4796:                 }
                   4797:                 $datatable .= '<tr>';
                   4798:             }
                   4799:             my $currval;
1.248     raeburn  4800:             if (ref($createsettings) eq 'HASH') {
                   4801:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
                   4802:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
                   4803:                 }
1.240     raeburn  4804:             }
                   4805:             $datatable .= '<td class="LC_left_item">'.
                   4806:                           '<span class="LC_nobreak">'.
                   4807:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
                   4808:                           'value="'.$currval.'" size="10" />&nbsp;'.
                   4809:                           $fieldtitles{$fields[$i]}.'</span></td>';
                   4810:         }
                   4811:         my $colsleft = $numperrow - $rem;
                   4812:         if ($colsleft > 1 ) {
                   4813:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
                   4814:                          '&nbsp;</td>';
                   4815:         } elsif ($colsleft == 1) {
                   4816:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
                   4817:         }
                   4818:         $datatable .= '</tr></table></td></tr>';
                   4819:         $$rowtotal ++;
1.224     raeburn  4820:     } elsif ($position eq 'middle') {
                   4821:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
                   4822:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
                   4823:         $usertypes->{'default'} = $othertitle;
                   4824:         if (ref($types) eq 'ARRAY') {
                   4825:             push(@{$types},'default');
                   4826:             $usertypes->{'default'} = $othertitle;
                   4827:             foreach my $status (@{$types}) {
                   4828:                 $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
1.228     raeburn  4829:                                                        $numinrow,$$rowtotal,$usertypes);
1.240     raeburn  4830:                 $$rowtotal ++;
1.224     raeburn  4831:             }
                   4832:         }
                   4833:     } else {
1.236     raeburn  4834:         my %choices = &Apache::lonlocal::texthash (
                   4835:                                                       cancreate_email => 'E-mail address as username',
                   4836:                                                   );
                   4837:         my @toggles = sort(keys(%choices));
                   4838:         my %defaultchecked = (
                   4839:                                'cancreate_email' => 'off',
                   4840:                              );
                   4841:         my $itemcount = 0;
                   4842:         my $display = 'none';
                   4843:         if (grep(/^\Qemail\E$/,@selfcreate)) {
                   4844:             $display = 'block';
                   4845:         }
                   4846:         my $onclick = "toggleDisplay(this.form,'emailoptions');";
                   4847:         my $additional = '<div id="emailoptions" style="display: '.$display.'">';
                   4848:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
                   4849:         my $usertypes = {};
                   4850:         my $order = [];
                   4851:         if ((ref($domdefaults{'inststatustypes'}) eq 'HASH') && (ref($domdefaults{'inststatusguest'}) eq 'ARRAY')) {
                   4852:             $usertypes = $domdefaults{'inststatustypes'};
                   4853:             $order = $domdefaults{'inststatusguest'};
                   4854:         }
                   4855:         if (ref($order) eq 'ARRAY') {
                   4856:             push(@{$order},'default');
                   4857:             if (@{$order} > 1) {
                   4858:                 $usertypes->{'default'} = &mt('Other users');
                   4859:                 $additional .= '<table><tr>';
                   4860:                 foreach my $status (@{$order}) {
                   4861:                     $additional .= '<th>'.$usertypes->{$status}.'</th>';
                   4862:                 }
                   4863:                 $additional .= '</tr><tr>';
                   4864:                 foreach my $status (@{$order}) {
                   4865:                     $additional .= '<td>'.&email_as_username($rowtotal,$processing,$status).'</td>';
1.224     raeburn  4866:                 }
1.236     raeburn  4867:                 $additional .= '</tr></table>';
1.224     raeburn  4868:             } else {
1.236     raeburn  4869:                 $usertypes->{'default'} = &mt('All users');
                   4870:                 $additional .= &email_as_username($rowtotal,$processing);
1.224     raeburn  4871:             }
                   4872:         }
1.236     raeburn  4873:         $additional .= '</div>'."\n";
                   4874: 
                   4875:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
1.240     raeburn  4876:                                                      \%choices,$$rowtotal,$onclick,$additional);
                   4877:         $$rowtotal ++;
1.236     raeburn  4878:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
1.228     raeburn  4879:         $$rowtotal ++;
1.224     raeburn  4880:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
1.228     raeburn  4881:         $numinrow = 1;
1.236     raeburn  4882:         if (ref($order) eq 'ARRAY') {
                   4883:             foreach my $status (@{$order}) {
1.228     raeburn  4884:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
                   4885:                                                        $numinrow,$$rowtotal,$usertypes,$infofields,$infotitles);
                   4886:                 $$rowtotal ++;
                   4887:             }
                   4888:         }
1.224     raeburn  4889:         my ($emailrules,$emailruleorder) =
                   4890:             &Apache::lonnet::inst_userrules($dom,'email');
                   4891:         if (ref($emailrules) eq 'HASH') {
                   4892:             if (keys(%{$emailrules}) > 0) {
                   4893:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
1.228     raeburn  4894:                                                 $emailruleorder,$numinrow,$$rowtotal);
1.224     raeburn  4895:                 $$rowtotal ++;
                   4896:             }
                   4897:         }
1.228     raeburn  4898:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
1.224     raeburn  4899:     }
                   4900:     return $datatable;
                   4901: }
                   4902: 
1.236     raeburn  4903: sub email_as_username {
                   4904:     my ($rowtotal,$processing,$type) = @_;
                   4905:     my %choices =
                   4906:         &Apache::lonlocal::texthash (
                   4907:                                       automatic => 'Automatic approval',
                   4908:                                       approval  => 'Queued for approval',
                   4909:                                     );
                   4910:     my $output;
                   4911:     foreach my $option ('automatic','approval') {
                   4912:         my $checked;
                   4913:         if (ref($processing) eq 'HASH') {
                   4914:             if ($type eq '') {   
                   4915:                 if (!exists($processing->{'default'})) {
                   4916:                     if ($option eq 'automatic') {
                   4917:                         $checked = ' checked="checked"';
                   4918:                     }
                   4919:                 } else {
                   4920:                     if ($processing->{'default'} eq $option) {
                   4921:                         $checked = ' checked="checked"';
                   4922:                     }
                   4923:                 }
                   4924:             } else {
                   4925:                 if (!exists($processing->{$type})) {
                   4926:                     if ($option eq 'automatic') {
                   4927:                         $checked = ' checked="checked"';
                   4928:                     }
                   4929:                 } else {
                   4930:                     if ($processing->{$type} eq $option) {
                   4931:                         $checked = ' checked="checked"';
                   4932:                     }
                   4933:                 }
                   4934:             }
                   4935:         } elsif ($option eq 'automatic') {
                   4936:             $checked = ' checked="checked"'; 
                   4937:         }
                   4938:         my $name = 'cancreate_emailprocess';
                   4939:         if (($type ne '') && ($type ne 'default')) {
                   4940:             $name .= '_'.$type;
                   4941:         }
                   4942:         $output .= '<span class="LC_nobreak"><label>'.
                   4943:                    '<input type="radio" name="'.$name.'"'.
                   4944:                    $checked.' value="'.$option.'" />'.
                   4945:                    $choices{$option}.'</label></span>';
                   4946:         if ($type eq '') {
                   4947:             $output .= '&nbsp;';
                   4948:         } else {
                   4949:             $output .= '<br />';
                   4950:         }
                   4951:     }
                   4952:     $$rowtotal ++;
                   4953:     return $output;
                   4954: }
                   4955: 
1.165     raeburn  4956: sub captcha_choice {
1.169     raeburn  4957:     my ($context,$settings,$itemcount) = @_;
1.165     raeburn  4958:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext);
                   4959:     my %lt = &captcha_phrases();
                   4960:     $keyentry = 'hidden';
                   4961:     if ($context eq 'cancreate') {
1.224     raeburn  4962:         $rowname = &mt('CAPTCHA validation');
1.169     raeburn  4963:     } elsif ($context eq 'login') {
                   4964:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
1.165     raeburn  4965:     }
                   4966:     if (ref($settings) eq 'HASH') {
                   4967:         if ($settings->{'captcha'}) {
                   4968:             $checked{$settings->{'captcha'}} = ' checked="checked"';
                   4969:         } else {
                   4970:             $checked{'original'} = ' checked="checked"';
                   4971:         }
                   4972:         if ($settings->{'captcha'} eq 'recaptcha') {
                   4973:             $pubtext = $lt{'pub'};
                   4974:             $privtext = $lt{'priv'};
                   4975:             $keyentry = 'text';
                   4976:         }
                   4977:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
                   4978:             $currpub = $settings->{'recaptchakeys'}{'public'};
                   4979:             $currpriv = $settings->{'recaptchakeys'}{'private'};
                   4980:         }
                   4981:     } else {
                   4982:         $checked{'original'} = ' checked="checked"';
                   4983:     }
1.169     raeburn  4984:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
                   4985:     my $output = '<tr'.$css_class.'>'.
                   4986:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
1.165     raeburn  4987:                  '<table><tr><td>'."\n";
                   4988:     foreach my $option ('original','recaptcha','notused') {
                   4989:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
                   4990:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
                   4991:                    $lt{$option}.'</label></span>';
                   4992:         unless ($option eq 'notused') {
                   4993:             $output .= ('&nbsp;'x2)."\n";
                   4994:         }
                   4995:     }
                   4996: #
                   4997: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
                   4998: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
1.210     raeburn  4999: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
1.165     raeburn  5000: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
1.210     raeburn  5001: #
1.165     raeburn  5002:     $output .= '</td></tr>'."\n".
                   5003:                '<tr><td>'."\n".
                   5004:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
                   5005:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
                   5006:                $currpub.'" size="40" /></span><br />'."\n".
                   5007:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
                   5008:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
                   5009:                $currpriv.'" size="40" /></span></td></tr></table>'."\n".
                   5010:                '</td></tr>';
                   5011:     return $output;
                   5012: }
                   5013: 
1.32      raeburn  5014: sub user_formats_row {
                   5015:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
                   5016:     my $output;
                   5017:     my %text = (
                   5018:                    'username' => 'new usernames',
                   5019:                    'id'       => 'IDs',
1.45      raeburn  5020:                    'email'    => 'self-created accounts (e-mail)',
1.32      raeburn  5021:                );
                   5022:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
                   5023:     $output = '<tr '.$css_class.'>'.
1.63      raeburn  5024:               '<td><span class="LC_nobreak">';
                   5025:     if ($type eq 'email') {
                   5026:         $output .= &mt("Formats disallowed for $text{$type}: ");
                   5027:     } else {
                   5028:         $output .= &mt("Format rules to check for $text{$type}: ");
                   5029:     }
                   5030:     $output .= '</span></td>'.
                   5031:                '<td class="LC_left_item" colspan="2"><table>';
1.27      raeburn  5032:     my $rem;
                   5033:     if (ref($ruleorder) eq 'ARRAY') {
                   5034:         for (my $i=0; $i<@{$ruleorder}; $i++) {
                   5035:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
                   5036:                 my $rem = $i%($numinrow);
                   5037:                 if ($rem == 0) {
                   5038:                     if ($i > 0) {
                   5039:                         $output .= '</tr>';
                   5040:                     }
                   5041:                     $output .= '<tr>';
                   5042:                 }
                   5043:                 my $check = ' ';
1.39      raeburn  5044:                 if (ref($settings) eq 'HASH') {
                   5045:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
                   5046:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
                   5047:                             $check = ' checked="checked" ';
                   5048:                         }
1.27      raeburn  5049:                     }
                   5050:                 }
                   5051:                 $output .= '<td class="LC_left_item">'.
                   5052:                            '<span class="LC_nobreak"><label>'.
1.32      raeburn  5053:                            '<input type="checkbox" name="'.$type.'_rule" '.
1.27      raeburn  5054:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
                   5055:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
                   5056:             }
                   5057:         }
                   5058:         $rem = @{$ruleorder}%($numinrow);
                   5059:     }
                   5060:     my $colsleft = $numinrow - $rem;
                   5061:     if ($colsleft > 1 ) {
                   5062:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
                   5063:                    '&nbsp;</td>';
                   5064:     } elsif ($colsleft == 1) {
                   5065:         $output .= '<td class="LC_left_item">&nbsp;</td>';
                   5066:     }
                   5067:     $output .= '</tr></table></td></tr>';
                   5068:     return $output;
                   5069: }
                   5070: 
1.34      raeburn  5071: sub usercreation_types {
                   5072:     my %lt = &Apache::lonlocal::texthash (
                   5073:                     author     => 'When adding a co-author',
                   5074:                     course     => 'When adding a user to a course',
1.100     raeburn  5075:                     requestcrs => 'When requesting a course',
1.34      raeburn  5076:                     any        => 'Any',
                   5077:                     official   => 'Institutional only ',
                   5078:                     unofficial => 'Non-institutional only',
                   5079:                     none       => 'None',
                   5080:     );
                   5081:     return %lt;
1.48      raeburn  5082: }
1.34      raeburn  5083: 
1.224     raeburn  5084: sub selfcreation_types {
                   5085:     my %lt = &Apache::lonlocal::texthash (
                   5086:                     selfcreate => 'User creates own account',
                   5087:                     any        => 'Any',
                   5088:                     official   => 'Institutional only ',
                   5089:                     unofficial => 'Non-institutional only',
                   5090:                     email      => 'E-mail address',
                   5091:                     login      => 'Institutional Login',
                   5092:                     sso        => 'SSO',
                   5093:              );
                   5094: }
                   5095: 
1.28      raeburn  5096: sub authtype_names {
                   5097:     my %lt = &Apache::lonlocal::texthash(
                   5098:                       int    => 'Internal',
                   5099:                       krb4   => 'Kerberos 4',
                   5100:                       krb5   => 'Kerberos 5',
                   5101:                       loc    => 'Local',
                   5102:                   );
                   5103:     return %lt;
                   5104: }
                   5105: 
                   5106: sub context_names {
                   5107:     my %context_title = &Apache::lonlocal::texthash(
                   5108:        author => 'Creating users when an Author',
                   5109:        course => 'Creating users when in a course',
                   5110:        domain => 'Creating users when a Domain Coordinator',
                   5111:     );
                   5112:     return %context_title;
                   5113: }
                   5114: 
1.33      raeburn  5115: sub print_usermodification {
                   5116:     my ($position,$dom,$settings,$rowtotal) = @_;
                   5117:     my $numinrow = 4;
                   5118:     my ($context,$datatable,$rowcount);
                   5119:     if ($position eq 'top') {
                   5120:         $rowcount = 0;
                   5121:         $context = 'author'; 
                   5122:         foreach my $role ('ca','aa') {
                   5123:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
                   5124:                                                    $numinrow,$rowcount);
                   5125:             $$rowtotal ++;
                   5126:             $rowcount ++;
                   5127:         }
1.230     raeburn  5128:     } elsif ($position eq 'bottom') {
1.33      raeburn  5129:         $context = 'course';
                   5130:         $rowcount = 0;
                   5131:         foreach my $role ('st','ep','ta','in','cr') {
                   5132:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
                   5133:                                                    $numinrow,$rowcount);
                   5134:             $$rowtotal ++;
                   5135:             $rowcount ++;
                   5136:         }
                   5137:     }
                   5138:     return $datatable;
                   5139: }
                   5140: 
1.43      raeburn  5141: sub print_defaults {
1.236     raeburn  5142:     my ($position,$dom,$settings,$rowtotal) = @_;
1.43      raeburn  5143:     my $rownum = 0;
                   5144:     my ($datatable,$css_class);
1.236     raeburn  5145:     if ($position eq 'top') {
                   5146:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
                   5147:                      'datelocale_def','portal_def');
                   5148:         my %defaults;
                   5149:         if (ref($settings) eq 'HASH') {
                   5150:             %defaults = %{$settings};
1.43      raeburn  5151:         } else {
1.236     raeburn  5152:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
                   5153:             foreach my $item (@items) {
                   5154:                 $defaults{$item} = $domdefaults{$item};
                   5155:             }
1.43      raeburn  5156:         }
1.236     raeburn  5157:         my $titles = &defaults_titles($dom);
                   5158:         foreach my $item (@items) {
                   5159:             if ($rownum%2) {
                   5160:                 $css_class = '';
                   5161:             } else {
                   5162:                 $css_class = ' class="LC_odd_row" ';
                   5163:             }
                   5164:             $datatable .= '<tr'.$css_class.'>'.
                   5165:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
                   5166:                           '</span></td><td class="LC_right_item" colspan="3">';
                   5167:             if ($item eq 'auth_def') {
                   5168:                 my @authtypes = ('internal','krb4','krb5','localauth');
                   5169:                 my %shortauth = (
                   5170:                                  internal => 'int',
                   5171:                                  krb4 => 'krb4',
                   5172:                                  krb5 => 'krb5',
                   5173:                                  localauth  => 'loc'
                   5174:                                 );
                   5175:                 my %authnames = &authtype_names();
                   5176:                 foreach my $auth (@authtypes) {
                   5177:                     my $checked = ' ';
                   5178:                     if ($defaults{$item} eq $auth) {
                   5179:                         $checked = ' checked="checked" ';
                   5180:                     }
                   5181:                     $datatable .= '<label><input type="radio" name="'.$item.
                   5182:                                   '" value="'.$auth.'"'.$checked.'/>'.
                   5183:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
                   5184:                 }
                   5185:             } elsif ($item eq 'timezone_def') {
                   5186:                 my $includeempty = 1;
                   5187:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
                   5188:             } elsif ($item eq 'datelocale_def') {
                   5189:                 my $includeempty = 1;
                   5190:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
                   5191:             } elsif ($item eq 'lang_def') {
1.263     raeburn  5192:                 my $includeempty = 1;
                   5193:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
1.236     raeburn  5194:             } else {
                   5195:                 my $size;
                   5196:                 if ($item eq 'portal_def') {
                   5197:                     $size = ' size="25"';
                   5198:                 }
                   5199:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
                   5200:                               $defaults{$item}.'"'.$size.' />';
1.43      raeburn  5201:             }
1.236     raeburn  5202:             $datatable .= '</td></tr>';
                   5203:             $rownum ++;
                   5204:         }
                   5205:     } else {
                   5206:         my (%defaults);
                   5207:         if (ref($settings) eq 'HASH') {
                   5208:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH') &&
                   5209:                 (ref($settings->{'inststatusguest'}) eq 'ARRAY')) {
                   5210:                 my $maxnum = @{$settings->{'inststatusorder'}};
                   5211:                 for (my $i=0; $i<$maxnum; $i++) {
                   5212:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
                   5213:                     my $item = $settings->{'inststatusorder'}->[$i];
                   5214:                     my $title = $settings->{'inststatustypes'}->{$item};
                   5215:                     my $guestok;
                   5216:                     if (grep(/^\Q$item\E$/,@{$settings->{'inststatusguest'}})) {
                   5217:                         $guestok = 1;
                   5218:                     }
                   5219:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
                   5220:                     $datatable .= '<tr'.$css_class.'>'.
                   5221:                                   '<td><span class="LC_nobreak">'.
                   5222:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
                   5223:                     for (my $k=0; $k<=$maxnum; $k++) {
                   5224:                         my $vpos = $k+1;
                   5225:                         my $selstr;
                   5226:                         if ($k == $i) {
                   5227:                             $selstr = ' selected="selected" ';
                   5228:                         }
                   5229:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
                   5230:                     }
                   5231:                     my ($checkedon,$checkedoff);
                   5232:                     $checkedoff = ' checked="checked"';
                   5233:                     if ($guestok) {
                   5234:                         $checkedon = $checkedoff;
                   5235:                         $checkedoff = ''; 
                   5236:                     }
                   5237:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
                   5238:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
                   5239:                                   &mt('delete').'</span></td>'.
                   5240:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
                   5241:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
                   5242:                                   '</span></td>'.
                   5243:                                   '<td class="LC_right_item"><span class="LC_nobreak">'.
                   5244:                                   '<label><input type="radio" value="1" name="inststatus_guest_'.$item.'"'.$checkedon.' />'.
                   5245:                                   &mt('Yes').'</label>'.('&nbsp;'x2).
                   5246:                                   '<label><input type="radio" value="0" name="inststatus_guest_'.$item.'"'.$checkedoff.' />'.
                   5247:                                   &mt('No').'</label></span></td></tr>';
                   5248:                 }
                   5249:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
                   5250:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
                   5251:                 $datatable .= '<tr '.$css_class.'>'.
                   5252:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
                   5253:                 for (my $k=0; $k<=$maxnum; $k++) {
                   5254:                     my $vpos = $k+1;
                   5255:                     my $selstr;
                   5256:                     if ($k == $maxnum) {
                   5257:                         $selstr = ' selected="selected" ';
                   5258:                     }
                   5259:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
                   5260:                 }
                   5261:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
1.263     raeburn  5262:                               '<input type="text" size="10" name="addinststatus" value="" />'.
1.236     raeburn  5263:                               '&nbsp;'.&mt('(new)').
                   5264:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
                   5265:                               &mt('Name displayed:').
                   5266:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
                   5267:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
                   5268:                               '<label><input type="radio" value="1" name="addinststatus_guest" />'.
                   5269:                               &mt('Yes').'</label>'.('&nbsp;'x2).
                   5270:                               '<label><input type="radio" value="0" name="addinststatus_guest" />'.
                   5271:                               &mt('No').'</label></span></td></tr>';
                   5272:                               '</tr>'."\n";
                   5273:                 $rownum ++;
1.141     raeburn  5274:             }
1.43      raeburn  5275:         }
                   5276:     }
                   5277:     $$rowtotal += $rownum;
                   5278:     return $datatable;
                   5279: }
                   5280: 
1.168     raeburn  5281: sub get_languages_hash {
                   5282:     my %langchoices;
                   5283:     foreach my $id (&Apache::loncommon::languageids()) {
                   5284:         my $code = &Apache::loncommon::supportedlanguagecode($id);
                   5285:         if ($code ne '') {
                   5286:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
                   5287:         }
                   5288:     }
                   5289:     return %langchoices;
                   5290: }
                   5291: 
1.43      raeburn  5292: sub defaults_titles {
1.141     raeburn  5293:     my ($dom) = @_;
1.43      raeburn  5294:     my %titles = &Apache::lonlocal::texthash (
                   5295:                    'auth_def'      => 'Default authentication type',
                   5296:                    'auth_arg_def'  => 'Default authentication argument',
                   5297:                    'lang_def'      => 'Default language',
1.54      raeburn  5298:                    'timezone_def'  => 'Default timezone',
1.68      raeburn  5299:                    'datelocale_def' => 'Default locale for dates',
1.141     raeburn  5300:                    'portal_def'     => 'Portal/Default URL',
1.43      raeburn  5301:                  );
1.141     raeburn  5302:     if ($dom) {
                   5303:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
                   5304:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
                   5305:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
                   5306:         $protocol = 'http' if ($protocol ne 'https');
                   5307:         if ($uint_dom) {
                   5308:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
                   5309:                                          $uint_dom);
                   5310:         }
                   5311:     }
1.43      raeburn  5312:     return (\%titles);
                   5313: }
                   5314: 
1.46      raeburn  5315: sub print_scantronformat {
                   5316:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
                   5317:     my $itemcount = 1;
1.60      raeburn  5318:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
                   5319:         %confhash);
1.46      raeburn  5320:     my $switchserver = &check_switchserver($dom,$confname);
                   5321:     my %lt = &Apache::lonlocal::texthash (
1.95      www      5322:                 default => 'Default bubblesheet format file error',
                   5323:                 custom  => 'Custom bubblesheet format file error',
1.46      raeburn  5324:              );
                   5325:     my %scantronfiles = (
                   5326:         default => 'default.tab',
                   5327:         custom => 'custom.tab',
                   5328:     );
                   5329:     foreach my $key (keys(%scantronfiles)) {
                   5330:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
                   5331:                               .$scantronfiles{$key};
                   5332:     }
                   5333:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
                   5334:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
                   5335:         if (!$switchserver) {
                   5336:             my $servadm = $r->dir_config('lonAdmEMail');
                   5337:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
                   5338:             if ($configuserok eq 'ok') {
                   5339:                 if ($author_ok eq 'ok') {
                   5340:                     my %legacyfile = (
                   5341:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
                   5342:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
                   5343:                     );
                   5344:                     my %md5chk;
                   5345:                     foreach my $type (keys(%legacyfile)) {
1.60      raeburn  5346:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
                   5347:                         chomp($md5chk{$type});
1.46      raeburn  5348:                     }
                   5349:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
                   5350:                         foreach my $type (keys(%legacyfile)) {
1.60      raeburn  5351:                             ($scantronurls{$type},my $error) = 
1.46      raeburn  5352:                                 &legacy_scantronformat($r,$dom,$confname,
                   5353:                                                  $type,$legacyfile{$type},
                   5354:                                                  $scantronurls{$type},
                   5355:                                                  $scantronfiles{$type});
1.60      raeburn  5356:                             if ($error ne '') {
                   5357:                                 $error{$type} = $error;
                   5358:                             }
                   5359:                         }
                   5360:                         if (keys(%error) == 0) {
                   5361:                             $is_custom = 1;
                   5362:                             $confhash{'scantron'}{'scantronformat'} = 
                   5363:                                 $scantronurls{'custom'};
                   5364:                             my $putresult = 
                   5365:                                 &Apache::lonnet::put_dom('configuration',
                   5366:                                                          \%confhash,$dom);
                   5367:                             if ($putresult ne 'ok') {
                   5368:                                 $error{'custom'} = 
                   5369:                                     '<span class="LC_error">'.
                   5370:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
                   5371:                             }
1.46      raeburn  5372:                         }
                   5373:                     } else {
1.60      raeburn  5374:                         ($scantronurls{'default'},my $error) =
1.46      raeburn  5375:                             &legacy_scantronformat($r,$dom,$confname,
                   5376:                                           'default',$legacyfile{'default'},
                   5377:                                           $scantronurls{'default'},
                   5378:                                           $scantronfiles{'default'});
1.60      raeburn  5379:                         if ($error eq '') {
                   5380:                             $confhash{'scantron'}{'scantronformat'} = ''; 
                   5381:                             my $putresult =
                   5382:                                 &Apache::lonnet::put_dom('configuration',
                   5383:                                                          \%confhash,$dom);
                   5384:                             if ($putresult ne 'ok') {
                   5385:                                 $error{'default'} =
                   5386:                                     '<span class="LC_error">'.
                   5387:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
                   5388:                             }
                   5389:                         } else {
                   5390:                             $error{'default'} = $error;
                   5391:                         }
1.46      raeburn  5392:                     }
                   5393:                 }
                   5394:             }
                   5395:         } else {
1.95      www      5396:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
1.46      raeburn  5397:         }
                   5398:     }
                   5399:     if (ref($settings) eq 'HASH') {
                   5400:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
                   5401:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
                   5402:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
                   5403:                 $scantronurl = '';
                   5404:             } else {
                   5405:                 $scantronurl = $settings->{'scantronformat'};
                   5406:             }
                   5407:             $is_custom = 1;
                   5408:         } else {
                   5409:             $scantronurl = $scantronurls{'default'};
                   5410:         }
                   5411:     } else {
1.60      raeburn  5412:         if ($is_custom) {
                   5413:             $scantronurl = $scantronurls{'custom'};
                   5414:         } else {
                   5415:             $scantronurl = $scantronurls{'default'};
                   5416:         }
1.46      raeburn  5417:     }
                   5418:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
                   5419:     $datatable .= '<tr'.$css_class.'>';
                   5420:     if (!$is_custom) {
1.65      raeburn  5421:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
                   5422:                       '<span class="LC_nobreak">';
1.46      raeburn  5423:         if ($scantronurl) {
1.199     raeburn  5424:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
                   5425:                                                          undef,undef,undef,undef,'background-color:#ffffff');
1.46      raeburn  5426:         } else {
                   5427:             $datatable = &mt('File unavailable for display');
                   5428:         }
1.65      raeburn  5429:         $datatable .= '</span></td>';
1.60      raeburn  5430:         if (keys(%error) == 0) { 
                   5431:             $datatable .= '<td valign="bottom">';
                   5432:             if (!$switchserver) {
                   5433:                 $datatable .= &mt('Upload:').'<br />';
                   5434:             }
                   5435:         } else {
                   5436:             my $errorstr;
                   5437:             foreach my $key (sort(keys(%error))) {
                   5438:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
                   5439:             }
                   5440:             $datatable .= '<td>'.$errorstr;
                   5441:         }
1.46      raeburn  5442:     } else {
                   5443:         if (keys(%error) > 0) {
                   5444:             my $errorstr;
                   5445:             foreach my $key (sort(keys(%error))) {
                   5446:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
                   5447:             } 
1.60      raeburn  5448:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
1.46      raeburn  5449:         } elsif ($scantronurl) {
1.199     raeburn  5450:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
                   5451:                                                        undef,undef,undef,undef,'background-color:#ffffff');
1.65      raeburn  5452:             $datatable .= '<td><span class="LC_nobreak">'.
1.199     raeburn  5453:                           $link.
                   5454:                           '<label><input type="checkbox" name="scantronformat_del"'.
                   5455:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
1.65      raeburn  5456:                           '<td><span class="LC_nobreak">&nbsp;'.
                   5457:                           &mt('Replace:').'</span><br />';
1.46      raeburn  5458:         }
                   5459:     }
                   5460:     if (keys(%error) == 0) {
                   5461:         if ($switchserver) {
                   5462:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
                   5463:         } else {
1.65      raeburn  5464:             $datatable .='<span class="LC_nobreak">&nbsp;'.
                   5465:                          '<input type="file" name="scantronformat" /></span>';
1.46      raeburn  5466:         }
                   5467:     }
                   5468:     $datatable .= '</td></tr>';
                   5469:     $$rowtotal ++;
                   5470:     return $datatable;
                   5471: }
                   5472: 
                   5473: sub legacy_scantronformat {
                   5474:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
                   5475:     my ($url,$error);
                   5476:     my @statinfo = &Apache::lonnet::stat_file($newurl);
                   5477:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
                   5478:         (my $result,$url) =
                   5479:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
                   5480:                          '','',$newfile);
                   5481:         if ($result ne 'ok') {
1.130     raeburn  5482:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
1.46      raeburn  5483:         }
                   5484:     }
                   5485:     return ($url,$error);
                   5486: }
1.43      raeburn  5487: 
1.49      raeburn  5488: sub print_coursecategories {
1.57      raeburn  5489:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
                   5490:     my $datatable;
                   5491:     if ($position eq 'top') {
1.238     raeburn  5492:         my (%checked);
                   5493:         my @catitems = ('unauth','auth');
                   5494:         my @cattypes = ('std','domonly','codesrch','none');
                   5495:         $checked{'unauth'} = 'std';
                   5496:         $checked{'auth'} = 'std';
                   5497:         if (ref($settings) eq 'HASH') {
                   5498:             foreach my $type (@cattypes) {
                   5499:                 if ($type eq $settings->{'unauth'}) {
                   5500:                     $checked{'unauth'} = $type;
                   5501:                 }
                   5502:                 if ($type eq $settings->{'auth'}) {
                   5503:                     $checked{'auth'} = $type;
                   5504:                 }
                   5505:             }
                   5506:         }
                   5507:         my %lt = &Apache::lonlocal::texthash (
                   5508:                                                unauth   => 'Catalog type for unauthenticated users',
                   5509:                                                auth     => 'Catalog type for authenticated users',
                   5510:                                                none     => 'No catalog',
                   5511:                                                std      => 'Standard catalog',
                   5512:                                                domonly  => 'Domain-only catalog',
                   5513:                                                codesrch => "Code search form",
                   5514:                                              );
                   5515:        my $itemcount = 0;
                   5516:        foreach my $item (@catitems) {
                   5517:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
                   5518:            $datatable .= '<tr '.$css_class.'>'.
                   5519:                          '<td>'.$lt{$item}.'</td>'.
                   5520:                          '<td class="LC_right_item"><span class="LC_nobreak">';
                   5521:            foreach my $type (@cattypes) {
                   5522:                my $ischecked;
                   5523:                if ($checked{$item} eq $type) {
                   5524:                    $ischecked=' checked="checked"';
                   5525:                }
                   5526:                $datatable .= '<label>'.
                   5527:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
                   5528:                              ' />'.$lt{$type}.'</label>&nbsp;';
                   5529:            }
                   5530:            $datatable .= '</td></tr>';
                   5531:            $itemcount ++;
                   5532:         }
                   5533:         $$rowtotal += $itemcount;
                   5534:     } elsif ($position eq 'middle') {
1.57      raeburn  5535:         my $toggle_cats_crs = ' ';
                   5536:         my $toggle_cats_dom = ' checked="checked" ';
                   5537:         my $can_cat_crs = ' ';
                   5538:         my $can_cat_dom = ' checked="checked" ';
1.120     raeburn  5539:         my $toggle_catscomm_comm = ' ';
                   5540:         my $toggle_catscomm_dom = ' checked="checked" ';
                   5541:         my $can_catcomm_comm = ' ';
                   5542:         my $can_catcomm_dom = ' checked="checked" ';
                   5543: 
1.57      raeburn  5544:         if (ref($settings) eq 'HASH') {
                   5545:             if ($settings->{'togglecats'} eq 'crs') {
                   5546:                 $toggle_cats_crs = $toggle_cats_dom;
                   5547:                 $toggle_cats_dom = ' ';
                   5548:             }
                   5549:             if ($settings->{'categorize'} eq 'crs') {
                   5550:                 $can_cat_crs = $can_cat_dom;
                   5551:                 $can_cat_dom = ' ';
                   5552:             }
1.120     raeburn  5553:             if ($settings->{'togglecatscomm'} eq 'comm') {
                   5554:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
                   5555:                 $toggle_catscomm_dom = ' ';
                   5556:             }
                   5557:             if ($settings->{'categorizecomm'} eq 'comm') {
                   5558:                 $can_catcomm_comm = $can_catcomm_dom;
                   5559:                 $can_catcomm_dom = ' ';
                   5560:             }
1.57      raeburn  5561:         }
                   5562:         my %title = &Apache::lonlocal::texthash (
1.120     raeburn  5563:                      togglecats     => 'Show/Hide a course in catalog',
                   5564:                      togglecatscomm => 'Show/Hide a community in catalog',
                   5565:                      categorize     => 'Assign a category to a course',
                   5566:                      categorizecomm => 'Assign a category to a community',
1.57      raeburn  5567:                     );
                   5568:         my %level = &Apache::lonlocal::texthash (
1.120     raeburn  5569:                      dom  => 'Set in Domain',
                   5570:                      crs  => 'Set in Course',
                   5571:                      comm => 'Set in Community',
1.57      raeburn  5572:                     );
                   5573:         $datatable = '<tr class="LC_odd_row">'.
                   5574:                   '<td>'.$title{'togglecats'}.'</td>'.
                   5575:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
                   5576:                   '<input type="radio" name="togglecats"'.
                   5577:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
                   5578:                   '<label><input type="radio" name="togglecats"'.
                   5579:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
                   5580:                   '</tr><tr>'.
                   5581:                   '<td>'.$title{'categorize'}.'</td>'.
                   5582:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
                   5583:                   '<label><input type="radio" name="categorize"'.
                   5584:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
                   5585:                   '<label><input type="radio" name="categorize"'.
                   5586:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
1.120     raeburn  5587:                   '</tr><tr class="LC_odd_row">'.
                   5588:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
                   5589:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
                   5590:                   '<input type="radio" name="togglecatscomm"'.
                   5591:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
                   5592:                   '<label><input type="radio" name="togglecatscomm"'.
                   5593:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
                   5594:                   '</tr><tr>'.
                   5595:                   '<td>'.$title{'categorizecomm'}.'</td>'.
                   5596:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
                   5597:                   '<label><input type="radio" name="categorizecomm"'.
                   5598:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
                   5599:                   '<label><input type="radio" name="categorizecomm"'.
                   5600:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
1.57      raeburn  5601:                   '</tr>';
1.120     raeburn  5602:         $$rowtotal += 4;
1.57      raeburn  5603:     } else {
                   5604:         my $css_class;
                   5605:         my $itemcount = 1;
                   5606:         my $cathash; 
                   5607:         if (ref($settings) eq 'HASH') {
                   5608:             $cathash = $settings->{'cats'};
                   5609:         }
                   5610:         if (ref($cathash) eq 'HASH') {
                   5611:             my (@cats,@trails,%allitems,%idx,@jsarray);
                   5612:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
                   5613:                                                    \%allitems,\%idx,\@jsarray);
                   5614:             my $maxdepth = scalar(@cats);
                   5615:             my $colattrib = '';
                   5616:             if ($maxdepth > 2) {
                   5617:                 $colattrib = ' colspan="2" ';
                   5618:             }
                   5619:             my @path;
                   5620:             if (@cats > 0) {
                   5621:                 if (ref($cats[0]) eq 'ARRAY') {
                   5622:                     my $numtop = @{$cats[0]};
                   5623:                     my $maxnum = $numtop;
1.120     raeburn  5624:                     my %default_names = (
                   5625:                           instcode    => &mt('Official courses'),
                   5626:                           communities => &mt('Communities'),
                   5627:                     );
                   5628: 
                   5629:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
                   5630:                         ($cathash->{'instcode::0'} eq '') ||
                   5631:                         (!grep(/^communities$/,@{$cats[0]})) || 
                   5632:                         ($cathash->{'communities::0'} eq '')) {
1.57      raeburn  5633:                         $maxnum ++;
                   5634:                     }
                   5635:                     my $lastidx;
                   5636:                     for (my $i=0; $i<$numtop; $i++) {
                   5637:                         my $parent = $cats[0][$i];
                   5638:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
                   5639:                         my $item = &escape($parent).'::0';
                   5640:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
                   5641:                         $lastidx = $idx{$item};
                   5642:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
                   5643:                                       .'<select name="'.$item.'"'.$chgstr.'>';
                   5644:                         for (my $k=0; $k<=$maxnum; $k++) {
                   5645:                             my $vpos = $k+1;
                   5646:                             my $selstr;
                   5647:                             if ($k == $i) {
                   5648:                                 $selstr = ' selected="selected" ';
                   5649:                             }
                   5650:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
                   5651:                         }
1.214     raeburn  5652:                         $datatable .= '</select></span></td><td>';
1.120     raeburn  5653:                         if ($parent eq 'instcode' || $parent eq 'communities') {
                   5654:                             $datatable .=  '<span class="LC_nobreak">'
                   5655:                                            .$default_names{$parent}.'</span>';
                   5656:                             if ($parent eq 'instcode') {
                   5657:                                 $datatable .= '<br /><span class="LC_nobreak">('
                   5658:                                               .&mt('with institutional codes')
                   5659:                                               .')</span></td><td'.$colattrib.'>';
                   5660:                             } else {
                   5661:                                 $datatable .= '<table><tr><td>';
                   5662:                             }
                   5663:                             $datatable .= '<span class="LC_nobreak">'
                   5664:                                           .'<label><input type="radio" name="'
                   5665:                                           .$parent.'" value="1" checked="checked" />'
                   5666:                                           .&mt('Display').'</label>';
                   5667:                             if ($parent eq 'instcode') {
                   5668:                                 $datatable .= '&nbsp;';
                   5669:                             } else {
                   5670:                                 $datatable .= '</span></td></tr><tr><td>'
                   5671:                                               .'<span class="LC_nobreak">';
                   5672:                             }
                   5673:                             $datatable .= '<label><input type="radio" name="'
                   5674:                                           .$parent.'" value="0" />'
                   5675:                                           .&mt('Do not display').'</label></span>';
                   5676:                             if ($parent eq 'communities') {
                   5677:                                 $datatable .= '</td></tr></table>';
                   5678:                             }
                   5679:                             $datatable .= '</td>';
1.57      raeburn  5680:                         } else {
                   5681:                             $datatable .= $parent
1.214     raeburn  5682:                                           .'&nbsp;<span class="LC_nobreak"><label>'
                   5683:                                           .'<input type="checkbox" name="deletecategory" '
1.57      raeburn  5684:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
                   5685:                         }
                   5686:                         my $depth = 1;
                   5687:                         push(@path,$parent);
                   5688:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
                   5689:                         pop(@path);
                   5690:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
                   5691:                         $itemcount ++;
                   5692:                     }
1.48      raeburn  5693:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
1.57      raeburn  5694:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
                   5695:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
1.48      raeburn  5696:                     for (my $k=0; $k<=$maxnum; $k++) {
                   5697:                         my $vpos = $k+1;
                   5698:                         my $selstr;
1.57      raeburn  5699:                         if ($k == $numtop) {
1.48      raeburn  5700:                             $selstr = ' selected="selected" ';
                   5701:                         }
                   5702:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
                   5703:                     }
1.59      bisitz   5704:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
1.57      raeburn  5705:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
                   5706:                                   .'</tr>'."\n";
1.48      raeburn  5707:                     $itemcount ++;
1.120     raeburn  5708:                     foreach my $default ('instcode','communities') {
                   5709:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
                   5710:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
                   5711:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
                   5712:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
                   5713:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
                   5714:                             for (my $k=0; $k<=$maxnum; $k++) {
                   5715:                                 my $vpos = $k+1;
                   5716:                                 my $selstr;
                   5717:                                 if ($k == $maxnum) {
                   5718:                                     $selstr = ' selected="selected" ';
                   5719:                                 }
                   5720:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
1.57      raeburn  5721:                             }
1.120     raeburn  5722:                             $datatable .= '</select></span></td>'.
                   5723:                                           '<td><span class="LC_nobreak">'.
                   5724:                                           $default_names{$default}.'</span>';
                   5725:                             if ($default eq 'instcode') {
                   5726:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
                   5727:                                               .&mt('with institutional codes').')</span>';
                   5728:                             }
                   5729:                             $datatable .= '</td>'
                   5730:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
                   5731:                                           .&mt('Display').'</label>&nbsp;'
                   5732:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
                   5733:                                           .&mt('Do not display').'</label></span></td></tr>';
1.48      raeburn  5734:                         }
                   5735:                     }
                   5736:                 }
1.57      raeburn  5737:             } else {
                   5738:                 $datatable .= &initialize_categories($itemcount);
1.48      raeburn  5739:             }
                   5740:         } else {
1.238     raeburn  5741:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
1.57      raeburn  5742:                           .&initialize_categories($itemcount);
1.48      raeburn  5743:         }
1.57      raeburn  5744:         $$rowtotal += $itemcount;
1.48      raeburn  5745:     }
                   5746:     return $datatable;
                   5747: }
                   5748: 
1.69      raeburn  5749: sub print_serverstatuses {
                   5750:     my ($dom,$settings,$rowtotal) = @_;
                   5751:     my $datatable;
                   5752:     my @pages = &serverstatus_pages();
                   5753:     my (%namedaccess,%machineaccess);
                   5754:     foreach my $type (@pages) {
                   5755:         $namedaccess{$type} = '';
                   5756:         $machineaccess{$type}= '';
                   5757:     }
                   5758:     if (ref($settings) eq 'HASH') {
                   5759:         foreach my $type (@pages) {
                   5760:             if (exists($settings->{$type})) {
                   5761:                 if (ref($settings->{$type}) eq 'HASH') {
                   5762:                     foreach my $key (keys(%{$settings->{$type}})) {
                   5763:                         if ($key eq 'namedusers') {
                   5764:                             $namedaccess{$type} = $settings->{$type}->{$key};
                   5765:                         } elsif ($key eq 'machines') {
                   5766:                             $machineaccess{$type} = $settings->{$type}->{$key};
                   5767:                         }
                   5768:                     }
                   5769:                 }
                   5770:             }
                   5771:         }
                   5772:     }
1.81      raeburn  5773:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
1.69      raeburn  5774:     my $rownum = 0;
                   5775:     my $css_class;
                   5776:     foreach my $type (@pages) {
                   5777:         $rownum ++;
                   5778:         $css_class = $rownum%2?' class="LC_odd_row"':'';
                   5779:         $datatable .= '<tr'.$css_class.'>'.
                   5780:                       '<td><span class="LC_nobreak">'.
                   5781:                       $titles->{$type}.'</span></td>'.
                   5782:                       '<td class="LC_left_item">'.
                   5783:                       '<input type="text" name="'.$type.'_namedusers" '.
                   5784:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
                   5785:                       '<td class="LC_right_item">'.
                   5786:                       '<span class="LC_nobreak">'.
                   5787:                       '<input type="text" name="'.$type.'_machines" '.
                   5788:                       'value="'.$machineaccess{$type}.'" size="10" />'.
                   5789:                       '</td></tr>'."\n";
                   5790:     }
                   5791:     $$rowtotal += $rownum;
                   5792:     return $datatable;
                   5793: }
                   5794: 
                   5795: sub serverstatus_pages {
                   5796:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
1.189     raeburn  5797:             'checksums','clusterstatus','metadata_keywords','metadata_harvest',
1.221     raeburn  5798:             'takeoffline','takeonline','showenv','toggledebug','ping','domconf',
1.262     raeburn  5799:             'uniquecodes','diskusage','coursecatalog');
1.69      raeburn  5800: }
                   5801: 
1.236     raeburn  5802: sub defaults_javascript {
                   5803:     my ($settings) = @_;
1.248     raeburn  5804:     return unless (ref($settings) eq 'HASH'); 
1.236     raeburn  5805:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
                   5806:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
                   5807:         if ($maxnum eq '') {
                   5808:             $maxnum = 0;
                   5809:         }
                   5810:         $maxnum ++;
1.249     raeburn  5811:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
1.236     raeburn  5812:         return <<"ENDSCRIPT";
                   5813: <script type="text/javascript">
                   5814: // <![CDATA[
                   5815: function reorderTypes(form,caller) {
                   5816:     var changedVal;
                   5817: $jstext 
                   5818:     var newpos = 'addinststatus_pos';
                   5819:     var current = new Array;
                   5820:     var maxh = $maxnum;
                   5821:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
                   5822:     var oldVal;
                   5823:     if (caller == newpos) {
                   5824:         changedVal = newitemVal;
                   5825:     } else {
                   5826:         var curritem = 'inststatus_pos_'+caller;
                   5827:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
                   5828:         current[newitemVal] = newpos;
                   5829:     }
                   5830:     for (var i=0; i<inststatuses.length; i++) {
                   5831:         if (inststatuses[i] != caller) {
                   5832:             var elementName = 'inststatus_pos_'+inststatuses[i];
                   5833:             if (form.elements[elementName]) {
                   5834:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
                   5835:                 current[currVal] = elementName;
                   5836:             }
                   5837:         }
                   5838:     }
                   5839:     for (var j=0; j<maxh; j++) {
                   5840:         if (current[j] == undefined) {
                   5841:             oldVal = j;
                   5842:         }
                   5843:     }
                   5844:     if (oldVal < changedVal) {
                   5845:         for (var k=oldVal+1; k<=changedVal ; k++) {
                   5846:            var elementName = current[k];
                   5847:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
                   5848:         }
                   5849:     } else {
                   5850:         for (var k=changedVal; k<oldVal; k++) {
                   5851:             var elementName = current[k];
                   5852:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
                   5853:         }
                   5854:     }
                   5855:     return;
                   5856: }
                   5857: 
                   5858: // ]]>
                   5859: </script>
                   5860: 
                   5861: ENDSCRIPT
                   5862:     }
                   5863: }
                   5864: 
1.49      raeburn  5865: sub coursecategories_javascript {
                   5866:     my ($settings) = @_;
1.57      raeburn  5867:     my ($output,$jstext,$cathash);
1.49      raeburn  5868:     if (ref($settings) eq 'HASH') {
1.57      raeburn  5869:         $cathash = $settings->{'cats'};
                   5870:     }
                   5871:     if (ref($cathash) eq 'HASH') {
1.49      raeburn  5872:         my (@cats,@jsarray,%idx);
1.57      raeburn  5873:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
1.49      raeburn  5874:         if (@jsarray > 0) {
                   5875:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
                   5876:             for (my $i=0; $i<@jsarray; $i++) {
                   5877:                 if (ref($jsarray[$i]) eq 'ARRAY') {
                   5878:                     my $catstr = join('","',@{$jsarray[$i]});
                   5879:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
                   5880:                 }
                   5881:             }
                   5882:         }
                   5883:     } else {
                   5884:         $jstext  = '    var categories = Array(1);'."\n".
                   5885:                    '    categories[0] = Array("instcode_pos");'."\n"; 
                   5886:     }
1.237     bisitz   5887:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
                   5888:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
1.265     damieng  5889:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
                   5890:     &js_escape(\$instcode_reserved);
                   5891:     &js_escape(\$communities_reserved);
                   5892:     &js_escape(\$choose_again);
1.49      raeburn  5893:     $output = <<"ENDSCRIPT";
                   5894: <script type="text/javascript">
1.109     raeburn  5895: // <![CDATA[
1.49      raeburn  5896: function reorderCats(form,parent,item,idx) {
                   5897:     var changedVal;
                   5898: $jstext
                   5899:     var newpos = 'addcategory_pos';
                   5900:     if (parent == '') {
                   5901:         var has_instcode = 0;
                   5902:         var maxtop = categories[idx].length;
                   5903:         for (var j=0; j<maxtop; j++) {
                   5904:             if (categories[idx][j] == 'instcode::0') {
                   5905:                 has_instcode == 1;
                   5906:             }
                   5907:         }
                   5908:         if (has_instcode == 0) {
                   5909:             categories[idx][maxtop] = 'instcode_pos';
                   5910:         }
                   5911:     } else {
                   5912:         newpos += '_'+parent;
                   5913:     }
                   5914:     var maxh = 1 + categories[idx].length;
                   5915:     var current = new Array;
                   5916:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
                   5917:     if (item == newpos) {
                   5918:         changedVal = newitemVal;
                   5919:     } else {
                   5920:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
                   5921:         current[newitemVal] = newpos;
                   5922:     }
                   5923:     for (var i=0; i<categories[idx].length; i++) {
                   5924:         var elementName = categories[idx][i];
                   5925:         if (elementName != item) {
                   5926:             if (form.elements[elementName]) {
                   5927:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
                   5928:                 current[currVal] = elementName;
                   5929:             }
                   5930:         }
                   5931:     }
                   5932:     var oldVal;
                   5933:     for (var j=0; j<maxh; j++) {
                   5934:         if (current[j] == undefined) {
                   5935:             oldVal = j;
                   5936:         }
                   5937:     }
                   5938:     if (oldVal < changedVal) {
                   5939:         for (var k=oldVal+1; k<=changedVal ; k++) {
                   5940:            var elementName = current[k];
                   5941:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
                   5942:         }
                   5943:     } else {
                   5944:         for (var k=changedVal; k<oldVal; k++) {
                   5945:             var elementName = current[k];
                   5946:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
                   5947:         }
                   5948:     }
                   5949:     return;
                   5950: }
1.120     raeburn  5951: 
                   5952: function categoryCheck(form) {
                   5953:     if (form.elements['addcategory_name'].value == 'instcode') {
                   5954:         alert('$instcode_reserved\\n$choose_again');
                   5955:         return false;
                   5956:     }
                   5957:     if (form.elements['addcategory_name'].value == 'communities') {
                   5958:         alert('$communities_reserved\\n$choose_again');
                   5959:         return false;
                   5960:     }
                   5961:     return true;
                   5962: }
                   5963: 
1.109     raeburn  5964: // ]]>
1.49      raeburn  5965: </script>
                   5966: 
                   5967: ENDSCRIPT
                   5968:     return $output;
                   5969: }
                   5970: 
1.48      raeburn  5971: sub initialize_categories {
                   5972:     my ($itemcount) = @_;
1.120     raeburn  5973:     my ($datatable,$css_class,$chgstr);
                   5974:     my %default_names = (
                   5975:                       instcode    => 'Official courses (with institutional codes)',
                   5976:                       communities => 'Communities',
                   5977:                         );
                   5978:     my $select0 = ' selected="selected"';
                   5979:     my $select1 = '';
                   5980:     foreach my $default ('instcode','communities') {
                   5981:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
                   5982:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
                   5983:         if ($default eq 'communities') {
                   5984:             $select1 = $select0;
                   5985:             $select0 = '';
                   5986:         }
                   5987:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
                   5988:                      .'<select name="'.$default.'_pos">'
                   5989:                      .'<option value="0"'.$select0.'>1</option>'
                   5990:                      .'<option value="1"'.$select1.'>2</option>'
                   5991:                      .'<option value="2">3</option></select>&nbsp;'
                   5992:                      .$default_names{$default}
                   5993:                      .'</span></td><td><span class="LC_nobreak">'
                   5994:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
                   5995:                      .&mt('Display').'</label>&nbsp;<label>'
                   5996:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
1.48      raeburn  5997:                  .'</label></span></td></tr>';
1.120     raeburn  5998:         $itemcount ++;
                   5999:     }
1.48      raeburn  6000:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
1.49      raeburn  6001:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
1.48      raeburn  6002:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
1.120     raeburn  6003:                   .'<select name="addcategory_pos"'.$chgstr.'>'
                   6004:                   .'<option value="0">1</option>'
                   6005:                   .'<option value="1">2</option>'
                   6006:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
1.48      raeburn  6007:                   .&mt('Add category').'</td><td>'.&mt('Name:')
                   6008:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
                   6009:     return $datatable;
                   6010: }
                   6011: 
                   6012: sub build_category_rows {
1.49      raeburn  6013:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
                   6014:     my ($text,$name,$item,$chgstr);
1.48      raeburn  6015:     if (ref($cats) eq 'ARRAY') {
                   6016:         my $maxdepth = scalar(@{$cats});
                   6017:         if (ref($cats->[$depth]) eq 'HASH') {
                   6018:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
                   6019:                 my $numchildren = @{$cats->[$depth]{$parent}};
                   6020:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
1.204     raeburn  6021:                 $text .= '<td><table class="LC_data_table">';
1.49      raeburn  6022:                 my ($idxnum,$parent_name,$parent_item);
                   6023:                 my $higher = $depth - 1;
                   6024:                 if ($higher == 0) {
                   6025:                     $parent_name = &escape($parent).'::'.$higher;
                   6026:                 } else {
                   6027:                     if (ref($path) eq 'ARRAY') {
                   6028:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
                   6029:                     }
                   6030:                 }
                   6031:                 $parent_item = 'addcategory_pos_'.$parent_name;
1.48      raeburn  6032:                 for (my $j=0; $j<=$numchildren; $j++) {
1.49      raeburn  6033:                     if ($j < $numchildren) {
1.48      raeburn  6034:                         $name = $cats->[$depth]{$parent}[$j];
                   6035:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
1.49      raeburn  6036:                         $idxnum = $idx->{$item};
                   6037:                     } else {
                   6038:                         $name = $parent_name;
                   6039:                         $item = $parent_item;
1.48      raeburn  6040:                     }
1.49      raeburn  6041:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
                   6042:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
1.48      raeburn  6043:                     for (my $i=0; $i<=$numchildren; $i++) {
                   6044:                         my $vpos = $i+1;
                   6045:                         my $selstr;
                   6046:                         if ($j == $i) {
                   6047:                             $selstr = ' selected="selected" ';
                   6048:                         }
                   6049:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
                   6050:                     }
                   6051:                     $text .= '</select>&nbsp;';
                   6052:                     if ($j < $numchildren) {
                   6053:                         my $deeper = $depth+1;
                   6054:                         $text .= $name.'&nbsp;'
                   6055:                                  .'<label><input type="checkbox" name="deletecategory" value="'
                   6056:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
                   6057:                         if(ref($path) eq 'ARRAY') {
                   6058:                             push(@{$path},$name);
1.49      raeburn  6059:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
1.48      raeburn  6060:                             pop(@{$path});
                   6061:                         }
                   6062:                     } else {
1.59      bisitz   6063:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
1.48      raeburn  6064:                         if ($j == $numchildren) {
                   6065:                             $text .= $name;
                   6066:                         } else {
                   6067:                             $text .= $item;
                   6068:                         }
                   6069:                         $text .= '" value="" />';
                   6070:                     }
                   6071:                     $text .= '</td></tr>';
                   6072:                 }
                   6073:                 $text .= '</table></td>';
                   6074:             } else {
                   6075:                 my $higher = $depth-1;
                   6076:                 if ($higher == 0) {
                   6077:                     $name = &escape($parent).'::'.$higher;
                   6078:                 } else {
                   6079:                     if (ref($path) eq 'ARRAY') {
                   6080:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
                   6081:                     }
                   6082:                 }
                   6083:                 my $colspan;
                   6084:                 if ($parent ne 'instcode') {
                   6085:                     $colspan = $maxdepth - $depth - 1;
                   6086:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
                   6087:                 }
                   6088:             }
                   6089:         }
                   6090:     }
                   6091:     return $text;
                   6092: }
                   6093: 
1.33      raeburn  6094: sub modifiable_userdata_row {
1.228     raeburn  6095:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
                   6096:     my ($role,$rolename,$statustype);
                   6097:     $role = $item;
1.224     raeburn  6098:     if ($context eq 'cancreate') {
1.228     raeburn  6099:         if ($item =~ /^emailusername_(.+)$/) {
                   6100:             $statustype = $1;
                   6101:             $role = 'emailusername';
                   6102:             if (ref($usertypes) eq 'HASH') {
                   6103:                 if ($usertypes->{$statustype}) {
                   6104:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
                   6105:                 } else {
                   6106:                     $rolename = &mt('Data provided by user');
                   6107:                 }
                   6108:             }
1.224     raeburn  6109:         }
                   6110:     } elsif ($context eq 'selfcreate') {
1.63      raeburn  6111:         if (ref($usertypes) eq 'HASH') {
                   6112:             $rolename = $usertypes->{$role};
                   6113:         } else {
                   6114:             $rolename = $role;
                   6115:         }
1.33      raeburn  6116:     } else {
1.63      raeburn  6117:         if ($role eq 'cr') {
                   6118:             $rolename = &mt('Custom role');
                   6119:         } else {
                   6120:             $rolename = &Apache::lonnet::plaintext($role);
                   6121:         }
1.33      raeburn  6122:     }
1.224     raeburn  6123:     my (@fields,%fieldtitles);
                   6124:     if (ref($fieldsref) eq 'ARRAY') {
                   6125:         @fields = @{$fieldsref};
                   6126:     } else {
                   6127:         @fields = ('lastname','firstname','middlename','generation',
                   6128:                    'permanentemail','id');
                   6129:     }
                   6130:     if ((ref($titlesref) eq 'HASH')) {
                   6131:         %fieldtitles = %{$titlesref};
                   6132:     } else {
                   6133:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
                   6134:     }
1.33      raeburn  6135:     my $output;
                   6136:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
                   6137:     $output = '<tr '.$css_class.'>'.
                   6138:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
                   6139:               '<td class="LC_left_item" colspan="2"><table>';
                   6140:     my $rem;
                   6141:     my %checks;
                   6142:     if (ref($settings) eq 'HASH') {
                   6143:         if (ref($settings->{$context}) eq 'HASH') {
                   6144:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
1.228     raeburn  6145:                 my $hashref = $settings->{$context}->{$role};
                   6146:                 if ($role eq 'emailusername') {
                   6147:                     if ($statustype) {
                   6148:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
                   6149:                             $hashref = $settings->{$context}->{$role}->{$statustype};
                   6150:                             if (ref($hashref) eq 'HASH') { 
                   6151:                                 foreach my $field (@fields) {
                   6152:                                     if ($hashref->{$field}) {
                   6153:                                         $checks{$field} = $hashref->{$field};
                   6154:                                     }
                   6155:                                 }
                   6156:                             }
                   6157:                         }
                   6158:                     }
                   6159:                 } else {
                   6160:                     if (ref($hashref) eq 'HASH') {
                   6161:                         foreach my $field (@fields) {
                   6162:                             if ($hashref->{$field}) {
                   6163:                                 $checks{$field} = ' checked="checked" ';
                   6164:                             }
                   6165:                         }
1.33      raeburn  6166:                     }
                   6167:                 }
                   6168:             }
                   6169:         }
                   6170:     }
1.228     raeburn  6171:      
1.33      raeburn  6172:     for (my $i=0; $i<@fields; $i++) {
                   6173:         my $rem = $i%($numinrow);
                   6174:         if ($rem == 0) {
                   6175:             if ($i > 0) {
                   6176:                 $output .= '</tr>';
                   6177:             }
                   6178:             $output .= '<tr>';
                   6179:         }
                   6180:         my $check = ' ';
1.228     raeburn  6181:         unless ($role eq 'emailusername') {
                   6182:             if (exists($checks{$fields[$i]})) {
                   6183:                 $check = $checks{$fields[$i]}
                   6184:             } else {
                   6185:                 if ($role eq 'st') {
                   6186:                     if (ref($settings) ne 'HASH') {
                   6187:                         $check = ' checked="checked" '; 
                   6188:                     }
1.33      raeburn  6189:                 }
                   6190:             }
                   6191:         }
                   6192:         $output .= '<td class="LC_left_item">'.
1.228     raeburn  6193:                    '<span class="LC_nobreak">';
                   6194:         if ($role eq 'emailusername') {
                   6195:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
                   6196:                 $checks{$fields[$i]} = 'omit';
                   6197:             }
                   6198:             foreach my $option ('required','optional','omit') {
                   6199:                 my $checked='';
                   6200:                 if ($checks{$fields[$i]} eq $option) {
                   6201:                     $checked='checked="checked" ';
                   6202:                 }
                   6203:                 $output .= '<label>'.
                   6204:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
                   6205:                            &mt($option).'</label>'.('&nbsp;' x2);
                   6206:             }
                   6207:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
                   6208:         } else {
                   6209:             $output .= '<label>'.
                   6210:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
                   6211:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
                   6212:                        '</label>';
                   6213:         }
                   6214:         $output .= '</span></td>';
1.33      raeburn  6215:         $rem = @fields%($numinrow);
                   6216:     }
                   6217:     my $colsleft = $numinrow - $rem;
                   6218:     if ($colsleft > 1 ) {
                   6219:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
                   6220:                    '&nbsp;</td>';
                   6221:     } elsif ($colsleft == 1) {
                   6222:         $output .= '<td class="LC_left_item">&nbsp;</td>';
                   6223:     }
                   6224:     $output .= '</tr></table></td></tr>';
                   6225:     return $output;
                   6226: }
1.28      raeburn  6227: 
1.93      raeburn  6228: sub insttypes_row {
1.224     raeburn  6229:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rownum) = @_;
1.93      raeburn  6230:     my %lt = &Apache::lonlocal::texthash (
                   6231:                       cansearch => 'Users allowed to search',
                   6232:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
1.131     raeburn  6233:                       lockablenames => 'User preference to lock name',
1.93      raeburn  6234:              );
                   6235:     my $showdom;
                   6236:     if ($context eq 'cansearch') {
                   6237:         $showdom = ' ('.$dom.')';
                   6238:     }
1.165     raeburn  6239:     my $class = 'LC_left_item';
                   6240:     if ($context eq 'statustocreate') {
                   6241:         $class = 'LC_right_item';
                   6242:     }
1.224     raeburn  6243:     my $css_class = ' class="LC_odd_row"';
                   6244:     if ($rownum ne '') { 
                   6245:         $css_class = ($rownum%2? ' class="LC_odd_row"':'');
                   6246:     }
                   6247:     my $output = '<tr'.$css_class.'>'.
                   6248:                  '<td>'.$lt{$context}.$showdom.
                   6249:                  '</td><td class="'.$class.'" colspan="2"><table>';
1.26      raeburn  6250:     my $rem;
                   6251:     if (ref($types) eq 'ARRAY') {
                   6252:         for (my $i=0; $i<@{$types}; $i++) {
                   6253:             if (defined($usertypes->{$types->[$i]})) {
                   6254:                 my $rem = $i%($numinrow);
                   6255:                 if ($rem == 0) {
                   6256:                     if ($i > 0) {
                   6257:                         $output .= '</tr>';
                   6258:                     }
                   6259:                     $output .= '<tr>';
1.23      raeburn  6260:                 }
1.26      raeburn  6261:                 my $check = ' ';
1.99      raeburn  6262:                 if (ref($settings) eq 'HASH') {
                   6263:                     if (ref($settings->{$context}) eq 'ARRAY') {
                   6264:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
                   6265:                             $check = ' checked="checked" ';
                   6266:                         }
                   6267:                     } elsif ($context eq 'statustocreate') {
1.26      raeburn  6268:                         $check = ' checked="checked" ';
                   6269:                     }
1.23      raeburn  6270:                 }
1.26      raeburn  6271:                 $output .= '<td class="LC_left_item">'.
                   6272:                            '<span class="LC_nobreak"><label>'.
1.93      raeburn  6273:                            '<input type="checkbox" name="'.$context.'" '.
1.26      raeburn  6274:                            'value="'.$types->[$i].'"'.$check.'/>'.
                   6275:                            $usertypes->{$types->[$i]}.'</label></span></td>';
1.23      raeburn  6276:             }
                   6277:         }
1.26      raeburn  6278:         $rem = @{$types}%($numinrow);
1.23      raeburn  6279:     }
                   6280:     my $colsleft = $numinrow - $rem;
1.131     raeburn  6281:     if (($rem == 0) && (@{$types} > 0)) {
                   6282:         $output .= '<tr>';
                   6283:     }
1.23      raeburn  6284:     if ($colsleft > 1) {
1.25      raeburn  6285:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
1.23      raeburn  6286:     } else {
1.25      raeburn  6287:         $output .= '<td class="LC_left_item">';
1.23      raeburn  6288:     }
                   6289:     my $defcheck = ' ';
1.99      raeburn  6290:     if (ref($settings) eq 'HASH') {  
                   6291:         if (ref($settings->{$context}) eq 'ARRAY') {
                   6292:             if (grep(/^default$/,@{$settings->{$context}})) {
                   6293:                 $defcheck = ' checked="checked" ';
                   6294:             }
                   6295:         } elsif ($context eq 'statustocreate') {
1.26      raeburn  6296:             $defcheck = ' checked="checked" ';
                   6297:         }
1.23      raeburn  6298:     }
1.25      raeburn  6299:     $output .= '<span class="LC_nobreak"><label>'.
1.93      raeburn  6300:                '<input type="checkbox" name="'.$context.'" '.
1.25      raeburn  6301:                'value="default"'.$defcheck.'/>'.
                   6302:                $othertitle.'</label></span></td>'.
                   6303:                '</tr></table></td></tr>';
                   6304:     return $output;
1.23      raeburn  6305: }
                   6306: 
                   6307: sub sorted_searchtitles {
                   6308:     my %searchtitles = &Apache::lonlocal::texthash(
                   6309:                          'uname' => 'username',
                   6310:                          'lastname' => 'last name',
                   6311:                          'lastfirst' => 'last name, first name',
                   6312:                      );
                   6313:     my @titleorder = ('uname','lastname','lastfirst');
                   6314:     return (\%searchtitles,\@titleorder);
                   6315: }
                   6316: 
1.25      raeburn  6317: sub sorted_searchtypes {
                   6318:     my %srchtypes_desc = (
                   6319:                            exact    => 'is exact match',
                   6320:                            contains => 'contains ..',
                   6321:                            begins   => 'begins with ..',
                   6322:                          );
                   6323:     my @srchtypeorder = ('exact','begins','contains');
                   6324:     return (\%srchtypes_desc,\@srchtypeorder);
                   6325: }
                   6326: 
1.3       raeburn  6327: sub usertype_update_row {
                   6328:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
                   6329:     my $datatable;
                   6330:     my $numinrow = 4;
                   6331:     foreach my $type (@{$types}) {
                   6332:         if (defined($usertypes->{$type})) {
                   6333:             $$rownums ++;
                   6334:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
                   6335:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
                   6336:                           '</td><td class="LC_left_item"><table>';
                   6337:             for (my $i=0; $i<@{$fields}; $i++) {
                   6338:                 my $rem = $i%($numinrow);
                   6339:                 if ($rem == 0) {
                   6340:                     if ($i > 0) {
                   6341:                         $datatable .= '</tr>';
                   6342:                     }
                   6343:                     $datatable .= '<tr>';
                   6344:                 }
                   6345:                 my $check = ' ';
1.39      raeburn  6346:                 if (ref($settings) eq 'HASH') {
                   6347:                     if (ref($settings->{'fields'}) eq 'HASH') {
                   6348:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
                   6349:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
                   6350:                                 $check = ' checked="checked" ';
                   6351:                             }
1.3       raeburn  6352:                         }
                   6353:                     }
                   6354:                 }
                   6355: 
                   6356:                 if ($i == @{$fields}-1) {
                   6357:                     my $colsleft = $numinrow - $rem;
                   6358:                     if ($colsleft > 1) {
                   6359:                         $datatable .= '<td colspan="'.$colsleft.'">';
                   6360:                     } else {
                   6361:                         $datatable .= '<td>';
                   6362:                     }
                   6363:                 } else {
                   6364:                     $datatable .= '<td>';
                   6365:                 }
1.8       raeburn  6366:                 $datatable .= '<span class="LC_nobreak"><label>'.
                   6367:                               '<input type="checkbox" name="updateable_'.$type.
                   6368:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
                   6369:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
1.3       raeburn  6370:             }
                   6371:             $datatable .= '</tr></table></td></tr>';
                   6372:         }
                   6373:     }
                   6374:     return $datatable;
1.1       raeburn  6375: }
                   6376: 
                   6377: sub modify_login {
1.205     raeburn  6378:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
1.168     raeburn  6379:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
                   6380:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
                   6381:     %title = ( coursecatalog => 'Display course catalog',
                   6382:                adminmail => 'Display administrator E-mail address',
1.188     raeburn  6383:                helpdesk  => 'Display "Contact Helpdesk" link',
1.168     raeburn  6384:                newuser => 'Link for visitors to create a user account',
                   6385:                loginheader => 'Log-in box header');
                   6386:     @offon = ('off','on');
1.112     raeburn  6387:     if (ref($domconfig{login}) eq 'HASH') {
                   6388:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
                   6389:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
                   6390:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
                   6391:             }
                   6392:         }
                   6393:     }
1.9       raeburn  6394:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
                   6395:                                            \%domconfig,\%loginhash);
1.188     raeburn  6396:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
1.42      raeburn  6397:     foreach my $item (@toggles) {
                   6398:         $loginhash{login}{$item} = $env{'form.'.$item};
                   6399:     }
1.41      raeburn  6400:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
1.6       raeburn  6401:     if (ref($colchanges{'login'}) eq 'HASH') {  
                   6402:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
                   6403:                                          \%loginhash);
                   6404:     }
1.110     raeburn  6405: 
1.149     raeburn  6406:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
1.256     raeburn  6407:     my %domservers = &Apache::lonnet::get_servers($dom);
1.128     raeburn  6408:     my @loginvia_attribs = ('serverpath','custompath','exempt');
1.110     raeburn  6409:     if (keys(%servers) > 1) {
                   6410:         foreach my $lonhost (keys(%servers)) {
1.128     raeburn  6411:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
                   6412:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
                   6413:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
                   6414:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
                   6415:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
                   6416:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
                   6417:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
                   6418:                         $changes{'loginvia'}{$lonhost} = 1;
                   6419:                     } else {
                   6420:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
                   6421:                         $changes{'loginvia'}{$lonhost} = 1;
                   6422:                     }
                   6423:                 } else {
                   6424:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
                   6425:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
                   6426:                         $changes{'loginvia'}{$lonhost} = 1;
                   6427:                     }
                   6428:                 }
                   6429:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
                   6430:                     foreach my $item (@loginvia_attribs) {
                   6431:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
                   6432:                     }
                   6433:                 } else {
                   6434:                     foreach my $item (@loginvia_attribs) {
                   6435:                         my $new = $env{'form.'.$lonhost.'_'.$item};
                   6436:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
                   6437:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
                   6438:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
                   6439:                                 $new = '/';
                   6440:                             }
                   6441:                         }
                   6442:                         if (($item eq 'custompath') && 
                   6443:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
                   6444:                             $new = '';
                   6445:                         }
                   6446:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
                   6447:                             $changes{'loginvia'}{$lonhost} = 1;
                   6448:                         }
                   6449:                         if ($item eq 'exempt') {
1.256     raeburn  6450:                             $new = &check_exempt_addresses($new);
1.128     raeburn  6451:                         }
                   6452:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
                   6453:                     }
                   6454:                 }
1.112     raeburn  6455:             } else {
1.128     raeburn  6456:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
                   6457:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
1.112     raeburn  6458:                     $changes{'loginvia'}{$lonhost} = 1;
1.128     raeburn  6459:                     foreach my $item (@loginvia_attribs) {
                   6460:                         my $new = $env{'form.'.$lonhost.'_'.$item};
                   6461:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
                   6462:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
                   6463:                                 $new = '/';
                   6464:                             }
                   6465:                         }
                   6466:                         if (($item eq 'custompath') && 
                   6467:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
                   6468:                             $new = '';
                   6469:                         }
                   6470:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
                   6471:                     }
1.110     raeburn  6472:                 }
                   6473:             }
                   6474:         }
                   6475:     }
1.119     raeburn  6476: 
1.168     raeburn  6477:     my $servadm = $r->dir_config('lonAdmEMail');
                   6478:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
                   6479:     if (ref($domconfig{'login'}) eq 'HASH') {
                   6480:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
                   6481:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
                   6482:                 if ($lang eq 'nolang') {
                   6483:                     push(@currlangs,$lang);
                   6484:                 } elsif (defined($langchoices{$lang})) {
                   6485:                     push(@currlangs,$lang);
                   6486:                 } else {
                   6487:                     next;
                   6488:                 }
                   6489:             }
                   6490:         }
                   6491:     }
                   6492:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
                   6493:     if (@currlangs > 0) {
                   6494:         foreach my $lang (@currlangs) {
                   6495:             if (grep(/^\Q$lang\E$/,@delurls)) {
                   6496:                 $changes{'helpurl'}{$lang} = 1;
                   6497:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
                   6498:                 $changes{'helpurl'}{$lang} = 1;
                   6499:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
                   6500:                 push(@newlangs,$lang);
                   6501:             } else {
                   6502:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
                   6503:             }
                   6504:         }
                   6505:     }
                   6506:     unless (grep(/^nolang$/,@currlangs)) {
                   6507:         if ($env{'form.loginhelpurl_nolang.filename'}) {
                   6508:             $changes{'helpurl'}{'nolang'} = 1;
                   6509:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
                   6510:             push(@newlangs,'nolang');
                   6511:         }
                   6512:     }
                   6513:     if ($env{'form.loginhelpurl_add_lang'}) {
                   6514:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
                   6515:             ($env{'form.loginhelpurl_add_file.filename'})) {
                   6516:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
                   6517:             $addedfile = $env{'form.loginhelpurl_add_lang'};
                   6518:         }
                   6519:     }
                   6520:     if ((@newlangs > 0) || ($addedfile)) {
                   6521:         my $error;
                   6522:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
                   6523:         if ($configuserok eq 'ok') {
                   6524:             if ($switchserver) {
                   6525:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
                   6526:             } elsif ($author_ok eq 'ok') {
                   6527:                 my @allnew = @newlangs;
                   6528:                 if ($addedfile ne '') {
                   6529:                     push(@allnew,$addedfile);
                   6530:                 }
                   6531:                 foreach my $lang (@allnew) {
                   6532:                     my $formelem = 'loginhelpurl_'.$lang;
                   6533:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
                   6534:                         $formelem = 'loginhelpurl_add_file';
                   6535:                     }
                   6536:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
                   6537:                                                                "help/$lang",'','',$newfile{$lang});
                   6538:                     if ($result eq 'ok') {
                   6539:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
                   6540:                         $changes{'helpurl'}{$lang} = 1;
                   6541:                     } else {
                   6542:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
                   6543:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
1.210     raeburn  6544:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
1.168     raeburn  6545:                             (!grep(/^\Q$lang\E$/,@delurls))) {
                   6546:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
                   6547:                         }
                   6548:                     }
                   6549:                 }
                   6550:             } else {
                   6551:                 $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);
                   6552:             }
                   6553:         } else {
                   6554:             $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);
                   6555:         }
                   6556:         if ($error) {
                   6557:             &Apache::lonnet::logthis($error);
                   6558:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
                   6559:         }
                   6560:     }
1.256     raeburn  6561: 
                   6562:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
                   6563:     if (ref($domconfig{'login'}) eq 'HASH') {
                   6564:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
                   6565:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
                   6566:                 if ($domservers{$lonhost}) {
                   6567:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
                   6568:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
                   6569:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtagexempt'}{$lonhost}{'exempt'}
                   6570:                     }
                   6571:                 }
                   6572:             }
                   6573:         }
                   6574:     }
                   6575:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
                   6576:     foreach my $lonhost (sort(keys(%domservers))) {
                   6577:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
                   6578:             $changes{'headtag'}{$lonhost} = 1;
                   6579:         } else {
                   6580:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
                   6581:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
                   6582:             }
                   6583:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
                   6584:                 push(@newhosts,$lonhost);
                   6585:             } elsif ($currheadtagurls{$lonhost}) {
                   6586:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
                   6587:                 if ($currexempt{$lonhost}) {
                   6588:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) { 
                   6589:                         $changes{'headtag'}{$lonhost} = 1;
                   6590:                     }
                   6591:                 } elsif ($possexempt{$lonhost}) {
                   6592:                     $changes{'headtag'}{$lonhost} = 1;
                   6593:                 }
                   6594:                 if ($possexempt{$lonhost}) {
                   6595:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
                   6596:                 }
                   6597:             }
                   6598:         }
                   6599:     }
                   6600:     if (@newhosts) {
                   6601:         my $error;
                   6602:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
                   6603:         if ($configuserok eq 'ok') {
                   6604:             if ($switchserver) {
                   6605:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
                   6606:             } elsif ($author_ok eq 'ok') {
                   6607:                 foreach my $lonhost (@newhosts) {
                   6608:                     my $formelem = 'loginheadtag_'.$lonhost;
                   6609:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
                   6610:                                                                           "login/headtag/$lonhost",'','',
                   6611:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
                   6612:                     if ($result eq 'ok') {
                   6613:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
                   6614:                         $changes{'headtag'}{$lonhost} = 1;
                   6615:                         if ($possexempt{$lonhost}) {
                   6616:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
                   6617:                         }
                   6618:                     } else {
                   6619:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
                   6620:                                            $newheadtagurls{$lonhost},$result);
                   6621:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
                   6622:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
                   6623:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
                   6624:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
                   6625:                         }
                   6626:                     }
                   6627:                 }
                   6628:             } else {
                   6629:                 $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);
                   6630:             }
                   6631:         } else {
                   6632:             $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);
                   6633:         }
                   6634:         if ($error) {
                   6635:             &Apache::lonnet::logthis($error);
                   6636:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
                   6637:         }
                   6638:     }
1.169     raeburn  6639:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
1.168     raeburn  6640: 
                   6641:     my $defaulthelpfile = '/adm/loginproblems.html';
                   6642:     my $defaulttext = &mt('Default in use');
                   6643: 
1.1       raeburn  6644:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
                   6645:                                              $dom);
                   6646:     if ($putresult eq 'ok') {
1.188     raeburn  6647:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
1.42      raeburn  6648:         my %defaultchecked = (
                   6649:                     'coursecatalog' => 'on',
1.188     raeburn  6650:                     'helpdesk'      => 'on',
1.42      raeburn  6651:                     'adminmail'     => 'off',
1.43      raeburn  6652:                     'newuser'       => 'off',
1.42      raeburn  6653:         );
1.55      raeburn  6654:         if (ref($domconfig{'login'}) eq 'HASH') {
                   6655:             foreach my $item (@toggles) {
                   6656:                 if ($defaultchecked{$item} eq 'on') { 
                   6657:                     if (($domconfig{'login'}{$item} eq '0') &&
                   6658:                         ($env{'form.'.$item} eq '1')) {
                   6659:                         $changes{$item} = 1;
                   6660:                     } elsif (($domconfig{'login'}{$item} eq '' ||
                   6661:                               $domconfig{'login'}{$item} eq '1') &&
                   6662:                              ($env{'form.'.$item} eq '0')) {
                   6663:                         $changes{$item} = 1;
                   6664:                     }
                   6665:                 } elsif ($defaultchecked{$item} eq 'off') {
                   6666:                     if (($domconfig{'login'}{$item} eq '1') &&
                   6667:                         ($env{'form.'.$item} eq '0')) {
                   6668:                         $changes{$item} = 1;
                   6669:                     } elsif (($domconfig{'login'}{$item} eq '' ||
                   6670:                               $domconfig{'login'}{$item} eq '0') &&
                   6671:                              ($env{'form.'.$item} eq '1')) {
                   6672:                         $changes{$item} = 1;
                   6673:                     }
1.42      raeburn  6674:                 }
                   6675:             }
1.41      raeburn  6676:         }
1.6       raeburn  6677:         if (keys(%changes) > 0 || $colchgtext) {
1.41      raeburn  6678:             &Apache::loncommon::devalidate_domconfig_cache($dom);
1.212     raeburn  6679:             if (ref($lastactref) eq 'HASH') {
                   6680:                 $lastactref->{'domainconfig'} = 1;
                   6681:             }
1.1       raeburn  6682:             $resulttext = &mt('Changes made:').'<ul>';
                   6683:             foreach my $item (sort(keys(%changes))) {
1.135     bisitz   6684:                 if ($item eq 'loginvia') {
1.112     raeburn  6685:                     if (ref($changes{$item}) eq 'HASH') {
                   6686:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
                   6687:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
1.128     raeburn  6688:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
                   6689:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
                   6690:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
                   6691:                                     $protocol = 'http' if ($protocol ne 'https');
                   6692:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
                   6693: 
                   6694:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
                   6695:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
                   6696:                                     } else {
                   6697:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
                   6698:                                     }
                   6699:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
                   6700:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
                   6701:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
                   6702:                                     }
                   6703:                                     $resulttext .= '</li>';
                   6704:                                 } else {
                   6705:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
                   6706:                                 }
1.112     raeburn  6707:                             } else {
1.128     raeburn  6708:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
1.112     raeburn  6709:                             }
                   6710:                         }
1.128     raeburn  6711:                         $resulttext .= '</ul></li>';
1.112     raeburn  6712:                     }
1.168     raeburn  6713:                 } elsif ($item eq 'helpurl') {
                   6714:                     if (ref($changes{$item}) eq 'HASH') {
                   6715:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
                   6716:                             if (grep(/^\Q$lang\E$/,@delurls)) {
                   6717:                                 my ($chg,$link);
                   6718:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
                   6719:                                 if ($lang eq 'nolang') {
                   6720:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
                   6721:                                 } else {
                   6722:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
                   6723:                                 }
                   6724:                                 $resulttext .= '<li>'.$chg.'</li>';
                   6725:                             } else {
                   6726:                                 my $chg;
                   6727:                                 if ($lang eq 'nolang') {
                   6728:                                     $chg = &mt('custom log-in help file for no preferred language');
                   6729:                                 } else {
                   6730:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
                   6731:                                 }
                   6732:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
                   6733:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
                   6734:                                                       '?inhibitmenu=yes',$chg,600,500).
                   6735:                                                '</li>';
                   6736:                             }
                   6737:                         }
                   6738:                     }
1.256     raeburn  6739:                 } elsif ($item eq 'headtag') {
                   6740:                     if (ref($changes{$item}) eq 'HASH') {
                   6741:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
                   6742:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
                   6743:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
                   6744:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
                   6745:                                 $resulttext .= '<li><a href="'.
                   6746:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
                   6747:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
                   6748:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
                   6749:                                 if ($possexempt{$lonhost}) {
                   6750:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
                   6751:                                 } else {
                   6752:                                     $resulttext .= &mt('included for any client IP');
                   6753:                                 }
                   6754:                                 $resulttext .= '</li>';
                   6755:                             }
                   6756:                         }
                   6757:                     }
1.169     raeburn  6758:                 } elsif ($item eq 'captcha') {
                   6759:                     if (ref($loginhash{'login'}) eq 'HASH') {
1.210     raeburn  6760:                         my $chgtxt;
1.169     raeburn  6761:                         if ($loginhash{'login'}{$item} eq 'notused') {
                   6762:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
                   6763:                         } else {
                   6764:                             my %captchas = &captcha_phrases();
                   6765:                             if ($captchas{$loginhash{'login'}{$item}}) {
                   6766:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
                   6767:                             } else {
                   6768:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
                   6769:                             }
                   6770:                         }
                   6771:                         $resulttext .= '<li>'.$chgtxt.'</li>';
                   6772:                     }
                   6773:                 } elsif ($item eq 'recaptchakeys') {
                   6774:                     if (ref($loginhash{'login'}) eq 'HASH') {
                   6775:                         my ($privkey,$pubkey);
                   6776:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
                   6777:                             $pubkey = $loginhash{'login'}{$item}{'public'};
                   6778:                             $privkey = $loginhash{'login'}{$item}{'private'};
                   6779:                         }
                   6780:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
                   6781:                         if (!$pubkey) {
                   6782:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
                   6783:                         } else {
                   6784:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
                   6785:                         }
                   6786:                         if (!$privkey) {
                   6787:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
                   6788:                         } else {
1.251     raeburn  6789:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
1.169     raeburn  6790:                         }
                   6791:                         $chgtxt .= '</ul>';
                   6792:                         $resulttext .= '<li>'.$chgtxt.'</li>';
                   6793:                     }
1.41      raeburn  6794:                 } else {
                   6795:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
                   6796:                 }
1.1       raeburn  6797:             }
1.6       raeburn  6798:             $resulttext .= $colchgtext.'</ul>';
1.1       raeburn  6799:         } else {
                   6800:             $resulttext = &mt('No changes made to log-in page settings');
                   6801:         }
                   6802:     } else {
1.11      albertel 6803:         $resulttext = '<span class="LC_error">'.
                   6804: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
1.1       raeburn  6805:     }
1.6       raeburn  6806:     if ($errors) {
1.9       raeburn  6807:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
1.6       raeburn  6808:                        $errors.'</ul>';
                   6809:     }
                   6810:     return $resulttext;
                   6811: }
                   6812: 
1.256     raeburn  6813: 
                   6814: sub check_exempt_addresses {
                   6815:     my ($iplist) = @_;
                   6816:     $iplist =~ s/^\s+//;
                   6817:     $iplist =~ s/\s+$//;
                   6818:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
                   6819:     my (@okips,$new);
                   6820:     foreach my $ip (@poss_ips) {
                   6821:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
                   6822:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
                   6823:                 push(@okips,$ip);
                   6824:             }
                   6825:         }
                   6826:     }
                   6827:     if (@okips > 0) {
                   6828:         $new = join(',',@okips);
                   6829:     } else {
                   6830:         $new = '';
                   6831:     }
                   6832:     return $new;
                   6833: }
                   6834: 
1.6       raeburn  6835: sub color_font_choices {
                   6836:     my %choices =
                   6837:         &Apache::lonlocal::texthash (
                   6838:             img => "Header",
                   6839:             bgs => "Background colors",
                   6840:             links => "Link colors",
1.55      raeburn  6841:             images => "Images",
1.6       raeburn  6842:             font => "Font color",
1.201     raeburn  6843:             fontmenu => "Font menu",
1.76      raeburn  6844:             pgbg => "Page",
1.6       raeburn  6845:             tabbg => "Header",
                   6846:             sidebg => "Border",
                   6847:             link => "Link",
                   6848:             alink => "Active link",
                   6849:             vlink => "Visited link",
                   6850:         );
                   6851:     return %choices;
                   6852: }
                   6853: 
                   6854: sub modify_rolecolors {
1.205     raeburn  6855:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
1.6       raeburn  6856:     my ($resulttext,%rolehash);
                   6857:     $rolehash{'rolecolors'} = {};
1.55      raeburn  6858:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
                   6859:         if ($domconfig{'rolecolors'} eq '') {
                   6860:             $domconfig{'rolecolors'} = {};
                   6861:         }
                   6862:     }
1.9       raeburn  6863:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
1.6       raeburn  6864:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
                   6865:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
                   6866:                                              $dom);
                   6867:     if ($putresult eq 'ok') {
                   6868:         if (keys(%changes) > 0) {
1.41      raeburn  6869:             &Apache::loncommon::devalidate_domconfig_cache($dom);
1.212     raeburn  6870:             if (ref($lastactref) eq 'HASH') {
                   6871:                 $lastactref->{'domainconfig'} = 1;
                   6872:             }
1.6       raeburn  6873:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
                   6874:                                              $rolehash{'rolecolors'});
                   6875:         } else {
                   6876:             $resulttext = &mt('No changes made to default color schemes');
                   6877:         }
                   6878:     } else {
1.11      albertel 6879:         $resulttext = '<span class="LC_error">'.
                   6880: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
1.6       raeburn  6881:     }
                   6882:     if ($errors) {
                   6883:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
                   6884:                        $errors.'</ul>';
                   6885:     }
                   6886:     return $resulttext;
                   6887: }
                   6888: 
                   6889: sub modify_colors {
1.9       raeburn  6890:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
1.12      raeburn  6891:     my (%changes,%choices);
1.51      raeburn  6892:     my @bgs;
1.6       raeburn  6893:     my @links = ('link','alink','vlink');
1.41      raeburn  6894:     my @logintext;
1.6       raeburn  6895:     my @images;
                   6896:     my $servadm = $r->dir_config('lonAdmEMail');
                   6897:     my $errors;
1.200     raeburn  6898:     my %defaults;
1.6       raeburn  6899:     foreach my $role (@{$roles}) {
                   6900:         if ($role eq 'login') {
1.12      raeburn  6901:             %choices = &login_choices();
1.41      raeburn  6902:             @logintext = ('textcol','bgcol');
1.12      raeburn  6903:         } else {
                   6904:             %choices = &color_font_choices();
                   6905:         }
                   6906:         if ($role eq 'login') {
1.41      raeburn  6907:             @images = ('img','logo','domlogo','login');
1.51      raeburn  6908:             @bgs = ('pgbg','mainbg','sidebg');
1.6       raeburn  6909:         } else {
                   6910:             @images = ('img');
1.200     raeburn  6911:             @bgs = ('pgbg','tabbg','sidebg');
                   6912:         }
                   6913:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
                   6914:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
                   6915:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
                   6916:         }
                   6917:         if ($role eq 'login') {
                   6918:             foreach my $item (@logintext) {
1.234     raeburn  6919:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
                   6920:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
                   6921:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
                   6922:                 }
                   6923:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
1.200     raeburn  6924:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
                   6925:                 }
                   6926:             }
                   6927:         } else {
1.234     raeburn  6928:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
                   6929:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
                   6930:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
                   6931:             }
                   6932:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
1.200     raeburn  6933:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
                   6934:             }
1.6       raeburn  6935:         }
1.200     raeburn  6936:         foreach my $item (@bgs) {
1.234     raeburn  6937:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
                   6938:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
                   6939:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
                   6940:             }
                   6941:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
1.200     raeburn  6942:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
                   6943:             }
                   6944:         }
                   6945:         foreach my $item (@links) {
1.234     raeburn  6946:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
                   6947:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
                   6948:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
                   6949:             }
                   6950:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
1.200     raeburn  6951:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
                   6952:             }
1.6       raeburn  6953:         }
1.46      raeburn  6954:         my ($configuserok,$author_ok,$switchserver) = 
                   6955:             &config_check($dom,$confname,$servadm);
1.9       raeburn  6956:         my ($width,$height) = &thumb_dimensions();
1.40      raeburn  6957:         if (ref($domconfig->{$role}) ne 'HASH') {
                   6958:             $domconfig->{$role} = {};
                   6959:         }
1.8       raeburn  6960:         foreach my $img (@images) {
1.70      raeburn  6961:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
                   6962:                 if (defined($env{'form.login_showlogo_'.$img})) {
                   6963:                     $confhash->{$role}{'showlogo'}{$img} = 1;
                   6964:                 } else { 
                   6965:                     $confhash->{$role}{'showlogo'}{$img} = 0;
                   6966:                 }
                   6967:             } 
1.18      albertel 6968: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
                   6969: 		 && !defined($domconfig->{$role}{$img})
                   6970: 		 && !$env{'form.'.$role.'_del_'.$img}
                   6971: 		 && $env{'form.'.$role.'_import_'.$img}) {
                   6972: 		# import the old configured image from the .tab setting
                   6973: 		# if they haven't provided a new one 
                   6974: 		$domconfig->{$role}{$img} = 
                   6975: 		    $env{'form.'.$role.'_import_'.$img};
                   6976: 	    }
1.6       raeburn  6977:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
1.9       raeburn  6978:                 my $error;
1.6       raeburn  6979:                 if ($configuserok eq 'ok') {
1.9       raeburn  6980:                     if ($switchserver) {
1.12      raeburn  6981:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
1.9       raeburn  6982:                     } else {
                   6983:                         if ($author_ok eq 'ok') {
                   6984:                             my ($result,$logourl) = 
                   6985:                                 &publishlogo($r,'upload',$role.'_'.$img,
                   6986:                                            $dom,$confname,$img,$width,$height);
                   6987:                             if ($result eq 'ok') {
                   6988:                                 $confhash->{$role}{$img} = $logourl;
1.12      raeburn  6989:                                 $changes{$role}{'images'}{$img} = 1;
1.9       raeburn  6990:                             } else {
1.12      raeburn  6991:                                 $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);
1.9       raeburn  6992:                             }
                   6993:                         } else {
1.46      raeburn  6994:                             $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);
1.6       raeburn  6995:                         }
                   6996:                     }
                   6997:                 } else {
1.46      raeburn  6998:                     $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);
1.9       raeburn  6999:                 }
                   7000:                 if ($error) {
1.8       raeburn  7001:                     &Apache::lonnet::logthis($error);
1.11      albertel 7002:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
1.8       raeburn  7003:                 }
                   7004:             } elsif ($domconfig->{$role}{$img} ne '') {
1.9       raeburn  7005:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
                   7006:                     my $error;
                   7007:                     if ($configuserok eq 'ok') {
                   7008: # is confname an author?
                   7009:                         if ($switchserver eq '') {
                   7010:                             if ($author_ok eq 'ok') {
                   7011:                                 my ($result,$logourl) = 
                   7012:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
                   7013:                                             $dom,$confname,$img,$width,$height);
                   7014:                                 if ($result eq 'ok') {
                   7015:                                     $confhash->{$role}{$img} = $logourl;
1.18      albertel 7016: 				    $changes{$role}{'images'}{$img} = 1;
1.9       raeburn  7017:                                 }
                   7018:                             }
                   7019:                         }
                   7020:                     }
1.6       raeburn  7021:                 }
                   7022:             }
                   7023:         }
                   7024:         if (ref($domconfig) eq 'HASH') {
                   7025:             if (ref($domconfig->{$role}) eq 'HASH') {
                   7026:                 foreach my $img (@images) {
                   7027:                     if ($domconfig->{$role}{$img} ne '') {
                   7028:                         if ($env{'form.'.$role.'_del_'.$img}) {
                   7029:                             $confhash->{$role}{$img} = '';
1.12      raeburn  7030:                             $changes{$role}{'images'}{$img} = 1;
1.6       raeburn  7031:                         } else {
1.9       raeburn  7032:                             if ($confhash->{$role}{$img} eq '') {
                   7033:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
                   7034:                             }
1.6       raeburn  7035:                         }
                   7036:                     } else {
                   7037:                         if ($env{'form.'.$role.'_del_'.$img}) {
                   7038:                             $confhash->{$role}{$img} = '';
1.12      raeburn  7039:                             $changes{$role}{'images'}{$img} = 1;
1.6       raeburn  7040:                         } 
                   7041:                     }
1.70      raeburn  7042:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
                   7043:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
                   7044:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
                   7045:                                 $domconfig->{$role}{'showlogo'}{$img}) {
                   7046:                                 $changes{$role}{'showlogo'}{$img} = 1; 
                   7047:                             }
                   7048:                         } else {
                   7049:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
                   7050:                                 $changes{$role}{'showlogo'}{$img} = 1;
                   7051:                             }
                   7052:                         }
                   7053:                     }
                   7054:                 }
1.6       raeburn  7055:                 if ($domconfig->{$role}{'font'} ne '') {
                   7056:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
                   7057:                         $changes{$role}{'font'} = 1;
                   7058:                     }
                   7059:                 } else {
                   7060:                     if ($confhash->{$role}{'font'}) {
                   7061:                         $changes{$role}{'font'} = 1;
                   7062:                     }
                   7063:                 }
1.107     raeburn  7064:                 if ($role ne 'login') {
                   7065:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
                   7066:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
                   7067:                             $changes{$role}{'fontmenu'} = 1;
                   7068:                         }
                   7069:                     } else {
                   7070:                         if ($confhash->{$role}{'fontmenu'}) {
                   7071:                             $changes{$role}{'fontmenu'} = 1;
                   7072:                         }
1.97      tempelho 7073:                     }
                   7074:                 }
1.6       raeburn  7075:                 foreach my $item (@bgs) {
                   7076:                     if ($domconfig->{$role}{$item} ne '') {
                   7077:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
                   7078:                             $changes{$role}{'bgs'}{$item} = 1;
                   7079:                         } 
                   7080:                     } else {
                   7081:                         if ($confhash->{$role}{$item}) {
                   7082:                             $changes{$role}{'bgs'}{$item} = 1;
                   7083:                         }
                   7084:                     }
                   7085:                 }
                   7086:                 foreach my $item (@links) {
                   7087:                     if ($domconfig->{$role}{$item} ne '') {
                   7088:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
                   7089:                             $changes{$role}{'links'}{$item} = 1;
                   7090:                         }
                   7091:                     } else {
                   7092:                         if ($confhash->{$role}{$item}) {
                   7093:                             $changes{$role}{'links'}{$item} = 1;
                   7094:                         }
                   7095:                     }
                   7096:                 }
1.41      raeburn  7097:                 foreach my $item (@logintext) {
                   7098:                     if ($domconfig->{$role}{$item} ne '') {
                   7099:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
                   7100:                             $changes{$role}{'logintext'}{$item} = 1;
                   7101:                         }
                   7102:                     } else {
                   7103:                         if ($confhash->{$role}{$item}) {
                   7104:                             $changes{$role}{'logintext'}{$item} = 1;
                   7105:                         }
                   7106:                     }
                   7107:                 }
1.6       raeburn  7108:             } else {
                   7109:                 &default_change_checker($role,\@images,\@links,\@bgs,
1.41      raeburn  7110:                                         \@logintext,$confhash,\%changes); 
1.6       raeburn  7111:             }
                   7112:         } else {
                   7113:             &default_change_checker($role,\@images,\@links,\@bgs,
1.41      raeburn  7114:                                     \@logintext,$confhash,\%changes); 
1.6       raeburn  7115:         }
                   7116:     }
                   7117:     return ($errors,%changes);
                   7118: }
                   7119: 
1.46      raeburn  7120: sub config_check {
                   7121:     my ($dom,$confname,$servadm) = @_;
                   7122:     my ($configuserok,$author_ok,$switchserver,%currroles);
                   7123:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
                   7124:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
                   7125:                                                    $confname,$servadm);
                   7126:     if ($configuserok eq 'ok') {
                   7127:         $switchserver = &check_switchserver($dom,$confname);
                   7128:         if ($switchserver eq '') {
                   7129:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
                   7130:         }
                   7131:     }
                   7132:     return ($configuserok,$author_ok,$switchserver);
                   7133: }
                   7134: 
1.6       raeburn  7135: sub default_change_checker {
1.41      raeburn  7136:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
1.6       raeburn  7137:     foreach my $item (@{$links}) {
                   7138:         if ($confhash->{$role}{$item}) {
                   7139:             $changes->{$role}{'links'}{$item} = 1;
                   7140:         }
                   7141:     }
                   7142:     foreach my $item (@{$bgs}) {
                   7143:         if ($confhash->{$role}{$item}) {
                   7144:             $changes->{$role}{'bgs'}{$item} = 1;
                   7145:         }
                   7146:     }
1.41      raeburn  7147:     foreach my $item (@{$logintext}) {
                   7148:         if ($confhash->{$role}{$item}) {
                   7149:             $changes->{$role}{'logintext'}{$item} = 1;
                   7150:         }
                   7151:     }
1.6       raeburn  7152:     foreach my $img (@{$images}) {
                   7153:         if ($env{'form.'.$role.'_del_'.$img}) {
                   7154:             $confhash->{$role}{$img} = '';
1.12      raeburn  7155:             $changes->{$role}{'images'}{$img} = 1;
1.6       raeburn  7156:         }
1.70      raeburn  7157:         if ($role eq 'login') {
                   7158:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
                   7159:                 $changes->{$role}{'showlogo'}{$img} = 1;
                   7160:             }
                   7161:         }
1.6       raeburn  7162:     }
                   7163:     if ($confhash->{$role}{'font'}) {
                   7164:         $changes->{$role}{'font'} = 1;
                   7165:     }
1.48      raeburn  7166: }
1.6       raeburn  7167: 
                   7168: sub display_colorchgs {
                   7169:     my ($dom,$changes,$roles,$confhash) = @_;
                   7170:     my (%choices,$resulttext);
                   7171:     if (!grep(/^login$/,@{$roles})) {
                   7172:         $resulttext = &mt('Changes made:').'<br />';
                   7173:     }
                   7174:     foreach my $role (@{$roles}) {
                   7175:         if ($role eq 'login') {
                   7176:             %choices = &login_choices();
                   7177:         } else {
                   7178:             %choices = &color_font_choices();
                   7179:         }
                   7180:         if (ref($changes->{$role}) eq 'HASH') {
                   7181:             if ($role ne 'login') {
                   7182:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
                   7183:             }
                   7184:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
                   7185:                 if ($role ne 'login') {
                   7186:                     $resulttext .= '<ul>';
                   7187:                 }
                   7188:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
                   7189:                     if ($role ne 'login') {
                   7190:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
                   7191:                     }
                   7192:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
1.70      raeburn  7193:                         if (($role eq 'login') && ($key eq 'showlogo')) {
                   7194:                             if ($confhash->{$role}{$key}{$item}) {
                   7195:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
                   7196:                             } else {
                   7197:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
                   7198:                             }
                   7199:                         } elsif ($confhash->{$role}{$item} eq '') {
1.6       raeburn  7200:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
                   7201:                         } else {
1.12      raeburn  7202:                             my $newitem = $confhash->{$role}{$item};
                   7203:                             if ($key eq 'images') {
                   7204:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
                   7205:                             }
                   7206:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
1.6       raeburn  7207:                         }
                   7208:                     }
                   7209:                     if ($role ne 'login') {
                   7210:                         $resulttext .= '</ul></li>';
                   7211:                     }
                   7212:                 } else {
                   7213:                     if ($confhash->{$role}{$key} eq '') {
                   7214:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
                   7215:                     } else {
                   7216:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
                   7217:                     }
                   7218:                 }
                   7219:                 if ($role ne 'login') {
                   7220:                     $resulttext .= '</ul>';
                   7221:                 }
                   7222:             }
                   7223:         }
                   7224:     }
1.3       raeburn  7225:     return $resulttext;
1.1       raeburn  7226: }
                   7227: 
1.9       raeburn  7228: sub thumb_dimensions {
                   7229:     return ('200','50');
                   7230: }
                   7231: 
1.16      raeburn  7232: sub check_dimensions {
                   7233:     my ($inputfile) = @_;
                   7234:     my ($fullwidth,$fullheight);
                   7235:     if ($inputfile =~ m|^[/\w.\-]+$|) {
                   7236:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
                   7237:             my $imageinfo = <PIPE>;
                   7238:             if (!close(PIPE)) {
                   7239:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
                   7240:             }
                   7241:             chomp($imageinfo);
                   7242:             my ($fullsize) = 
1.21      raeburn  7243:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
1.16      raeburn  7244:             if ($fullsize) {
                   7245:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
                   7246:             }
                   7247:         }
                   7248:     }
                   7249:     return ($fullwidth,$fullheight);
                   7250: }
                   7251: 
1.9       raeburn  7252: sub check_configuser {
                   7253:     my ($uhome,$dom,$confname,$servadm) = @_;
                   7254:     my ($configuserok,%currroles);
                   7255:     if ($uhome eq 'no_host') {
                   7256:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
                   7257:         my $configpass = &LONCAPA::Enrollment::create_password();
                   7258:         $configuserok = 
                   7259:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
                   7260:                              $configpass,'','','','','',undef,$servadm);
                   7261:     } else {
                   7262:         $configuserok = 'ok';
                   7263:         %currroles = 
                   7264:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
                   7265:     }
                   7266:     return ($configuserok,%currroles);
                   7267: }
                   7268: 
                   7269: sub check_authorstatus {
                   7270:     my ($dom,$confname,%currroles) = @_;
                   7271:     my $author_ok;
1.40      raeburn  7272:     if (!$currroles{':'.$dom.':au'}) {
1.9       raeburn  7273:         my $start = time;
                   7274:         my $end = 0;
                   7275:         $author_ok = 
                   7276:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
1.47      raeburn  7277:                                         'au',$end,$start,'','','domconfig');
1.9       raeburn  7278:     } else {
                   7279:         $author_ok = 'ok';
                   7280:     }
                   7281:     return $author_ok;
                   7282: }
                   7283: 
                   7284: sub publishlogo {
1.46      raeburn  7285:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
1.267   ! raeburn  7286:     my ($output,$fname,$logourl,$madethumb);
1.9       raeburn  7287:     if ($action eq 'upload') {
                   7288:         $fname=$env{'form.'.$formname.'.filename'};
                   7289:         chop($env{'form.'.$formname});
                   7290:     } else {
                   7291:         ($fname) = ($formname =~ /([^\/]+)$/);
                   7292:     }
1.46      raeburn  7293:     if ($savefileas ne '') {
                   7294:         $fname = $savefileas;
                   7295:     }
1.9       raeburn  7296:     $fname=&Apache::lonnet::clean_filename($fname);
                   7297: # See if there is anything left
                   7298:     unless ($fname) { return ('error: no uploaded file'); }
                   7299:     $fname="$subdir/$fname";
1.210     raeburn  7300:     my $docroot=$r->dir_config('lonDocRoot');
1.164     raeburn  7301:     my $filepath="$docroot/priv";
                   7302:     my $relpath = "$dom/$confname";
1.9       raeburn  7303:     my ($fnamepath,$file,$fetchthumb);
                   7304:     $file=$fname;
                   7305:     if ($fname=~m|/|) {
                   7306:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
                   7307:     }
1.164     raeburn  7308:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
1.9       raeburn  7309:     my $count;
1.164     raeburn  7310:     for ($count=5;$count<=$#parts;$count++) {
1.9       raeburn  7311:         $filepath.="/$parts[$count]";
                   7312:         if ((-e $filepath)!=1) {
                   7313:             mkdir($filepath,02770);
                   7314:         }
                   7315:     }
                   7316:     # Check for bad extension and disallow upload
                   7317:     if ($file=~/\.(\w+)$/ &&
                   7318:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
                   7319:         $output = 
1.207     bisitz   7320:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
1.9       raeburn  7321:     } elsif ($file=~/\.(\w+)$/ &&
                   7322:         !defined(&Apache::loncommon::fileembstyle($1))) {
                   7323:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
                   7324:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
1.195     bisitz   7325:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
1.9       raeburn  7326:     } elsif (-d "$filepath/$file") {
1.195     bisitz   7327:         $output = &mt('Filename is a directory name - rename the file and re-upload');
1.9       raeburn  7328:     } else {
                   7329:         my $source = $filepath.'/'.$file;
                   7330:         my $logfile;
                   7331:         if (!open($logfile,">>$source".'.log')) {
1.196     raeburn  7332:             return (&mt('No write permission to Authoring Space'));
1.9       raeburn  7333:         }
                   7334:         print $logfile
                   7335: "\n================= Publish ".localtime()." ================\n".
                   7336: $env{'user.name'}.':'.$env{'user.domain'}."\n";
                   7337: # Save the file
                   7338:         if (!open(FH,'>'.$source)) {
                   7339:             &Apache::lonnet::logthis('Failed to create '.$source);
                   7340:             return (&mt('Failed to create file'));
                   7341:         }
                   7342:         if ($action eq 'upload') {
                   7343:             if (!print FH ($env{'form.'.$formname})) {
                   7344:                 &Apache::lonnet::logthis('Failed to write to '.$source);
                   7345:                 return (&mt('Failed to write file'));
                   7346:             }
                   7347:         } else {
                   7348:             my $original = &Apache::lonnet::filelocation('',$formname);
                   7349:             if(!copy($original,$source)) {
                   7350:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
                   7351:                 return (&mt('Failed to write file'));
                   7352:             }
                   7353:         }
                   7354:         close(FH);
                   7355:         chmod(0660, $source); # Permissions to rw-rw---.
                   7356: 
                   7357:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
                   7358:         my $copyfile=$targetdir.'/'.$file;
                   7359: 
                   7360:         my @parts=split(/\//,$targetdir);
                   7361:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
                   7362:         for (my $count=5;$count<=$#parts;$count++) {
                   7363:             $path.="/$parts[$count]";
                   7364:             if (!-e $path) {
                   7365:                 print $logfile "\nCreating directory ".$path;
                   7366:                 mkdir($path,02770);
                   7367:             }
                   7368:         }
                   7369:         my $versionresult;
                   7370:         if (-e $copyfile) {
                   7371:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
                   7372:         } else {
                   7373:             $versionresult = 'ok';
                   7374:         }
                   7375:         if ($versionresult eq 'ok') {
                   7376:             if (copy($source,$copyfile)) {
                   7377:                 print $logfile "\nCopied original source to ".$copyfile."\n";
                   7378:                 $output = 'ok';
                   7379:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
1.155     raeburn  7380:                 push(@{$modified_urls},[$copyfile,$source]);
                   7381:                 my $metaoutput = 
                   7382:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
                   7383:                 unless ($registered_cleanup) {
                   7384:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
                   7385:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
                   7386:                     $registered_cleanup=1;
                   7387:                 }
1.9       raeburn  7388:             } else {
                   7389:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
                   7390:                 $output = &mt('Failed to copy file to RES space').", $!";
                   7391:             }
                   7392:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
                   7393:                 my $inputfile = $filepath.'/'.$file;
                   7394:                 my $outfile = $filepath.'/'.'tn-'.$file;
1.16      raeburn  7395:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
                   7396:                 if ($fullwidth ne '' && $fullheight ne '') { 
                   7397:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
                   7398:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
                   7399:                         system("convert -sample $thumbsize $inputfile $outfile");
                   7400:                         chmod(0660, $filepath.'/tn-'.$file);
                   7401:                         if (-e $outfile) {
                   7402:                             my $copyfile=$targetdir.'/tn-'.$file;
                   7403:                             if (copy($outfile,$copyfile)) {
                   7404:                                 print $logfile "\nCopied source to ".$copyfile."\n";
1.155     raeburn  7405:                                 my $thumb_metaoutput = 
                   7406:                                     &write_metadata($dom,$confname,$formname,
                   7407:                                                     $targetdir,'tn-'.$file,$logfile);
                   7408:                                 push(@{$modified_urls},[$copyfile,$outfile]);
                   7409:                                 unless ($registered_cleanup) {
                   7410:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
                   7411:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
                   7412:                                     $registered_cleanup=1;
                   7413:                                 }
1.267   ! raeburn  7414:                                 $madethumb = 1;
1.16      raeburn  7415:                             } else {
                   7416:                                 print $logfile "\nUnable to write ".$copyfile.
                   7417:                                                ':'.$!."\n";
                   7418:                             }
                   7419:                         }
1.9       raeburn  7420:                     }
                   7421:                 }
                   7422:             }
                   7423:         } else {
                   7424:             $output = $versionresult;
                   7425:         }
                   7426:     }
1.267   ! raeburn  7427:     return ($output,$logourl,$madethumb);
1.9       raeburn  7428: }
                   7429: 
                   7430: sub logo_versioning {
                   7431:     my ($targetdir,$file,$logfile) = @_;
                   7432:     my $target = $targetdir.'/'.$file;
                   7433:     my ($maxversion,$fn,$extn,$output);
                   7434:     $maxversion = 0;
                   7435:     if ($file =~ /^(.+)\.(\w+)$/) {
                   7436:         $fn=$1;
                   7437:         $extn=$2;
                   7438:     }
                   7439:     opendir(DIR,$targetdir);
                   7440:     while (my $filename=readdir(DIR)) {
                   7441:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
                   7442:             $maxversion=($1>$maxversion)?$1:$maxversion;
                   7443:         }
                   7444:     }
                   7445:     $maxversion++;
                   7446:     print $logfile "\nCreating old version ".$maxversion."\n";
                   7447:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
                   7448:     if (copy($target,$copyfile)) {
                   7449:         print $logfile "Copied old target to ".$copyfile."\n";
                   7450:         $copyfile=$copyfile.'.meta';
                   7451:         if (copy($target.'.meta',$copyfile)) {
                   7452:             print $logfile "Copied old target metadata to ".$copyfile."\n";
                   7453:             $output = 'ok';
                   7454:         } else {
                   7455:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
                   7456:             $output = &mt('Failed to copy old meta').", $!, ";
                   7457:         }
                   7458:     } else {
                   7459:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
                   7460:         $output = &mt('Failed to copy old target').", $!, ";
                   7461:     }
                   7462:     return $output;
                   7463: }
                   7464: 
                   7465: sub write_metadata {
                   7466:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
                   7467:     my (%metadatafields,%metadatakeys,$output);
                   7468:     $metadatafields{'title'}=$formname;
                   7469:     $metadatafields{'creationdate'}=time;
                   7470:     $metadatafields{'lastrevisiondate'}=time;
                   7471:     $metadatafields{'copyright'}='public';
                   7472:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
                   7473:                                          $env{'user.domain'};
                   7474:     $metadatafields{'authorspace'}=$confname.':'.$dom;
                   7475:     $metadatafields{'domain'}=$dom;
                   7476:     {
                   7477:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
                   7478:         my $mfh;
1.155     raeburn  7479:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
1.184     raeburn  7480:             foreach (sort(keys(%metadatafields))) {
1.155     raeburn  7481:                 unless ($_=~/\./) {
                   7482:                     my $unikey=$_;
                   7483:                     $unikey=~/^([A-Za-z]+)/;
                   7484:                     my $tag=$1;
                   7485:                     $tag=~tr/A-Z/a-z/;
                   7486:                     print $mfh "\n\<$tag";
                   7487:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
                   7488:                         my $value=$metadatafields{$unikey.'.'.$_};
                   7489:                         $value=~s/\"/\'\'/g;
                   7490:                         print $mfh ' '.$_.'="'.$value.'"';
                   7491:                     }
                   7492:                     print $mfh '>'.
                   7493:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
                   7494:                             .'</'.$tag.'>';
                   7495:                 }
                   7496:             }
                   7497:             $output = 'ok';
                   7498:             print $logfile "\nWrote metadata";
                   7499:             close($mfh);
                   7500:         } else {
                   7501:             print $logfile "\nFailed to open metadata file";
1.9       raeburn  7502:             $output = &mt('Could not write metadata');
                   7503:         }
                   7504:     }
1.155     raeburn  7505:     return $output;
                   7506: }
                   7507: 
                   7508: sub notifysubscribed {
                   7509:     foreach my $targetsource (@{$modified_urls}){
                   7510:         next unless (ref($targetsource) eq 'ARRAY');
                   7511:         my ($target,$source)=@{$targetsource};
                   7512:         if ($source ne '') {
                   7513:             if (open(my $logfh,'>>'.$source.'.log')) {
                   7514:                 print $logfh "\nCleanup phase: Notifications\n";
                   7515:                 my @subscribed=&subscribed_hosts($target);
                   7516:                 foreach my $subhost (@subscribed) {
                   7517:                     print $logfh "\nNotifying host ".$subhost.':';
                   7518:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
                   7519:                     print $logfh $reply;
                   7520:                 }
                   7521:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
                   7522:                 foreach my $subhost (@subscribedmeta) {
                   7523:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
                   7524:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
                   7525:                                                         $subhost);
                   7526:                     print $logfh $reply;
                   7527:                 }
                   7528:                 print $logfh "\n============ Done ============\n";
1.160     raeburn  7529:                 close($logfh);
1.155     raeburn  7530:             }
                   7531:         }
                   7532:     }
                   7533:     return OK;
                   7534: }
                   7535: 
                   7536: sub subscribed_hosts {
                   7537:     my ($target) = @_;
                   7538:     my @subscribed;
                   7539:     if (open(my $fh,"<$target.subscription")) {
                   7540:         while (my $subline=<$fh>) {
                   7541:             if ($subline =~ /^($match_lonid):/) {
                   7542:                 my $host = $1;
                   7543:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
                   7544:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
                   7545:                         push(@subscribed,$host);
                   7546:                     }
                   7547:                 }
                   7548:             }
                   7549:         }
                   7550:     }
                   7551:     return @subscribed;
1.9       raeburn  7552: }
                   7553: 
                   7554: sub check_switchserver {
                   7555:     my ($dom,$confname) = @_;
                   7556:     my ($allowed,$switchserver);
                   7557:     my $home = &Apache::lonnet::homeserver($confname,$dom);
                   7558:     if ($home eq 'no_host') {
                   7559:         $home = &Apache::lonnet::domain($dom,'primary');
                   7560:     }
                   7561:     my @ids=&Apache::lonnet::current_machine_ids();
1.10      albertel 7562:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
                   7563:     if (!$allowed) {
1.180     raeburn  7564: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
1.9       raeburn  7565:     }
                   7566:     return $switchserver;
                   7567: }
                   7568: 
1.1       raeburn  7569: sub modify_quotas {
1.216     raeburn  7570:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
1.101     raeburn  7571:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
1.216     raeburn  7572:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
1.235     raeburn  7573:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
                   7574:         $validationfieldsref);
1.86      raeburn  7575:     if ($action eq 'quotas') {
                   7576:         $context = 'tools'; 
1.163     raeburn  7577:     } else {
1.86      raeburn  7578:         $context = $action;
                   7579:     }
                   7580:     if ($context eq 'requestcourses') {
1.216     raeburn  7581:         @usertools = ('official','unofficial','community','textbook');
1.106     raeburn  7582:         @options =('norequest','approval','validate','autolimit');
1.101     raeburn  7583:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
                   7584:         %titles = &courserequest_titles();
                   7585:         $toolregexp = join('|',@usertools);
                   7586:         %conditions = &courserequest_conditions();
1.216     raeburn  7587:         $confname = $dom.'-domainconfig';
                   7588:         my $servadm = $r->dir_config('lonAdmEMail');
                   7589:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
1.235     raeburn  7590:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
                   7591:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
1.163     raeburn  7592:     } elsif ($context eq 'requestauthor') {
                   7593:         @usertools = ('author');
                   7594:         %titles = &authorrequest_titles();
1.86      raeburn  7595:     } else {
1.162     raeburn  7596:         @usertools = ('aboutme','blog','webdav','portfolio');
1.101     raeburn  7597:         %titles = &tool_titles();
1.86      raeburn  7598:     }
1.212     raeburn  7599:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
1.44      raeburn  7600:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
1.1       raeburn  7601:     foreach my $key (keys(%env)) {
1.101     raeburn  7602:         if ($context eq 'requestcourses') {
                   7603:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
                   7604:                 my $item = $1;
                   7605:                 my $type = $2;
                   7606:                 if ($type =~ /^limit_(.+)/) {
                   7607:                     $limithash{$item}{$1} = $env{$key};
                   7608:                 } else {
                   7609:                     $confhash{$item}{$type} = $env{$key};
                   7610:                 }
                   7611:             }
1.163     raeburn  7612:         } elsif ($context eq 'requestauthor') {
                   7613:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
                   7614:                 $confhash{$1} = $env{$key};
                   7615:             }
1.101     raeburn  7616:         } else {
1.86      raeburn  7617:             if ($key =~ /^form\.quota_(.+)$/) {
                   7618:                 $confhash{'defaultquota'}{$1} = $env{$key};
1.197     raeburn  7619:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
                   7620:                 $confhash{'authorquota'}{$1} = $env{$key};
                   7621:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
1.101     raeburn  7622:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
                   7623:             }
1.72      raeburn  7624:         }
                   7625:     }
1.163     raeburn  7626:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
1.224     raeburn  7627:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
1.102     raeburn  7628:         @approvalnotify = sort(@approvalnotify);
                   7629:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
1.218     raeburn  7630:         my @crstypes = ('official','unofficial','community','textbook');
                   7631:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
                   7632:         foreach my $type (@hasuniquecode) {
                   7633:             if (grep(/^\Q$type\E$/,@crstypes)) {
                   7634:                 $confhash{'uniquecode'}{$type} = 1;
                   7635:             }
1.216     raeburn  7636:         }
1.242     raeburn  7637:         my (%newbook,%allpos);
1.216     raeburn  7638:         if ($context eq 'requestcourses') {
1.242     raeburn  7639:             foreach my $type ('textbooks','templates') {
                   7640:                 @{$allpos{$type}} = (); 
                   7641:                 my $invalid;
                   7642:                 if ($type eq 'textbooks') {
                   7643:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
                   7644:                 } else {
                   7645:                     $invalid = &mt('Invalid LON-CAPA course for template');
                   7646:                 }
                   7647:                 if ($env{'form.'.$type.'_addbook'}) {
                   7648:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
                   7649:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
                   7650:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
                   7651:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
                   7652:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
                   7653:                         } else {
                   7654:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
                   7655:                             my $position = $env{'form.'.$type.'_addbook_pos'};
                   7656:                             $position =~ s/\D+//g;
                   7657:                             if ($position ne '') {
                   7658:                                 $allpos{$type}[$position] = $newbook{$type};
                   7659:                             }
1.216     raeburn  7660:                         }
1.242     raeburn  7661:                     } else {
                   7662:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
1.216     raeburn  7663:                     }
                   7664:                 }
1.242     raeburn  7665:             } 
1.216     raeburn  7666:         }
1.102     raeburn  7667:         if (ref($domconfig{$action}) eq 'HASH') {
                   7668:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
                   7669:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
                   7670:                     $changes{'notify'}{'approval'} = 1;
                   7671:                 }
                   7672:             } else {
1.144     raeburn  7673:                 if ($confhash{'notify'}{'approval'}) {
1.102     raeburn  7674:                     $changes{'notify'}{'approval'} = 1;
                   7675:                 }
                   7676:             }
1.218     raeburn  7677:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
                   7678:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
                   7679:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
                   7680:                         unless ($confhash{'uniquecode'}{$crstype}) {
                   7681:                             $changes{'uniquecode'} = 1;
                   7682:                         }
                   7683:                     }
                   7684:                     unless ($changes{'uniquecode'}) {
                   7685:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
                   7686:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
                   7687:                                 $changes{'uniquecode'} = 1;
                   7688:                             }
                   7689:                         }
                   7690:                     }
                   7691:                } else {
                   7692:                    $changes{'uniquecode'} = 1;
                   7693:                }
                   7694:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
                   7695:                 $changes{'uniquecode'} = 1;
1.216     raeburn  7696:             }
                   7697:             if ($context eq 'requestcourses') {
1.242     raeburn  7698:                 foreach my $type ('textbooks','templates') {
                   7699:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
                   7700:                         my %deletions;
                   7701:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
                   7702:                         if (@todelete) {
                   7703:                             map { $deletions{$_} = 1; } @todelete;
                   7704:                         }
                   7705:                         my %imgdeletions;
                   7706:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
                   7707:                         if (@todeleteimages) {
                   7708:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
                   7709:                         }
                   7710:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
                   7711:                         for (my $i=0; $i<=$maxnum; $i++) {
                   7712:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
                   7713:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
                   7714:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
                   7715:                                 if ($deletions{$key}) {
                   7716:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
                   7717:                                         #FIXME need to obsolete item in RES space
                   7718:                                     }
                   7719:                                     next;
                   7720:                                 } else {
                   7721:                                     my $newpos = $env{'form.'.$itemid};
                   7722:                                     $newpos =~ s/\D+//g;
1.243     raeburn  7723:                                     foreach my $item ('subject','title','publisher','author') {
                   7724:                                         next if ((($item eq 'author') || ($item eq 'publisher')) && 
                   7725:                                                  ($type eq 'templates'));
1.242     raeburn  7726:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
                   7727:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
                   7728:                                             $changes{$type}{$key} = 1;
                   7729:                                         }
                   7730:                                     }
                   7731:                                     $allpos{$type}[$newpos] = $key;
                   7732:                                 }
                   7733:                                 if ($imgdeletions{$key}) {
                   7734:                                     $changes{$type}{$key} = 1;
1.216     raeburn  7735:                                     #FIXME need to obsolete item in RES space
1.242     raeburn  7736:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
                   7737:                                     my ($cdom,$cnum) = split(/_/,$key);
                   7738:                                     my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
                   7739:                                                                                   $cdom,$cnum,$type,$configuserok,
                   7740:                                                                                   $switchserver,$author_ok);
                   7741:                                     if ($imgurl) {
                   7742:                                         $confhash{$type}{$key}{'image'} = $imgurl;
                   7743:                                         $changes{$type}{$key} = 1; 
1.216     raeburn  7744:                                     }
1.242     raeburn  7745:                                     if ($error) {
                   7746:                                         &Apache::lonnet::logthis($error);
                   7747:                                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
                   7748:                                     } 
                   7749:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
                   7750:                                     $confhash{$type}{$key}{'image'} = 
                   7751:                                         $domconfig{$action}{$type}{$key}{'image'};
1.216     raeburn  7752:                                 }
                   7753:                             }
                   7754:                         }
                   7755:                     }
                   7756:                 }
                   7757:             }
1.102     raeburn  7758:         } else {
1.144     raeburn  7759:             if ($confhash{'notify'}{'approval'}) {
1.102     raeburn  7760:                 $changes{'notify'}{'approval'} = 1;
                   7761:             }
1.218     raeburn  7762:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
1.216     raeburn  7763:                 $changes{'uniquecode'} = 1;
                   7764:             }
                   7765:         }
                   7766:         if ($context eq 'requestcourses') {
1.242     raeburn  7767:             foreach my $type ('textbooks','templates') {
                   7768:                 if ($newbook{$type}) {
                   7769:                     $changes{$type}{$newbook{$type}} = 1;
1.243     raeburn  7770:                     foreach my $item ('subject','title','publisher','author') {
                   7771:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
                   7772:                                  ($type eq 'template'));
1.242     raeburn  7773:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
                   7774:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
                   7775:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
                   7776:                         }
                   7777:                     }
                   7778:                     if ($type eq 'textbooks') {
                   7779:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
                   7780:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
                   7781:                             my ($imageurl,$error) =
                   7782:                                 &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
                   7783:                                                         $configuserok,$switchserver,$author_ok);
                   7784:                             if ($imageurl) {
                   7785:                                 $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
                   7786:                             }
                   7787:                             if ($error) {
                   7788:                                 &Apache::lonnet::logthis($error);
                   7789:                                 $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
                   7790:                             }
                   7791:                         }
1.216     raeburn  7792:                     }
                   7793:                 }
1.242     raeburn  7794:                 if (@{$allpos{$type}} > 0) {
                   7795:                     my $idx = 0;
                   7796:                     foreach my $item (@{$allpos{$type}}) {
                   7797:                         if ($item ne '') {
                   7798:                             $confhash{$type}{$item}{'order'} = $idx;
                   7799:                             if (ref($domconfig{$action}) eq 'HASH') {
                   7800:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
                   7801:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
                   7802:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
                   7803:                                             $changes{$type}{$item} = 1;
                   7804:                                         }
1.216     raeburn  7805:                                     }
                   7806:                                 }
                   7807:                             }
1.242     raeburn  7808:                             $idx ++;
1.216     raeburn  7809:                         }
                   7810:                     }
                   7811:                 }
                   7812:             }
1.235     raeburn  7813:             if (ref($validationitemsref) eq 'ARRAY') {
                   7814:                 foreach my $item (@{$validationitemsref}) {
                   7815:                     if ($item eq 'fields') {
                   7816:                         my @changed;
                   7817:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
                   7818:                         if (@{$confhash{'validation'}{$item}} > 0) {
                   7819:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
                   7820:                         }
1.266     raeburn  7821:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
                   7822:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
                   7823:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
                   7824:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
                   7825:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
                   7826:                                 } else {
                   7827:                                     @changed = @{$confhash{'validation'}{$item}};
                   7828:                                 }
1.235     raeburn  7829:                             } else {
                   7830:                                 @changed = @{$confhash{'validation'}{$item}};
                   7831:                             }
                   7832:                         } else {
                   7833:                             @changed = @{$confhash{'validation'}{$item}};
                   7834:                         }
                   7835:                         if (@changed) {
                   7836:                             if ($confhash{'validation'}{$item}) {
                   7837:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
                   7838:                             } else {
                   7839:                                 $changes{'validation'}{$item} = &mt('None');
                   7840:                             }
                   7841:                         }
                   7842:                     } else {
                   7843:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
                   7844:                         if ($item eq 'markup') {
                   7845:                             if ($env{'form.requestcourses_validation_'.$item}) {
                   7846:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
                   7847:                             }
                   7848:                         }
1.266     raeburn  7849:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
                   7850:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
                   7851:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
                   7852:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
                   7853:                                 }
                   7854:                             } else {
                   7855:                                 if ($confhash{'validation'}{$item} ne '') {
                   7856:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
                   7857:                                 }
1.235     raeburn  7858:                             }
                   7859:                         } else {
                   7860:                             if ($confhash{'validation'}{$item} ne '') {
                   7861:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
                   7862:                             }
                   7863:                         }
                   7864:                     }
                   7865:                 }
                   7866:             }
                   7867:             if ($env{'form.validationdc'}) {
                   7868:                 my $newval = $env{'form.validationdc'};
                   7869:                 my %domcoords = &get_active_dcs($dom);
                   7870:                 if (exists($domcoords{$newval})) {
                   7871:                     $confhash{'validation'}{'dc'} = $newval;
                   7872:                 }
                   7873:             }
                   7874:             if (ref($confhash{'validation'}) eq 'HASH') {
1.266     raeburn  7875:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
                   7876:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
                   7877:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
                   7878:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
                   7879:                                 if ($confhash{'validation'}{'dc'} eq '') {
                   7880:                                     $changes{'validation'}{'dc'} = &mt('None');
                   7881:                                 } else {
                   7882:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
                   7883:                                 }
1.235     raeburn  7884:                             }
1.266     raeburn  7885:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
                   7886:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
1.235     raeburn  7887:                         }
                   7888:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
                   7889:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
                   7890:                     }
                   7891:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
                   7892:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
1.266     raeburn  7893:                 }  
                   7894:             } else {
                   7895:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
                   7896:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
                   7897:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
                   7898:                             $changes{'validation'}{'dc'} = &mt('None');
                   7899:                         }
                   7900:                     }
1.235     raeburn  7901:                 }
                   7902:             }
1.102     raeburn  7903:         }
                   7904:     } else {
1.86      raeburn  7905:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
1.197     raeburn  7906:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
1.86      raeburn  7907:     }
1.72      raeburn  7908:     foreach my $item (@usertools) {
                   7909:         foreach my $type (@{$types},'default','_LC_adv') {
1.104     raeburn  7910:             my $unset; 
1.101     raeburn  7911:             if ($context eq 'requestcourses') {
1.104     raeburn  7912:                 $unset = '0';
                   7913:                 if ($type eq '_LC_adv') {
                   7914:                     $unset = '';
                   7915:                 }
1.101     raeburn  7916:                 if ($confhash{$item}{$type} eq 'autolimit') {
                   7917:                     $confhash{$item}{$type} .= '=';
                   7918:                     unless ($limithash{$item}{$type} =~ /\D/) {
                   7919:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
                   7920:                     }
                   7921:                 }
1.163     raeburn  7922:             } elsif ($context eq 'requestauthor') {
                   7923:                 $unset = '0';
                   7924:                 if ($type eq '_LC_adv') {
                   7925:                     $unset = '';
                   7926:                 }
1.72      raeburn  7927:             } else {
1.101     raeburn  7928:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
                   7929:                     $confhash{$item}{$type} = 1;
                   7930:                 } else {
                   7931:                     $confhash{$item}{$type} = 0;
                   7932:                 }
1.72      raeburn  7933:             }
1.86      raeburn  7934:             if (ref($domconfig{$action}) eq 'HASH') {
1.163     raeburn  7935:                 if ($action eq 'requestauthor') {
                   7936:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
                   7937:                         $changes{$type} = 1;
                   7938:                     }
                   7939:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
1.86      raeburn  7940:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
                   7941:                         $changes{$item}{$type} = 1;
                   7942:                     }
                   7943:                 } else {
                   7944:                     if ($context eq 'requestcourses') {
1.104     raeburn  7945:                         if ($confhash{$item}{$type} ne $unset) {
1.86      raeburn  7946:                             $changes{$item}{$type} = 1;
                   7947:                         }
                   7948:                     } else {
                   7949:                         if (!$confhash{$item}{$type}) {
                   7950:                             $changes{$item}{$type} = 1;
                   7951:                         }
                   7952:                     }
                   7953:                 }
                   7954:             } else {
                   7955:                 if ($context eq 'requestcourses') {
1.104     raeburn  7956:                     if ($confhash{$item}{$type} ne $unset) {
1.72      raeburn  7957:                         $changes{$item}{$type} = 1;
                   7958:                     }
1.163     raeburn  7959:                 } elsif ($context eq 'requestauthor') {
                   7960:                     if ($confhash{$type} ne $unset) {
                   7961:                         $changes{$type} = 1;
                   7962:                     }
1.72      raeburn  7963:                 } else {
                   7964:                     if (!$confhash{$item}{$type}) {
                   7965:                         $changes{$item}{$type} = 1;
                   7966:                     }
                   7967:                 }
                   7968:             }
1.1       raeburn  7969:         }
                   7970:     }
1.163     raeburn  7971:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
1.86      raeburn  7972:         if (ref($domconfig{'quotas'}) eq 'HASH') {
                   7973:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
                   7974:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
                   7975:                     if (exists($confhash{'defaultquota'}{$key})) {
                   7976:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
                   7977:                             $changes{'defaultquota'}{$key} = 1;
                   7978:                         }
                   7979:                     } else {
                   7980:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
1.72      raeburn  7981:                     }
                   7982:                 }
1.86      raeburn  7983:             } else {
                   7984:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
                   7985:                     if (exists($confhash{'defaultquota'}{$key})) {
                   7986:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
                   7987:                             $changes{'defaultquota'}{$key} = 1;
                   7988:                         }
                   7989:                     } else {
                   7990:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
1.72      raeburn  7991:                     }
1.1       raeburn  7992:                 }
                   7993:             }
1.197     raeburn  7994:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
                   7995:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
                   7996:                     if (exists($confhash{'authorquota'}{$key})) {
                   7997:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
                   7998:                             $changes{'authorquota'}{$key} = 1;
                   7999:                         }
                   8000:                     } else {
                   8001:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
                   8002:                     }
                   8003:                 }
                   8004:             }
1.1       raeburn  8005:         }
1.86      raeburn  8006:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
                   8007:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
                   8008:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
                   8009:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
                   8010:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
                   8011:                             $changes{'defaultquota'}{$key} = 1;
                   8012:                         }
                   8013:                     } else {
                   8014:                         if (!exists($domconfig{'quotas'}{$key})) {
                   8015:                             $changes{'defaultquota'}{$key} = 1;
                   8016:                         }
1.72      raeburn  8017:                     }
                   8018:                 } else {
1.86      raeburn  8019:                     $changes{'defaultquota'}{$key} = 1;
1.55      raeburn  8020:                 }
1.1       raeburn  8021:             }
                   8022:         }
1.197     raeburn  8023:         if (ref($confhash{'authorquota'}) eq 'HASH') {
                   8024:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
                   8025:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
                   8026:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
                   8027:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
                   8028:                             $changes{'authorquota'}{$key} = 1;
                   8029:                         }
                   8030:                     } else {
                   8031:                         $changes{'authorquota'}{$key} = 1;
                   8032:                     }
                   8033:                 } else {
                   8034:                     $changes{'authorquota'}{$key} = 1;
                   8035:                 }
                   8036:             }
                   8037:         }
1.1       raeburn  8038:     }
1.72      raeburn  8039: 
1.163     raeburn  8040:     if ($context eq 'requestauthor') {
                   8041:         $domdefaults{'requestauthor'} = \%confhash;
                   8042:     } else {
                   8043:         foreach my $key (keys(%confhash)) {
1.242     raeburn  8044:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
1.216     raeburn  8045:                 $domdefaults{$key} = $confhash{$key};
                   8046:             }
1.163     raeburn  8047:         }
1.72      raeburn  8048:     }
1.163     raeburn  8049: 
1.1       raeburn  8050:     my %quotahash = (
1.86      raeburn  8051:                       $action => { %confhash }
1.1       raeburn  8052:                     );
                   8053:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
                   8054:                                              $dom);
                   8055:     if ($putresult eq 'ok') {
                   8056:         if (keys(%changes) > 0) {
1.72      raeburn  8057:             my $cachetime = 24*60*60;
                   8058:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
1.212     raeburn  8059:             if (ref($lastactref) eq 'HASH') {
                   8060:                 $lastactref->{'domdefaults'} = 1;
                   8061:             }
1.1       raeburn  8062:             $resulttext = &mt('Changes made:').'<ul>';
1.210     raeburn  8063:             unless (($context eq 'requestcourses') ||
1.163     raeburn  8064:                     ($context eq 'requestauthor')) {
1.86      raeburn  8065:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
                   8066:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
                   8067:                     foreach my $type (@{$types},'default') {
                   8068:                         if (defined($changes{'defaultquota'}{$type})) {
                   8069:                             my $typetitle = $usertypes->{$type};
                   8070:                             if ($type eq 'default') {
                   8071:                                 $typetitle = $othertitle;
                   8072:                             }
1.213     raeburn  8073:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
1.72      raeburn  8074:                         }
                   8075:                     }
1.86      raeburn  8076:                     $resulttext .= '</ul></li>';
1.72      raeburn  8077:                 }
1.197     raeburn  8078:                 if (ref($changes{'authorquota'}) eq 'HASH') {
1.223     bisitz   8079:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
1.197     raeburn  8080:                     foreach my $type (@{$types},'default') {
                   8081:                         if (defined($changes{'authorquota'}{$type})) {
                   8082:                             my $typetitle = $usertypes->{$type};
                   8083:                             if ($type eq 'default') {
                   8084:                                 $typetitle = $othertitle;
                   8085:                             }
1.213     raeburn  8086:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
1.197     raeburn  8087:                         }
                   8088:                     }
                   8089:                     $resulttext .= '</ul></li>';
                   8090:                 }
1.72      raeburn  8091:             }
1.80      raeburn  8092:             my %newenv;
1.72      raeburn  8093:             foreach my $item (@usertools) {
1.163     raeburn  8094:                 my (%haschgs,%inconf);
                   8095:                 if ($context eq 'requestauthor') {
                   8096:                     %haschgs = %changes;
1.210     raeburn  8097:                     %inconf = %confhash;
1.163     raeburn  8098:                 } else {
                   8099:                     if (ref($changes{$item}) eq 'HASH') {
                   8100:                         %haschgs = %{$changes{$item}};
                   8101:                     }
                   8102:                     if (ref($confhash{$item}) eq 'HASH') {
                   8103:                         %inconf = %{$confhash{$item}};
                   8104:                     }
                   8105:                 }
                   8106:                 if (keys(%haschgs) > 0) {
1.80      raeburn  8107:                     my $newacc = 
                   8108:                         &Apache::lonnet::usertools_access($env{'user.name'},
                   8109:                                                           $env{'user.domain'},
1.86      raeburn  8110:                                                           $item,'reload',$context);
1.210     raeburn  8111:                     if (($context eq 'requestcourses') ||
1.163     raeburn  8112:                         ($context eq 'requestauthor')) {
1.108     raeburn  8113:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
                   8114:                             $newenv{'environment.canrequest.'.$item} = $newacc;
1.86      raeburn  8115:                         }
                   8116:                     } else {
                   8117:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
                   8118:                             $newenv{'environment.availabletools.'.$item} = $newacc;
                   8119:                         }
1.80      raeburn  8120:                     }
1.163     raeburn  8121:                     unless ($context eq 'requestauthor') {
                   8122:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
                   8123:                     }
1.72      raeburn  8124:                     foreach my $type (@{$types},'default','_LC_adv') {
1.163     raeburn  8125:                         if ($haschgs{$type}) {
1.72      raeburn  8126:                             my $typetitle = $usertypes->{$type};
                   8127:                             if ($type eq 'default') {
                   8128:                                 $typetitle = $othertitle;
                   8129:                             } elsif ($type eq '_LC_adv') {
                   8130:                                 $typetitle = 'LON-CAPA Advanced Users'; 
                   8131:                             }
1.163     raeburn  8132:                             if ($inconf{$type}) {
1.101     raeburn  8133:                                 if ($context eq 'requestcourses') {
                   8134:                                     my $cond;
1.163     raeburn  8135:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
1.101     raeburn  8136:                                         if ($1 eq '') {
                   8137:                                             $cond = &mt('(Automatic processing of any request).');
                   8138:                                         } else {
                   8139:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
                   8140:                                         }
                   8141:                                     } else { 
1.163     raeburn  8142:                                         $cond = $conditions{$inconf{$type}};
1.101     raeburn  8143:                                     }
                   8144:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
1.172     raeburn  8145:                                 } elsif ($context eq 'requestauthor') {
                   8146:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
                   8147:                                                              $titles{$inconf{$type}},$typetitle);
                   8148: 
1.101     raeburn  8149:                                 } else {
                   8150:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
                   8151:                                 }
1.72      raeburn  8152:                             } else {
1.104     raeburn  8153:                                 if ($type eq '_LC_adv') {
1.163     raeburn  8154:                                     if ($inconf{$type} eq '0') {
1.104     raeburn  8155:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
                   8156:                                     } else { 
                   8157:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
                   8158:                                     }
                   8159:                                 } else {
                   8160:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
                   8161:                                 }
1.72      raeburn  8162:                             }
                   8163:                         }
1.26      raeburn  8164:                     }
1.163     raeburn  8165:                     unless ($context eq 'requestauthor') {
                   8166:                         $resulttext .= '</ul></li>';
                   8167:                     }
1.26      raeburn  8168:                 }
1.1       raeburn  8169:             }
1.163     raeburn  8170:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
1.102     raeburn  8171:                 if (ref($changes{'notify'}) eq 'HASH') {
                   8172:                     if ($changes{'notify'}{'approval'}) {
                   8173:                         if (ref($confhash{'notify'}) eq 'HASH') {
                   8174:                             if ($confhash{'notify'}{'approval'}) {
                   8175:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
                   8176:                             } else {
1.163     raeburn  8177:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
1.102     raeburn  8178:                             }
                   8179:                         }
                   8180:                     }
                   8181:                 }
                   8182:             }
1.216     raeburn  8183:             if ($action eq 'requestcourses') {
                   8184:                 my @offon = ('off','on');
                   8185:                 if ($changes{'uniquecode'}) {
1.218     raeburn  8186:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
                   8187:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
                   8188:                         $resulttext .= '<li>'.
                   8189:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
                   8190:                                        '</li>';
                   8191:                     } else {
                   8192:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
                   8193:                                        '</li>';
                   8194:                     }
1.216     raeburn  8195:                 }
1.242     raeburn  8196:                 foreach my $type ('textbooks','templates') {
                   8197:                     if (ref($changes{$type}) eq 'HASH') {
                   8198:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
                   8199:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
                   8200:                             my %coursehash = &Apache::lonnet::coursedescription($key);
                   8201:                             my $coursetitle = $coursehash{'description'};
                   8202:                             my $position = $confhash{$type}{$key}{'order'} + 1;
                   8203:                             $resulttext .= '<li>';
1.243     raeburn  8204:                             foreach my $item ('subject','title','publisher','author') {
                   8205:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
                   8206:                                          ($type eq 'templates'));
1.242     raeburn  8207:                                 my $name = $item.':';
                   8208:                                 $name =~ s/^(\w)/\U$1/;
                   8209:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
                   8210:                             }
                   8211:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
                   8212:                             if ($type eq 'textbooks') {
                   8213:                                 if ($confhash{$type}{$key}{'image'}) {
                   8214:                                     $resulttext .= ' '.&mt('Image: [_1]',
                   8215:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
                   8216:                                                    ' alt="Textbook cover" />').'<br />';
                   8217:                                 }
                   8218:                             }
                   8219:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
1.216     raeburn  8220:                         }
1.242     raeburn  8221:                         $resulttext .= '</ul></li>';
1.216     raeburn  8222:                     }
                   8223:                 }
1.235     raeburn  8224:                 if (ref($changes{'validation'}) eq 'HASH') {
                   8225:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
                   8226:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
                   8227:                         foreach my $item (@{$validationitemsref}) {
                   8228:                             if (exists($changes{'validation'}{$item})) {
                   8229:                                 if ($item eq 'markup') {
                   8230:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
                   8231:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
                   8232:                                 } else {
                   8233:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
                   8234:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
                   8235:                                 }
                   8236:                             }
                   8237:                         }
                   8238:                         if (exists($changes{'validation'}{'dc'})) {
                   8239:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
                   8240:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
                   8241:                         }
                   8242:                     }
                   8243:                 }
1.216     raeburn  8244:             }
1.1       raeburn  8245:             $resulttext .= '</ul>';
1.80      raeburn  8246:             if (keys(%newenv)) {
                   8247:                 &Apache::lonnet::appenv(\%newenv);
                   8248:             }
1.1       raeburn  8249:         } else {
1.86      raeburn  8250:             if ($context eq 'requestcourses') {
                   8251:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
1.163     raeburn  8252:             } elsif ($context eq 'requestauthor') {
                   8253:                 $resulttext = &mt('No changes made to rights to request author space.');
1.86      raeburn  8254:             } else {
1.90      weissno  8255:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
1.86      raeburn  8256:             }
1.1       raeburn  8257:         }
                   8258:     } else {
1.11      albertel 8259:         $resulttext = '<span class="LC_error">'.
                   8260: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
1.1       raeburn  8261:     }
1.216     raeburn  8262:     if ($errors) {
                   8263:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
                   8264:                        '<ul>'.$errors.'</ul></p>';
                   8265:     }
1.3       raeburn  8266:     return $resulttext;
1.1       raeburn  8267: }
                   8268: 
1.216     raeburn  8269: sub process_textbook_image {
1.242     raeburn  8270:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
1.216     raeburn  8271:     my $filename = $env{'form.'.$caller.'.filename'};
                   8272:     my ($error,$url);
                   8273:     my ($width,$height) = (50,50);
                   8274:     if ($configuserok eq 'ok') {
                   8275:         if ($switchserver) {
                   8276:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
                   8277:                          $switchserver);
                   8278:         } elsif ($author_ok eq 'ok') {
                   8279:             my ($result,$imageurl) =
                   8280:                 &publishlogo($r,'upload',$caller,$dom,$confname,
1.242     raeburn  8281:                              "$type/$dom/$cnum/cover",$width,$height);
1.216     raeburn  8282:             if ($result eq 'ok') {
                   8283:                 $url = $imageurl;
                   8284:             } else {
                   8285:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
                   8286:             }
                   8287:         } else {
                   8288:             $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);
                   8289:         }
                   8290:     } else {
                   8291:         $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);
                   8292:     }
                   8293:     return ($url,$error);
                   8294: }
                   8295: 
1.267   ! raeburn  8296: sub modify_ltitools {
        !          8297:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
        !          8298:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
        !          8299:     my ($newid,@allpos,%changes,%confhash,$errors,$resulttext);
        !          8300:     my $confname = $dom.'-domainconfig';
        !          8301:     my $servadm = $r->dir_config('lonAdmEMail');
        !          8302:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
        !          8303:     my (%posslti,%possfield);
        !          8304:     my @courseroles = ('cc','in','ta','ep','st');
        !          8305:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
        !          8306:     map { $posslti{$_} = 1; } @ltiroles;
        !          8307:     my @allfields = ('fullname','firstname','lastname','email','user','roles');
        !          8308:     map { $possfield{$_} = 1; } @allfields;
        !          8309:     my %lt = &ltitools_names(); 
        !          8310:     if ($env{'form.ltitools_add'}) {
        !          8311:         my $title = $env{'form.ltitools_add_title'};
        !          8312:         $title =~ s/(`)/'/g;
        !          8313:         ($newid,my $error) = &get_ltitools_id($dom,$title);
        !          8314:         if ($newid) {
        !          8315:             my $position = $env{'form.ltitools_add_pos'};
        !          8316:             $position =~ s/\D+//g;
        !          8317:             if ($position ne '') {
        !          8318:                 $allpos[$position] = $newid;
        !          8319:             }
        !          8320:             $changes{$newid} = 1;
        !          8321:             foreach my $item ('title','url','key','secret') {
        !          8322:                 $env{'form.ltitools_add_'.$item} =~ s/(`)/'/g;
        !          8323:                 if ($env{'form.ltitools_add_'.$item}) {
        !          8324:                     $confhash{$newid}{$item} = $env{'form.ltitools_add_'.$item};
        !          8325:                 }
        !          8326:             }
        !          8327:             if ($env{'form.ltitools_add_version'} eq 'LTI-1p0') {
        !          8328:                 $confhash{$newid}{'version'} = $env{'form.ltitools_add_version'};
        !          8329:             }
        !          8330:             if ($env{'form.ltitools_add_msgtype'} eq 'basic-lti-launch-request') {
        !          8331:                 $confhash{$newid}{'msgtype'} = $env{'form.ltitools_add_msgtype'};
        !          8332:             }
        !          8333:             foreach my $item ('width','height') {
        !          8334:                 $env{'form.ltitools_add_'.$item} =~ s/^\s+//;
        !          8335:                 $env{'form.ltitools_add_'.$item} =~ s/\s+$//;
        !          8336:                 if ($env{'form.ltitools_add_'.$item} =~ /^\d+$/) {
        !          8337:                     $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
        !          8338:                 }
        !          8339:             }
        !          8340:             if ($env{'form.ltitools_add_target'} eq 'window') {
        !          8341:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
        !          8342:             } else {
        !          8343:                 $confhash{$newid}{'display'}{'target'} = 'iframe';
        !          8344:             }
        !          8345:             foreach my $item ('passback','roster') {
        !          8346:                 if ($env{'form.ltitools_add_'.$item}) {
        !          8347:                     $confhash{$newid}{$item} = 1;
        !          8348:                 }
        !          8349:             }
        !          8350:             if ($env{'form.ltitools_add_image.filename'} ne '') {
        !          8351:                 my ($imageurl,$error) =
        !          8352:                     &process_ltitools_image($r,$dom,$confname,'ltitools_add_image',$dom,
        !          8353:                                             $configuserok,$switchserver,$author_ok);
        !          8354:                 if ($imageurl) {
        !          8355:                     $confhash{$newid}{'image'} = $imageurl;
        !          8356:                 }
        !          8357:                 if ($error) {
        !          8358:                     &Apache::lonnet::logthis($error);
        !          8359:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
        !          8360:                 }
        !          8361:             }
        !          8362:             my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_add_fields');
        !          8363:             foreach my $field (@fields) {
        !          8364:                 if ($possfield{$field}) {
        !          8365:                     if ($field eq 'roles') {
        !          8366:                         foreach my $role (@courseroles) {
        !          8367:                             my $choice = $env{'form.ltitools_add_roles_'.$role};
        !          8368:                             if (($choice ne '') && ($posslti{$choice})) {
        !          8369:                                 $confhash{$newid}{'roles'}{$role} = $choice;
        !          8370:                                 if ($role eq 'cc') {
        !          8371:                                     $confhash{$newid}{'roles'}{'co'} = $choice; 
        !          8372:                                 }
        !          8373:                             }
        !          8374:                         }
        !          8375:                     } else {
        !          8376:                         $confhash{$newid}{'fields'}{$field} = 1;
        !          8377:                     }
        !          8378:                 }
        !          8379:             }
        !          8380:             if ($env{'form.ltitools_add_custom'}) {
        !          8381:                 my $name = $env{'form.ltitools_add_custom_name'};
        !          8382:                 my $value = $env{'form.ltitools_add_custom_value'};
        !          8383:                 $value =~ s/(`)/'/g;
        !          8384:                 $name =~ s/(`)/'/g;
        !          8385:                 $confhash{$newid}{'custom'}{$name} = $value;
        !          8386:             }
        !          8387:         } else {
        !          8388:             my $error = &mt('Failed to acquire unique ID for new external tool');   
        !          8389:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
        !          8390:         }
        !          8391:     }
        !          8392:     if (ref($domconfig{$action}) eq 'HASH') {
        !          8393:         my %deletions;
        !          8394:         my @todelete = &Apache::loncommon::get_env_multiple('form.ltitools_del');
        !          8395:         if (@todelete) {
        !          8396:             map { $deletions{$_} = 1; } @todelete;
        !          8397:         }
        !          8398:         my %customadds;
        !          8399:         my @newcustom = &Apache::loncommon::get_env_multiple('form.ltitools_customadd');
        !          8400:         if (@newcustom) {
        !          8401:             map { $customadds{$_} = 1; } @newcustom;
        !          8402:         } 
        !          8403:         my %imgdeletions;
        !          8404:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.ltitools_image_del');
        !          8405:         if (@todeleteimages) {
        !          8406:             map { $imgdeletions{$_} = 1; } @todeleteimages;
        !          8407:         }
        !          8408:         my $maxnum = $env{'form.ltitools_maxnum'};
        !          8409:         for (my $i=0; $i<=$maxnum; $i++) {
        !          8410:             my $itemid = $env{'form.ltitools_id_'.$i};
        !          8411:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
        !          8412:                 if ($deletions{$itemid}) {
        !          8413:                     if ($domconfig{$action}{$itemid}{'image'}) {
        !          8414:                         #FIXME need to obsolete item in RES space
        !          8415:                     }
        !          8416:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'title'};
        !          8417:                     next;
        !          8418:                 } else {
        !          8419:                     my $newpos = $env{'form.ltitools_'.$itemid};
        !          8420:                     $newpos =~ s/\D+//g;
        !          8421:                     foreach my $item ('title','url','key','secret') {
        !          8422:                         $confhash{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
        !          8423:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
        !          8424:                             $changes{$itemid} = 1;
        !          8425:                         }
        !          8426:                     }
        !          8427:                     if ($env{'form.ltitools_version_'.$i} eq 'LTI-1p0') {
        !          8428:                         $confhash{$itemid}{'version'} = $env{'form.ltitools_version_'.$i};
        !          8429:                     }
        !          8430:                     if ($env{'form.ltitools_msgtype_'.$i} eq 'basic-lti-launch-request') {
        !          8431:                         $confhash{$itemid}{'msgtype'} = $env{'form.ltitools_msgtype_'.$i};
        !          8432:                     }
        !          8433:                     foreach my $size ('width','height') {
        !          8434:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/^\s+//;
        !          8435:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/\s+$//;
        !          8436:                         if ($env{'form.ltitools_'.$size.'_'.$i} =~ /^\d+$/) {
        !          8437:                             $confhash{$itemid}{'display'}{$size} = $env{'form.ltitools_'.$size.'_'.$i};
        !          8438:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
        !          8439:                                 if ($domconfig{$action}{$itemid}{'display'}{$size} ne $confhash{$itemid}{'display'}{$size}) {
        !          8440:                                     $changes{$itemid} = 1;
        !          8441:                                 }
        !          8442:                             } else {
        !          8443:                                 $changes{$itemid} = 1;
        !          8444:                             }
        !          8445:                         }
        !          8446:                     }
        !          8447:                     if ($env{'form.ltitools_target_'.$i} eq 'window') {
        !          8448:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
        !          8449:                     } else {
        !          8450:                         $confhash{$itemid}{'display'}{'target'} = 'iframe';
        !          8451:                     }
        !          8452:                     if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
        !          8453:                         if ($domconfig{$action}{$itemid}{'display'}{'target'} ne $confhash{$itemid}{'display'}{'target'}) {
        !          8454:                             $changes{$itemid} = 1;
        !          8455:                         }
        !          8456:                     } else {
        !          8457:                         $changes{$itemid} = 1;
        !          8458:                     }
        !          8459:                     foreach my $extra ('passback','roster') {
        !          8460:                         if ($env{'form.ltitools_'.$extra.'_'.$i}) {
        !          8461:                             $confhash{$itemid}{$extra} = 1;
        !          8462:                         }
        !          8463:                         if ($domconfig{$action}{$itemid}{$extra} ne $confhash{$itemid}{$extra}) {
        !          8464:                             $changes{$itemid} = 1;
        !          8465:                         }
        !          8466:                     }
        !          8467:                     my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_fields_'.$i);
        !          8468:                     foreach my $field (@fields) {
        !          8469:                         if ($possfield{$field}) {
        !          8470:                             if ($field eq 'roles') {
        !          8471:                                 foreach my $role (@courseroles) {
        !          8472:                                     my $choice = $env{'form.ltitools_roles_'.$role.'_'.$i};
        !          8473:                                     if (($choice ne '') && ($posslti{$choice})) {
        !          8474:                                         $confhash{$itemid}{'roles'}{$role} = $choice;
        !          8475:                                         if ($role eq 'cc') {
        !          8476:                                             $confhash{$itemid}{'roles'}{'co'} = $choice;
        !          8477:                                         }
        !          8478:                                     }
        !          8479:                                     if (ref($domconfig{$action}{$itemid}{'roles'}) eq 'HASH') {
        !          8480:                                         if ($domconfig{$action}{$itemid}{'roles'}{$role} ne $confhash{$itemid}{'roles'}{$role}) {
        !          8481:                                             $changes{$itemid} = 1;
        !          8482:                                         }
        !          8483:                                     } elsif ($confhash{$itemid}{'roles'}{$role}) {
        !          8484:                                         $changes{$itemid} = 1;
        !          8485:                                     }
        !          8486:                                 }
        !          8487:                             } else {
        !          8488:                                 $confhash{$itemid}{'fields'}{$field} = 1;
        !          8489:                                 if (ref($domconfig{$action}{$itemid}{'fields'}) eq 'HASH') {
        !          8490:                                     if ($domconfig{$action}{$itemid}{'fields'}{$field} ne $confhash{$itemid}{'fields'}{$field}) {
        !          8491:                                         $changes{$itemid} = 1;
        !          8492:                                     }
        !          8493:                                 } else {
        !          8494:                                     $changes{$itemid} = 1;
        !          8495:                                 }
        !          8496:                             }
        !          8497:                         }
        !          8498:                     }
        !          8499:                     $allpos[$newpos] = $itemid;
        !          8500:                 }
        !          8501:                 if ($imgdeletions{$itemid}) {
        !          8502:                     $changes{$itemid} = 1;
        !          8503:                     #FIXME need to obsolete item in RES space
        !          8504:                 } elsif ($env{'form.ltitools_image_'.$i.'.filename'}) {
        !          8505:                     my ($imgurl,$error) = &process_ltitools_image($r,$dom,$confname,'ltitools_image_'.$i,
        !          8506:                                                                  $itemid,$configuserok,$switchserver,
        !          8507:                                                                  $author_ok);
        !          8508:                     if ($imgurl) {
        !          8509:                         $confhash{$itemid}{'image'} = $imgurl;
        !          8510:                         $changes{$itemid} = 1;
        !          8511:                     }
        !          8512:                     if ($error) {
        !          8513:                         &Apache::lonnet::logthis($error);
        !          8514:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
        !          8515:                     }
        !          8516:                 } elsif ($domconfig{$action}{$itemid}{'image'}) {
        !          8517:                     $confhash{$itemid}{'image'} =
        !          8518:                        $domconfig{$action}{$itemid}{'image'};
        !          8519:                 }
        !          8520:                 if ($customadds{$i}) {
        !          8521:                     my $name = $env{'form.ltitools_custom_name_'.$i};
        !          8522:                     $name =~ s/(`)/'/g;
        !          8523:                     $name =~ s/^\s+//;
        !          8524:                     $name =~ s/\s+$//;
        !          8525:                     my $value = $env{'form.ltitools_custom_value_'.$i};
        !          8526:                     $value =~ s/(`)/'/g;
        !          8527:                     $value =~ s/^\s+//;
        !          8528:                     $value =~ s/\s+$//;
        !          8529:                     if ($name ne '') {
        !          8530:                         $confhash{$itemid}{'custom'}{$name} = $value;
        !          8531:                         $changes{$itemid} = 1;
        !          8532:                     }
        !          8533:                 }
        !          8534:                 my %customdels;
        !          8535:                 my @customdeletions = &Apache::loncommon::get_env_multiple('form.ltitools_customdel_'.$i); 
        !          8536:                 if (@customdeletions) {
        !          8537:                     $changes{$itemid} = 1;
        !          8538:                 }
        !          8539:                 map { $customdels{$_} = 1; } @customdeletions;
        !          8540:                 if (ref($domconfig{$action}{$itemid}{'custom'}) eq 'HASH') {
        !          8541:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}{'custom'}})) {
        !          8542:                         unless ($customdels{$key}) {
        !          8543:                             if ($env{'form.ltitools_customval_'.$key.'_'.$i} ne '') {
        !          8544:                                 $confhash{$itemid}{'custom'}{$key} = $env{'form.ltitools_customval_'.$key.'_'.$i}; 
        !          8545:                             }
        !          8546:                             if ($domconfig{$action}{$itemid}{'custom'}{$key} ne $env{'form.ltitools_customval_'.$key.'_'.$i}) {
        !          8547:                                 $changes{$itemid} = 1;
        !          8548:                             }
        !          8549:                         }
        !          8550:                     }
        !          8551:                 }
        !          8552:                 unless ($changes{$itemid}) {
        !          8553:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}})) {
        !          8554:                         if (ref($domconfig{$action}{$itemid}{$key}) eq 'HASH') {
        !          8555:                             if (ref($confhash{$itemid}{$key}) eq 'HASH') {
        !          8556:                                 foreach my $innerkey (keys(%{$domconfig{$action}{$itemid}{$key}})) {
        !          8557:                                     unless (exists($confhash{$itemid}{$key}{$innerkey})) {
        !          8558:                                         $changes{$itemid} = 1;
        !          8559:                                         last;
        !          8560:                                     }
        !          8561:                                 }
        !          8562:                             } elsif (keys(%{$domconfig{$action}{$itemid}{$key}}) > 0) {
        !          8563:                                 $changes{$itemid} = 1;
        !          8564:                             }
        !          8565:                         }
        !          8566:                         last if ($changes{$itemid});
        !          8567:                     }
        !          8568:                 }
        !          8569:             }
        !          8570:         }
        !          8571:     }
        !          8572:     if (@allpos > 0) {
        !          8573:         my $idx = 0;
        !          8574:         foreach my $itemid (@allpos) {
        !          8575:             if ($itemid ne '') {
        !          8576:                 $confhash{$itemid}{'order'} = $idx;
        !          8577:                 if (ref($domconfig{$action}) eq 'HASH') {
        !          8578:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
        !          8579:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
        !          8580:                             $changes{$itemid} = 1;
        !          8581:                         }
        !          8582:                     }
        !          8583:                 }
        !          8584:                 $idx ++;
        !          8585:             }
        !          8586:         }
        !          8587:     }
        !          8588:     my %ltitoolshash = (
        !          8589:                           $action => { %confhash }
        !          8590:                        );
        !          8591:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,
        !          8592:                                              $dom);
        !          8593:     if ($putresult eq 'ok') {
        !          8594:         if (keys(%changes) > 0) {
        !          8595:             my $cachetime = 24*60*60;
        !          8596:             &Apache::lonnet::do_cache_new('ltitools',$dom,\%confhash,$cachetime);
        !          8597:             if (ref($lastactref) eq 'HASH') {
        !          8598:                 $lastactref->{'ltitools'} = 1;
        !          8599:             }
        !          8600:             $resulttext = &mt('Changes made:').'<ul>';
        !          8601:             my %bynum;
        !          8602:             foreach my $itemid (sort(keys(%changes))) {
        !          8603:                 my $position = $confhash{$itemid}{'order'};
        !          8604:                 $bynum{$position} = $itemid;
        !          8605:             }
        !          8606:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
        !          8607:                 my $itemid = $bynum{$pos}; 
        !          8608:                 if (ref($confhash{$itemid}) ne 'HASH') {
        !          8609:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
        !          8610:                 } else {
        !          8611:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'title'}.'</b>';
        !          8612:                     if ($confhash{$itemid}{'image'}) {
        !          8613:                         $resulttext .= '&nbsp;'.
        !          8614:                                        '<img src="'.$confhash{$itemid}{'image'}.'"'.
        !          8615:                                        ' alt="'.&mt('Tool Provider icon').'" />';
        !          8616:                     }
        !          8617:                     $resulttext .= '</li><ul>';
        !          8618:                     my $position = $pos + 1;
        !          8619:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
        !          8620:                     foreach my $item ('version','msgtype','url','key') {
        !          8621:                         if ($confhash{$itemid}{$item} ne '') {
        !          8622:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
        !          8623:                         }
        !          8624:                     }
        !          8625:                     if ($confhash{$itemid}{'secret'} ne '') {
        !          8626:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
        !          8627:                         my $num = length($confhash{$itemid}{'secret'});
        !          8628:                         $resulttext .= ('*'x$num).'</li>';
        !          8629:                     }
        !          8630:                     foreach my $item ('passback','roster') {
        !          8631:                         $resulttext .= '<li>'.$lt{$item}.'&nbsp;';
        !          8632:                         if ($confhash{$itemid}{$item}) {
        !          8633:                             $resulttext .= &mt('Yes');
        !          8634:                         } else {
        !          8635:                             $resulttext .= &mt('No');
        !          8636:                         }
        !          8637:                         $resulttext .= '</li>';
        !          8638:                     }
        !          8639:                     if (ref($confhash{$itemid}{'display'}) eq 'HASH') {
        !          8640:                         my $displaylist;
        !          8641:                         if ($confhash{$itemid}{'display'}{'target'}) {
        !          8642:                             $displaylist = &mt('Display target').':&nbsp;'.
        !          8643:                                            $confhash{$itemid}{'display'}{'target'}.',';
        !          8644:                         }
        !          8645:                         foreach my $size ('width','height') { 
        !          8646:                             if ($confhash{$itemid}{'display'}{$size}) {
        !          8647:                                 $displaylist .= ('&nbsp;'x2).$lt{$size}.':&nbsp;'.
        !          8648:                                                 $confhash{$itemid}{'display'}{$size}.',';
        !          8649:                             }
        !          8650:                         }
        !          8651:                         if ($displaylist) {
        !          8652:                             $displaylist =~ s/,$//;
        !          8653:                             $resulttext .= '<li>'.$displaylist.'</li>';
        !          8654:                         }
        !          8655:                     } 
        !          8656:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
        !          8657:                         my $fieldlist;
        !          8658:                         foreach my $field (@allfields) {
        !          8659:                             if ($confhash{$itemid}{'fields'}{$field}) {
        !          8660:                                 $fieldlist .= ('&nbsp;'x2).$lt{$field}.',';
        !          8661:                             }
        !          8662:                         }
        !          8663:                         if ($fieldlist) {
        !          8664:                             $fieldlist =~ s/,$//;
        !          8665:                             $resulttext .= '<li>'.&mt('Data sent').':'.$fieldlist.'</li>';
        !          8666:                         }
        !          8667:                     }
        !          8668:                     if (ref($confhash{$itemid}{'roles'}) eq 'HASH') {
        !          8669:                         my $rolemaps;
        !          8670:                         foreach my $role (@courseroles) {
        !          8671:                             if ($confhash{$itemid}{'roles'}{$role}) {
        !          8672:                                 $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
        !          8673:                                              $confhash{$itemid}{'roles'}{$role}.',';
        !          8674:                             }
        !          8675:                         }
        !          8676:                         if ($rolemaps) {
        !          8677:                             $rolemaps =~ s/,$//; 
        !          8678:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
        !          8679:                         }
        !          8680:                     }
        !          8681:                     if (ref($confhash{$itemid}{'custom'}) eq 'HASH') {
        !          8682:                         my $customlist;
        !          8683:                         if (keys(%{$confhash{$itemid}{'custom'}})) {
        !          8684:                             foreach my $key (sort(keys(%{$confhash{$itemid}{'custom'}}))) {
        !          8685:                                 $customlist .= $key.':'.$confhash{$itemid}{'custom'}{$key}.('&nbsp;'x2);
        !          8686:                             } 
        !          8687:                         }
        !          8688:                         if ($customlist) {
        !          8689:                             $resulttext .= '<li>'.&mt('Custom items').':'.$customlist.'</li>';
        !          8690:                         }
        !          8691:                     } 
        !          8692:                     $resulttext .= '</ul></li>';
        !          8693:                 }
        !          8694:             }
        !          8695:             $resulttext .= '</ul>';
        !          8696:         } else {
        !          8697:             $resulttext = &mt('No changes made.');
        !          8698:         }
        !          8699:     } else {
        !          8700:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
        !          8701:     }
        !          8702:     if ($errors) {
        !          8703:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
        !          8704:                        $errors.'</ul>';
        !          8705:     }
        !          8706:     return $resulttext;
        !          8707: }
        !          8708: 
        !          8709: sub process_ltitools_image {
        !          8710:     my ($r,$dom,$confname,$caller,$itemid,$configuserok,$switchserver,$author_ok) = @_;
        !          8711:     my $filename = $env{'form.'.$caller.'.filename'};
        !          8712:     my ($error,$url);
        !          8713:     my ($width,$height) = (21,21);
        !          8714:     if ($configuserok eq 'ok') {
        !          8715:         if ($switchserver) {
        !          8716:             $error = &mt('Upload of Tool Provider (LTI) icon is not permitted to this server: [_1]',
        !          8717:                          $switchserver);
        !          8718:         } elsif ($author_ok eq 'ok') {
        !          8719:             my ($result,$imageurl,$madethumb) =
        !          8720:                 &publishlogo($r,'upload',$caller,$dom,$confname,
        !          8721:                              "ltitools/$itemid/icon",$width,$height);
        !          8722:             if ($result eq 'ok') {
        !          8723:                 if ($madethumb) {
        !          8724:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
        !          8725:                     my $imagethumb = "$path/tn-".$imagefile;
        !          8726:                     $url = $imagethumb;
        !          8727:                 } else {
        !          8728:                     $url = $imageurl;
        !          8729:                 }
        !          8730:             } else {
        !          8731:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
        !          8732:             }
        !          8733:         } else {
        !          8734:             $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);
        !          8735:         }
        !          8736:     } else {
        !          8737:         $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);
        !          8738:     }
        !          8739:     return ($url,$error);
        !          8740: }
        !          8741: 
        !          8742: sub get_ltitools_id {
        !          8743:     my ($cdom,$title) = @_;
        !          8744:     # get lock on ltitools db
        !          8745:     my $lockhash = {
        !          8746:                       lock => $env{'user.name'}.
        !          8747:                               ':'.$env{'user.domain'},
        !          8748:                    };
        !          8749:     my $tries = 0;
        !          8750:     my $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
        !          8751:     my ($id,$error);
        !          8752:  
        !          8753:     while (($gotlock ne 'ok') && ($tries<10)) {
        !          8754:         $tries ++;
        !          8755:         sleep (0.1);
        !          8756:         $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
        !          8757:     }
        !          8758:     if ($gotlock eq 'ok') {
        !          8759:         my %currids = &Apache::lonnet::dump_dom('ltitools',$cdom);
        !          8760:         if ($currids{'lock'}) {
        !          8761:             delete($currids{'lock'});
        !          8762:             if (keys(%currids)) {
        !          8763:                 my @curr = sort { $a <=> $b } keys(%currids);
        !          8764:                 if ($curr[-1] =~ /^\d+$/) {
        !          8765:                     $id = 1 + $curr[-1];
        !          8766:                 }
        !          8767:             } else {
        !          8768:                 $id = 1;
        !          8769:             }
        !          8770:             if ($id) {
        !          8771:                 unless (&Apache::lonnet::newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
        !          8772:                     $error = 'nostore';
        !          8773:                 }
        !          8774:             } else {
        !          8775:                 $error = 'nonumber';
        !          8776:             }
        !          8777:         }
        !          8778:         my $dellockoutcome = &Apache::lonnet::del_dom('ltitools',['lock'],$cdom);
        !          8779:     } else {
        !          8780:         $error = 'nolock';
        !          8781:     }
        !          8782:     return ($id,$error);
        !          8783: }
        !          8784: 
1.3       raeburn  8785: sub modify_autoenroll {
1.205     raeburn  8786:     my ($dom,$lastactref,%domconfig) = @_;
1.1       raeburn  8787:     my ($resulttext,%changes);
                   8788:     my %currautoenroll;
                   8789:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
                   8790:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
                   8791:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
                   8792:         }
                   8793:     }
                   8794:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
                   8795:     my %title = ( run => 'Auto-enrollment active',
1.129     raeburn  8796:                   sender => 'Sender for notification messages',
                   8797:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
1.1       raeburn  8798:     my @offon = ('off','on');
1.17      raeburn  8799:     my $sender_uname = $env{'form.sender_uname'};
                   8800:     my $sender_domain = $env{'form.sender_domain'};
                   8801:     if ($sender_domain eq '') {
                   8802:         $sender_uname = '';
                   8803:     } elsif ($sender_uname eq '') {
                   8804:         $sender_domain = '';
                   8805:     }
1.129     raeburn  8806:     my $coowners = $env{'form.autoassign_coowners'};
1.1       raeburn  8807:     my %autoenrollhash =  (
1.129     raeburn  8808:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
                   8809:                                        'sender_uname' => $sender_uname,
                   8810:                                        'sender_domain' => $sender_domain,
                   8811:                                        'co-owners' => $coowners,
1.1       raeburn  8812:                                 }
                   8813:                      );
1.4       raeburn  8814:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
                   8815:                                              $dom);
1.1       raeburn  8816:     if ($putresult eq 'ok') {
                   8817:         if (exists($currautoenroll{'run'})) {
                   8818:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
                   8819:                  $changes{'run'} = 1;
                   8820:              }
                   8821:         } elsif ($autorun) {
                   8822:             if ($env{'form.autoenroll_run'} ne '1') {
1.23      raeburn  8823:                  $changes{'run'} = 1;
1.1       raeburn  8824:             }
                   8825:         }
1.17      raeburn  8826:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
1.1       raeburn  8827:             $changes{'sender'} = 1;
                   8828:         }
1.17      raeburn  8829:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
1.1       raeburn  8830:             $changes{'sender'} = 1;
                   8831:         }
1.129     raeburn  8832:         if ($currautoenroll{'co-owners'} ne '') {
                   8833:             if ($currautoenroll{'co-owners'} ne $coowners) {
                   8834:                 $changes{'coowners'} = 1;
                   8835:             }
                   8836:         } elsif ($coowners) {
                   8837:             $changes{'coowners'} = 1;
                   8838:         }      
1.1       raeburn  8839:         if (keys(%changes) > 0) {
                   8840:             $resulttext = &mt('Changes made:').'<ul>';
1.3       raeburn  8841:             if ($changes{'run'}) {
1.1       raeburn  8842:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
                   8843:             }
                   8844:             if ($changes{'sender'}) {
1.17      raeburn  8845:                 if ($sender_uname eq '' || $sender_domain eq '') {
                   8846:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
                   8847:                 } else {
                   8848:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
                   8849:                 }
1.1       raeburn  8850:             }
1.129     raeburn  8851:             if ($changes{'coowners'}) {
                   8852:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
                   8853:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
1.212     raeburn  8854:                 if (ref($lastactref) eq 'HASH') {
                   8855:                     $lastactref->{'domainconfig'} = 1;
                   8856:                 }
1.129     raeburn  8857:             }
1.1       raeburn  8858:             $resulttext .= '</ul>';
                   8859:         } else {
                   8860:             $resulttext = &mt('No changes made to auto-enrollment settings');
                   8861:         }
                   8862:     } else {
1.11      albertel 8863:         $resulttext = '<span class="LC_error">'.
                   8864: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
1.1       raeburn  8865:     }
1.3       raeburn  8866:     return $resulttext;
1.1       raeburn  8867: }
                   8868: 
                   8869: sub modify_autoupdate {
1.3       raeburn  8870:     my ($dom,%domconfig) = @_;
1.1       raeburn  8871:     my ($resulttext,%currautoupdate,%fields,%changes);
                   8872:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
                   8873:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
                   8874:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
                   8875:         }
                   8876:     }
                   8877:     my @offon = ('off','on');
                   8878:     my %title = &Apache::lonlocal::texthash (
                   8879:                    run => 'Auto-update:',
                   8880:                    classlists => 'Updates to user information in classlists?'
                   8881:                 );
1.44      raeburn  8882:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
1.1       raeburn  8883:     my %fieldtitles = &Apache::lonlocal::texthash (
                   8884:                         id => 'Student/Employee ID',
1.20      raeburn  8885:                         permanentemail => 'E-mail address',
1.1       raeburn  8886:                         lastname => 'Last Name',
                   8887:                         firstname => 'First Name',
                   8888:                         middlename => 'Middle Name',
1.132     raeburn  8889:                         generation => 'Generation',
1.1       raeburn  8890:                       );
1.142     raeburn  8891:     $othertitle = &mt('All users');
1.1       raeburn  8892:     if (keys(%{$usertypes}) >  0) {
1.26      raeburn  8893:         $othertitle = &mt('Other users');
1.1       raeburn  8894:     }
                   8895:     foreach my $key (keys(%env)) {
                   8896:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
1.132     raeburn  8897:             my ($usertype,$item) = ($1,$2);
                   8898:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
                   8899:                 if ($usertype eq 'default') {   
                   8900:                     push(@{$fields{$1}},$2);
                   8901:                 } elsif (ref($types) eq 'ARRAY') {
                   8902:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
                   8903:                         push(@{$fields{$1}},$2);
                   8904:                     }
                   8905:                 }
                   8906:             }
1.1       raeburn  8907:         }
                   8908:     }
1.131     raeburn  8909:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
                   8910:     @lockablenames = sort(@lockablenames);
                   8911:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
                   8912:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
                   8913:         if (@changed) {
                   8914:             $changes{'lockablenames'} = 1;
                   8915:         }
                   8916:     } else {
                   8917:         if (@lockablenames) {
                   8918:             $changes{'lockablenames'} = 1;
                   8919:         }
                   8920:     }
1.1       raeburn  8921:     my %updatehash = (
                   8922:                       autoupdate => { run => $env{'form.autoupdate_run'},
                   8923:                                       classlists => $env{'form.classlists'},
                   8924:                                       fields => {%fields},
1.131     raeburn  8925:                                       lockablenames => \@lockablenames,
1.1       raeburn  8926:                                     }
                   8927:                      );
                   8928:     foreach my $key (keys(%currautoupdate)) {
                   8929:         if (($key eq 'run') || ($key eq 'classlists')) {
                   8930:             if (exists($updatehash{autoupdate}{$key})) {
                   8931:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
                   8932:                     $changes{$key} = 1;
                   8933:                 }
                   8934:             }
                   8935:         } elsif ($key eq 'fields') {
                   8936:             if (ref($currautoupdate{$key}) eq 'HASH') {
1.26      raeburn  8937:                 foreach my $item (@{$types},'default') {
1.1       raeburn  8938:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
                   8939:                         my $change = 0;
                   8940:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
                   8941:                             if (!exists($fields{$item})) {
                   8942:                                 $change = 1;
1.132     raeburn  8943:                                 last;
1.1       raeburn  8944:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
1.26      raeburn  8945:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
1.1       raeburn  8946:                                     $change = 1;
1.132     raeburn  8947:                                     last;
1.1       raeburn  8948:                                 }
                   8949:                             }
                   8950:                         }
                   8951:                         if ($change) {
                   8952:                             push(@{$changes{$key}},$item);
                   8953:                         }
1.26      raeburn  8954:                     } 
1.1       raeburn  8955:                 }
                   8956:             }
1.131     raeburn  8957:         } elsif ($key eq 'lockablenames') {
                   8958:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
                   8959:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
                   8960:                 if (@changed) {
                   8961:                     $changes{'lockablenames'} = 1;
                   8962:                 }
                   8963:             } else {
                   8964:                 if (@lockablenames) {
                   8965:                     $changes{'lockablenames'} = 1;
                   8966:                 }
                   8967:             }
                   8968:         }
                   8969:     }
                   8970:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
                   8971:         if (@lockablenames) {
                   8972:             $changes{'lockablenames'} = 1;
1.1       raeburn  8973:         }
                   8974:     }
1.26      raeburn  8975:     foreach my $item (@{$types},'default') {
                   8976:         if (defined($fields{$item})) {
                   8977:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
1.132     raeburn  8978:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
                   8979:                     my $change = 0;
                   8980:                     if (ref($fields{$item}) eq 'ARRAY') {
                   8981:                         foreach my $type (@{$fields{$item}}) {
                   8982:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
                   8983:                                 $change = 1;
                   8984:                                 last;
                   8985:                             }
                   8986:                         }
                   8987:                     }
                   8988:                     if ($change) {
                   8989:                         push(@{$changes{'fields'}},$item);
                   8990:                     }
                   8991:                 } else {
1.26      raeburn  8992:                     push(@{$changes{'fields'}},$item);
                   8993:                 }
                   8994:             } else {
                   8995:                 push(@{$changes{'fields'}},$item);
1.1       raeburn  8996:             }
                   8997:         }
                   8998:     }
                   8999:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
                   9000:                                              $dom);
                   9001:     if ($putresult eq 'ok') {
                   9002:         if (keys(%changes) > 0) {
                   9003:             $resulttext = &mt('Changes made:').'<ul>';
                   9004:             foreach my $key (sort(keys(%changes))) {
1.131     raeburn  9005:                 if ($key eq 'lockablenames') {
                   9006:                     $resulttext .= '<li>';
                   9007:                     if (@lockablenames) {
                   9008:                         $usertypes->{'default'} = $othertitle;
                   9009:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
                   9010:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
                   9011:                     } else {
                   9012:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
                   9013:                     }
                   9014:                     $resulttext .= '</li>';
                   9015:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
1.1       raeburn  9016:                     foreach my $item (@{$changes{$key}}) {
                   9017:                         my @newvalues;
                   9018:                         foreach my $type (@{$fields{$item}}) {
                   9019:                             push(@newvalues,$fieldtitles{$type});
                   9020:                         }
1.3       raeburn  9021:                         my $newvaluestr;
                   9022:                         if (@newvalues > 0) {
                   9023:                             $newvaluestr = join(', ',@newvalues);
                   9024:                         } else {
                   9025:                             $newvaluestr = &mt('none');
1.6       raeburn  9026:                         }
1.1       raeburn  9027:                         if ($item eq 'default') {
1.26      raeburn  9028:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
1.1       raeburn  9029:                         } else {
1.26      raeburn  9030:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
1.1       raeburn  9031:                         }
                   9032:                     }
                   9033:                 } else {
                   9034:                     my $newvalue;
                   9035:                     if ($key eq 'run') {
                   9036:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
                   9037:                     } else {
                   9038:                         $newvalue = $offon[$env{'form.'.$key}];
1.3       raeburn  9039:                     }
1.1       raeburn  9040:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
                   9041:                 }
                   9042:             }
                   9043:             $resulttext .= '</ul>';
                   9044:         } else {
1.3       raeburn  9045:             $resulttext = &mt('No changes made to autoupdates');
1.1       raeburn  9046:         }
                   9047:     } else {
1.11      albertel 9048:         $resulttext = '<span class="LC_error">'.
                   9049: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
1.1       raeburn  9050:     }
1.3       raeburn  9051:     return $resulttext;
1.1       raeburn  9052: }
                   9053: 
1.125     raeburn  9054: sub modify_autocreate {
                   9055:     my ($dom,%domconfig) = @_;
                   9056:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
                   9057:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
                   9058:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
                   9059:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
                   9060:         }
                   9061:     }
                   9062:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
                   9063:                  req => 'Auto-creation of validated requests for official courses',
                   9064:                  xmldc => 'Identity of course creator of courses from XML files',
                   9065:                );
                   9066:     my @types = ('xml','req');
                   9067:     foreach my $item (@types) {
                   9068:         $newvals{$item} = $env{'form.autocreate_'.$item};
                   9069:         $newvals{$item} =~ s/\D//g;
                   9070:         $newvals{$item} = 0 if ($newvals{$item} eq '');
                   9071:     }
                   9072:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
                   9073:     my %domcoords = &get_active_dcs($dom);
                   9074:     unless (exists($domcoords{$newvals{'xmldc'}})) {
                   9075:         $newvals{'xmldc'} = '';
                   9076:     } 
                   9077:     %autocreatehash =  (
                   9078:                         autocreate => { xml => $newvals{'xml'},
                   9079:                                         req => $newvals{'req'},
                   9080:                                       }
                   9081:                        );
                   9082:     if ($newvals{'xmldc'} ne '') {
                   9083:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
                   9084:     }
                   9085:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
                   9086:                                              $dom);
                   9087:     if ($putresult eq 'ok') {
                   9088:         my @items = @types;
                   9089:         if ($newvals{'xml'}) {
                   9090:             push(@items,'xmldc');
                   9091:         }
                   9092:         foreach my $item (@items) {
                   9093:             if (exists($currautocreate{$item})) {
                   9094:                 if ($currautocreate{$item} ne $newvals{$item}) {
                   9095:                     $changes{$item} = 1;
                   9096:                 }
                   9097:             } elsif ($newvals{$item}) {
                   9098:                 $changes{$item} = 1;
                   9099:             }
                   9100:         }
                   9101:         if (keys(%changes) > 0) {
                   9102:             my @offon = ('off','on'); 
                   9103:             $resulttext = &mt('Changes made:').'<ul>';
                   9104:             foreach my $item (@types) {
                   9105:                 if ($changes{$item}) {
                   9106:                     my $newtxt = $offon[$newvals{$item}];
1.178     raeburn  9107:                     $resulttext .= '<li>'.
                   9108:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
                   9109:                                        '<b>','</b>').
                   9110:                                    '</li>';
1.125     raeburn  9111:                 }
                   9112:             }
                   9113:             if ($changes{'xmldc'}) {
                   9114:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
                   9115:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
1.178     raeburn  9116:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
1.125     raeburn  9117:             }
                   9118:             $resulttext .= '</ul>';
                   9119:         } else {
                   9120:             $resulttext = &mt('No changes made to auto-creation settings');
                   9121:         }
                   9122:     } else {
                   9123:         $resulttext = '<span class="LC_error">'.
                   9124:             &mt('An error occurred: [_1]',$putresult).'</span>';
                   9125:     }
                   9126:     return $resulttext;
                   9127: }
                   9128: 
1.23      raeburn  9129: sub modify_directorysrch {
                   9130:     my ($dom,%domconfig) = @_;
                   9131:     my ($resulttext,%changes);
                   9132:     my %currdirsrch;
                   9133:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
                   9134:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
                   9135:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
                   9136:         }
                   9137:     }
                   9138:     my %title = ( available => 'Directory search available',
1.24      raeburn  9139:                   localonly => 'Other domains can search',
1.23      raeburn  9140:                   searchby => 'Search types',
                   9141:                   searchtypes => 'Search latitude');
                   9142:     my @offon = ('off','on');
1.24      raeburn  9143:     my @otherdoms = ('Yes','No');
1.23      raeburn  9144: 
1.25      raeburn  9145:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
1.23      raeburn  9146:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
                   9147:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
                   9148: 
1.44      raeburn  9149:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
1.26      raeburn  9150:     if (keys(%{$usertypes}) == 0) {
                   9151:         @cansearch = ('default');
                   9152:     } else {
                   9153:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
                   9154:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
                   9155:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
                   9156:                     push(@{$changes{'cansearch'}},$type);
                   9157:                 }
1.23      raeburn  9158:             }
1.26      raeburn  9159:             foreach my $type (@cansearch) {
                   9160:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
                   9161:                     push(@{$changes{'cansearch'}},$type);
                   9162:                 }
1.23      raeburn  9163:             }
1.26      raeburn  9164:         } else {
                   9165:             push(@{$changes{'cansearch'}},@cansearch);
1.23      raeburn  9166:         }
                   9167:     }
                   9168: 
                   9169:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
                   9170:         foreach my $by (@{$currdirsrch{'searchby'}}) {
                   9171:             if (!grep(/^\Q$by\E$/,@searchby)) {
                   9172:                 push(@{$changes{'searchby'}},$by);
                   9173:             }
                   9174:         }
                   9175:         foreach my $by (@searchby) {
                   9176:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
                   9177:                 push(@{$changes{'searchby'}},$by);
                   9178:             }
                   9179:         }
                   9180:     } else {
                   9181:         push(@{$changes{'searchby'}},@searchby);
                   9182:     }
1.25      raeburn  9183: 
                   9184:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
                   9185:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
                   9186:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
                   9187:                 push(@{$changes{'searchtypes'}},$type);
                   9188:             }
                   9189:         }
                   9190:         foreach my $type (@searchtypes) {
                   9191:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
                   9192:                 push(@{$changes{'searchtypes'}},$type);
                   9193:             }
                   9194:         }
                   9195:     } else {
                   9196:         if (exists($currdirsrch{'searchtypes'})) {
                   9197:             foreach my $type (@searchtypes) {  
                   9198:                 if ($type ne $currdirsrch{'searchtypes'}) { 
                   9199:                     push(@{$changes{'searchtypes'}},$type);
                   9200:                 }
                   9201:             }
                   9202:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
                   9203:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
                   9204:             }   
                   9205:         } else {
                   9206:             push(@{$changes{'searchtypes'}},@searchtypes); 
                   9207:         }
                   9208:     }
                   9209: 
1.23      raeburn  9210:     my %dirsrch_hash =  (
                   9211:             directorysrch => { available => $env{'form.dirsrch_available'},
                   9212:                                cansearch => \@cansearch,
1.24      raeburn  9213:                                localonly => $env{'form.dirsrch_localonly'},
1.23      raeburn  9214:                                searchby => \@searchby,
1.25      raeburn  9215:                                searchtypes => \@searchtypes,
1.23      raeburn  9216:                              }
                   9217:             );
                   9218:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
                   9219:                                              $dom);
                   9220:     if ($putresult eq 'ok') {
                   9221:         if (exists($currdirsrch{'available'})) {
                   9222:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
                   9223:                  $changes{'available'} = 1;
                   9224:              }
                   9225:         } else {
                   9226:             if ($env{'form.dirsrch_available'} eq '1') {
                   9227:                 $changes{'available'} = 1;
                   9228:             }
                   9229:         }
1.24      raeburn  9230:         if (exists($currdirsrch{'localonly'})) {
                   9231:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
                   9232:                  $changes{'localonly'} = 1;
                   9233:              }
                   9234:         } else {
                   9235:             if ($env{'form.dirsrch_localonly'} eq '1') {
                   9236:                 $changes{'localonly'} = 1;
                   9237:             }
                   9238:         }
1.23      raeburn  9239:         if (keys(%changes) > 0) {
                   9240:             $resulttext = &mt('Changes made:').'<ul>';
                   9241:             if ($changes{'available'}) {
                   9242:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
                   9243:             }
1.24      raeburn  9244:             if ($changes{'localonly'}) {
                   9245:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
                   9246:             }
                   9247: 
1.23      raeburn  9248:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
                   9249:                 my $chgtext;
1.26      raeburn  9250:                 if (ref($usertypes) eq 'HASH') {
                   9251:                     if (keys(%{$usertypes}) > 0) {
                   9252:                         foreach my $type (@{$types}) {
                   9253:                             if (grep(/^\Q$type\E$/,@cansearch)) {
                   9254:                                 $chgtext .= $usertypes->{$type}.'; ';
                   9255:                             }
                   9256:                         }
                   9257:                         if (grep(/^default$/,@cansearch)) {
                   9258:                             $chgtext .= $othertitle;
                   9259:                         } else {
                   9260:                             $chgtext =~ s/\; $//;
                   9261:                         }
1.210     raeburn  9262:                         $resulttext .=
1.178     raeburn  9263:                             '<li>'.
                   9264:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
                   9265:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
                   9266:                             '</li>';
1.23      raeburn  9267:                     }
                   9268:                 }
                   9269:             }
                   9270:             if (ref($changes{'searchby'}) eq 'ARRAY') {
                   9271:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
                   9272:                 my $chgtext;
                   9273:                 foreach my $type (@{$titleorder}) {
                   9274:                     if (grep(/^\Q$type\E$/,@searchby)) {
                   9275:                         if (defined($searchtitles->{$type})) {
                   9276:                             $chgtext .= $searchtitles->{$type}.'; ';
                   9277:                         }
                   9278:                     }
                   9279:                 }
                   9280:                 $chgtext =~ s/\; $//;
                   9281:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
                   9282:             }
1.25      raeburn  9283:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
                   9284:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
                   9285:                 my $chgtext;
                   9286:                 foreach my $type (@{$srchtypeorder}) {
                   9287:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
                   9288:                         if (defined($srchtypes_desc->{$type})) {
                   9289:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
                   9290:                         }
                   9291:                     }
                   9292:                 }
                   9293:                 $chgtext =~ s/\; $//;
1.178     raeburn  9294:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
1.23      raeburn  9295:             }
                   9296:             $resulttext .= '</ul>';
                   9297:         } else {
                   9298:             $resulttext = &mt('No changes made to institution directory search settings');
                   9299:         }
                   9300:     } else {
                   9301:         $resulttext = '<span class="LC_error">'.
1.27      raeburn  9302:                       &mt('An error occurred: [_1]',$putresult).'</span>';
                   9303:     }
                   9304:     return $resulttext;
                   9305: }
                   9306: 
1.28      raeburn  9307: sub modify_contacts {
1.205     raeburn  9308:     my ($dom,$lastactref,%domconfig) = @_;
1.28      raeburn  9309:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
                   9310:     if (ref($domconfig{'contacts'}) eq 'HASH') {
                   9311:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
                   9312:             $currsetting{$key} = $domconfig{'contacts'}{$key};
                   9313:         }
                   9314:     }
1.134     raeburn  9315:     my (%others,%to,%bcc);
1.28      raeburn  9316:     my @contacts = ('supportemail','adminemail');
1.102     raeburn  9317:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
1.203     raeburn  9318:                     'requestsmail','updatesmail','idconflictsmail');
                   9319:     my @toggles = ('reporterrors','reportupdates');
1.28      raeburn  9320:     foreach my $type (@mailings) {
                   9321:         @{$newsetting{$type}} = 
                   9322:             &Apache::loncommon::get_env_multiple('form.'.$type);
                   9323:         foreach my $item (@contacts) {
                   9324:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
                   9325:                 $contacts_hash{contacts}{$type}{$item} = 1;
                   9326:             } else {
                   9327:                 $contacts_hash{contacts}{$type}{$item} = 0;
                   9328:             }
                   9329:         }  
                   9330:         $others{$type} = $env{'form.'.$type.'_others'};
                   9331:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
1.134     raeburn  9332:         if ($type eq 'helpdeskmail') {
                   9333:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
                   9334:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
                   9335:         }
1.28      raeburn  9336:     }
                   9337:     foreach my $item (@contacts) {
                   9338:         $to{$item} = $env{'form.'.$item};
                   9339:         $contacts_hash{'contacts'}{$item} = $to{$item};
                   9340:     }
1.203     raeburn  9341:     foreach my $item (@toggles) {
                   9342:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
                   9343:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
                   9344:         }
                   9345:     }
1.28      raeburn  9346:     if (keys(%currsetting) > 0) {
                   9347:         foreach my $item (@contacts) {
                   9348:             if ($to{$item} ne $currsetting{$item}) {
                   9349:                 $changes{$item} = 1;
                   9350:             }
                   9351:         }
                   9352:         foreach my $type (@mailings) {
                   9353:             foreach my $item (@contacts) {
                   9354:                 if (ref($currsetting{$type}) eq 'HASH') {
                   9355:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
                   9356:                         push(@{$changes{$type}},$item);
                   9357:                     }
                   9358:                 } else {
                   9359:                     push(@{$changes{$type}},@{$newsetting{$type}});
                   9360:                 }
                   9361:             }
                   9362:             if ($others{$type} ne $currsetting{$type}{'others'}) {
                   9363:                 push(@{$changes{$type}},'others');
                   9364:             }
1.134     raeburn  9365:             if ($type eq 'helpdeskmail') {   
                   9366:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
                   9367:                     push(@{$changes{$type}},'bcc'); 
                   9368:                 }
                   9369:             }
1.28      raeburn  9370:         }
                   9371:     } else {
                   9372:         my %default;
                   9373:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
                   9374:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
                   9375:         $default{'errormail'} = 'adminemail';
                   9376:         $default{'packagesmail'} = 'adminemail';
                   9377:         $default{'helpdeskmail'} = 'supportemail';
1.89      raeburn  9378:         $default{'lonstatusmail'} = 'adminemail';
1.102     raeburn  9379:         $default{'requestsmail'} = 'adminemail';
1.190     raeburn  9380:         $default{'updatesmail'} = 'adminemail';
1.28      raeburn  9381:         foreach my $item (@contacts) {
                   9382:            if ($to{$item} ne $default{$item}) {
                   9383:               $changes{$item} = 1;
1.203     raeburn  9384:            }
1.28      raeburn  9385:         }
                   9386:         foreach my $type (@mailings) {
                   9387:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
                   9388:                
                   9389:                 push(@{$changes{$type}},@{$newsetting{$type}});
                   9390:             }
                   9391:             if ($others{$type} ne '') {
                   9392:                 push(@{$changes{$type}},'others');
1.134     raeburn  9393:             }
                   9394:             if ($type eq 'helpdeskmail') {
                   9395:                 if ($bcc{$type} ne '') {
                   9396:                     push(@{$changes{$type}},'bcc');
                   9397:                 }
                   9398:             }
1.28      raeburn  9399:         }
                   9400:     }
1.203     raeburn  9401:     foreach my $item (@toggles) {
                   9402:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
                   9403:             $changes{$item} = 1;
                   9404:         } elsif ((!$env{'form.'.$item}) &&
                   9405:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
                   9406:             $changes{$item} = 1;
                   9407:         }
                   9408:     }
1.28      raeburn  9409:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
                   9410:                                              $dom);
                   9411:     if ($putresult eq 'ok') {
                   9412:         if (keys(%changes) > 0) {
1.205     raeburn  9413:             &Apache::loncommon::devalidate_domconfig_cache($dom);
1.212     raeburn  9414:             if (ref($lastactref) eq 'HASH') {
                   9415:                 $lastactref->{'domainconfig'} = 1;
                   9416:             }
1.28      raeburn  9417:             my ($titles,$short_titles)  = &contact_titles();
                   9418:             $resulttext = &mt('Changes made:').'<ul>';
                   9419:             foreach my $item (@contacts) {
                   9420:                 if ($changes{$item}) {
                   9421:                     $resulttext .= '<li>'.$titles->{$item}.
                   9422:                                     &mt(' set to: ').
                   9423:                                     '<span class="LC_cusr_emph">'.
                   9424:                                     $to{$item}.'</span></li>';
                   9425:                 }
                   9426:             }
                   9427:             foreach my $type (@mailings) {
                   9428:                 if (ref($changes{$type}) eq 'ARRAY') {
                   9429:                     $resulttext .= '<li>'.$titles->{$type}.': ';
                   9430:                     my @text;
                   9431:                     foreach my $item (@{$newsetting{$type}}) {
                   9432:                         push(@text,$short_titles->{$item});
                   9433:                     }
                   9434:                     if ($others{$type} ne '') {
                   9435:                         push(@text,$others{$type});
                   9436:                     }
                   9437:                     $resulttext .= '<span class="LC_cusr_emph">'.
1.134     raeburn  9438:                                    join(', ',@text).'</span>';
                   9439:                     if ($type eq 'helpdeskmail') {
                   9440:                         if ($bcc{$type} ne '') {
                   9441:                             $resulttext .= '&nbsp;'.&mt('with Bcc to').': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
                   9442:                         }
                   9443:                     }
                   9444:                     $resulttext .= '</li>';
1.28      raeburn  9445:                 }
                   9446:             }
1.203     raeburn  9447:             my @offon = ('off','on');
                   9448:             if ($changes{'reporterrors'}) {
                   9449:                 $resulttext .= '<li>'.
                   9450:                                &mt('E-mail error reports to [_1] set to "'.
                   9451:                                    $offon[$env{'form.reporterrors'}].'".',
                   9452:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
                   9453:                                        &mt('LON-CAPA core group - MSU'),600,500)).
                   9454:                                '</li>';
                   9455:             }
                   9456:             if ($changes{'reportupdates'}) {
                   9457:                 $resulttext .= '<li>'.
                   9458:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
                   9459:                                     $offon[$env{'form.reportupdates'}].'".',
                   9460:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
                   9461:                                         &mt('LON-CAPA core group - MSU'),600,500)).
                   9462:                                 '</li>';
                   9463:             }
1.28      raeburn  9464:             $resulttext .= '</ul>';
                   9465:         } else {
1.34      raeburn  9466:             $resulttext = &mt('No changes made to contact information');
1.28      raeburn  9467:         }
                   9468:     } else {
                   9469:         $resulttext = '<span class="LC_error">'.
                   9470:             &mt('An error occurred: [_1].',$putresult).'</span>';
                   9471:     }
                   9472:     return $resulttext;
                   9473: }
                   9474: 
                   9475: sub modify_usercreation {
1.27      raeburn  9476:     my ($dom,%domconfig) = @_;
1.224     raeburn  9477:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
1.43      raeburn  9478:     my $warningmsg;
1.27      raeburn  9479:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
                   9480:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
1.224     raeburn  9481:             if ($key eq 'cancreate') {
                   9482:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
                   9483:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
                   9484:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
                   9485:                             ($item eq 'captcha') || ($item eq 'recaptchakeys')) {
                   9486:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
                   9487:                         } else {
                   9488:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
                   9489:                         }
                   9490:                     }
                   9491:                 }
                   9492:             } elsif ($key eq 'email_rule') {
                   9493:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
                   9494:             } else {
                   9495:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
                   9496:             }
1.27      raeburn  9497:         }
                   9498:     }
                   9499:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
1.32      raeburn  9500:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
1.224     raeburn  9501:     my @contexts = ('author','course','requestcrs');
1.34      raeburn  9502:     foreach my $item(@contexts) {
1.224     raeburn  9503:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
1.93      raeburn  9504:     }
1.34      raeburn  9505:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
                   9506:         foreach my $item (@contexts) {
1.224     raeburn  9507:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
                   9508:                 push(@{$changes{'cancreate'}},$item);
1.50      raeburn  9509:             }
1.27      raeburn  9510:         }
1.34      raeburn  9511:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
                   9512:         foreach my $item (@contexts) {
1.43      raeburn  9513:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
1.34      raeburn  9514:                 if ($cancreate{$item} ne 'any') {
                   9515:                     push(@{$changes{'cancreate'}},$item);
                   9516:                 }
                   9517:             } else {
                   9518:                 if ($cancreate{$item} ne 'none') {
                   9519:                     push(@{$changes{'cancreate'}},$item);
                   9520:                 }
1.27      raeburn  9521:             }
                   9522:         }
                   9523:     } else {
1.43      raeburn  9524:         foreach my $item (@contexts)  {
1.34      raeburn  9525:             push(@{$changes{'cancreate'}},$item);
                   9526:         }
1.27      raeburn  9527:     }
1.34      raeburn  9528: 
1.27      raeburn  9529:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
                   9530:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
                   9531:             if (!grep(/^\Q$type\E$/,@username_rule)) {
                   9532:                 push(@{$changes{'username_rule'}},$type);
                   9533:             }
                   9534:         }
                   9535:         foreach my $type (@username_rule) {
                   9536:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
                   9537:                 push(@{$changes{'username_rule'}},$type);
                   9538:             }
                   9539:         }
                   9540:     } else {
                   9541:         push(@{$changes{'username_rule'}},@username_rule);
                   9542:     }
                   9543: 
1.32      raeburn  9544:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
                   9545:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
                   9546:             if (!grep(/^\Q$type\E$/,@id_rule)) {
                   9547:                 push(@{$changes{'id_rule'}},$type);
                   9548:             }
                   9549:         }
                   9550:         foreach my $type (@id_rule) {
                   9551:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
                   9552:                 push(@{$changes{'id_rule'}},$type);
                   9553:             }
                   9554:         }
                   9555:     } else {
                   9556:         push(@{$changes{'id_rule'}},@id_rule);
                   9557:     }
                   9558: 
1.43      raeburn  9559:     my @authen_contexts = ('author','course','domain');
1.28      raeburn  9560:     my @authtypes = ('int','krb4','krb5','loc');
                   9561:     my %authhash;
1.43      raeburn  9562:     foreach my $item (@authen_contexts) {
1.28      raeburn  9563:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
                   9564:         foreach my $auth (@authtypes) {
                   9565:             if (grep(/^\Q$auth\E$/,@authallowed)) {
                   9566:                 $authhash{$item}{$auth} = 1;
                   9567:             } else {
                   9568:                 $authhash{$item}{$auth} = 0;
                   9569:             }
                   9570:         }
                   9571:     }
                   9572:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
1.43      raeburn  9573:         foreach my $item (@authen_contexts) {
1.28      raeburn  9574:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
                   9575:                 foreach my $auth (@authtypes) {
                   9576:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
                   9577:                         push(@{$changes{'authtypes'}},$item);
                   9578:                         last;
                   9579:                     }
                   9580:                 }
                   9581:             }
                   9582:         }
                   9583:     } else {
1.43      raeburn  9584:         foreach my $item (@authen_contexts) {
1.28      raeburn  9585:             push(@{$changes{'authtypes'}},$item);
                   9586:         }
                   9587:     }
                   9588: 
1.224     raeburn  9589:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
                   9590:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
                   9591:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
                   9592:     $save_usercreate{'id_rule'} = \@id_rule;
                   9593:     $save_usercreate{'username_rule'} = \@username_rule,
                   9594:     $save_usercreate{'authtypes'} = \%authhash;
                   9595: 
1.27      raeburn  9596:     my %usercreation_hash =  (
1.224     raeburn  9597:         usercreation     => \%save_usercreate,
                   9598:     );
1.27      raeburn  9599: 
                   9600:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
                   9601:                                              $dom);
1.50      raeburn  9602: 
1.224     raeburn  9603:     if ($putresult eq 'ok') {
                   9604:         if (keys(%changes) > 0) {
                   9605:             $resulttext = &mt('Changes made:').'<ul>';
                   9606:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
                   9607:                 my %lt = &usercreation_types();
                   9608:                 foreach my $type (@{$changes{'cancreate'}}) {
                   9609:                     my $chgtext = $lt{$type}.', ';
                   9610:                     if ($cancreate{$type} eq 'none') {
                   9611:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
                   9612:                     } elsif ($cancreate{$type} eq 'any') {
                   9613:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
                   9614:                     } elsif ($cancreate{$type} eq 'official') {
                   9615:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
                   9616:                     } elsif ($cancreate{$type} eq 'unofficial') {
                   9617:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
                   9618:                     }
                   9619:                     $resulttext .= '<li>'.$chgtext.'</li>';
                   9620:                 }
                   9621:             }
                   9622:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
                   9623:                 my ($rules,$ruleorder) = 
                   9624:                     &Apache::lonnet::inst_userrules($dom,'username');
                   9625:                 my $chgtext = '<ul>';
                   9626:                 foreach my $type (@username_rule) {
                   9627:                     if (ref($rules->{$type}) eq 'HASH') {
                   9628:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
                   9629:                     }
                   9630:                 }
                   9631:                 $chgtext .= '</ul>';
                   9632:                 if (@username_rule > 0) {
                   9633:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
                   9634:                 } else {
                   9635:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
                   9636:                 }
                   9637:             }
                   9638:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
                   9639:                 my ($idrules,$idruleorder) = 
                   9640:                     &Apache::lonnet::inst_userrules($dom,'id');
                   9641:                 my $chgtext = '<ul>';
                   9642:                 foreach my $type (@id_rule) {
                   9643:                     if (ref($idrules->{$type}) eq 'HASH') {
                   9644:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
                   9645:                     }
                   9646:                 }
                   9647:                 $chgtext .= '</ul>';
                   9648:                 if (@id_rule > 0) {
                   9649:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
                   9650:                 } else {
                   9651:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
                   9652:                 }
                   9653:             }
                   9654:             my %authname = &authtype_names();
                   9655:             my %context_title = &context_names();
                   9656:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
                   9657:                 my $chgtext = '<ul>';
                   9658:                 foreach my $type (@{$changes{'authtypes'}}) {
                   9659:                     my @allowed;
                   9660:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
                   9661:                     foreach my $auth (@authtypes) {
                   9662:                         if ($authhash{$type}{$auth}) {
                   9663:                             push(@allowed,$authname{$auth});
                   9664:                         }
                   9665:                     }
                   9666:                     if (@allowed > 0) {
                   9667:                         $chgtext .= join(', ',@allowed).'</li>';
                   9668:                     } else {
                   9669:                         $chgtext .= &mt('none').'</li>';
                   9670:                     }
                   9671:                 }
                   9672:                 $chgtext .= '</ul>';
                   9673:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
                   9674:                 $resulttext .= '</li>';
                   9675:             }
                   9676:             $resulttext .= '</ul>';
                   9677:         } else {
                   9678:             $resulttext = &mt('No changes made to user creation settings');
                   9679:         }
                   9680:     } else {
                   9681:         $resulttext = '<span class="LC_error">'.
                   9682:             &mt('An error occurred: [_1]',$putresult).'</span>';
                   9683:     }
                   9684:     if ($warningmsg ne '') {
                   9685:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
                   9686:     }
                   9687:     return $resulttext;
                   9688: }
                   9689: 
                   9690: sub modify_selfcreation {
                   9691:     my ($dom,%domconfig) = @_;
                   9692:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
                   9693:     my (%save_usercreate,%save_usermodify);
1.228     raeburn  9694:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
                   9695:     if (ref($types) eq 'ARRAY') {
                   9696:         $usertypes->{'default'} = $othertitle;
                   9697:         push(@{$types},'default');
                   9698:     }
1.224     raeburn  9699: #
                   9700: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
                   9701: #
                   9702:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
                   9703:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
                   9704:             if ($key eq 'cancreate') {
                   9705:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
                   9706:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
                   9707:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
                   9708:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
1.236     raeburn  9709:                             ($item eq 'emailusername') || ($item eq 'notify') ||
1.240     raeburn  9710:                             ($item eq 'selfcreateprocessing') || ($item eq 'shibenv')) {
1.224     raeburn  9711:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
                   9712:                         } else {
                   9713:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
                   9714:                         }
                   9715:                     }
                   9716:                 }
                   9717:             } elsif ($key eq 'email_rule') {
                   9718:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
                   9719:             } else {
                   9720:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
                   9721:             }
                   9722:         }
                   9723:     }
                   9724: #
                   9725: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
                   9726: #
                   9727:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
                   9728:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
                   9729:             if ($key eq 'selfcreate') {
                   9730:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
                   9731:             } else {
                   9732:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
                   9733:             }
                   9734:         }
                   9735:     }
                   9736: 
                   9737:     my @contexts = ('selfcreate');
                   9738:     @{$cancreate{'selfcreate'}} = ();
                   9739:     %{$cancreate{'emailusername'}} = ();
                   9740:     @{$cancreate{'statustocreate'}} = ();
1.236     raeburn  9741:     %{$cancreate{'selfcreateprocessing'}} = ();
1.240     raeburn  9742:     %{$cancreate{'shibenv'}} = ();
1.50      raeburn  9743:     my %selfcreatetypes = (
                   9744:                              sso   => 'users authenticated by institutional single sign on',
                   9745:                              login => 'users authenticated by institutional log-in',
1.236     raeburn  9746:                              email => 'users who provide a valid e-mail address for use as username',
1.50      raeburn  9747:                           );
1.224     raeburn  9748: #
                   9749: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
                   9750: # is permitted.
                   9751: #
1.236     raeburn  9752: 
                   9753:     my @statuses;
                   9754:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
                   9755:         if (ref($domconfig{'inststatus'}{'inststatusguest'}) eq 'ARRAY') {
                   9756:             @statuses = @{$domconfig{'inststatus'}{'inststatusguest'}};
                   9757:         }
                   9758:     }
                   9759:     push(@statuses,'default');
                   9760: 
1.228     raeburn  9761:     foreach my $item ('login','sso','email') {
1.224     raeburn  9762:         if ($item eq 'email') {
1.236     raeburn  9763:             if ($env{'form.cancreate_email'}) {
1.224     raeburn  9764:                 push(@{$cancreate{'selfcreate'}},'email');
1.236     raeburn  9765:                 push(@contexts,'selfcreateprocessing');
                   9766:                 foreach my $type (@statuses) {
                   9767:                     if ($type eq 'default') {
                   9768:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess'};
                   9769:                     } else { 
                   9770:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
                   9771:                     }
                   9772:                 }
1.224     raeburn  9773:             }
                   9774:         } else {
                   9775:             if ($env{'form.cancreate_'.$item}) {
                   9776:                 push(@{$cancreate{'selfcreate'}},$item);
                   9777:             }
                   9778:         }
                   9779:     }
                   9780:     my (@email_rule,%userinfo,%savecaptcha);
                   9781:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
                   9782: #
1.228     raeburn  9783: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
                   9784: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
1.224     raeburn  9785: #
1.236     raeburn  9786: 
1.244     raeburn  9787:     if ($env{'form.cancreate_email'}) {
1.228     raeburn  9788:         push(@contexts,'emailusername');
                   9789:         if (ref($types) eq 'ARRAY') {
                   9790:             foreach my $type (@{$types}) {
                   9791:                 if (ref($infofields) eq 'ARRAY') {
                   9792:                     foreach my $field (@{$infofields}) {
                   9793:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
                   9794:                             $cancreate{'emailusername'}{$type}{$field} = $1;
                   9795:                         }
                   9796:                     }
1.224     raeburn  9797:                 }
                   9798:             }
                   9799:         }
                   9800: #
                   9801: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
                   9802: # queued requests for self-creation of account using e-mail address as username
                   9803: #
                   9804: 
                   9805:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
                   9806:         @approvalnotify = sort(@approvalnotify);
                   9807:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
                   9808:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
                   9809:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
                   9810:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
                   9811:                     push(@{$changes{'cancreate'}},'notify');
                   9812:                 }
                   9813:             } else {
                   9814:                 if ($cancreate{'notify'}{'approval'}) {
                   9815:                     push(@{$changes{'cancreate'}},'notify');
                   9816:                 }
                   9817:             }
                   9818:         } elsif ($cancreate{'notify'}{'approval'}) {
                   9819:             push(@{$changes{'cancreate'}},'notify');
                   9820:         }
                   9821: 
                   9822: #
                   9823: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
                   9824: #
                   9825:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
                   9826:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
                   9827:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
                   9828:             if (@{$curr_usercreation{'email_rule'}} > 0) {
                   9829:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
                   9830:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
                   9831:                         push(@{$changes{'email_rule'}},$type);
                   9832:                     }
                   9833:                 }
                   9834:             }
                   9835:             if (@email_rule > 0) {
                   9836:                 foreach my $type (@email_rule) {
                   9837:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
                   9838:                         push(@{$changes{'email_rule'}},$type);
                   9839:                     }
                   9840:                 }
                   9841:             }
                   9842:         } elsif (@email_rule > 0) {
                   9843:             push(@{$changes{'email_rule'}},@email_rule);
                   9844:         }
                   9845:     }
                   9846: #  
1.236     raeburn  9847: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
1.224     raeburn  9848: # institutional log-in.
                   9849: #
                   9850:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
                   9851:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
                   9852:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
                   9853:                ($domdefaults{'auth_def'} eq 'localauth'))) {
                   9854:             $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.').' '.
                   9855:                           &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.');
                   9856:         }
                   9857:     }
                   9858:     my @fields = ('lastname','firstname','middlename','generation',
                   9859:                   'permanentemail','id');
1.240     raeburn  9860:     my @shibfields = (@fields,'inststatus');
1.224     raeburn  9861:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
                   9862: #
                   9863: # Where usernames may created for institutional log-in and/or institutional single sign on:
                   9864: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
                   9865: # may self-create accounts 
                   9866: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
                   9867: # which the user may supply, if institutional data is unavailable.
                   9868: #
                   9869:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
                   9870:         if (ref($types) eq 'ARRAY') {
1.228     raeburn  9871:             if (@{$types} > 1) {
1.224     raeburn  9872:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
                   9873:                 push(@contexts,'statustocreate');
                   9874:             } else {
                   9875:                 undef($cancreate{'statustocreate'});
                   9876:             } 
                   9877:             foreach my $type (@{$types}) {
                   9878:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
                   9879:                 foreach my $field (@fields) {
                   9880:                     if (grep(/^\Q$field\E$/,@modifiable)) {
                   9881:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
                   9882:                     } else {
                   9883:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
                   9884:                     }
                   9885:                 }
                   9886:             }
                   9887:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
                   9888:                 foreach my $type (@{$types}) {
                   9889:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
                   9890:                         foreach my $field (@fields) {
                   9891:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
                   9892:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
                   9893:                                 push(@{$changes{'selfcreate'}},$type);
                   9894:                                 last;
                   9895:                             }
                   9896:                         }
                   9897:                     }
                   9898:                 }
                   9899:             } else {
                   9900:                 foreach my $type (@{$types}) {
                   9901:                     push(@{$changes{'selfcreate'}},$type);
                   9902:                 }
                   9903:             }
                   9904:         }
1.240     raeburn  9905:         foreach my $field (@shibfields) {
                   9906:             if ($env{'form.shibenv_'.$field} ne '') {
                   9907:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
                   9908:             }
                   9909:         }
                   9910:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
                   9911:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
                   9912:                 foreach my $field (@shibfields) {
                   9913:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
                   9914:                         push(@{$changes{'cancreate'}},'shibenv');
                   9915:                     }
                   9916:                 }
                   9917:             } else {
                   9918:                 foreach my $field (@shibfields) {
                   9919:                     if ($env{'form.shibenv_'.$field}) {
                   9920:                         push(@{$changes{'cancreate'}},'shibenv');
                   9921:                         last;
                   9922:                     }
                   9923:                 }
                   9924:             }
                   9925:         }
1.224     raeburn  9926:     }
                   9927:     foreach my $item (@contexts) {
                   9928:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
                   9929:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
                   9930:                 if (ref($cancreate{$item}) eq 'ARRAY') {
                   9931:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
                   9932:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
                   9933:                             push(@{$changes{'cancreate'}},$item);
                   9934:                         }
                   9935:                     }
                   9936:                 }
                   9937:             }
                   9938:             if (ref($cancreate{$item}) eq 'ARRAY') {
                   9939:                 foreach my $type (@{$cancreate{$item}}) {
                   9940:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
                   9941:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
                   9942:                             push(@{$changes{'cancreate'}},$item);
                   9943:                         }
                   9944:                     }
                   9945:                 }
                   9946:             }
                   9947:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
                   9948:             if (ref($cancreate{$item}) eq 'HASH') {
                   9949:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
1.228     raeburn  9950:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
                   9951:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
                   9952:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
                   9953:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
                   9954:                                     push(@{$changes{'cancreate'}},$item);
                   9955:                                 }
                   9956:                             }
                   9957:                         }
1.236     raeburn  9958:                     } elsif ($item eq 'selfcreateprocessing') {
                   9959:                         if ($cancreate{$item}{$curr} ne $curr_usercreation{'cancreate'}{$item}{$curr}) {
                   9960:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
                   9961:                                 push(@{$changes{'cancreate'}},$item);
                   9962:                             }
                   9963:                         }
1.228     raeburn  9964:                     } else {
                   9965:                         if (!$cancreate{$item}{$curr}) {
                   9966:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
                   9967:                                 push(@{$changes{'cancreate'}},$item);
                   9968:                             }
1.224     raeburn  9969:                         }
                   9970:                     }
                   9971:                 }
                   9972:                 foreach my $field (keys(%{$cancreate{$item}})) {
1.228     raeburn  9973:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
                   9974:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
                   9975:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
                   9976:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
                   9977:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
                   9978:                                         push(@{$changes{'cancreate'}},$item);
                   9979:                                     }
                   9980:                                 }
                   9981:                             } else {
                   9982:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
                   9983:                                     push(@{$changes{'cancreate'}},$item);
                   9984:                                 }
                   9985:                             }
                   9986:                         }
1.236     raeburn  9987:                     } elsif ($item eq 'selfcreateprocessing') {
                   9988:                         if ($cancreate{$item}{$field} ne $curr_usercreation{'cancreate'}{$item}{$field}) {
                   9989:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
                   9990:                                 push(@{$changes{'cancreate'}},$item);
                   9991:                             }
                   9992:                         }
1.228     raeburn  9993:                     } else {
                   9994:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
                   9995:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
                   9996:                                 push(@{$changes{'cancreate'}},$item);
                   9997:                             }
1.224     raeburn  9998:                         }
                   9999:                     }
                   10000:                 }
                   10001:             }
                   10002:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
                   10003:             if (ref($cancreate{$item}) eq 'ARRAY') {
                   10004:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
                   10005:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
                   10006:                         push(@{$changes{'cancreate'}},$item);
                   10007:                     }
                   10008:                 }
                   10009:             } elsif (ref($cancreate{$item}) eq 'HASH') {
                   10010:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
                   10011:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
                   10012:                         push(@{$changes{'cancreate'}},$item);
                   10013:                     }
                   10014:                 }
                   10015:             }
                   10016:         } elsif ($item eq 'emailusername') {
1.228     raeburn  10017:             if (ref($cancreate{$item}) eq 'HASH') {
                   10018:                 foreach my $type (keys(%{$cancreate{$item}})) {
                   10019:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
                   10020:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
                   10021:                             if ($cancreate{$item}{$type}{$field}) {
                   10022:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
                   10023:                                     push(@{$changes{'cancreate'}},$item);
                   10024:                                 }
                   10025:                                 last;
                   10026:                             }
                   10027:                         }
                   10028:                     }
                   10029:                 }
1.224     raeburn  10030:             }
                   10031:         }
                   10032:     }
                   10033: #
                   10034: # Populate %save_usercreate hash with updates to self-creation configuration.
                   10035: #
                   10036:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
                   10037:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
                   10038:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
                   10039:     if (ref($cancreate{'notify'}) eq 'HASH') {
                   10040:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
                   10041:     }
1.236     raeburn  10042:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
                   10043:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
                   10044:     }
1.224     raeburn  10045:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
                   10046:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
                   10047:     }
1.240     raeburn  10048:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
                   10049:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
                   10050:     }
1.224     raeburn  10051:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
                   10052:     $save_usercreate{'emailrule'} = \@email_rule;
                   10053: 
                   10054:     my %userconfig_hash = (
                   10055:             usercreation     => \%save_usercreate,
                   10056:             usermodification => \%save_usermodify,
                   10057:     );
                   10058:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
                   10059:                                              $dom);
                   10060: #
                   10061: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
                   10062: #
1.27      raeburn  10063:     if ($putresult eq 'ok') {
                   10064:         if (keys(%changes) > 0) {
                   10065:             $resulttext = &mt('Changes made:').'<ul>';
                   10066:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
1.224     raeburn  10067:                 my %lt = &selfcreation_types();
1.34      raeburn  10068:                 foreach my $type (@{$changes{'cancreate'}}) {
1.100     raeburn  10069:                     my $chgtext;
1.45      raeburn  10070:                     if ($type eq 'selfcreate') {
1.50      raeburn  10071:                         if (@{$cancreate{$type}} == 0) {
1.224     raeburn  10072:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
1.50      raeburn  10073:                         } else {
1.224     raeburn  10074:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
                   10075:                                         '<ul>';
1.50      raeburn  10076:                             foreach my $case (@{$cancreate{$type}}) {
                   10077:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
                   10078:                             }
                   10079:                             $chgtext .= '</ul>';
1.100     raeburn  10080:                             if (ref($cancreate{$type}) eq 'ARRAY') {
                   10081:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
                   10082:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
                   10083:                                         if (@{$cancreate{'statustocreate'}} == 0) {
1.224     raeburn  10084:                                             $chgtext .= '<br />'.
                   10085:                                                         '<span class="LC_warning">'.
                   10086:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
                   10087:                                                         '</span>';
1.100     raeburn  10088:                                         }
                   10089:                                     }
                   10090:                                 }
                   10091:                             }
1.43      raeburn  10092:                         }
1.240     raeburn  10093:                     } elsif ($type eq 'shibenv') {
                   10094:                         if (keys(%{$cancreate{$type}}) == 0) {
                   10095:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information'); 
                   10096:                         } else {
                   10097:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
                   10098:                                         '<ul>';
                   10099:                             foreach my $field (@shibfields) {
                   10100:                                 next if ($cancreate{$type}{$field} eq '');
                   10101:                                 if ($field eq 'inststatus') {
                   10102:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
                   10103:                                 } else {
                   10104:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
                   10105:                                 }
                   10106:                             }
                   10107:                             $chgtext .= '</ul>';
                   10108:                         }  
1.93      raeburn  10109:                     } elsif ($type eq 'statustocreate') {
1.96      raeburn  10110:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
                   10111:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
                   10112:                             if (@{$cancreate{'selfcreate'}} > 0) {
                   10113:                                 if (@{$cancreate{'statustocreate'}} == 0) {
1.100     raeburn  10114:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
1.96      raeburn  10115:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
1.224     raeburn  10116:                                         $chgtext .= '<br />'.
                   10117:                                                     '<span class="LC_warning">'.
                   10118:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
                   10119:                                                     '</span>';
                   10120:                                     }
1.96      raeburn  10121:                                 } elsif (ref($usertypes) eq 'HASH') {
                   10122:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
1.100     raeburn  10123:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
                   10124:                                     } else {
                   10125:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
                   10126:                                     }
                   10127:                                     $chgtext .= '<ul>';
                   10128:                                     foreach my $case (@{$cancreate{$type}}) {
                   10129:                                         if ($case eq 'default') {
                   10130:                                             $chgtext .= '<li>'.$othertitle.'</li>';
                   10131:                                         } else {
                   10132:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
1.93      raeburn  10133:                                         }
                   10134:                                     }
1.100     raeburn  10135:                                     $chgtext .= '</ul>';
                   10136:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
1.224     raeburn  10137:                                         $chgtext .= '<br /><span class="LC_warning">'.
                   10138:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
                   10139:                                                     '</span>';
1.100     raeburn  10140:                                     }
                   10141:                                 }
                   10142:                             } else {
                   10143:                                 if (@{$cancreate{$type}} == 0) {
                   10144:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
                   10145:                                 } else {
                   10146:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
1.93      raeburn  10147:                                 }
                   10148:                             }
                   10149:                         }
1.236     raeburn  10150:                     } elsif ($type eq 'selfcreateprocessing') {
                   10151:                         my %choices = &Apache::lonlocal::texthash (
                   10152:                                                                     automatic => 'Automatic approval',
                   10153:                                                                     approval  => 'Queued for approval',
                   10154:                                                                   );
                   10155:                         if (@statuses > 1) {
                   10156:                             $chgtext .= &mt('Processing of requests to create account with e-mail address as username set as follows:'). 
                   10157:                                         '<ul>';
                   10158:                            foreach my $type (@statuses) {
                   10159:                                if ($type eq 'default') {
                   10160:                                    $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
                   10161:                                } else {
                   10162:                                    $chgtext .= '<li>'.$usertypes->{$type}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
                   10163:                                }
                   10164:                            }
                   10165:                            $chgtext .= '</ul>';
                   10166:                         } else {
                   10167:                            $chgtext .= &mt('Processing of requests to create account with e-mail address as username set to: "[_1]"',
                   10168:                                          $choices{$cancreate{'selfcreateprocessing'}{'default'}});
                   10169:                         }
1.165     raeburn  10170:                     } elsif ($type eq 'captcha') {
1.224     raeburn  10171:                         if ($savecaptcha{$type} eq 'notused') {
1.165     raeburn  10172:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
                   10173:                         } else {
                   10174:                             my %captchas = &captcha_phrases();
1.224     raeburn  10175:                             if ($captchas{$savecaptcha{$type}}) {
                   10176:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
1.165     raeburn  10177:                             } else {
1.210     raeburn  10178:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
1.165     raeburn  10179:                             }
                   10180:                         }
                   10181:                     } elsif ($type eq 'recaptchakeys') {
                   10182:                         my ($privkey,$pubkey);
1.224     raeburn  10183:                         if (ref($savecaptcha{$type}) eq 'HASH') {
                   10184:                             $pubkey = $savecaptcha{$type}{'public'};
                   10185:                             $privkey = $savecaptcha{$type}{'private'};
1.165     raeburn  10186:                         }
                   10187:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
                   10188:                         if (!$pubkey) {
                   10189:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
                   10190:                         } else {
                   10191:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
                   10192:                         }
                   10193:                         if (!$privkey) {
                   10194:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
                   10195:                         } else {
                   10196:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
                   10197:                         }
                   10198:                         $chgtext .= '</ul>';
1.224     raeburn  10199:                     } elsif ($type eq 'emailusername') {
                   10200:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
1.228     raeburn  10201:                             if (ref($types) eq 'ARRAY') {
                   10202:                                 foreach my $type (@{$types}) {
                   10203:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
                   10204:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
1.245     raeburn  10205:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',"'$usertypes->{$type}'").
1.228     raeburn  10206:                                                     '<ul>';
                   10207:                                             foreach my $field (@{$infofields}) {
                   10208:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
                   10209:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
                   10210:                                                 }
                   10211:                                             }
1.245     raeburn  10212:                                             $chgtext .= '</ul>';
                   10213:                                         } else {
                   10214:                                             $chgtext .= &mt('When self creating account with e-mail as username, no information besides e-mail address will be provided by [_1].',"'$usertypes->{$type}'").'<br />';
1.228     raeburn  10215:                                         }
                   10216:                                     } else {
1.245     raeburn  10217:                                         $chgtext .= &mt('When self creating account with e-mail as username, no information besides e-mail address will be provided by [_1].',"'$usertypes->{$type}'").'<br />';
1.224     raeburn  10218:                                     }
                   10219:                                 }
                   10220:                             }
                   10221:                         }
                   10222:                     } elsif ($type eq 'notify') {
                   10223:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
                   10224:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
                   10225:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
                   10226:                                 if ($cancreate{'notify'}{'approval'}) {
                   10227:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
                   10228:                                 }
                   10229:                             }
1.43      raeburn  10230:                         }
1.34      raeburn  10231:                     }
1.224     raeburn  10232:                     if ($chgtext) {
                   10233:                         $resulttext .= '<li>'.$chgtext.'</li>';
1.32      raeburn  10234:                     }
                   10235:                 }
                   10236:             }
1.43      raeburn  10237:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
                   10238:                 my ($emailrules,$emailruleorder) =
                   10239:                     &Apache::lonnet::inst_userrules($dom,'email');
                   10240:                 my $chgtext = '<ul>';
                   10241:                 foreach my $type (@email_rule) {
                   10242:                     if (ref($emailrules->{$type}) eq 'HASH') {
                   10243:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
                   10244:                     }
                   10245:                 }
                   10246:                 $chgtext .= '</ul>';
                   10247:                 if (@email_rule > 0) {
1.224     raeburn  10248:                     $resulttext .= '<li>'.
                   10249:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
                   10250:                                        $chgtext.
                   10251:                                    '</li>';
1.43      raeburn  10252:                 } else {
1.224     raeburn  10253:                     $resulttext .= '<li>'.
                   10254:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
                   10255:                                    '</li>';
1.43      raeburn  10256:                 }
                   10257:             }
1.224     raeburn  10258:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
                   10259:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
                   10260:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
                   10261:                 foreach my $type (@{$changes{'selfcreate'}}) {
                   10262:                     my $typename = $type;
                   10263:                     if (ref($usertypes) eq 'HASH') {
                   10264:                         if ($usertypes->{$type} ne '') {
                   10265:                             $typename = $usertypes->{$type};
                   10266:                         }
                   10267:                     }
                   10268:                     my @modifiable;
                   10269:                     $resulttext .= '<li>'.
                   10270:                                     &mt('Self-creation of account by users with status: [_1]',
                   10271:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
                   10272:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
                   10273:                     foreach my $field (@fields) {
                   10274:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
                   10275:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
1.28      raeburn  10276:                         }
                   10277:                     }
1.224     raeburn  10278:                     if (@modifiable > 0) {
                   10279:                         $resulttext .= join(', ',@modifiable);
1.43      raeburn  10280:                     } else {
1.224     raeburn  10281:                         $resulttext .= &mt('none');
1.43      raeburn  10282:                     }
1.224     raeburn  10283:                     $resulttext .= '</li>';
1.28      raeburn  10284:                 }
1.224     raeburn  10285:                 $resulttext .= '</ul></li>';
1.28      raeburn  10286:             }
1.27      raeburn  10287:             $resulttext .= '</ul>';
                   10288:         } else {
1.224     raeburn  10289:             $resulttext = &mt('No changes made to self-creation settings');
1.27      raeburn  10290:         }
                   10291:     } else {
                   10292:         $resulttext = '<span class="LC_error">'.
1.23      raeburn  10293:             &mt('An error occurred: [_1]',$putresult).'</span>';
                   10294:     }
1.43      raeburn  10295:     if ($warningmsg ne '') {
                   10296:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
                   10297:     }
1.23      raeburn  10298:     return $resulttext;
                   10299: }
                   10300: 
1.165     raeburn  10301: sub process_captcha {
                   10302:     my ($container,$changes,$newsettings,$current) = @_;
                   10303:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
                   10304:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
                   10305:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
                   10306:         $newsettings->{'captcha'} = 'original';
                   10307:     }
                   10308:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
1.210     raeburn  10309:         if ($container eq 'cancreate') {
1.169     raeburn  10310:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
                   10311:                 push(@{$changes->{'cancreate'}},'captcha');
                   10312:             } elsif (!defined($changes->{'cancreate'})) {
                   10313:                 $changes->{'cancreate'} = ['captcha'];
                   10314:             }
                   10315:         } else {
                   10316:             $changes->{'captcha'} = 1;
1.165     raeburn  10317:         }
                   10318:     }
                   10319:     my ($newpub,$newpriv,$currpub,$currpriv);
                   10320:     if ($newsettings->{'captcha'} eq 'recaptcha') {
                   10321:         $newpub = $env{'form.'.$container.'_recaptchapub'};
                   10322:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
1.250     raeburn  10323:         $newpub =~ s/[^\w\-]//g;
                   10324:         $newpriv =~ s/[^\w\-]//g;
1.169     raeburn  10325:         $newsettings->{'recaptchakeys'} = {
                   10326:                                              public  => $newpub,
                   10327:                                              private => $newpriv,
                   10328:                                           };
1.165     raeburn  10329:     }
                   10330:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
                   10331:         $currpub = $current->{'recaptchakeys'}{'public'};
                   10332:         $currpriv = $current->{'recaptchakeys'}{'private'};
1.179     raeburn  10333:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
                   10334:             $newsettings->{'recaptchakeys'} = {
                   10335:                                                  public  => '',
                   10336:                                                  private => '',
                   10337:                                               }
                   10338:         }
1.165     raeburn  10339:     }
                   10340:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
1.169     raeburn  10341:         if ($container eq 'cancreate') {
                   10342:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
                   10343:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
                   10344:             } elsif (!defined($changes->{'cancreate'})) {
                   10345:                 $changes->{'cancreate'} = ['recaptchakeys'];
                   10346:             }
                   10347:         } else {
1.210     raeburn  10348:             $changes->{'recaptchakeys'} = 1;
1.165     raeburn  10349:         }
                   10350:     }
                   10351:     return;
                   10352: }
                   10353: 
1.33      raeburn  10354: sub modify_usermodification {
                   10355:     my ($dom,%domconfig) = @_;
1.224     raeburn  10356:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
1.33      raeburn  10357:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
                   10358:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
1.224     raeburn  10359:             if ($key eq 'selfcreate') {
                   10360:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
                   10361:             } else {  
                   10362:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
                   10363:             }
1.33      raeburn  10364:         }
                   10365:     }
1.224     raeburn  10366:     my @contexts = ('author','course');
1.33      raeburn  10367:     my %context_title = (
                   10368:                            author => 'In author context',
                   10369:                            course => 'In course context',
                   10370:                         );
                   10371:     my @fields = ('lastname','firstname','middlename','generation',
                   10372:                   'permanentemail','id');
                   10373:     my %roles = (
                   10374:                   author => ['ca','aa'],
                   10375:                   course => ['st','ep','ta','in','cr'],
                   10376:                 );
                   10377:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
                   10378:     foreach my $context (@contexts) {
                   10379:         foreach my $role (@{$roles{$context}}) {
                   10380:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
                   10381:             foreach my $item (@fields) {
                   10382:                 if (grep(/^\Q$item\E$/,@modifiable)) {
                   10383:                     $modifyhash{$context}{$role}{$item} = 1;
                   10384:                 } else {
                   10385:                     $modifyhash{$context}{$role}{$item} = 0;
                   10386:                 }
                   10387:             }
                   10388:         }
                   10389:         if (ref($curr_usermodification{$context}) eq 'HASH') {
                   10390:             foreach my $role (@{$roles{$context}}) {
                   10391:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
                   10392:                     foreach my $field (@fields) {
                   10393:                         if ($modifyhash{$context}{$role}{$field} ne 
                   10394:                                 $curr_usermodification{$context}{$role}{$field}) {
                   10395:                             push(@{$changes{$context}},$role);
                   10396:                             last;
                   10397:                         }
                   10398:                     }
                   10399:                 }
                   10400:             }
                   10401:         } else {
                   10402:             foreach my $context (@contexts) {
                   10403:                 foreach my $role (@{$roles{$context}}) {
                   10404:                     push(@{$changes{$context}},$role);
                   10405:                 }
                   10406:             }
                   10407:         }
                   10408:     }
                   10409:     my %usermodification_hash =  (
                   10410:                                    usermodification => \%modifyhash,
                   10411:                                  );
                   10412:     my $putresult = &Apache::lonnet::put_dom('configuration',
                   10413:                                              \%usermodification_hash,$dom);
                   10414:     if ($putresult eq 'ok') {
                   10415:         if (keys(%changes) > 0) {
                   10416:             $resulttext = &mt('Changes made: ').'<ul>';
                   10417:             foreach my $context (@contexts) {
                   10418:                 if (ref($changes{$context}) eq 'ARRAY') {
                   10419:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
                   10420:                     if (ref($changes{$context}) eq 'ARRAY') {
                   10421:                         foreach my $role (@{$changes{$context}}) {
                   10422:                             my $rolename;
1.224     raeburn  10423:                             if ($role eq 'cr') {
                   10424:                                 $rolename = &mt('Custom');
1.33      raeburn  10425:                             } else {
1.224     raeburn  10426:                                 $rolename = &Apache::lonnet::plaintext($role);
1.33      raeburn  10427:                             }
                   10428:                             my @modifiable;
1.224     raeburn  10429:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
1.33      raeburn  10430:                             foreach my $field (@fields) {
                   10431:                                 if ($modifyhash{$context}{$role}{$field}) {
                   10432:                                     push(@modifiable,$fieldtitles{$field});
                   10433:                                 }
                   10434:                             }
                   10435:                             if (@modifiable > 0) {
                   10436:                                 $resulttext .= join(', ',@modifiable);
                   10437:                             } else {
                   10438:                                 $resulttext .= &mt('none'); 
                   10439:                             }
                   10440:                             $resulttext .= '</li>';
                   10441:                         }
                   10442:                         $resulttext .= '</ul></li>';
                   10443:                     }
                   10444:                 }
                   10445:             }
                   10446:             $resulttext .= '</ul>';
                   10447:         } else {
                   10448:             $resulttext = &mt('No changes made to user modification settings');
                   10449:         }
                   10450:     } else {
                   10451:         $resulttext = '<span class="LC_error">'.
                   10452:             &mt('An error occurred: [_1]',$putresult).'</span>';
                   10453:     }
                   10454:     return $resulttext;
                   10455: }
                   10456: 
1.43      raeburn  10457: sub modify_defaults {
1.212     raeburn  10458:     my ($dom,$lastactref,%domconfig) = @_;
1.43      raeburn  10459:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
1.212     raeburn  10460:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
1.141     raeburn  10461:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def','portal_def');
1.43      raeburn  10462:     my @authtypes = ('internal','krb4','krb5','localauth');
                   10463:     foreach my $item (@items) {
                   10464:         $newvalues{$item} = $env{'form.'.$item};
                   10465:         if ($item eq 'auth_def') {
                   10466:             if ($newvalues{$item} ne '') {
                   10467:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
                   10468:                     push(@errors,$item);
                   10469:                 }
                   10470:             }
                   10471:         } elsif ($item eq 'lang_def') {
                   10472:             if ($newvalues{$item} ne '') {
                   10473:                 if ($newvalues{$item} =~ /^(\w+)/) {
                   10474:                     my $langcode = $1;
1.103     raeburn  10475:                     if ($langcode ne 'x_chef') {
                   10476:                         if (code2language($langcode) eq '') {
                   10477:                             push(@errors,$item);
                   10478:                         }
1.43      raeburn  10479:                     }
                   10480:                 } else {
                   10481:                     push(@errors,$item);
                   10482:                 }
                   10483:             }
1.54      raeburn  10484:         } elsif ($item eq 'timezone_def') {
                   10485:             if ($newvalues{$item} ne '') {
1.62      raeburn  10486:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
1.54      raeburn  10487:                     push(@errors,$item);   
                   10488:                 }
                   10489:             }
1.68      raeburn  10490:         } elsif ($item eq 'datelocale_def') {
                   10491:             if ($newvalues{$item} ne '') {
                   10492:                 my @datelocale_ids = DateTime::Locale->ids();
                   10493:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
                   10494:                     push(@errors,$item);
                   10495:                 }
                   10496:             }
1.141     raeburn  10497:         } elsif ($item eq 'portal_def') {
                   10498:             if ($newvalues{$item} ne '') {
                   10499:                 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])\/?$/) {
                   10500:                     push(@errors,$item);
                   10501:                 }
                   10502:             }
1.43      raeburn  10503:         }
                   10504:         if (grep(/^\Q$item\E$/,@errors)) {
                   10505:             $newvalues{$item} = $domdefaults{$item};
                   10506:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
                   10507:             $changes{$item} = 1;
                   10508:         }
1.72      raeburn  10509:         $domdefaults{$item} = $newvalues{$item};
1.43      raeburn  10510:     }
                   10511:     my %defaults_hash = (
1.72      raeburn  10512:                          defaults => \%newvalues,
                   10513:                         );
1.43      raeburn  10514:     my $title = &defaults_titles();
1.236     raeburn  10515: 
                   10516:     my $currinststatus;
                   10517:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
                   10518:         $currinststatus = $domconfig{'inststatus'};
                   10519:     } else {
                   10520:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
                   10521:         $currinststatus = {
                   10522:                              inststatustypes => $usertypes,
                   10523:                              inststatusorder => $types,
                   10524:                              inststatusguest => [],
                   10525:                           };
                   10526:     }
                   10527:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
                   10528:     my @allpos;
                   10529:     my %guests;
                   10530:     my %alltypes;
                   10531:     my ($currtitles,$currguests,$currorder);
                   10532:     if (ref($currinststatus) eq 'HASH') {
                   10533:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
                   10534:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
                   10535:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
                   10536:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
                   10537:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
                   10538:                     }
                   10539:                 }
                   10540:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
                   10541:                     my $position = $env{'form.inststatus_pos_'.$type};
                   10542:                     $position =~ s/\D+//g;
                   10543:                     $allpos[$position] = $type;
                   10544:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
                   10545:                     $alltypes{$type} =~ s/`//g;
                   10546:                     if ($env{'form.inststatus_guest_'.$type}) {
                   10547:                         $guests{$type} = 1;
                   10548:                     }
                   10549:                 }
                   10550:             }
                   10551:             if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
                   10552:                 $currguests = join(',',@{$currinststatus->{'inststatusguest'}});
                   10553:             }
                   10554:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
                   10555:             $currtitles =~ s/,$//;
                   10556:         }
                   10557:     }
                   10558:     if ($env{'form.addinststatus'}) {
                   10559:         my $newtype = $env{'form.addinststatus'};
                   10560:         $newtype =~ s/\W//g;
                   10561:         unless (exists($alltypes{$newtype})) {
                   10562:             if ($env{'form.addinststatus_guest'}) {
                   10563:                 $guests{$newtype} = 1;
                   10564:             }
                   10565:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
                   10566:             $alltypes{$newtype} =~ s/`//g; 
                   10567:             my $position = $env{'form.addinststatus_pos'};
                   10568:             $position =~ s/\D+//g;
                   10569:             if ($position ne '') {
                   10570:                 $allpos[$position] = $newtype;
                   10571:             }
                   10572:         }
                   10573:     }
                   10574:     my (@orderedstatus,@orderedguests);
                   10575:     foreach my $type (@allpos) {
                   10576:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
                   10577:             push(@orderedstatus,$type);
                   10578:             if ($guests{$type}) {
                   10579:                 push(@orderedguests,$type);
                   10580:             }
                   10581:         }
                   10582:     }
                   10583:     foreach my $type (keys(%alltypes)) {
                   10584:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
                   10585:             delete($alltypes{$type});
                   10586:         }
                   10587:     }
                   10588:     $defaults_hash{'inststatus'} = {
                   10589:                                      inststatustypes => \%alltypes,
                   10590:                                      inststatusorder => \@orderedstatus,
                   10591:                                      inststatusguest => \@orderedguests,
                   10592:                                    };
                   10593:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
                   10594:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
                   10595:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
                   10596:         }
                   10597:     }
                   10598:     if ($currorder ne join(',',@orderedstatus)) {
                   10599:         $changes{'inststatus'}{'inststatusorder'} = 1;
                   10600:     }
                   10601:     if ($currguests ne join(',',@orderedguests)) {
                   10602:         $changes{'inststatus'}{'inststatusguest'} = 1;
                   10603:     }
                   10604:     my $newtitles;
                   10605:     foreach my $item (@orderedstatus) {
                   10606:         $newtitles .= $alltypes{$item}.',';
                   10607:     }
                   10608:     $newtitles =~ s/,$//;
                   10609:     if ($currtitles ne $newtitles) {
                   10610:         $changes{'inststatus'}{'inststatustypes'} = 1;
                   10611:     }
1.43      raeburn  10612:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
                   10613:                                              $dom);
                   10614:     if ($putresult eq 'ok') {
                   10615:         if (keys(%changes) > 0) {
                   10616:             $resulttext = &mt('Changes made:').'<ul>';
1.212     raeburn  10617:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
1.43      raeburn  10618:             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";
                   10619:             foreach my $item (sort(keys(%changes))) {
1.236     raeburn  10620:                 if ($item eq 'inststatus') {
                   10621:                     if (ref($changes{'inststatus'}) eq 'HASH') {
                   10622:                         if (($changes{'inststatus'}{'inststatustypes'}) || $changes{'inststatus'}{'inststatusorder'}) {
                   10623:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
                   10624:                             foreach my $type (@orderedstatus) { 
                   10625:                                 $resulttext .= $alltypes{$type}.', ';
                   10626:                             }
                   10627:                             $resulttext =~ s/, $//;
                   10628:                             $resulttext .= '</li>';
                   10629:                         }
                   10630:                         if ($changes{'inststatus'}{'inststatusguest'}) {
                   10631:                             $resulttext .= '<li>'; 
                   10632:                             if (@orderedguests) {
                   10633:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to:').' ';
                   10634:                                 foreach my $type (@orderedguests) {
                   10635:                                     $resulttext .= $alltypes{$type}.', ';
                   10636:                                 }
                   10637:                                 $resulttext =~ s/, $//;
                   10638:                             } else {
                   10639:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to none.');
                   10640:                             }
                   10641:                             $resulttext .= '</li>';
                   10642:                         }
                   10643:                     }
                   10644:                 } else {
                   10645:                     my $value = $env{'form.'.$item};
                   10646:                     if ($value eq '') {
                   10647:                         $value = &mt('none');
                   10648:                     } elsif ($item eq 'auth_def') {
                   10649:                         my %authnames = &authtype_names();
                   10650:                         my %shortauth = (
                   10651:                                           internal   => 'int',
                   10652:                                           krb4       => 'krb4',
                   10653:                                           krb5       => 'krb5',
                   10654:                                           localauth  => 'loc',
                   10655:                         );
                   10656:                         $value = $authnames{$shortauth{$value}};
                   10657:                     }
                   10658:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
                   10659:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
1.43      raeburn  10660:                 }
                   10661:             }
                   10662:             $resulttext .= '</ul>';
                   10663:             $mailmsgtext .= "\n";
                   10664:             my $cachetime = 24*60*60;
1.72      raeburn  10665:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
1.212     raeburn  10666:             if (ref($lastactref) eq 'HASH') {
                   10667:                 $lastactref->{'domdefaults'} = 1;
                   10668:             }
1.68      raeburn  10669:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
1.203     raeburn  10670:                 my $notify = 1;
                   10671:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
                   10672:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
                   10673:                         $notify = 0;
                   10674:                     }
                   10675:                 }
                   10676:                 if ($notify) {
                   10677:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
                   10678:                                                "LON-CAPA Domain Settings Change - $dom",
                   10679:                                                $mailmsgtext);
                   10680:                 }
1.54      raeburn  10681:             }
1.43      raeburn  10682:         } else {
1.54      raeburn  10683:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
1.43      raeburn  10684:         }
                   10685:     } else {
                   10686:         $resulttext = '<span class="LC_error">'.
                   10687:             &mt('An error occurred: [_1]',$putresult).'</span>';
                   10688:     }
                   10689:     if (@errors > 0) {
                   10690:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
                   10691:         foreach my $item (@errors) {
                   10692:             $resulttext .= ' "'.$title->{$item}.'",';
                   10693:         }
                   10694:         $resulttext =~ s/,$//;
                   10695:     }
                   10696:     return $resulttext;
                   10697: }
                   10698: 
1.46      raeburn  10699: sub modify_scantron {
1.205     raeburn  10700:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
1.46      raeburn  10701:     my ($resulttext,%confhash,%changes,$errors);
                   10702:     my $custom = 'custom.tab';
                   10703:     my $default = 'default.tab';
                   10704:     my $servadm = $r->dir_config('lonAdmEMail');
                   10705:     my ($configuserok,$author_ok,$switchserver) = 
                   10706:         &config_check($dom,$confname,$servadm);
                   10707:     if ($env{'form.scantronformat.filename'} ne '') {
                   10708:         my $error;
                   10709:         if ($configuserok eq 'ok') {
                   10710:             if ($switchserver) {
1.130     raeburn  10711:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
1.46      raeburn  10712:             } else {
                   10713:                 if ($author_ok eq 'ok') {
                   10714:                     my ($result,$scantronurl) =
                   10715:                         &publishlogo($r,'upload','scantronformat',$dom,
                   10716:                                      $confname,'scantron','','',$custom);
                   10717:                     if ($result eq 'ok') {
                   10718:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
1.48      raeburn  10719:                         $changes{'scantronformat'} = 1;
1.46      raeburn  10720:                     } else {
                   10721:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
                   10722:                     }
                   10723:                 } else {
                   10724:                     $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);
                   10725:                 }
                   10726:             }
                   10727:         } else {
                   10728:             $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);
                   10729:         }
                   10730:         if ($error) {
                   10731:             &Apache::lonnet::logthis($error);
                   10732:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
                   10733:         }
                   10734:     }
1.48      raeburn  10735:     if (ref($domconfig{'scantron'}) eq 'HASH') {
                   10736:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
                   10737:             if ($env{'form.scantronformat_del'}) {
                   10738:                 $confhash{'scantron'}{'scantronformat'} = '';
                   10739:                 $changes{'scantronformat'} = 1;
1.46      raeburn  10740:             }
                   10741:         }
                   10742:     }
                   10743:     if (keys(%confhash) > 0) {
                   10744:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
                   10745:                                                  $dom);
                   10746:         if ($putresult eq 'ok') {
                   10747:             if (keys(%changes) > 0) {
1.48      raeburn  10748:                 if (ref($confhash{'scantron'}) eq 'HASH') {
                   10749:                     $resulttext = &mt('Changes made:').'<ul>';
                   10750:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
1.130     raeburn  10751:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
1.48      raeburn  10752:                     } else {
1.130     raeburn  10753:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
1.46      raeburn  10754:                     }
1.48      raeburn  10755:                     $resulttext .= '</ul>';
                   10756:                 } else {
1.130     raeburn  10757:                     $resulttext = &mt('Changes made to bubblesheet format file.');
1.46      raeburn  10758:                 }
                   10759:                 $resulttext .= '</ul>';
                   10760:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
1.212     raeburn  10761:                 if (ref($lastactref) eq 'HASH') {
                   10762:                     $lastactref->{'domainconfig'} = 1;
                   10763:                 }
1.46      raeburn  10764:             } else {
1.130     raeburn  10765:                 $resulttext = &mt('No changes made to bubblesheet format file');
1.46      raeburn  10766:             }
                   10767:         } else {
                   10768:             $resulttext = '<span class="LC_error">'.
                   10769:                 &mt('An error occurred: [_1]',$putresult).'</span>';
                   10770:         }
                   10771:     } else {
1.130     raeburn  10772:         $resulttext = &mt('No changes made to bubblesheet format file'); 
1.46      raeburn  10773:     }
                   10774:     if ($errors) {
                   10775:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
                   10776:                        $errors.'</ul>';
                   10777:     }
                   10778:     return $resulttext;
                   10779: }
                   10780: 
1.48      raeburn  10781: sub modify_coursecategories {
1.239     raeburn  10782:     my ($dom,$lastactref,%domconfig) = @_;
1.57      raeburn  10783:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
                   10784:         $cathash);
1.48      raeburn  10785:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
1.238     raeburn  10786:     my @catitems = ('unauth','auth');
                   10787:     my @cattypes = ('std','domonly','codesrch','none');
1.55      raeburn  10788:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
1.57      raeburn  10789:         $cathash = $domconfig{'coursecategories'}{'cats'};
                   10790:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
                   10791:             $changes{'togglecats'} = 1;
                   10792:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
                   10793:         }
                   10794:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
                   10795:             $changes{'categorize'} = 1;
                   10796:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
                   10797:         }
1.120     raeburn  10798:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
                   10799:             $changes{'togglecatscomm'} = 1;
                   10800:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
                   10801:         }
                   10802:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
                   10803:             $changes{'categorizecomm'} = 1;
                   10804:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
                   10805:         }
1.238     raeburn  10806:         foreach my $item (@catitems) {
                   10807:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
                   10808:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
                   10809:                     $changes{$item} = 1;
                   10810:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
                   10811:                 }
                   10812:             }
                   10813:         }
1.57      raeburn  10814:     } else {
                   10815:         $changes{'togglecats'} = 1;
                   10816:         $changes{'categorize'} = 1;
1.124     raeburn  10817:         $changes{'togglecatscomm'} = 1;
                   10818:         $changes{'categorizecomm'} = 1;
1.87      raeburn  10819:         $domconfig{'coursecategories'} = {
                   10820:                                              togglecats => $env{'form.togglecats'},
                   10821:                                              categorize => $env{'form.categorize'},
1.124     raeburn  10822:                                              togglecatscomm => $env{'form.togglecatscomm'},
                   10823:                                              categorizecomm => $env{'form.categorizecomm'},
1.120     raeburn  10824:                                          };
1.238     raeburn  10825:         foreach my $item (@catitems) {
                   10826:             if ($env{'form.coursecat_'.$item} ne 'std') {
                   10827:                 $changes{$item} = 1;
                   10828:             }
                   10829:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
                   10830:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
                   10831:             }
                   10832:         }
1.57      raeburn  10833:     }
                   10834:     if (ref($cathash) eq 'HASH') {
                   10835:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
1.55      raeburn  10836:             push (@deletecategory,'instcode::0');
                   10837:         }
1.120     raeburn  10838:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
                   10839:             push(@deletecategory,'communities::0');
                   10840:         }
1.48      raeburn  10841:     }
1.57      raeburn  10842:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
                   10843:     if (ref($cathash) eq 'HASH') {
1.48      raeburn  10844:         if (@deletecategory > 0) {
                   10845:             #FIXME Need to remove category from all courses using a deleted category 
1.57      raeburn  10846:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
1.48      raeburn  10847:             foreach my $item (@deletecategory) {
1.57      raeburn  10848:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
                   10849:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
1.48      raeburn  10850:                     $deletions{$item} = 1;
1.57      raeburn  10851:                     &recurse_cat_deletes($item,$cathash,\%deletions);
1.48      raeburn  10852:                 }
                   10853:             }
                   10854:         }
1.57      raeburn  10855:         foreach my $item (keys(%{$cathash})) {
1.48      raeburn  10856:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
1.57      raeburn  10857:             if ($cathash->{$item} ne $env{'form.'.$item}) {
1.48      raeburn  10858:                 $reorderings{$item} = 1;
1.57      raeburn  10859:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
1.48      raeburn  10860:             }
                   10861:             if ($env{'form.addcategory_name_'.$item} ne '') {
                   10862:                 my $newcat = $env{'form.addcategory_name_'.$item};
                   10863:                 my $newdepth = $depth+1;
                   10864:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
1.57      raeburn  10865:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
1.48      raeburn  10866:                 $adds{$newitem} = 1; 
                   10867:             }
                   10868:             if ($env{'form.subcat_'.$item} ne '') {
                   10869:                 my $newcat = $env{'form.subcat_'.$item};
                   10870:                 my $newdepth = $depth+1;
                   10871:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
1.57      raeburn  10872:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
1.48      raeburn  10873:                 $adds{$newitem} = 1;
                   10874:             }
                   10875:         }
                   10876:     }
                   10877:     if ($env{'form.instcode'} eq '1') {
1.57      raeburn  10878:         if (ref($cathash) eq 'HASH') {
1.48      raeburn  10879:             my $newitem = 'instcode::0';
1.57      raeburn  10880:             if ($cathash->{$newitem} eq '') {  
                   10881:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
1.48      raeburn  10882:                 $adds{$newitem} = 1;
                   10883:             }
                   10884:         } else {
                   10885:             my $newitem = 'instcode::0';
1.57      raeburn  10886:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
1.48      raeburn  10887:             $adds{$newitem} = 1;
                   10888:         }
                   10889:     }
1.120     raeburn  10890:     if ($env{'form.communities'} eq '1') {
                   10891:         if (ref($cathash) eq 'HASH') {
                   10892:             my $newitem = 'communities::0';
                   10893:             if ($cathash->{$newitem} eq '') {
                   10894:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
                   10895:                 $adds{$newitem} = 1;
                   10896:             }
                   10897:         } else {
                   10898:             my $newitem = 'communities::0';
                   10899:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
                   10900:             $adds{$newitem} = 1;
                   10901:         }
                   10902:     }
1.48      raeburn  10903:     if ($env{'form.addcategory_name'} ne '') {
1.120     raeburn  10904:         if (($env{'form.addcategory_name'} ne 'instcode') &&
                   10905:             ($env{'form.addcategory_name'} ne 'communities')) {
                   10906:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
                   10907:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
                   10908:             $adds{$newitem} = 1;
                   10909:         }
1.48      raeburn  10910:     }
1.57      raeburn  10911:     my $putresult;
1.48      raeburn  10912:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
                   10913:         if (keys(%deletions) > 0) {
                   10914:             foreach my $key (keys(%deletions)) {
                   10915:                 if ($predelallitems{$key} ne '') {
                   10916:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
                   10917:                 }
                   10918:             }
                   10919:         }
                   10920:         my (@chkcats,@chktrails,%chkallitems);
1.57      raeburn  10921:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
1.48      raeburn  10922:         if (ref($chkcats[0]) eq 'ARRAY') {
                   10923:             my $depth = 0;
                   10924:             my $chg = 0;
                   10925:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
                   10926:                 my $name = $chkcats[0][$i];
                   10927:                 my $item;
                   10928:                 if ($name eq '') {
                   10929:                     $chg ++;
                   10930:                 } else {
                   10931:                     $item = &escape($name).'::0';
                   10932:                     if ($chg) {
1.57      raeburn  10933:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
1.48      raeburn  10934:                     }
                   10935:                     $depth ++; 
1.57      raeburn  10936:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
1.48      raeburn  10937:                     $depth --;
                   10938:                 }
                   10939:             }
                   10940:         }
1.57      raeburn  10941:     }
                   10942:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
                   10943:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
1.48      raeburn  10944:         if ($putresult eq 'ok') {
1.57      raeburn  10945:             my %title = (
1.120     raeburn  10946:                          togglecats     => 'Show/Hide a course in catalog',
                   10947:                          categorize     => 'Assign a category to a course',
                   10948:                          togglecatscomm => 'Show/Hide a community in catalog',
                   10949:                          categorizecomm => 'Assign a category to a community',
1.57      raeburn  10950:                         );
                   10951:             my %level = (
1.120     raeburn  10952:                          dom  => 'set in Domain ("Modify Course/Community")',
                   10953:                          crs  => 'set in Course ("Course Configuration")',
                   10954:                          comm => 'set in Community ("Community Configuration")',
1.238     raeburn  10955:                          none     => 'No catalog',
                   10956:                          std      => 'Standard catalog',
                   10957:                          domonly  => 'Domain-only catalog',
                   10958:                          codesrch => 'Code search form',
1.57      raeburn  10959:                         );
1.48      raeburn  10960:             $resulttext = &mt('Changes made:').'<ul>';
1.57      raeburn  10961:             if ($changes{'togglecats'}) {
                   10962:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
                   10963:             }
                   10964:             if ($changes{'categorize'}) {
                   10965:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
1.48      raeburn  10966:             }
1.120     raeburn  10967:             if ($changes{'togglecatscomm'}) {
                   10968:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
                   10969:             }
                   10970:             if ($changes{'categorizecomm'}) {
                   10971:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
                   10972:             }
1.238     raeburn  10973:             if ($changes{'unauth'}) {
                   10974:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
                   10975:             }
                   10976:             if ($changes{'auth'}) {
                   10977:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
                   10978:             }
1.57      raeburn  10979:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
                   10980:                 my $cathash;
                   10981:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
                   10982:                     $cathash = $domconfig{'coursecategories'}{'cats'};
                   10983:                 } else {
                   10984:                     $cathash = {};
                   10985:                 } 
                   10986:                 my (@cats,@trails,%allitems);
                   10987:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
                   10988:                 if (keys(%deletions) > 0) {
                   10989:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
                   10990:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
                   10991:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
                   10992:                     }
                   10993:                     $resulttext .= '</ul></li>';
                   10994:                 }
                   10995:                 if (keys(%reorderings) > 0) {
                   10996:                     my %sort_by_trail;
                   10997:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
                   10998:                     foreach my $key (keys(%reorderings)) {
                   10999:                         if ($allitems{$key} ne '') {
                   11000:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
                   11001:                         }
1.48      raeburn  11002:                     }
1.57      raeburn  11003:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
                   11004:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
                   11005:                     }
                   11006:                     $resulttext .= '</ul></li>';
1.48      raeburn  11007:                 }
1.57      raeburn  11008:                 if (keys(%adds) > 0) {
                   11009:                     my %sort_by_trail;
                   11010:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
                   11011:                     foreach my $key (keys(%adds)) {
                   11012:                         if ($allitems{$key} ne '') {
                   11013:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
                   11014:                         }
                   11015:                     }
                   11016:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
                   11017:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
1.48      raeburn  11018:                     }
1.57      raeburn  11019:                     $resulttext .= '</ul></li>';
1.48      raeburn  11020:                 }
                   11021:             }
                   11022:             $resulttext .= '</ul>';
1.239     raeburn  11023:             if ($changes{'unauth'} || $changes{'auth'}) {
1.246     raeburn  11024:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
                   11025:                 if ($changes{'auth'}) {
                   11026:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
                   11027:                 }
                   11028:                 if ($changes{'unauth'}) {
                   11029:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
                   11030:                 }
                   11031:                 my $cachetime = 24*60*60;
                   11032:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
1.239     raeburn  11033:                 if (ref($lastactref) eq 'HASH') {
1.246     raeburn  11034:                     $lastactref->{'domdefaults'} = 1;
1.239     raeburn  11035:                 }
                   11036:             }
1.48      raeburn  11037:         } else {
                   11038:             $resulttext = '<span class="LC_error">'.
1.57      raeburn  11039:                           &mt('An error occurred: [_1]',$putresult).'</span>';
1.48      raeburn  11040:         }
                   11041:     } else {
1.120     raeburn  11042:         $resulttext = &mt('No changes made to course and community categories');
1.48      raeburn  11043:     }
                   11044:     return $resulttext;
                   11045: }
                   11046: 
1.69      raeburn  11047: sub modify_serverstatuses {
                   11048:     my ($dom,%domconfig) = @_;
                   11049:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
                   11050:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
                   11051:         %currserverstatus = %{$domconfig{'serverstatuses'}};
                   11052:     }
                   11053:     my @pages = &serverstatus_pages();
                   11054:     foreach my $type (@pages) {
                   11055:         $newserverstatus{$type}{'namedusers'} = '';
                   11056:         $newserverstatus{$type}{'machines'} = '';
                   11057:         if (defined($env{'form.'.$type.'_namedusers'})) {
                   11058:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
                   11059:             my @okusers;
                   11060:             foreach my $user (@users) {
                   11061:                 my ($uname,$udom) = split(/:/,$user);
                   11062:                 if (($udom =~ /^$match_domain$/) &&   
                   11063:                     (&Apache::lonnet::domain($udom)) &&
                   11064:                     ($uname =~ /^$match_username$/)) {
                   11065:                     if (!grep(/^\Q$user\E/,@okusers)) {
                   11066:                         push(@okusers,$user);
                   11067:                     }
                   11068:                 }
                   11069:             }
                   11070:             if (@okusers > 0) {
                   11071:                  @okusers = sort(@okusers);
                   11072:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
                   11073:             }
                   11074:         }
                   11075:         if (defined($env{'form.'.$type.'_machines'})) {
                   11076:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
                   11077:             my @okmachines;
                   11078:             foreach my $ip (@machines) {
                   11079:                 my @parts = split(/\./,$ip);
                   11080:                 next if (@parts < 4);
                   11081:                 my $badip = 0;
                   11082:                 for (my $i=0; $i<4; $i++) {
                   11083:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
                   11084:                         $badip = 1;
                   11085:                         last;
                   11086:                     }
                   11087:                 }
                   11088:                 if (!$badip) {
                   11089:                     push(@okmachines,$ip);     
                   11090:                 }
                   11091:             }
                   11092:             @okmachines = sort(@okmachines);
                   11093:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
                   11094:         }
                   11095:     }
                   11096:     my %serverstatushash =  (
                   11097:                                 serverstatuses => \%newserverstatus,
                   11098:                             );
                   11099:     foreach my $type (@pages) {
1.83      raeburn  11100:         foreach my $setting ('namedusers','machines') {
1.84      raeburn  11101:             my (@current,@new);
1.83      raeburn  11102:             if (ref($currserverstatus{$type}) eq 'HASH') {
1.84      raeburn  11103:                 if ($currserverstatus{$type}{$setting} ne '') { 
                   11104:                     @current = split(/,/,$currserverstatus{$type}{$setting});
                   11105:                 }
                   11106:             }
                   11107:             if ($newserverstatus{$type}{$setting} ne '') {
                   11108:                 @new = split(/,/,$newserverstatus{$type}{$setting});
1.83      raeburn  11109:             }
                   11110:             if (@current > 0) {
                   11111:                 if (@new > 0) {
                   11112:                     foreach my $item (@current) {
                   11113:                         if (!grep(/^\Q$item\E$/,@new)) {
                   11114:                             $changes{$type}{$setting} = 1;
1.82      raeburn  11115:                             last;
                   11116:                         }
                   11117:                     }
1.84      raeburn  11118:                     foreach my $item (@new) {
                   11119:                         if (!grep(/^\Q$item\E$/,@current)) {
                   11120:                             $changes{$type}{$setting} = 1;
                   11121:                             last;
1.82      raeburn  11122:                         }
                   11123:                     }
                   11124:                 } else {
1.83      raeburn  11125:                     $changes{$type}{$setting} = 1;
1.69      raeburn  11126:                 }
1.83      raeburn  11127:             } elsif (@new > 0) {
                   11128:                 $changes{$type}{$setting} = 1;
1.69      raeburn  11129:             }
                   11130:         }
                   11131:     }
                   11132:     if (keys(%changes) > 0) {
1.81      raeburn  11133:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
1.69      raeburn  11134:         my $putresult = &Apache::lonnet::put_dom('configuration',
                   11135:                                                  \%serverstatushash,$dom);
                   11136:         if ($putresult eq 'ok') {
                   11137:             $resulttext .= &mt('Changes made:').'<ul>';
                   11138:             foreach my $type (@pages) {
1.84      raeburn  11139:                 if (ref($changes{$type}) eq 'HASH') {
1.69      raeburn  11140:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
1.84      raeburn  11141:                     if ($changes{$type}{'namedusers'}) {
1.69      raeburn  11142:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
                   11143:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
                   11144:                         } else {
                   11145:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
                   11146:                         }
1.84      raeburn  11147:                     }
                   11148:                     if ($changes{$type}{'machines'}) {
1.69      raeburn  11149:                         if ($newserverstatus{$type}{'machines'} eq '') {
                   11150:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
                   11151:                         } else {
                   11152:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
                   11153:                         }
                   11154: 
                   11155:                     }
                   11156:                     $resulttext .= '</ul></li>';
                   11157:                 }
                   11158:             }
                   11159:             $resulttext .= '</ul>';
                   11160:         } else {
                   11161:             $resulttext = '<span class="LC_error">'.
                   11162:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
                   11163: 
                   11164:         }
                   11165:     } else {
                   11166:         $resulttext = &mt('No changes made to access to server status pages');
                   11167:     }
                   11168:     return $resulttext;
                   11169: }
                   11170: 
1.118     jms      11171: sub modify_helpsettings {
1.122     jms      11172:     my ($r,$dom,$confname,%domconfig) = @_;
1.166     raeburn  11173:     my ($resulttext,$errors,%changes,%helphash);
                   11174:     my %defaultchecked = ('submitbugs' => 'on');
                   11175:     my @offon = ('off','on');
1.118     jms      11176:     my @toggles = ('submitbugs');
                   11177:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
                   11178:         foreach my $item (@toggles) {
1.166     raeburn  11179:             if ($defaultchecked{$item} eq 'on') { 
                   11180:                 if ($domconfig{'helpsettings'}{$item} eq '') {
                   11181:                     if ($env{'form.'.$item} eq '0') {
                   11182:                         $changes{$item} = 1;
                   11183:                     }
                   11184:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
                   11185:                     $changes{$item} = 1;
                   11186:                 }
                   11187:             } elsif ($defaultchecked{$item} eq 'off') {
                   11188:                 if ($domconfig{'helpsettings'}{$item} eq '') {
                   11189:                     if ($env{'form.'.$item} eq '1') {
                   11190:                         $changes{$item} = 1;
                   11191:                     }
                   11192:                 } elsif ($domconfig{'helpsettings'}{$item} ne $env{'form.'.$item}) {
                   11193:                     $changes{$item} = 1;
                   11194:                 }
                   11195:             }
1.210     raeburn  11196:             if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
1.166     raeburn  11197:                 $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
                   11198:             }
                   11199:         }
1.118     jms      11200:     }
1.123     jms      11201:     my $putresult;
                   11202:     if (keys(%changes) > 0) {
1.166     raeburn  11203:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
1.168     raeburn  11204:         if ($putresult eq 'ok') {
1.166     raeburn  11205:             $resulttext = &mt('Changes made:').'<ul>';
                   11206:             foreach my $item (sort(keys(%changes))) {
                   11207:                 if ($item eq 'submitbugs') {
                   11208:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
                   11209:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
                   11210:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
                   11211:                 }
                   11212:             }
                   11213:             $resulttext .= '</ul>';
                   11214:         } else {
                   11215:             $resulttext = &mt('No changes made to help settings');
1.168     raeburn  11216:             $errors .= '<li><span class="LC_error">'.
                   11217:                        &mt('An error occurred storing the settings: [_1]',
                   11218:                            $putresult).'</span></li>';
1.166     raeburn  11219:         }
1.118     jms      11220:     }
                   11221:     if ($errors) {
1.168     raeburn  11222:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
1.118     jms      11223:                        $errors.'</ul>';
                   11224:     }
                   11225:     return $resulttext;
                   11226: }
                   11227: 
1.121     raeburn  11228: sub modify_coursedefaults {
1.212     raeburn  11229:     my ($dom,$lastactref,%domconfig) = @_;
1.121     raeburn  11230:     my ($resulttext,$errors,%changes,%defaultshash);
1.257     raeburn  11231:     my %defaultchecked = (
                   11232:                            'canuse_pdfforms' => 'off',
                   11233:                            'uselcmath'       => 'on',
                   11234:                            'usejsme'         => 'on'
                   11235:                          );
                   11236:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme');
1.198     raeburn  11237:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
1.216     raeburn  11238:                    'uploadquota_community','uploadquota_textbook');
                   11239:     my @types = ('official','unofficial','community','textbook');
1.198     raeburn  11240:     my %staticdefaults = (
                   11241:                            anonsurvey_threshold => 10,
                   11242:                            uploadquota          => 500,
1.257     raeburn  11243:                            postsubmit           => 60,
1.198     raeburn  11244:                          );
1.121     raeburn  11245: 
                   11246:     $defaultshash{'coursedefaults'} = {};
                   11247: 
                   11248:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
                   11249:         if ($domconfig{'coursedefaults'} eq '') {
                   11250:             $domconfig{'coursedefaults'} = {};
                   11251:         }
                   11252:     }
                   11253: 
                   11254:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
                   11255:         foreach my $item (@toggles) {
                   11256:             if ($defaultchecked{$item} eq 'on') {
                   11257:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
                   11258:                     ($env{'form.'.$item} eq '0')) {
                   11259:                     $changes{$item} = 1;
1.192     raeburn  11260:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
1.121     raeburn  11261:                     $changes{$item} = 1;
                   11262:                 }
                   11263:             } elsif ($defaultchecked{$item} eq 'off') {
                   11264:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
                   11265:                     ($env{'form.'.$item} eq '1')) {
                   11266:                     $changes{$item} = 1;
                   11267:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
                   11268:                     $changes{$item} = 1;
                   11269:                 }
                   11270:             }
                   11271:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
                   11272:         }
1.198     raeburn  11273:         foreach my $item (@numbers) {
                   11274:             my ($currdef,$newdef);
1.208     raeburn  11275:             $newdef = $env{'form.'.$item};
1.198     raeburn  11276:             if ($item eq 'anonsurvey_threshold') {
                   11277:                 $currdef = $domconfig{'coursedefaults'}{$item};
                   11278:                 $newdef =~ s/\D//g;
                   11279:                 if ($newdef eq '' || $newdef < 1) {
                   11280:                     $newdef = 1;
                   11281:                 }
                   11282:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
                   11283:             } else {
                   11284:                 my ($type) = ($item =~ /^\Quploadquota_\E(\w+)$/);
                   11285:                 if (ref($domconfig{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
                   11286:                     $currdef = $domconfig{'coursedefaults'}{'uploadquota'}{$type};
                   11287:                 }
                   11288:                 $newdef =~ s/[^\w.\-]//g;
                   11289:                 $defaultshash{'coursedefaults'}{'uploadquota'}{$type} = $newdef;
                   11290:             }
                   11291:             if ($currdef ne $newdef) {
                   11292:                 my $staticdef;
                   11293:                 if ($item eq 'anonsurvey_threshold') {
                   11294:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
                   11295:                         $changes{$item} = 1;
                   11296:                     }
                   11297:                 } else {
                   11298:                     unless (($currdef eq '') && ($newdef == $staticdefaults{'uploadquota'})) {
                   11299:                         $changes{'uploadquota'} = 1;
                   11300:                     }
                   11301:                 }
1.139     raeburn  11302:             }
                   11303:         }
1.264     raeburn  11304:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
                   11305:         my @currclonecode;
                   11306:         if (ref($currclone) eq 'HASH') {
                   11307:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
                   11308:                 @currclonecode = @{$currclone->{'instcode'}};
                   11309:             }
                   11310:         }
                   11311:         my $newclone;
                   11312:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) { 
                   11313:             $newclone = $env{'form.canclone'};
                   11314:         }
                   11315:         if ($newclone eq 'instcode') {
                   11316:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
                   11317:             my (%codedefaults,@code_order,@clonecode);
                   11318:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
                   11319:                                                     \@code_order);
                   11320:             foreach my $item (@code_order) {
                   11321:                 if (grep(/^\Q$item\E$/,@newcodes)) {
                   11322:                     push(@clonecode,$item);
                   11323:                 }
                   11324:             }
                   11325:             if (@clonecode) {
                   11326:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
                   11327:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
                   11328:                 if (@diffs) {
                   11329:                     $changes{'canclone'} = 1;
                   11330:                 }
                   11331:             } else {
                   11332:                 $newclone eq '';
                   11333:             }
                   11334:         } elsif ($newclone ne '') {
                   11335:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone; 
                   11336:         } 
                   11337:         if ($newclone ne $currclone) {
                   11338:             $changes{'canclone'} = 1;
                   11339:         }
1.257     raeburn  11340:         my %credits;
                   11341:         foreach my $type (@types) {
                   11342:             unless ($type eq 'community') {
                   11343:                 $credits{$type} = $env{'form.'.$type.'_credits'};
                   11344:                 $credits{$type} =~ s/[^\d.]+//g;
                   11345:             }
                   11346:         }
                   11347:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
                   11348:             ($env{'form.coursecredits'} eq '1')) {
                   11349:             $changes{'coursecredits'} = 1;
                   11350:             foreach my $type (keys(%credits)) {
                   11351:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
                   11352:             }
                   11353:         } else {
                   11354:             if ($env{'form.coursecredits'} eq '1') { 
                   11355:                 foreach my $type (@types) {
                   11356:                     unless ($type eq 'community') {
                   11357:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {        
                   11358:                             $changes{'coursecredits'} = 1;
                   11359:                         }
                   11360:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
                   11361:                     }
                   11362:                 }
                   11363:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
                   11364:                 foreach my $type (@types) {
                   11365:                     unless ($type eq 'community') {
                   11366:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
                   11367:                             $changes{'coursecredits'} = 1;
                   11368:                             last;
                   11369:                         }
                   11370:                     }
                   11371:                 }
                   11372:             }
                   11373:         }
                   11374:         if ($env{'form.postsubmit'} eq '1') {
                   11375:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
                   11376:             my %currtimeout;
                   11377:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
                   11378:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
                   11379:                     $changes{'postsubmit'} = 1;
                   11380:                 }
                   11381:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
                   11382:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
                   11383:                 }
                   11384:             } else {
                   11385:                 $changes{'postsubmit'} = 1;
                   11386:             }
                   11387:             foreach my $type (@types) {
                   11388:                 my $timeout = $env{'form.'.$type.'_timeout'};
                   11389:                 $timeout =~ s/\D//g;
                   11390:                 if ($timeout == $staticdefaults{'postsubmit'}) {
                   11391:                     $timeout = '';
                   11392:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
                   11393:                     $timeout = '0';
                   11394:                 }
                   11395:                 unless ($timeout eq '') {
                   11396:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
                   11397:                 }
                   11398:                 if (exists($currtimeout{$type})) {
                   11399:                     if ($timeout ne $currtimeout{$type}) {
                   11400:                         $changes{'postsubmit'} = 1; 
                   11401:                     }
                   11402:                 } elsif ($timeout ne '') {
                   11403:                     $changes{'postsubmit'} = 1;
                   11404:                 }
                   11405:             }
                   11406:         } else {
                   11407:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
                   11408:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
                   11409:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
                   11410:                     $changes{'postsubmit'} = 1;
                   11411:                 }
                   11412:             } else {
                   11413:                 $changes{'postsubmit'} = 1;
                   11414:             }
1.192     raeburn  11415:         }
1.121     raeburn  11416:     }
                   11417:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
                   11418:                                              $dom);
                   11419:     if ($putresult eq 'ok') {
                   11420:         if (keys(%changes) > 0) {
1.213     raeburn  11421:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
1.257     raeburn  11422:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
1.264     raeburn  11423:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
                   11424:                 ($changes{'canclone'})) {
1.257     raeburn  11425:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme') { 
                   11426:                     if ($changes{$item}) {
                   11427:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
                   11428:                     }
                   11429:                 } 
1.192     raeburn  11430:                 if ($changes{'coursecredits'}) {
                   11431:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
1.257     raeburn  11432:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
                   11433:                             $domdefaults{$type.'credits'} =
                   11434:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
                   11435:                         }
                   11436:                     }
                   11437:                 }
                   11438:                 if ($changes{'postsubmit'}) {
                   11439:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
                   11440:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
                   11441:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
                   11442:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
                   11443:                                 $domdefaults{$type.'postsubtimeout'} =
                   11444:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
                   11445:                             }
                   11446:                         }
1.192     raeburn  11447:                     }
                   11448:                 }
1.198     raeburn  11449:                 if ($changes{'uploadquota'}) {
                   11450:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
                   11451:                         foreach my $type (@types) {
                   11452:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
                   11453:                         }
                   11454:                     }
                   11455:                 }
1.264     raeburn  11456:                 if ($changes{'canclone'}) {
                   11457:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
                   11458:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
                   11459:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
                   11460:                             if (@clonecodes) {
                   11461:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
                   11462:                             }
                   11463:                         }
                   11464:                     } else {
                   11465:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
                   11466:                     }
                   11467:                 }
1.121     raeburn  11468:                 my $cachetime = 24*60*60;
                   11469:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
1.212     raeburn  11470:                 if (ref($lastactref) eq 'HASH') {
                   11471:                     $lastactref->{'domdefaults'} = 1;
                   11472:                 }
1.121     raeburn  11473:             }
                   11474:             $resulttext = &mt('Changes made:').'<ul>';
                   11475:             foreach my $item (sort(keys(%changes))) {
                   11476:                 if ($item eq 'canuse_pdfforms') {
                   11477:                     if ($env{'form.'.$item} eq '1') {
                   11478:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
                   11479:                     } else {
                   11480:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
                   11481:                     }
1.257     raeburn  11482:                 } elsif ($item eq 'uselcmath') {
                   11483:                     if ($env{'form.'.$item} eq '1') {
                   11484:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
                   11485:                     } else {
                   11486:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
                   11487:                     }
                   11488:                 } elsif ($item eq 'usejsme') {
                   11489:                     if ($env{'form.'.$item} eq '1') {
                   11490:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
                   11491:                     } else {
                   11492:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>'; 
                   11493:                     }
1.139     raeburn  11494:                 } elsif ($item eq 'anonsurvey_threshold') {
1.192     raeburn  11495:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
1.198     raeburn  11496:                 } elsif ($item eq 'uploadquota') {
                   11497:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
                   11498:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
                   11499:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
                   11500:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
1.216     raeburn  11501:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
                   11502: 
1.198     raeburn  11503:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
                   11504:                                        '</ul>'.
                   11505:                                        '</li>';
                   11506:                     } else {
                   11507:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
                   11508:                     }
1.257     raeburn  11509:                 } elsif ($item eq 'postsubmit') {
                   11510:                     if ($domdefaults{'postsubmit'} eq 'off') {
                   11511:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
                   11512:                     } else {
                   11513:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
                   11514:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') { 
                   11515:                             $resulttext .= &mt('durations:').'<ul>';
                   11516:                             foreach my $type (@types) {
                   11517:                                 $resulttext .= '<li>';
                   11518:                                 my $timeout;
                   11519:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
                   11520:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
                   11521:                                 }
                   11522:                                 my $display;
                   11523:                                 if ($timeout eq '0') {
                   11524:                                     $display = &mt('unlimited');
                   11525:                                 } elsif ($timeout eq '') {
                   11526:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
                   11527:                                 } else {
                   11528:                                     $display = &mt('[quant,_1,second]',$timeout);
                   11529:                                 }
                   11530:                                 if ($type eq 'community') {
                   11531:                                     $resulttext .= &mt('Communities');
                   11532:                                 } elsif ($type eq 'official') {
                   11533:                                     $resulttext .= &mt('Official courses');
                   11534:                                 } elsif ($type eq 'unofficial') {
                   11535:                                     $resulttext .= &mt('Unofficial courses');
                   11536:                                 } elsif ($type eq 'textbook') {
                   11537:                                     $resulttext .= &mt('Textbook courses');
                   11538:                                 }
                   11539:                                 $resulttext .= ' -- '.$display.'</li>';
                   11540:                             }
                   11541:                             $resulttext .= '</ul>';
                   11542:                         }
                   11543:                         $resulttext .= '</li>';    
                   11544:                     }
1.192     raeburn  11545:                 } elsif ($item eq 'coursecredits') {
                   11546:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
                   11547:                         if (($domdefaults{'officialcredits'} eq '') &&
1.216     raeburn  11548:                             ($domdefaults{'unofficialcredits'} eq '') &&
                   11549:                             ($domdefaults{'textbookcredits'} eq '')) {
1.192     raeburn  11550:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
                   11551:                         } else {
                   11552:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
                   11553:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
                   11554:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
1.216     raeburn  11555:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
1.192     raeburn  11556:                                            '</ul>'.
                   11557:                                            '</li>';
                   11558:                         }
                   11559:                     } else {
                   11560:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
                   11561:                     }
1.264     raeburn  11562:                 } elsif ($item eq 'canclone') {
                   11563:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
                   11564:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
                   11565:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
                   11566:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
                   11567:                         }
                   11568:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
                   11569:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
                   11570:                     } else {
                   11571:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>'; 
                   11572:                     }
1.140     raeburn  11573:                 }
1.121     raeburn  11574:             }
                   11575:             $resulttext .= '</ul>';
                   11576:         } else {
                   11577:             $resulttext = &mt('No changes made to course defaults');
                   11578:         }
                   11579:     } else {
                   11580:         $resulttext = '<span class="LC_error">'.
                   11581:             &mt('An error occurred: [_1]',$putresult).'</span>';
                   11582:     }
                   11583:     return $resulttext;
                   11584: }
                   11585: 
1.231     raeburn  11586: sub modify_selfenrollment {
                   11587:     my ($dom,$lastactref,%domconfig) = @_;
                   11588:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
                   11589:     my @types = ('official','unofficial','community','textbook');
                   11590:     my %titles = &tool_titles();
1.232     raeburn  11591:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
                   11592:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
1.231     raeburn  11593:     $ordered{'default'} = ['types','registered','approval','limit'];
                   11594: 
                   11595:     my (%roles,%shown,%toplevel);
                   11596:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
                   11597: 
                   11598:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
                   11599:         if ($domconfig{'selfenrollment'} eq '') {
                   11600:             $domconfig{'selfenrollment'} = {};
                   11601:         }
                   11602:     }
                   11603:     %toplevel = (
                   11604:                   admin      => 'Configuration Rights',
                   11605:                   default    => 'Default settings',
                   11606:                   validation => 'Validation of self-enrollment requests',
                   11607:                 );
1.233     raeburn  11608:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
1.231     raeburn  11609: 
                   11610:     if (ref($ordered{'admin'}) eq 'ARRAY') {
                   11611:         foreach my $item (@{$ordered{'admin'}}) {
                   11612:             foreach my $type (@types) {
                   11613:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
                   11614:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
                   11615:                 } else {
                   11616:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
                   11617:                 }
                   11618:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
                   11619:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
                   11620:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
                   11621:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
                   11622:                             push(@{$changes{'admin'}{$type}},$item);
                   11623:                         }
                   11624:                     } else {
                   11625:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
                   11626:                             push(@{$changes{'admin'}{$type}},$item);
                   11627:                         }
                   11628:                     }
                   11629:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
                   11630:                     push(@{$changes{'admin'}{$type}},$item);
                   11631:                 }
                   11632:             }
                   11633:         }
                   11634:     }
                   11635: 
                   11636:     foreach my $item (@{$ordered{'default'}}) {
                   11637:         foreach my $type (@types) {
                   11638:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
                   11639:             if ($item eq 'types') {
                   11640:                 unless (($value eq 'all') || ($value eq 'dom')) {
                   11641:                     $value = '';
                   11642:                 }
                   11643:             } elsif ($item eq 'registered') {
                   11644:                 unless ($value eq '1') {
                   11645:                     $value = 0;
                   11646:                 }
                   11647:             } elsif ($item eq 'approval') {
                   11648:                 unless ($value =~ /^[012]$/) {
                   11649:                     $value = 0;
                   11650:                 }
                   11651:             } else {
                   11652:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
                   11653:                     $value = 'none';
                   11654:                 }
                   11655:             }
                   11656:             $selfenrollhash{'default'}{$type}{$item} = $value;
                   11657:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
                   11658:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
                   11659:                     if ($selfenrollhash{'default'}{$type}{$item} ne
                   11660:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
                   11661:                          push(@{$changes{'default'}{$type}},$item);
                   11662:                     }
                   11663:                 } else {
                   11664:                     push(@{$changes{'default'}{$type}},$item);
                   11665:                 }
                   11666:             } else {
                   11667:                 push(@{$changes{'default'}{$type}},$item);
                   11668:             }
                   11669:             if ($item eq 'limit') {
                   11670:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
                   11671:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
                   11672:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
                   11673:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
                   11674:                     }
                   11675:                 } else {
                   11676:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
                   11677:                 }
                   11678:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
                   11679:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
                   11680:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
                   11681:                          push(@{$changes{'default'}{$type}},'cap');
                   11682:                     }
                   11683:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
                   11684:                     push(@{$changes{'default'}{$type}},'cap');
                   11685:                 }
                   11686:             }
                   11687:         }
                   11688:     }
                   11689: 
                   11690:     foreach my $item (@{$itemsref}) {
                   11691:         if ($item eq 'fields') {
                   11692:             my @changed;
                   11693:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
                   11694:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
                   11695:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
                   11696:             }
                   11697:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
                   11698:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
                   11699:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
                   11700:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
                   11701:                 } else {
                   11702:                     @changed = @{$selfenrollhash{'validation'}{$item}};
                   11703:                 }
                   11704:             } else {
                   11705:                 @changed = @{$selfenrollhash{'validation'}{$item}};
                   11706:             }
                   11707:             if (@changed) {
                   11708:                 if ($selfenrollhash{'validation'}{$item}) { 
                   11709:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
                   11710:                 } else {
                   11711:                     $changes{'validation'}{$item} = &mt('None');
                   11712:                 }
                   11713:             }
                   11714:         } else {
                   11715:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
                   11716:             if ($item eq 'markup') {
                   11717:                if ($env{'form.selfenroll_validation_'.$item}) {
                   11718:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
                   11719:                }
                   11720:             }
                   11721:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
                   11722:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
                   11723:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
                   11724:                 }
                   11725:             }
                   11726:         }
                   11727:     }
                   11728: 
                   11729:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
                   11730:                                              $dom);
                   11731:     if ($putresult eq 'ok') {
                   11732:         if (keys(%changes) > 0) {
                   11733:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
                   11734:             $resulttext = &mt('Changes made:').'<ul>';
                   11735:             foreach my $key ('admin','default','validation') {
                   11736:                 if (ref($changes{$key}) eq 'HASH') {
                   11737:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
                   11738:                     if ($key eq 'validation') {
                   11739:                         foreach my $item (@{$itemsref}) {
                   11740:                             if (exists($changes{$key}{$item})) {
                   11741:                                 if ($item eq 'markup') {
                   11742:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
                   11743:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
                   11744:                                 } else {  
                   11745:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
                   11746:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
                   11747:                                 }
                   11748:                             }
                   11749:                         }
                   11750:                     } else {
                   11751:                         foreach my $type (@types) {
                   11752:                             if ($type eq 'community') {
                   11753:                                 $roles{'1'} = &mt('Community personnel');
                   11754:                             } else {
                   11755:                                 $roles{'1'} = &mt('Course personnel');
                   11756:                             }
                   11757:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
1.232     raeburn  11758:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
                   11759:                                     if ($key eq 'admin') {
                   11760:                                         my @mgrdc = ();
                   11761:                                         if (ref($ordered{$key}) eq 'ARRAY') {
                   11762:                                             foreach my $item (@{$ordered{'admin'}}) {
                   11763:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
                   11764:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
                   11765:                                                         push(@mgrdc,$item);
                   11766:                                                     }
                   11767:                                                 }
                   11768:                                             }
                   11769:                                             if (@mgrdc) {
                   11770:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
                   11771:                                             } else {
                   11772:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
                   11773:                                             }
                   11774:                                         }
                   11775:                                     } else {
                   11776:                                         if (ref($ordered{$key}) eq 'ARRAY') {
                   11777:                                             foreach my $item (@{$ordered{$key}}) {
                   11778:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
                   11779:                                                     $domdefaults{$type.'selfenroll'.$item} =
                   11780:                                                         $selfenrollhash{$key}{$type}{$item};
                   11781:                                                 }
                   11782:                                             }
                   11783:                                         }
                   11784:                                     }
                   11785:                                 }
1.231     raeburn  11786:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
                   11787:                                 foreach my $item (@{$ordered{$key}}) {
                   11788:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
                   11789:                                         $resulttext .= '<li>';
                   11790:                                         if ($key eq 'admin') {
                   11791:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
                   11792:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
                   11793:                                         } else {
                   11794:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
                   11795:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
                   11796:                                         }
                   11797:                                         $resulttext .= '</li>';
                   11798:                                     }
                   11799:                                 }
                   11800:                                 $resulttext .= '</ul></li>';
                   11801:                             }
                   11802:                         }
                   11803:                         $resulttext .= '</ul></li>'; 
                   11804:                     }
                   11805:                 }
1.232     raeburn  11806:                 if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
                   11807:                     my $cachetime = 24*60*60;
                   11808:                     &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
                   11809:                     if (ref($lastactref) eq 'HASH') {
                   11810:                         $lastactref->{'domdefaults'} = 1;
                   11811:                     }
                   11812:                 }
1.231     raeburn  11813:             }
                   11814:             $resulttext .= '</ul>';
                   11815:         } else {
                   11816:             $resulttext = &mt('No changes made to self-enrollment settings');
                   11817:         }
                   11818:     } else {
                   11819:         $resulttext = '<span class="LC_error">'.
                   11820:             &mt('An error occurred: [_1]',$putresult).'</span>';
                   11821:     }
                   11822:     return $resulttext;
                   11823: }
                   11824: 
1.137     raeburn  11825: sub modify_usersessions {
1.212     raeburn  11826:     my ($dom,$lastactref,%domconfig) = @_;
1.145     raeburn  11827:     my @hostingtypes = ('version','excludedomain','includedomain');
                   11828:     my @offloadtypes = ('primary','default');
                   11829:     my %types = (
                   11830:                   remote => \@hostingtypes,
                   11831:                   hosted => \@hostingtypes,
                   11832:                   spares => \@offloadtypes,
                   11833:                 );
                   11834:     my @prefixes = ('remote','hosted','spares');
1.137     raeburn  11835:     my @lcversions = &Apache::lonnet::all_loncaparevs();
1.138     raeburn  11836:     my (%by_ip,%by_location,@intdoms);
                   11837:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location);
                   11838:     my @locations = sort(keys(%by_location));
1.137     raeburn  11839:     my (%defaultshash,%changes);
                   11840:     foreach my $prefix (@prefixes) {
                   11841:         $defaultshash{'usersessions'}{$prefix} = {};
                   11842:     }
1.212     raeburn  11843:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
1.137     raeburn  11844:     my $resulttext;
1.138     raeburn  11845:     my %iphost = &Apache::lonnet::get_iphost();
1.137     raeburn  11846:     foreach my $prefix (@prefixes) {
1.145     raeburn  11847:         next if ($prefix eq 'spares');
                   11848:         foreach my $type (@{$types{$prefix}}) {
1.137     raeburn  11849:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
                   11850:             if ($type eq 'version') {
                   11851:                 my $value = $env{'form.'.$prefix.'_'.$type};
                   11852:                 my $okvalue;
                   11853:                 if ($value ne '') {
                   11854:                     if (grep(/^\Q$value\E$/,@lcversions)) {
                   11855:                         $okvalue = $value;
                   11856:                     }
                   11857:                 }
                   11858:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
                   11859:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
                   11860:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
                   11861:                             if ($inuse == 0) {
                   11862:                                 $changes{$prefix}{$type} = 1;
                   11863:                             } else {
                   11864:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
                   11865:                                     $changes{$prefix}{$type} = 1;
                   11866:                                 }
                   11867:                                 if ($okvalue ne '') {
                   11868:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
                   11869:                                 } 
                   11870:                             }
                   11871:                         } else {
                   11872:                             if (($inuse == 1) && ($okvalue ne '')) {
                   11873:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
                   11874:                                 $changes{$prefix}{$type} = 1;
                   11875:                             }
                   11876:                         }
                   11877:                     } else {
                   11878:                         if (($inuse == 1) && ($okvalue ne '')) {
                   11879:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
                   11880:                             $changes{$prefix}{$type} = 1;
                   11881:                         }
                   11882:                     }
                   11883:                 } else {
                   11884:                     if (($inuse == 1) && ($okvalue ne '')) {
                   11885:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
                   11886:                         $changes{$prefix}{$type} = 1;
                   11887:                     }
                   11888:                 }
                   11889:             } else {
                   11890:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
                   11891:                 my @okvals;
                   11892:                 foreach my $val (@vals) {
1.138     raeburn  11893:                     if ($val =~ /:/) {
                   11894:                         my @items = split(/:/,$val);
                   11895:                         foreach my $item (@items) {
                   11896:                             if (ref($by_location{$item}) eq 'ARRAY') {
                   11897:                                 push(@okvals,$item);
                   11898:                             }
                   11899:                         }
                   11900:                     } else {
                   11901:                         if (ref($by_location{$val}) eq 'ARRAY') {
                   11902:                             push(@okvals,$val);
                   11903:                         }
1.137     raeburn  11904:                     }
                   11905:                 }
                   11906:                 @okvals = sort(@okvals);
                   11907:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
                   11908:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
                   11909:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
                   11910:                             if ($inuse == 0) {
                   11911:                                 $changes{$prefix}{$type} = 1; 
                   11912:                             } else {
                   11913:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
                   11914:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
                   11915:                                 if (@changed > 0) {
                   11916:                                     $changes{$prefix}{$type} = 1;
                   11917:                                 }
                   11918:                             }
                   11919:                         } else {
                   11920:                             if ($inuse == 1) {
                   11921:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
                   11922:                                 $changes{$prefix}{$type} = 1;
                   11923:                             }
                   11924:                         } 
                   11925:                     } else {
                   11926:                         if ($inuse == 1) {
                   11927:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
                   11928:                             $changes{$prefix}{$type} = 1;
                   11929:                         }
                   11930:                     }
                   11931:                 } else {
                   11932:                     if ($inuse == 1) {
                   11933:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
                   11934:                         $changes{$prefix}{$type} = 1;
                   11935:                     }
                   11936:                 }
                   11937:             }
                   11938:         }
                   11939:     }
1.145     raeburn  11940: 
                   11941:     my @alldoms = &Apache::lonnet::all_domains();
1.149     raeburn  11942:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
1.145     raeburn  11943:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
                   11944:     my $savespares;
                   11945: 
                   11946:     foreach my $lonhost (sort(keys(%servers))) {
                   11947:         my $serverhomeID =
                   11948:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
1.152     raeburn  11949:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
1.145     raeburn  11950:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
                   11951:         my %spareschg;
                   11952:         foreach my $type (@{$types{'spares'}}) {
                   11953:             my @okspares;
                   11954:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
                   11955:             foreach my $server (@checked) {
1.152     raeburn  11956:                 if (&Apache::lonnet::hostname($server) ne '') {
                   11957:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
                   11958:                         unless (grep(/^\Q$server\E$/,@okspares)) {
                   11959:                             push(@okspares,$server);
                   11960:                         }
1.145     raeburn  11961:                     }
                   11962:                 }
                   11963:             }
                   11964:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
                   11965:             my $newspare;
1.152     raeburn  11966:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
                   11967:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
1.145     raeburn  11968:                     $newspare = $new;
                   11969:                 }
                   11970:             }
1.152     raeburn  11971:             my @spares;
                   11972:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
                   11973:                 @spares = sort(@okspares,$newspare);
                   11974:             } else {
                   11975:                 @spares = sort(@okspares);
                   11976:             }
                   11977:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
1.145     raeburn  11978:             if (ref($spareid{$lonhost}) eq 'HASH') {
                   11979:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
1.152     raeburn  11980:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
1.145     raeburn  11981:                     if (@diffs > 0) {
                   11982:                         $spareschg{$type} = 1;
                   11983:                     }
                   11984:                 }
                   11985:             }
                   11986:         }
                   11987:         if (keys(%spareschg) > 0) {
                   11988:             $changes{'spares'}{$lonhost} = \%spareschg;
                   11989:         }
                   11990:     }
1.261     raeburn  11991:     $defaultshash{'usersessions'}{'offloadnow'} = {};
                   11992:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
                   11993:     my @okoffload;
                   11994:     if (@offloadnow) {
                   11995:         foreach my $server (@offloadnow) {
                   11996:             if (&Apache::lonnet::hostname($server) ne '') {
                   11997:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
                   11998:                     push(@okoffload,$server);
                   11999:                 }
                   12000:             }
                   12001:         }
                   12002:         if (@okoffload) {
                   12003:             foreach my $lonhost (@okoffload) {
                   12004:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
                   12005:             }
                   12006:         }
                   12007:     }
1.145     raeburn  12008:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
                   12009:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
                   12010:             if (ref($changes{'spares'}) eq 'HASH') {
                   12011:                 if (keys(%{$changes{'spares'}}) > 0) {
                   12012:                     $savespares = 1;
                   12013:                 }
                   12014:             }
                   12015:         } else {
                   12016:             $savespares = 1;
                   12017:         }
1.261     raeburn  12018:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
                   12019:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
                   12020:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
                   12021:                     $changes{'offloadnow'} = 1;
                   12022:                     last;
                   12023:                 }
                   12024:             }
                   12025:             unless ($changes{'offloadnow'}) {
                   12026:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) { 
                   12027:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
                   12028:                         $changes{'offloadnow'} = 1;
                   12029:                         last;
                   12030:                     }
                   12031:                 }
                   12032:             }
                   12033:         } elsif (@okoffload) {
                   12034:             $changes{'offloadnow'} = 1;
                   12035:         }
                   12036:     } elsif (@okoffload) {
                   12037:         $changes{'offloadnow'} = 1;
1.145     raeburn  12038:     }
1.147     raeburn  12039:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
                   12040:     if ((keys(%changes) > 0) || ($savespares)) {
1.137     raeburn  12041:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
                   12042:                                                  $dom);
                   12043:         if ($putresult eq 'ok') {
                   12044:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
                   12045:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
                   12046:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
                   12047:                 }
                   12048:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
                   12049:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
                   12050:                 }
1.261     raeburn  12051:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
                   12052:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
                   12053:                 }
1.137     raeburn  12054:             }
                   12055:             my $cachetime = 24*60*60;
                   12056:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
1.212     raeburn  12057:             if (ref($lastactref) eq 'HASH') {
                   12058:                 $lastactref->{'domdefaults'} = 1;
                   12059:             }
1.147     raeburn  12060:             if (keys(%changes) > 0) {
                   12061:                 my %lt = &usersession_titles();
                   12062:                 $resulttext = &mt('Changes made:').'<ul>';
                   12063:                 foreach my $prefix (@prefixes) {
                   12064:                     if (ref($changes{$prefix}) eq 'HASH') {
                   12065:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
                   12066:                         if ($prefix eq 'spares') {
                   12067:                             if (ref($changes{$prefix}) eq 'HASH') {
                   12068:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
                   12069:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
1.148     raeburn  12070:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
1.211     raeburn  12071:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
                   12072:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
1.147     raeburn  12073:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
                   12074:                                         foreach my $type (@{$types{$prefix}}) {
                   12075:                                             if ($changes{$prefix}{$lonhost}{$type}) {
                   12076:                                                 my $offloadto = &mt('None');
                   12077:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
                   12078:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
                   12079:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
                   12080:                                                     }
1.145     raeburn  12081:                                                 }
1.147     raeburn  12082:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
1.145     raeburn  12083:                                             }
1.137     raeburn  12084:                                         }
                   12085:                                     }
1.147     raeburn  12086:                                     $resulttext .= '</li>';
1.137     raeburn  12087:                                 }
                   12088:                             }
1.147     raeburn  12089:                         } else {
                   12090:                             foreach my $type (@{$types{$prefix}}) {
                   12091:                                 if (defined($changes{$prefix}{$type})) {
                   12092:                                     my $newvalue;
                   12093:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
                   12094:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
                   12095:                                             if ($type eq 'version') {
                   12096:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
                   12097:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
                   12098:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
                   12099:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
                   12100:                                                 }
1.145     raeburn  12101:                                             }
                   12102:                                         }
                   12103:                                     }
1.147     raeburn  12104:                                     if ($newvalue eq '') {
                   12105:                                         if ($type eq 'version') {
                   12106:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
                   12107:                                         } else {
                   12108:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
                   12109:                                         }
1.145     raeburn  12110:                                     } else {
1.147     raeburn  12111:                                         if ($type eq 'version') {
                   12112:                                             $newvalue .= ' '.&mt('(or later)'); 
                   12113:                                         }
                   12114:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
1.145     raeburn  12115:                                     }
1.137     raeburn  12116:                                 }
                   12117:                             }
                   12118:                         }
1.147     raeburn  12119:                         $resulttext .= '</ul>';
1.137     raeburn  12120:                     }
                   12121:                 }
1.261     raeburn  12122:                 if ($changes{'offloadnow'}) {
                   12123:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
                   12124:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
                   12125:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
                   12126:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
                   12127:                                 $resulttext .= '<li>'.$lonhost.'</li>';
                   12128:                             }
                   12129:                             $resulttext .= '</ul>';
                   12130:                         } else {
                   12131:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
                   12132:                         }
                   12133:                     } else {
                   12134:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
                   12135:                     }
                   12136:                 }
1.147     raeburn  12137:                 $resulttext .= '</ul>';
                   12138:             } else {
                   12139:                 $resulttext = $nochgmsg;
1.137     raeburn  12140:             }
                   12141:         } else {
                   12142:             $resulttext = '<span class="LC_error">'.
                   12143:                           &mt('An error occurred: [_1]',$putresult).'</span>';
                   12144:         }
                   12145:     } else {
1.147     raeburn  12146:         $resulttext = $nochgmsg;
1.137     raeburn  12147:     }
                   12148:     return $resulttext;
                   12149: }
                   12150: 
1.150     raeburn  12151: sub modify_loadbalancing {
                   12152:     my ($dom,%domconfig) = @_;
                   12153:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
                   12154:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
                   12155:     my ($othertitle,$usertypes,$types) =
                   12156:         &Apache::loncommon::sorted_inst_types($dom);
                   12157:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
1.253     raeburn  12158:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
1.150     raeburn  12159:     my @sparestypes = ('primary','default');
                   12160:     my %typetitles = &sparestype_titles();
                   12161:     my $resulttext;
1.171     raeburn  12162:     my (%currbalancer,%currtargets,%currrules,%existing);
                   12163:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
                   12164:         %existing = %{$domconfig{'loadbalancing'}};
                   12165:     }
                   12166:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
                   12167:                               \%currtargets,\%currrules);
                   12168:     my ($saveloadbalancing,%defaultshash,%changes);
                   12169:     my ($alltypes,$othertypes,$titles) =
                   12170:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
                   12171:     my %ruletitles = &offloadtype_text();
                   12172:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
                   12173:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
                   12174:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
                   12175:         if ($balancer eq '') {
                   12176:             next;
                   12177:         }
1.210     raeburn  12178:         if (!exists($servers{$balancer})) {
1.171     raeburn  12179:             if (exists($currbalancer{$balancer})) {
                   12180:                 push(@{$changes{'delete'}},$balancer);
1.150     raeburn  12181:             }
1.171     raeburn  12182:             next;
                   12183:         }
                   12184:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
                   12185:             push(@{$changes{'delete'}},$balancer);
                   12186:             next;
                   12187:         }
                   12188:         if (!exists($currbalancer{$balancer})) {
                   12189:             push(@{$changes{'add'}},$balancer);
                   12190:         }
                   12191:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
                   12192:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
                   12193:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
                   12194:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
                   12195:             $saveloadbalancing = 1;
                   12196:         }
                   12197:         foreach my $sparetype (@sparestypes) {
                   12198:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
                   12199:             my @offloadto;
                   12200:             foreach my $target (@targets) {
                   12201:                 if (($servers{$target}) && ($target ne $balancer)) {
                   12202:                     if ($sparetype eq 'default') {
                   12203:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
                   12204:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
1.150     raeburn  12205:                         }
                   12206:                     }
1.171     raeburn  12207:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
                   12208:                         push(@offloadto,$target);
                   12209:                     }
1.150     raeburn  12210:                 }
1.171     raeburn  12211:                 $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
1.150     raeburn  12212:             }
                   12213:         }
1.171     raeburn  12214:         if (ref($currtargets{$balancer}) eq 'HASH') {
1.150     raeburn  12215:             foreach my $sparetype (@sparestypes) {
1.171     raeburn  12216:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
                   12217:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
1.150     raeburn  12218:                     if (@targetdiffs > 0) {
1.171     raeburn  12219:                         $changes{'curr'}{$balancer}{'targets'} = 1;
1.150     raeburn  12220:                     }
1.171     raeburn  12221:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
                   12222:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
                   12223:                         $changes{'curr'}{$balancer}{'targets'} = 1;
1.150     raeburn  12224:                     }
                   12225:                 }
                   12226:             }
                   12227:         } else {
1.171     raeburn  12228:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
1.210     raeburn  12229:                 foreach my $sparetype (@sparestypes) {
1.171     raeburn  12230:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
                   12231:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
                   12232:                             $changes{'curr'}{$balancer}{'targets'} = 1;
                   12233:                         }
1.150     raeburn  12234:                     }
                   12235:                 }
1.210     raeburn  12236:             }
1.150     raeburn  12237:         }
                   12238:         my $ishomedom;
1.171     raeburn  12239:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
                   12240:             $ishomedom = 1;
1.150     raeburn  12241:         }
                   12242:         if (ref($alltypes) eq 'ARRAY') {
                   12243:             foreach my $type (@{$alltypes}) {
                   12244:                 my $rule;
1.210     raeburn  12245:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
1.150     raeburn  12246:                          (!$ishomedom)) {
1.171     raeburn  12247:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
                   12248:                 }
                   12249:                 if ($rule eq 'specific') {
1.255     raeburn  12250:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
                   12251:                     if (exists($servers{$specifiedhost})) { 
                   12252:                         $rule = $specifiedhost;
                   12253:                     }
1.150     raeburn  12254:                 }
1.171     raeburn  12255:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
                   12256:                 if (ref($currrules{$balancer}) eq 'HASH') {
                   12257:                     if ($rule ne $currrules{$balancer}{$type}) {
                   12258:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
1.150     raeburn  12259:                     }
                   12260:                 } elsif ($rule ne '') {
1.171     raeburn  12261:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
1.150     raeburn  12262:                 }
                   12263:             }
                   12264:         }
1.171     raeburn  12265:     }
                   12266:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
                   12267:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
                   12268:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
                   12269:             $defaultshash{'loadbalancing'} = {};
                   12270:         }
                   12271:         my $putresult = &Apache::lonnet::put_dom('configuration',
                   12272:                                                  \%defaultshash,$dom);
                   12273:         if ($putresult eq 'ok') {
                   12274:             if (keys(%changes) > 0) {
1.252     raeburn  12275:                 my %toupdate;
1.171     raeburn  12276:                 if (ref($changes{'delete'}) eq 'ARRAY') {
                   12277:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
                   12278:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
1.252     raeburn  12279:                         $toupdate{$balancer} = 1;
1.150     raeburn  12280:                     }
1.171     raeburn  12281:                 }
                   12282:                 if (ref($changes{'add'}) eq 'ARRAY') {
1.210     raeburn  12283:                     foreach my $balancer (sort(@{$changes{'add'}})) {
1.171     raeburn  12284:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
1.252     raeburn  12285:                         $toupdate{$balancer} = 1;
1.171     raeburn  12286:                     }
                   12287:                 }
                   12288:                 if (ref($changes{'curr'}) eq 'HASH') {
                   12289:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
1.253     raeburn  12290:                         $toupdate{$balancer} = 1;
1.171     raeburn  12291:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
                   12292:                             if ($changes{'curr'}{$balancer}{'targets'}) {
                   12293:                                 my %offloadstr;
                   12294:                                 foreach my $sparetype (@sparestypes) {
                   12295:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
                   12296:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
                   12297:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
                   12298:                                         }
                   12299:                                     }
1.150     raeburn  12300:                                 }
1.171     raeburn  12301:                                 if (keys(%offloadstr) == 0) {
                   12302:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
1.150     raeburn  12303:                                 } else {
1.171     raeburn  12304:                                     my $showoffload;
                   12305:                                     foreach my $sparetype (@sparestypes) {
                   12306:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
                   12307:                                         if (defined($offloadstr{$sparetype})) {
                   12308:                                             $showoffload .= $offloadstr{$sparetype};
                   12309:                                         } else {
                   12310:                                             $showoffload .= &mt('None');
                   12311:                                         }
                   12312:                                         $showoffload .= ('&nbsp;'x3);
                   12313:                                     }
                   12314:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
1.150     raeburn  12315:                                 }
                   12316:                             }
                   12317:                         }
1.171     raeburn  12318:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
                   12319:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
                   12320:                                 foreach my $type (@{$alltypes}) {
                   12321:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
                   12322:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
                   12323:                                         my $balancetext;
                   12324:                                         if ($rule eq '') {
                   12325:                                             $balancetext =  $ruletitles{'default'};
1.209     raeburn  12326:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
1.254     raeburn  12327:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) { 
                   12328:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
1.252     raeburn  12329:                                                 foreach my $sparetype (@sparestypes) {
                   12330:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
                   12331:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
                   12332:                                                     }
                   12333:                                                 }
1.253     raeburn  12334:                                                 foreach my $item (@{$alltypes}) {
                   12335:                                                     next if ($item =~  /^_LC_ipchange/);
                   12336:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
                   12337:                                                     if ($hasrule eq 'homeserver') {
                   12338:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
                   12339:                                                     } else {
                   12340:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
                   12341:                                                             if ($servers{$hasrule}) {
                   12342:                                                                 $toupdate{$hasrule} = 1;
                   12343:                                                             }
                   12344:                                                         }
                   12345:                                                     }
                   12346:                                                 }
1.254     raeburn  12347:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
                   12348:                                                     $balancetext =  $ruletitles{$rule};
                   12349:                                                 } else {
                   12350:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
                   12351:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
                   12352:                                                     if ($receiver) {
                   12353:                                                         $toupdate{$receiver};
                   12354:                                                     }
                   12355:                                                 }
                   12356:                                             } else {
                   12357:                                                 $balancetext =  $ruletitles{$rule};
1.252     raeburn  12358:                                             }
1.171     raeburn  12359:                                         } else {
                   12360:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
                   12361:                                         }
1.210     raeburn  12362:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
1.150     raeburn  12363:                                     }
                   12364:                                 }
                   12365:                             }
                   12366:                         }
1.252     raeburn  12367:                         if (keys(%toupdate)) {
                   12368:                             my %thismachine;
                   12369:                             my $updatedhere;
                   12370:                             my $cachetime = 60*60*24;
                   12371:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
                   12372:                             foreach my $lonhost (keys(%toupdate)) {
                   12373:                                 if ($thismachine{$lonhost}) {
                   12374:                                     unless ($updatedhere) {
                   12375:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
                   12376:                                                                       $defaultshash{'loadbalancing'},
                   12377:                                                                       $cachetime);
                   12378:                                         $updatedhere = 1;
                   12379:                                     }
                   12380:                                 } else {
                   12381:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
                   12382:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
                   12383:                                 }
                   12384:                             }
                   12385:                         }
1.150     raeburn  12386:                     }
1.171     raeburn  12387:                 }
                   12388:                 if ($resulttext ne '') {
                   12389:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
1.150     raeburn  12390:                 } else {
                   12391:                     $resulttext = $nochgmsg;
                   12392:                 }
                   12393:             } else {
1.171     raeburn  12394:                 $resulttext = $nochgmsg;
1.150     raeburn  12395:             }
                   12396:         } else {
1.171     raeburn  12397:             $resulttext = '<span class="LC_error">'.
                   12398:                           &mt('An error occurred: [_1]',$putresult).'</span>';
1.150     raeburn  12399:         }
                   12400:     } else {
1.171     raeburn  12401:         $resulttext = $nochgmsg;
1.150     raeburn  12402:     }
                   12403:     return $resulttext;
                   12404: }
                   12405: 
1.48      raeburn  12406: sub recurse_check {
                   12407:     my ($chkcats,$categories,$depth,$name) = @_;
                   12408:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
                   12409:         my $chg = 0;
                   12410:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
                   12411:             my $category = $chkcats->[$depth]{$name}[$j];
                   12412:             my $item;
                   12413:             if ($category eq '') {
                   12414:                 $chg ++;
                   12415:             } else {
                   12416:                 my $deeper = $depth + 1;
                   12417:                 $item = &escape($category).':'.&escape($name).':'.$depth;
                   12418:                 if ($chg) {
                   12419:                     $categories->{$item} -= $chg;
                   12420:                 }
                   12421:                 &recurse_check($chkcats,$categories,$deeper,$category);
                   12422:                 $deeper --;
                   12423:             }
                   12424:         }
                   12425:     }
                   12426:     return;
                   12427: }
                   12428: 
                   12429: sub recurse_cat_deletes {
                   12430:     my ($item,$coursecategories,$deletions) = @_;
                   12431:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
                   12432:     my $subdepth = $depth + 1;
                   12433:     if (ref($coursecategories) eq 'HASH') {
                   12434:         foreach my $subitem (keys(%{$coursecategories})) {
                   12435:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
                   12436:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
                   12437:                 delete($coursecategories->{$subitem});
                   12438:                 $deletions->{$subitem} = 1;
                   12439:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
1.168     raeburn  12440:             }
1.48      raeburn  12441:         }
                   12442:     }
                   12443:     return;
                   12444: }
                   12445: 
1.125     raeburn  12446: sub get_active_dcs {
                   12447:     my ($dom) = @_;
1.191     raeburn  12448:     my $now = time;
                   12449:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
1.125     raeburn  12450:     my %domcoords;
                   12451:     my $numdcs = 0;
                   12452:     foreach my $server (keys(%dompersonnel)) {
                   12453:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
                   12454:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
1.191     raeburn  12455:             $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
1.125     raeburn  12456:         }
                   12457:     }
                   12458:     return %domcoords;
                   12459: }
                   12460: 
                   12461: sub active_dc_picker {
1.191     raeburn  12462:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
1.235     raeburn  12463:     my %domcoords = &get_active_dcs($dom);
1.191     raeburn  12464:     my @domcoord = keys(%domcoords);
                   12465:     if (keys(%currhash)) {
                   12466:         foreach my $dc (keys(%currhash)) {
                   12467:             unless (exists($domcoords{$dc})) {
                   12468:                 push(@domcoord,$dc);
                   12469:             }
                   12470:         }
                   12471:     }
                   12472:     @domcoord = sort(@domcoord);
1.210     raeburn  12473:     my $numdcs = scalar(@domcoord);
1.191     raeburn  12474:     my $rows = 0;
                   12475:     my $table;
1.125     raeburn  12476:     if ($numdcs > 1) {
1.191     raeburn  12477:         $table = '<table>';
                   12478:         for (my $i=0; $i<@domcoord; $i++) {
1.125     raeburn  12479:             my $rem = $i%($numinrow);
                   12480:             if ($rem == 0) {
                   12481:                 if ($i > 0) {
1.191     raeburn  12482:                     $table .= '</tr>';
1.125     raeburn  12483:                 }
1.191     raeburn  12484:                 $table .= '<tr>';
                   12485:                 $rows ++;
1.125     raeburn  12486:             }
1.191     raeburn  12487:             my $check = '';
                   12488:             if ($inputtype eq 'radio') {
                   12489:                 if (keys(%currhash) == 0) {
                   12490:                     if (!$i) {
                   12491:                         $check = ' checked="checked"';
                   12492:                     }
                   12493:                 } elsif (exists($currhash{$domcoord[$i]})) {
                   12494:                     $check = ' checked="checked"';
                   12495:                 }
                   12496:             } else {
                   12497:                 if (exists($currhash{$domcoord[$i]})) {
                   12498:                     $check = ' checked="checked"';
1.125     raeburn  12499:                 }
                   12500:             }
1.191     raeburn  12501:             if ($i == @domcoord - 1) {
1.125     raeburn  12502:                 my $colsleft = $numinrow - $rem;
                   12503:                 if ($colsleft > 1) {
1.191     raeburn  12504:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
1.125     raeburn  12505:                 } else {
1.191     raeburn  12506:                     $table .= '<td class="LC_left_item">';
1.125     raeburn  12507:                 }
                   12508:             } else {
1.191     raeburn  12509:                 $table .= '<td class="LC_left_item">';
                   12510:             }
                   12511:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
                   12512:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
                   12513:             $table .= '<span class="LC_nobreak"><label>'.
                   12514:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
                   12515:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
                   12516:             if ($user ne $dcname.':'.$dcdom) {
1.219     raeburn  12517:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
1.191     raeburn  12518:             }
1.219     raeburn  12519:             $table .= '</label></span></td>';
1.191     raeburn  12520:         }
                   12521:         $table .= '</tr></table>';
                   12522:     } elsif ($numdcs == 1) {
1.219     raeburn  12523:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
                   12524:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
1.191     raeburn  12525:         if ($inputtype eq 'radio') {
1.247     raeburn  12526:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
1.219     raeburn  12527:             if ($user ne $dcname.':'.$dcdom) {
                   12528:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
                   12529:             }
1.191     raeburn  12530:         } else {
                   12531:             my $check;
                   12532:             if (exists($currhash{$domcoord[0]})) {
                   12533:                 $check = ' checked="checked"';
1.125     raeburn  12534:             }
1.247     raeburn  12535:             $table = '<span class="LC_nobreak"><label>'.
                   12536:                      '<input type="checkbox" name="'.$name.'" '.
                   12537:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
1.219     raeburn  12538:             if ($user ne $dcname.':'.$dcdom) {
1.220     raeburn  12539:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
1.219     raeburn  12540:             }
1.220     raeburn  12541:             $table .= '</label></span>';
1.191     raeburn  12542:             $rows ++;
1.125     raeburn  12543:         }
                   12544:     }
1.191     raeburn  12545:     return ($numdcs,$table,$rows);
1.125     raeburn  12546: }
                   12547: 
1.137     raeburn  12548: sub usersession_titles {
                   12549:     return &Apache::lonlocal::texthash(
                   12550:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
                   12551:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
1.145     raeburn  12552:                spares => 'Servers offloaded to, when busy',
1.137     raeburn  12553:                version => 'LON-CAPA version requirement',
1.138     raeburn  12554:                excludedomain => 'Allow all, but exclude specific domains',
                   12555:                includedomain => 'Deny all, but include specific domains',
1.145     raeburn  12556:                primary => 'Primary (checked first)',
1.154     raeburn  12557:                default => 'Default',
1.137     raeburn  12558:            );
                   12559: }
                   12560: 
1.152     raeburn  12561: sub id_for_thisdom {
                   12562:     my (%servers) = @_;
                   12563:     my %altids;
                   12564:     foreach my $server (keys(%servers)) {
                   12565:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
                   12566:         if ($serverhome ne $server) {
                   12567:             $altids{$serverhome} = $server;
                   12568:         }
                   12569:     }
                   12570:     return %altids;
                   12571: }
                   12572: 
1.150     raeburn  12573: sub count_servers {
                   12574:     my ($currbalancer,%servers) = @_;
                   12575:     my (@spares,$numspares);
                   12576:     foreach my $lonhost (sort(keys(%servers))) {
                   12577:         next if ($currbalancer eq $lonhost);
                   12578:         push(@spares,$lonhost);
                   12579:     }
                   12580:     if ($currbalancer) {
                   12581:         $numspares = scalar(@spares);
                   12582:     } else {
                   12583:         $numspares = scalar(@spares) - 1;
                   12584:     }
                   12585:     return ($numspares,@spares);
                   12586: }
                   12587: 
                   12588: sub lonbalance_targets_js {
1.171     raeburn  12589:     my ($dom,$types,$servers,$settings) = @_;
1.150     raeburn  12590:     my $select = &mt('Select');
                   12591:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
                   12592:     if (ref($servers) eq 'HASH') {
                   12593:         $alltargets = join("','",sort(keys(%{$servers})));
                   12594:         my @homedoms;
                   12595:         foreach my $server (sort(keys(%{$servers}))) {
                   12596:             if (&Apache::lonnet::host_domain($server) eq $dom) {
                   12597:                 push(@homedoms,'1');
                   12598:             } else {
                   12599:                 push(@homedoms,'0');
                   12600:             }
                   12601:         }
                   12602:         $allishome = join("','",@homedoms);
                   12603:     }
                   12604:     if (ref($types) eq 'ARRAY') {
                   12605:         if (@{$types} > 0) {
                   12606:             @alltypes = @{$types};
                   12607:         }
                   12608:     }
                   12609:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
                   12610:     $allinsttypes = join("','",@alltypes);
1.171     raeburn  12611:     my (%currbalancer,%currtargets,%currrules,%existing);
                   12612:     if (ref($settings) eq 'HASH') {
                   12613:         %existing = %{$settings};
                   12614:     }
                   12615:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
                   12616:                               \%currtargets,\%currrules);
1.210     raeburn  12617:     my $balancers = join("','",sort(keys(%currbalancer)));
1.150     raeburn  12618:     return <<"END";
                   12619: 
                   12620: <script type="text/javascript">
                   12621: // <![CDATA[
                   12622: 
1.171     raeburn  12623: currBalancers = new Array('$balancers');
                   12624: 
                   12625: function toggleTargets(balnum) {
                   12626:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
                   12627:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
                   12628:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
                   12629:     var prevbalancer = prevhostitem.value;
                   12630:     var baltotal = document.getElementById('loadbalancing_total').value;
                   12631:     prevhostitem.value = balancer;
                   12632:     if (prevbalancer != '') {
                   12633:         var prevIdx = currBalancers.indexOf(prevbalancer);
                   12634:         if (prevIdx != -1) {
                   12635:             currBalancers.splice(prevIdx,1);
                   12636:         }
                   12637:     }
1.150     raeburn  12638:     if (balancer == '') {
1.171     raeburn  12639:         hideSpares(balnum);
1.150     raeburn  12640:     } else {
1.171     raeburn  12641:         var currIdx = currBalancers.indexOf(balancer);
                   12642:         if (currIdx == -1) {
                   12643:             currBalancers.push(balancer);
                   12644:         }
1.150     raeburn  12645:         var homedoms = new Array('$allishome');
1.171     raeburn  12646:         var ishomedom = homedoms[lonhostitem.selectedIndex];
                   12647:         showSpares(balancer,ishomedom,balnum);
1.150     raeburn  12648:     }
1.171     raeburn  12649:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
1.150     raeburn  12650:     return;
                   12651: }
                   12652: 
1.171     raeburn  12653: function showSpares(balancer,ishomedom,balnum) {
1.150     raeburn  12654:     var alltargets = new Array('$alltargets');
                   12655:     var insttypes = new Array('$allinsttypes');
1.151     raeburn  12656:     var offloadtypes = new Array('primary','default');
                   12657: 
1.171     raeburn  12658:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
                   12659:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
1.152     raeburn  12660:  
1.151     raeburn  12661:     for (var i=0; i<offloadtypes.length; i++) {
                   12662:         var count = 0;
                   12663:         for (var j=0; j<alltargets.length; j++) {
                   12664:             if (alltargets[j] != balancer) {
1.171     raeburn  12665:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
                   12666:                 item.value = alltargets[j];
                   12667:                 item.style.textAlign='left';
                   12668:                 item.style.textFace='normal';
                   12669:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
                   12670:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
                   12671:                     item.disabled = '';
                   12672:                 } else {
                   12673:                     item.disabled = 'disabled';
                   12674:                     item.checked = false;
                   12675:                 }
1.151     raeburn  12676:                 count ++;
                   12677:             }
1.150     raeburn  12678:         }
                   12679:     }
1.151     raeburn  12680:     for (var k=0; k<insttypes.length; k++) {
                   12681:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
1.150     raeburn  12682:             if (ishomedom == 1) {
1.171     raeburn  12683:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
                   12684:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
1.150     raeburn  12685:             } else {
1.171     raeburn  12686:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
                   12687:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
1.150     raeburn  12688:             }
                   12689:         } else {
1.171     raeburn  12690:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
                   12691:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
1.150     raeburn  12692:         }
1.151     raeburn  12693:         if ((insttypes[k] != '_LC_external') && 
                   12694:             ((insttypes[k] != '_LC_internetdom') ||
                   12695:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
1.171     raeburn  12696:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
                   12697:             item.options.length = 0;
                   12698:             item.options[0] = new Option("","",true,true);
1.210     raeburn  12699:             var idx = 0;
1.151     raeburn  12700:             for (var m=0; m<alltargets.length; m++) {
1.171     raeburn  12701:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
                   12702:                     idx ++;
                   12703:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
1.150     raeburn  12704:                 }
                   12705:             }
                   12706:         }
                   12707:     }
                   12708:     return;
                   12709: }
                   12710: 
1.171     raeburn  12711: function hideSpares(balnum) {
1.150     raeburn  12712:     var alltargets = new Array('$alltargets');
                   12713:     var insttypes = new Array('$allinsttypes');
                   12714:     var offloadtypes = new Array('primary','default');
                   12715: 
1.171     raeburn  12716:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
                   12717:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
1.150     raeburn  12718: 
                   12719:     var total = alltargets.length - 1;
                   12720:     for (var i=0; i<offloadtypes; i++) {
                   12721:         for (var j=0; j<total; j++) {
1.171     raeburn  12722:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
                   12723:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
                   12724:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
1.151     raeburn  12725:         }
1.150     raeburn  12726:     }
                   12727:     for (var k=0; k<insttypes.length; k++) {
1.171     raeburn  12728:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
                   12729:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
1.151     raeburn  12730:         if (insttypes[k] != '_LC_external') {
1.171     raeburn  12731:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
                   12732:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
1.150     raeburn  12733:         }
                   12734:     }
                   12735:     return;
                   12736: }
                   12737: 
1.171     raeburn  12738: function checkOffloads(item,balnum,type) {
1.150     raeburn  12739:     var alltargets = new Array('$alltargets');
                   12740:     var offloadtypes = new Array('primary','default');
                   12741:     if (item.checked) {
                   12742:         var total = alltargets.length - 1;
                   12743:         var other;
                   12744:         if (type == offloadtypes[0]) {
1.151     raeburn  12745:             other = offloadtypes[1];
1.150     raeburn  12746:         } else {
1.151     raeburn  12747:             other = offloadtypes[0];
1.150     raeburn  12748:         }
                   12749:         for (var i=0; i<total; i++) {
1.171     raeburn  12750:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
1.150     raeburn  12751:             if (server == item.value) {
1.171     raeburn  12752:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
                   12753:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
1.150     raeburn  12754:                 }
                   12755:             }
                   12756:         }
                   12757:     }
                   12758:     return;
                   12759: }
                   12760: 
1.171     raeburn  12761: function singleServerToggle(balnum,type) {
                   12762:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
1.150     raeburn  12763:     if (offloadtoSelIdx == 0) {
1.171     raeburn  12764:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
                   12765:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
1.150     raeburn  12766: 
                   12767:     } else {
1.171     raeburn  12768:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
                   12769:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
1.150     raeburn  12770:     }
                   12771:     return;
                   12772: }
                   12773: 
1.171     raeburn  12774: function balanceruleChange(formname,balnum,type) {
1.150     raeburn  12775:     if (type == '_LC_external') {
1.171     raeburn  12776:         return;
1.150     raeburn  12777:     }
1.171     raeburn  12778:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
1.150     raeburn  12779:     for (var i=0; i<typesRules.length; i++) {
                   12780:         if (formname.elements[typesRules[i]].checked) {
                   12781:             if (formname.elements[typesRules[i]].value != 'specific') {
1.171     raeburn  12782:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
                   12783:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
1.150     raeburn  12784:             } else {
1.171     raeburn  12785:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
                   12786:             }
                   12787:         }
                   12788:     }
                   12789:     return;
                   12790: }
                   12791: 
                   12792: function balancerDeleteChange(balnum) {
                   12793:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
                   12794:     var baltotal = document.getElementById('loadbalancing_total').value;
                   12795:     var addtarget;
                   12796:     var removetarget;
                   12797:     var action = 'delete';
                   12798:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
                   12799:         var lonhost = hostitem.value;
                   12800:         var currIdx = currBalancers.indexOf(lonhost);
                   12801:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
                   12802:             if (currIdx != -1) {
                   12803:                 currBalancers.splice(currIdx,1);
                   12804:             }
                   12805:             addtarget = lonhost;
                   12806:         } else {
                   12807:             if (currIdx == -1) {
                   12808:                 currBalancers.push(lonhost);
                   12809:             }
                   12810:             removetarget = lonhost;
                   12811:             action = 'undelete';
                   12812:         }
                   12813:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
                   12814:     }
                   12815:     return;
                   12816: }
                   12817: 
                   12818: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
                   12819:     if (baltotal > 1) {
                   12820:         var offloadtypes = new Array('primary','default');
                   12821:         var alltargets = new Array('$alltargets');
                   12822:         var insttypes = new Array('$allinsttypes');
                   12823:         for (var i=0; i<baltotal; i++) {
                   12824:             if (i != balnum) {
                   12825:                 for (var j=0; j<offloadtypes.length; j++) {
                   12826:                     var total = alltargets.length - 1;
                   12827:                     for (var k=0; k<total; k++) {
                   12828:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
                   12829:                         var server = serveritem.value;
                   12830:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
                   12831:                             if (server == addtarget) {
                   12832:                                 serveritem.disabled = '';
                   12833:                             }
                   12834:                         }
                   12835:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
                   12836:                             if (server == removetarget) {
                   12837:                                 serveritem.disabled = 'disabled';
                   12838:                                 serveritem.checked = false;
                   12839:                             }
                   12840:                         }
                   12841:                     }
                   12842:                 }
                   12843:                 for (var j=0; j<insttypes.length; j++) {
                   12844:                     if (insttypes[j] != '_LC_external') {
                   12845:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
                   12846:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
                   12847:                             var currSel = singleserver.selectedIndex;
                   12848:                             var currVal = singleserver.options[currSel].value;
                   12849:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
                   12850:                                 var numoptions = singleserver.options.length;
                   12851:                                 var needsnew = 1;
                   12852:                                 for (var k=0; k<numoptions; k++) {
                   12853:                                     if (singleserver.options[k] == addtarget) {
                   12854:                                         needsnew = 0;
                   12855:                                         break;
                   12856:                                     }
                   12857:                                 }
                   12858:                                 if (needsnew == 1) {
                   12859:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
                   12860:                                 }
                   12861:                             }
                   12862:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
                   12863:                                 singleserver.options.length = 0;
                   12864:                                 if ((currVal) && (currVal != removetarget)) {
                   12865:                                     singleserver.options[0] = new Option("","",false,false);
                   12866:                                 } else {
                   12867:                                     singleserver.options[0] = new Option("","",true,true);
                   12868:                                 }
                   12869:                                 var idx = 0;
                   12870:                                 for (var m=0; m<alltargets.length; m++) {
                   12871:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
                   12872:                                         idx ++;
                   12873:                                         if (currVal == alltargets[m]) {
                   12874:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
                   12875:                                         } else {
                   12876:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
                   12877:                                         }
                   12878:                                     }
                   12879:                                 }
                   12880:                             }
                   12881:                         }
                   12882:                     }
                   12883:                 }
1.150     raeburn  12884:             }
                   12885:         }
                   12886:     }
                   12887:     return;
                   12888: }
                   12889: 
1.152     raeburn  12890: // ]]>
                   12891: </script>
                   12892: 
                   12893: END
                   12894: }
                   12895: 
                   12896: sub new_spares_js {
                   12897:     my @sparestypes = ('primary','default');
                   12898:     my $types = join("','",@sparestypes);
                   12899:     my $select = &mt('Select');
                   12900:     return <<"END";
                   12901: 
                   12902: <script type="text/javascript">
                   12903: // <![CDATA[
                   12904: 
                   12905: function updateNewSpares(formname,lonhost) {
                   12906:     var types = new Array('$types');
                   12907:     var include = new Array();
                   12908:     var exclude = new Array();
                   12909:     for (var i=0; i<types.length; i++) {
                   12910:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
                   12911:         for (var j=0; j<spareboxes.length; j++) {
                   12912:             if (formname.elements[spareboxes[j]].checked) {
                   12913:                 exclude.push(formname.elements[spareboxes[j]].value);
                   12914:             } else {
                   12915:                 include.push(formname.elements[spareboxes[j]].value);
                   12916:             }
                   12917:         }
                   12918:     }
                   12919:     for (var i=0; i<types.length; i++) {
                   12920:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
                   12921:         var selIdx = newSpare.selectedIndex;
                   12922:         var currnew = newSpare.options[selIdx].value;
                   12923:         var okSpares = new Array();
                   12924:         for (var j=0; j<newSpare.options.length; j++) {
                   12925:             var possible = newSpare.options[j].value;
                   12926:             if (possible != '') {
                   12927:                 if (exclude.indexOf(possible) == -1) {
                   12928:                     okSpares.push(possible);
                   12929:                 } else {
                   12930:                     if (currnew == possible) {
                   12931:                         selIdx = 0;
                   12932:                     }
                   12933:                 }
                   12934:             }
                   12935:         }
                   12936:         for (var k=0; k<include.length; k++) {
                   12937:             if (okSpares.indexOf(include[k]) == -1) {
                   12938:                 okSpares.push(include[k]);
                   12939:             }
                   12940:         }
                   12941:         okSpares.sort();
                   12942:         newSpare.options.length = 0;
                   12943:         if (selIdx == 0) {
                   12944:             newSpare.options[0] = new Option("$select","",true,true);
                   12945:         } else {
                   12946:             newSpare.options[0] = new Option("$select","",false,false);
                   12947:         }
                   12948:         for (var m=0; m<okSpares.length; m++) {
                   12949:             var idx = m+1;
                   12950:             var selThis = 0;
                   12951:             if (selIdx != 0) {
                   12952:                 if (okSpares[m] == currnew) {
                   12953:                     selThis = 1;
                   12954:                 }
                   12955:             }
                   12956:             if (selThis == 1) {
                   12957:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
                   12958:             } else {
                   12959:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
                   12960:             }
                   12961:         }
                   12962:     }
                   12963:     return;
                   12964: }
                   12965: 
                   12966: function checkNewSpares(lonhost,type) {
                   12967:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
                   12968:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
                   12969:     if (chosen != '') { 
                   12970:         var othertype;
                   12971:         var othernewSpare;
                   12972:         if (type == 'primary') {
                   12973:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
                   12974:         }
                   12975:         if (type == 'default') {
                   12976:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
                   12977:         }
                   12978:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
                   12979:             othernewSpare.selectedIndex = 0;
                   12980:         }
                   12981:     }
                   12982:     return;
                   12983: }
                   12984: 
                   12985: // ]]>
                   12986: </script>
                   12987: 
                   12988: END
                   12989: 
                   12990: }
                   12991: 
                   12992: sub common_domprefs_js {
                   12993:     return <<"END";
                   12994: 
                   12995: <script type="text/javascript">
                   12996: // <![CDATA[
                   12997: 
1.150     raeburn  12998: function getIndicesByName(formname,item) {
1.152     raeburn  12999:     var group = new Array();
1.150     raeburn  13000:     for (var i=0;i<formname.elements.length;i++) {
                   13001:         if (formname.elements[i].name == item) {
1.152     raeburn  13002:             group.push(formname.elements[i].id);
1.150     raeburn  13003:         }
                   13004:     }
1.152     raeburn  13005:     return group;
1.150     raeburn  13006: }
                   13007: 
                   13008: // ]]>
                   13009: </script>
                   13010: 
                   13011: END
1.152     raeburn  13012: 
1.150     raeburn  13013: }
                   13014: 
1.165     raeburn  13015: sub recaptcha_js {
                   13016:     my %lt = &captcha_phrases();
                   13017:     return <<"END";
                   13018: 
                   13019: <script type="text/javascript">
                   13020: // <![CDATA[
                   13021: 
                   13022: function updateCaptcha(caller,context) {
                   13023:     var privitem;
                   13024:     var pubitem;
                   13025:     var privtext;
                   13026:     var pubtext;
                   13027:     if (document.getElementById(context+'_recaptchapub')) {
                   13028:         pubitem = document.getElementById(context+'_recaptchapub');
                   13029:     } else {
                   13030:         return;
                   13031:     }
                   13032:     if (document.getElementById(context+'_recaptchapriv')) {
                   13033:         privitem = document.getElementById(context+'_recaptchapriv');
                   13034:     } else {
                   13035:         return;
                   13036:     }
                   13037:     if (document.getElementById(context+'_recaptchapubtxt')) {
                   13038:         pubtext = document.getElementById(context+'_recaptchapubtxt');
                   13039:     } else {
                   13040:         return;
                   13041:     }
                   13042:     if (document.getElementById(context+'_recaptchaprivtxt')) {
                   13043:         privtext = document.getElementById(context+'_recaptchaprivtxt');
                   13044:     } else {
                   13045:         return;
                   13046:     }
                   13047:     if (caller.checked) {
                   13048:         if (caller.value == 'recaptcha') {
                   13049:             pubitem.type = 'text';
                   13050:             privitem.type = 'text';
                   13051:             pubitem.size = '40';
                   13052:             privitem.size = '40';
                   13053:             pubtext.innerHTML = "$lt{'pub'}";
                   13054:             privtext.innerHTML = "$lt{'priv'}";
                   13055:         } else {
                   13056:             pubitem.type = 'hidden';
                   13057:             privitem.type = 'hidden';
                   13058:             pubtext.innerHTML = '';
                   13059:             privtext.innerHTML = '';
                   13060:         }
                   13061:     }
                   13062:     return;
                   13063: }
                   13064: 
                   13065: // ]]>
                   13066: </script>
                   13067: 
                   13068: END
                   13069: 
                   13070: }
                   13071: 
1.236     raeburn  13072: sub toggle_display_js {
1.192     raeburn  13073:     return <<"END";
                   13074: 
                   13075: <script type="text/javascript">
                   13076: // <![CDATA[
                   13077: 
1.236     raeburn  13078: function toggleDisplay(domForm,caller) {
                   13079:     if (document.getElementById(caller)) {
                   13080:         var divitem = document.getElementById(caller);
                   13081:         var optionsElement = domForm.coursecredits;
1.264     raeburn  13082:         var checkval = 1;
                   13083:         var dispval = 'block';
1.236     raeburn  13084:         if (caller == 'emailoptions') {
                   13085:             optionsElement = domForm.cancreate_email; 
                   13086:         }
1.257     raeburn  13087:         if (caller == 'studentsubmission') {
                   13088:             optionsElement = domForm.postsubmit;
                   13089:         }
1.264     raeburn  13090:         if (caller == 'cloneinstcode') {
                   13091:             optionsElement = domForm.canclone;
                   13092:             checkval = 'instcode';
                   13093:         }
1.236     raeburn  13094:         if (optionsElement.length) {
1.192     raeburn  13095:             var currval;
1.236     raeburn  13096:             for (var i=0; i<optionsElement.length; i++) {
                   13097:                 if (optionsElement[i].checked) {
                   13098:                    currval = optionsElement[i].value;
1.192     raeburn  13099:                 }
                   13100:             }
1.264     raeburn  13101:             if (currval == checkval) {
                   13102:                 divitem.style.display = dispval;
1.192     raeburn  13103:             } else {
1.236     raeburn  13104:                 divitem.style.display = 'none';
1.192     raeburn  13105:             }
                   13106:         }
                   13107:     }
                   13108:     return;
                   13109: }
                   13110: 
                   13111: // ]]>
                   13112: </script>
                   13113: 
                   13114: END
                   13115: 
                   13116: }
                   13117: 
1.165     raeburn  13118: sub captcha_phrases {
                   13119:     return &Apache::lonlocal::texthash (
                   13120:                  priv => 'Private key',
                   13121:                  pub  => 'Public key',
                   13122:                  original  => 'original (CAPTCHA)',
                   13123:                  recaptcha => 'successor (ReCAPTCHA)',
                   13124:                  notused   => 'unused',
                   13125:     );
                   13126: }
                   13127: 
1.205     raeburn  13128: sub devalidate_remote_domconfs {
1.212     raeburn  13129:     my ($dom,$cachekeys) = @_;
                   13130:     return unless (ref($cachekeys) eq 'HASH');
1.205     raeburn  13131:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
                   13132:     my %thismachine;
                   13133:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
1.267   ! raeburn  13134:     my @posscached = ('domainconfig','domdefaults','ltitools');
1.260     raeburn  13135:     if (keys(%servers)) {
1.205     raeburn  13136:         foreach my $server (keys(%servers)) {
                   13137:             next if ($thismachine{$server});
1.212     raeburn  13138:             my @cached;
                   13139:             foreach my $name (@posscached) {
                   13140:                 if ($cachekeys->{$name}) {
                   13141:                     push(@cached,&escape($name).':'.&escape($dom));
                   13142:                 }
                   13143:             }
                   13144:             if (@cached) {
                   13145:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
                   13146:             }
1.205     raeburn  13147:         }
                   13148:     }
                   13149:     return;
                   13150: }
                   13151: 
1.3       raeburn  13152: 1;

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