File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.314: download - view: text, annotated - select for diffs
Sat Oct 7 00:50:47 2017 UTC (6 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6867
  - Domain configuration to set default math renderer for <m> tag content.
  - Default renderer for unconfigured domains changed from tth to MathJax.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.314 2017/10/07 00:50:47 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: #
   29: ###############################################################
   30: ###############################################################
   31: 
   32: =pod
   33: 
   34: =head1 NAME
   35: 
   36: Apache::domainprefs.pm
   37: 
   38: =head1 SYNOPSIS
   39: 
   40: Handles configuration of a LON-CAPA domain.  
   41: 
   42: This is part of the LearningOnline Network with CAPA project
   43: described at http://www.lon-capa.org.
   44: 
   45: 
   46: =head1 OVERVIEW
   47: 
   48: Each institution using LON-CAPA will typically have a single domain designated 
   49: for use by individuals affiliated with the institution.  Accordingly, each domain
   50: may define a default set of logos and a color scheme which can be used to "brand"
   51: the LON-CAPA instance. In addition, an institution will typically have a language
   52: and timezone which are used for the majority of courses.
   53: 
   54: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   55: host of other domain-wide settings which determine the types of functionality
   56: available to users and courses in the domain.
   57: 
   58: There is also a mechanism to configure cataloging of courses in the domain, and
   59: controls on the operation of automated processes which govern such things as
   60: roster updates, user directory updates and processing of course requests.
   61: 
   62: The domain coordination manual which is built dynamically on install/update of 
   63: LON-CAPA from the relevant help items provides more information about domain 
   64: configuration.
   65: 
   66: Most of the domain settings are stored in the configuration.db GDBM file which is
   67: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   68: where $dom is the domain.  The configuration.db stores settings in a number of 
   69: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   70: the domain as files (e.g., image files for logos etc., or plain text files for
   71: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   72: session hosted on the primary library server in the domain, as these files are 
   73: stored in author space belonging to a special $dom-domainconfig user.   
   74: 
   75: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   76: the current settings, and provides an interface to make modifications.
   77: 
   78: =head1 SUBROUTINES
   79: 
   80: =over
   81: 
   82: =item print_quotas()
   83: 
   84: Inputs: 4 
   85: 
   86: $dom,$settings,$rowtotal,$action.
   87: 
   88: $dom is the domain, $settings is a reference to a hash of current settings for
   89: the current context, $rowtotal is a reference to the scalar used to record the 
   90: number of rows displayed on the page, and $action is the context (quotas, 
   91: requestcourses or requestauthor).
   92: 
   93: The print_quotas routine was orginally created to display/store information
   94: about default quota sizes for portfolio spaces for the different types of 
   95: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   96: but is now also used to manage availability of user tools: 
   97: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   98: used by course owners to request creation of a course, and to display/store
   99: default quota sizes for Authoring Spaces.
  100: 
  101: Outputs: 1
  102: 
  103: $datatable  - HTML containing form elements which allow settings to be changed. 
  104: 
  105: In the case of course requests, radio buttons are displayed for each institutional
  106: affiliate type (and also default, and _LC_adv) for each of the course types 
  107: (official, unofficial, community, textbook, and placement).  
  108: In each case the radio buttons allow the selection of one of four values:  
  109: 
  110: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  111: which have the following effects:
  112: 
  113: 0
  114: 
  115: =over
  116: 
  117: - course requests are not allowed for this course types/affiliation
  118: 
  119: =back
  120: 
  121: approval 
  122: 
  123: =over 
  124: 
  125: - course requests must be approved by a Doman Coordinator in the 
  126: course's domain
  127: 
  128: =back
  129: 
  130: validate 
  131: 
  132: =over
  133: 
  134: - an institutional validation (e.g., check requestor is instructor
  135: of record) needs to be passed before the course will be created.  The required
  136: validation is in localenroll.pm on the primary library server for the course 
  137: domain.
  138: 
  139: =back
  140: 
  141: autolimit 
  142: 
  143: =over
  144:  
  145: - course requests will be processed automatically up to a limit of
  146: N requests for the course type for the particular requestor.
  147: If N is undefined, there is no limit to the number of course requests
  148: which a course owner may submit and have processed automatically. 
  149: 
  150: =back
  151: 
  152: =item modify_quotas() 
  153: 
  154: =back
  155: 
  156: =cut
  157: 
  158: package Apache::domainprefs;
  159: 
  160: use strict;
  161: use Apache::Constants qw(:common :http);
  162: use Apache::lonnet;
  163: use Apache::loncommon();
  164: use Apache::lonhtmlcommon();
  165: use Apache::lonlocal;
  166: use Apache::lonmsg();
  167: use Apache::lonconfigsettings;
  168: use Apache::lonuserutils();
  169: use Apache::loncoursequeueadmin();
  170: use LONCAPA qw(:DEFAULT :match);
  171: use LONCAPA::Enrollment;
  172: use LONCAPA::lonauthcgi();
  173: use LONCAPA::SSL;
  174: use File::Copy;
  175: use Locale::Language;
  176: use DateTime::TimeZone;
  177: use DateTime::Locale;
  178: use Time::HiRes qw( sleep );
  179: 
  180: my $registered_cleanup;
  181: my $modified_urls;
  182: 
  183: sub handler {
  184:     my $r=shift;
  185:     if ($r->header_only) {
  186:         &Apache::loncommon::content_type($r,'text/html');
  187:         $r->send_http_header;
  188:         return OK;
  189:     }
  190: 
  191:     my $context = 'domain';
  192:     my $dom = $env{'request.role.domain'};
  193:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  194:     if (&Apache::lonnet::allowed('mau',$dom)) {
  195:         &Apache::loncommon::content_type($r,'text/html');
  196:         $r->send_http_header;
  197:     } else {
  198:         $env{'user.error.msg'}=
  199:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  200:         return HTTP_NOT_ACCEPTABLE;
  201:     }
  202: 
  203:     $registered_cleanup=0;
  204:     @{$modified_urls}=();
  205: 
  206:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  207:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  208:                                             ['phase','actions']);
  209:     my $phase = 'pickactions';
  210:     if ( exists($env{'form.phase'}) ) {
  211:         $phase = $env{'form.phase'};
  212:     }
  213:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  214:     my %domconfig =
  215:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  216:                 'quotas','autoenroll','autoupdate','autocreate',
  217:                 'directorysrch','usercreation','usermodification',
  218:                 'contacts','defaults','scantron','coursecategories',
  219:                 'serverstatuses','requestcourses','helpsettings',
  220:                 'coursedefaults','usersessions','loadbalancing',
  221:                 'requestauthor','selfenrollment','inststatus',
  222:                 'ltitools','ssl','trust'],$dom);
  223:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
  224:         my %encconfig =
  225:             &Apache::lonnet::get_dom('encconfig',['ltitools'],$dom);
  226:         if (ref($encconfig{'ltitools'}) eq 'HASH') {
  227:             foreach my $id (keys(%{$domconfig{'ltitools'}})) {
  228:                 if (ref($domconfig{'ltitools'}{$id}) eq 'HASH') {
  229:                     foreach my $item ('key','secret') {
  230:                         $domconfig{'ltitools'}{$id}{$item} = $encconfig{'ltitools'}{$id}{$item};
  231:                     }
  232:                 }
  233:             }
  234:         }
  235:     }
  236:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  237:                        'autoupdate','autocreate','directorysrch','contacts',
  238:                        'usercreation','selfcreation','usermodification','scantron',
  239:                        'requestcourses','requestauthor','coursecategories',
  240:                        'serverstatuses','helpsettings','coursedefaults',
  241:                        'ltitools','selfenrollment','usersessions','ssl','trust');
  242:     my %existing;
  243:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  244:         %existing = %{$domconfig{'loadbalancing'}};
  245:     }
  246:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  247:         push(@prefs_order,'loadbalancing');
  248:     }
  249:     my %prefs = (
  250:         'rolecolors' =>
  251:                    { text => 'Default color schemes',
  252:                      help => 'Domain_Configuration_Color_Schemes',
  253:                      header => [{col1 => 'Student Settings',
  254:                                  col2 => '',},
  255:                                 {col1 => 'Coordinator Settings',
  256:                                  col2 => '',},
  257:                                 {col1 => 'Author Settings',
  258:                                  col2 => '',},
  259:                                 {col1 => 'Administrator Settings',
  260:                                  col2 => '',}],
  261:                       print => \&print_rolecolors,
  262:                       modify => \&modify_rolecolors,
  263:                     },
  264:         'login' =>
  265:                     { text => 'Log-in page options',
  266:                       help => 'Domain_Configuration_Login_Page',
  267:                       header => [{col1 => 'Log-in Page Items',
  268:                                   col2 => '',},
  269:                                  {col1 => 'Log-in Help',
  270:                                   col2 => 'Value'},
  271:                                  {col1 => 'Custom HTML in document head',
  272:                                   col2 => 'Value'}],
  273:                       print => \&print_login,
  274:                       modify => \&modify_login,
  275:                     },
  276:         'defaults' => 
  277:                     { text => 'Default authentication/language/timezone/portal/types',
  278:                       help => 'Domain_Configuration_LangTZAuth',
  279:                       header => [{col1 => 'Setting',
  280:                                   col2 => 'Value'},
  281:                                  {col1 => 'Internal Authentication',
  282:                                   col2 => 'Value'},
  283:                                  {col1 => 'Institutional user types',
  284:                                   col2 => 'Name displayed'}],
  285:                       print => \&print_defaults,
  286:                       modify => \&modify_defaults,
  287:                     },
  288:         'quotas' => 
  289:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  290:                       help => 'Domain_Configuration_Quotas',
  291:                       header => [{col1 => 'User affiliation',
  292:                                   col2 => 'Available tools',
  293:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  294:                       print => \&print_quotas,
  295:                       modify => \&modify_quotas,
  296:                     },
  297:         'autoenroll' =>
  298:                    { text => 'Auto-enrollment settings',
  299:                      help => 'Domain_Configuration_Auto_Enrollment',
  300:                      header => [{col1 => 'Configuration setting',
  301:                                  col2 => 'Value(s)'}],
  302:                      print => \&print_autoenroll,
  303:                      modify => \&modify_autoenroll,
  304:                    },
  305:         'autoupdate' => 
  306:                    { text => 'Auto-update settings',
  307:                      help => 'Domain_Configuration_Auto_Updates',
  308:                      header => [{col1 => 'Setting',
  309:                                  col2 => 'Value',},
  310:                                 {col1 => 'Setting',
  311:                                  col2 => 'Affiliation'},
  312:                                 {col1 => 'User population',
  313:                                  col2 => 'Updatable user data'}],
  314:                      print => \&print_autoupdate,
  315:                      modify => \&modify_autoupdate,
  316:                   },
  317:         'autocreate' => 
  318:                   { text => 'Auto-course creation settings',
  319:                      help => 'Domain_Configuration_Auto_Creation',
  320:                      header => [{col1 => 'Configuration Setting',
  321:                                  col2 => 'Value',}],
  322:                      print => \&print_autocreate,
  323:                      modify => \&modify_autocreate,
  324:                   },
  325:         'directorysrch' => 
  326:                   { text => 'Directory searches',
  327:                     help => 'Domain_Configuration_InstDirectory_Search',
  328:                     header => [{col1 => 'Institutional Directory Setting',
  329:                                 col2 => 'Value',},
  330:                                {col1 => 'LON-CAPA Directory Setting',
  331:                                 col2 => 'Value',}],
  332:                     print => \&print_directorysrch,
  333:                     modify => \&modify_directorysrch,
  334:                   },
  335:         'contacts' =>
  336:                   { text => 'E-mail addresses and helpform',
  337:                     help => 'Domain_Configuration_Contact_Info',
  338:                     header => [{col1 => 'Default e-mail addresses',
  339:                                 col2 => 'Value',},
  340:                                {col1 => 'Recipient(s) for notifications',
  341:                                 col2 => 'Value',},
  342:                                {col1 => 'Ask helpdesk form settings',
  343:                                 col2 => 'Value',},],
  344:                     print => \&print_contacts,
  345:                     modify => \&modify_contacts,
  346:                   },
  347:         'usercreation' => 
  348:                   { text => 'User creation',
  349:                     help => 'Domain_Configuration_User_Creation',
  350:                     header => [{col1 => 'Format rule type',
  351:                                 col2 => 'Format rules in force'},
  352:                                {col1 => 'User account creation',
  353:                                 col2 => 'Usernames which may be created',},
  354:                                {col1 => 'Context',
  355:                                 col2 => 'Assignable authentication types'}],
  356:                     print => \&print_usercreation,
  357:                     modify => \&modify_usercreation,
  358:                   },
  359:         'selfcreation' => 
  360:                   { text => 'Users self-creating accounts',
  361:                     help => 'Domain_Configuration_Self_Creation', 
  362:                     header => [{col1 => 'Self-creation with institutional username',
  363:                                 col2 => 'Enabled?'},
  364:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  365:                                 col2 => 'Information user can enter'},
  366:                                {col1 => 'Self-creation with e-mail verification',
  367:                                 col2 => 'Settings'}],
  368:                     print => \&print_selfcreation,
  369:                     modify => \&modify_selfcreation,
  370:                   },
  371:         'usermodification' =>
  372:                   { text => 'User modification',
  373:                     help => 'Domain_Configuration_User_Modification',
  374:                     header => [{col1 => 'Target user has role',
  375:                                 col2 => 'User information updatable in author context'},
  376:                                {col1 => 'Target user has role',
  377:                                 col2 => 'User information updatable in course context'}],
  378:                     print => \&print_usermodification,
  379:                     modify => \&modify_usermodification,
  380:                   },
  381:         'scantron' =>
  382:                   { text => 'Bubblesheet format file',
  383:                     help => 'Domain_Configuration_Scantron_Format',
  384:                     header => [ {col1 => 'Item',
  385:                                  col2 => '',
  386:                               }],
  387:                     print => \&print_scantron,
  388:                     modify => \&modify_scantron,
  389:                   },
  390:         'requestcourses' => 
  391:                  {text => 'Request creation of courses',
  392:                   help => 'Domain_Configuration_Request_Courses',
  393:                   header => [{col1 => 'User affiliation',
  394:                               col2 => 'Availability/Processing of requests',},
  395:                              {col1 => 'Setting',
  396:                               col2 => 'Value'},
  397:                              {col1 => 'Available textbooks',
  398:                               col2 => ''},
  399:                              {col1 => 'Available templates',
  400:                               col2 => ''},
  401:                              {col1 => 'Validation (not official courses)',
  402:                               col2 => 'Value'},],
  403:                   print => \&print_quotas,
  404:                   modify => \&modify_quotas,
  405:                  },
  406:         'requestauthor' =>
  407:                  {text => 'Request Authoring Space',
  408:                   help => 'Domain_Configuration_Request_Author',
  409:                   header => [{col1 => 'User affiliation',
  410:                               col2 => 'Availability/Processing of requests',},
  411:                              {col1 => 'Setting',
  412:                               col2 => 'Value'}],
  413:                   print => \&print_quotas,
  414:                   modify => \&modify_quotas,
  415:                  },
  416:         'coursecategories' =>
  417:                   { text => 'Cataloging of courses/communities',
  418:                     help => 'Domain_Configuration_Cataloging_Courses',
  419:                     header => [{col1 => 'Catalog type/availability',
  420:                                 col2 => '',},
  421:                                {col1 => 'Category settings for standard catalog',
  422:                                 col2 => '',},
  423:                                {col1 => 'Categories',
  424:                                 col2 => '',
  425:                                }],
  426:                     print => \&print_coursecategories,
  427:                     modify => \&modify_coursecategories,
  428:                   },
  429:         'serverstatuses' =>
  430:                  {text   => 'Access to server status pages',
  431:                   help   => 'Domain_Configuration_Server_Status',
  432:                   header => [{col1 => 'Status Page',
  433:                               col2 => 'Other named users',
  434:                               col3 => 'Specific IPs',
  435:                             }],
  436:                   print => \&print_serverstatuses,
  437:                   modify => \&modify_serverstatuses,
  438:                  },
  439:         'helpsettings' =>
  440:                  {text   => 'Support settings',
  441:                   help   => 'Domain_Configuration_Help_Settings',
  442:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  443:                               col2 => 'Value'},
  444:                              {col1 => 'Helpdesk Roles',
  445:                               col2 => 'Settings'},],
  446:                   print  => \&print_helpsettings,
  447:                   modify => \&modify_helpsettings,
  448:                  },
  449:         'coursedefaults' => 
  450:                  {text => 'Course/Community defaults',
  451:                   help => 'Domain_Configuration_Course_Defaults',
  452:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  453:                               col2 => 'Value',},
  454:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  455:                               col2 => 'Value',},],
  456:                   print => \&print_coursedefaults,
  457:                   modify => \&modify_coursedefaults,
  458:                  },
  459:         'selfenrollment' => 
  460:                  {text   => 'Self-enrollment in Course/Community',
  461:                   help   => 'Domain_Configuration_Selfenrollment',
  462:                   header => [{col1 => 'Configuration Rights',
  463:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  464:                              {col1 => 'Defaults',
  465:                               col2 => 'Value'},
  466:                              {col1 => 'Self-enrollment validation (optional)',
  467:                               col2 => 'Value'},],
  468:                   print => \&print_selfenrollment,
  469:                   modify => \&modify_selfenrollment,
  470:                  },
  471:         'privacy' => 
  472:                  {text   => 'User Privacy',
  473:                   help   => 'Domain_Configuration_User_Privacy',
  474:                   header => [{col1 => 'Setting',
  475:                               col2 => 'Value',}],
  476:                   print => \&print_privacy,
  477:                   modify => \&modify_privacy,
  478:                  },
  479:         'usersessions' =>
  480:                  {text  => 'User session hosting/offloading',
  481:                   help  => 'Domain_Configuration_User_Sessions',
  482:                   header => [{col1 => 'Domain server',
  483:                               col2 => 'Servers to offload sessions to when busy'},
  484:                              {col1 => 'Hosting of users from other domains',
  485:                               col2 => 'Rules'},
  486:                              {col1 => "Hosting domain's own users elsewhere",
  487:                               col2 => 'Rules'}],
  488:                   print => \&print_usersessions,
  489:                   modify => \&modify_usersessions,
  490:                  },
  491:         'loadbalancing' =>
  492:                  {text  => 'Dedicated Load Balancer(s)',
  493:                   help  => 'Domain_Configuration_Load_Balancing',
  494:                   header => [{col1 => 'Balancers',
  495:                               col2 => 'Default destinations',
  496:                               col3 => 'User affiliation',
  497:                               col4 => 'Overrides'},
  498:                             ],
  499:                   print => \&print_loadbalancing,
  500:                   modify => \&modify_loadbalancing,
  501:                  },
  502:         'ltitools' => 
  503:                  {text => 'External Tools (LTI)',
  504:                   help => 'Domain_Configuration_LTI_Tools',
  505:                   header => [{col1 => 'Setting',
  506:                               col2 => 'Value',}],
  507:                   print => \&print_ltitools,
  508:                   modify => \&modify_ltitools,
  509:                  },
  510:         'ssl' =>
  511:                  {text  => 'LON-CAPA Network (SSL)',
  512:                   help  => 'Domain_Configuration_Network_SSL',
  513:                   header => [{col1 => 'Server',
  514:                               col2 => 'Certificate Status'},
  515:                              {col1 => 'Connections to other servers',
  516:                               col2 => 'Rules'},
  517:                              {col1 => 'Connections from other servers',
  518:                               col2 => 'Rules'},
  519:                              {col1 => "Replicating domain's published content",
  520:                               col2 => 'Rules'}],
  521:                   print => \&print_ssl,
  522:                   modify => \&modify_ssl,
  523:                  },
  524:         'trust' =>
  525:                  {text   => 'Trust Settings',
  526:                   help   => 'Domain_Configuration_Trust',
  527:                   header => [{col1 => "Access to this domain's content by others",
  528:                               col2 => 'Rules'},
  529:                              {col1 => "Access to other domain's content by this domain",
  530:                               col2 => 'Rules'},
  531:                              {col1 => "Enrollment in this domain's courses by others",
  532:                               col2 => 'Rules',},
  533:                              {col1 => "Co-author roles in this domain for others",
  534:                               col2 => 'Rules',},
  535:                              {col1 => "Co-author roles for this domain's users elsewhere",
  536:                               col2 => 'Rules',},
  537:                              {col1 => "Domain roles in this domain assignable to others",
  538:                               col2 => 'Rules'},
  539:                              {col1 => "Course catalog for this domain displayed elsewhere",
  540:                               col2 => 'Rules'},
  541:                              {col1 => "Requests for creation of courses in this domain by others",
  542:                               col2 => 'Rules'},
  543:                              {col1 => "Users in other domains can send messages to this domain",
  544:                               col2 => 'Rules'},],
  545:                   print => \&print_trust,
  546:                   modify => \&modify_trust,
  547:                  },
  548:     );
  549:     if (keys(%servers) > 1) {
  550:         $prefs{'login'}  = { text   => 'Log-in page options',
  551:                              help   => 'Domain_Configuration_Login_Page',
  552:                             header => [{col1 => 'Log-in Service',
  553:                                         col2 => 'Server Setting',},
  554:                                        {col1 => 'Log-in Page Items',
  555:                                         col2 => ''},
  556:                                        {col1 => 'Log-in Help',
  557:                                         col2 => 'Value'},
  558:                                        {col1 => 'Custom HTML in document head',
  559:                                         col2 => 'Value'}],
  560:                             print => \&print_login,
  561:                             modify => \&modify_login,
  562:                            };
  563:     }
  564: 
  565:     my @roles = ('student','coordinator','author','admin');
  566:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  567:     &Apache::lonhtmlcommon::add_breadcrumb
  568:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  569:       text=>"Settings to display/modify"});
  570:     my $confname = $dom.'-domainconfig';
  571: 
  572:     if ($phase eq 'process') {
  573:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  574:                                                               \%prefs,\%domconfig,$confname,\@roles);
  575:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  576:             $r->rflush();
  577:             &devalidate_remote_domconfs($dom,$result);
  578:         }
  579:     } elsif ($phase eq 'display') {
  580:         my $js = &recaptcha_js().
  581:                  &toggle_display_js();
  582:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  583:             my ($othertitle,$usertypes,$types) =
  584:                 &Apache::loncommon::sorted_inst_types($dom);
  585:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  586:                                           $domconfig{'loadbalancing'}).
  587:                    &new_spares_js().
  588:                    &common_domprefs_js().
  589:                    &Apache::loncommon::javascript_array_indexof();
  590:         }
  591:         if (grep(/^requestcourses$/,@actions)) {
  592:             my $javascript_validations;
  593:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  594:             $js .= <<END;
  595: <script type="text/javascript">
  596: $javascript_validations
  597: </script>
  598: $coursebrowserjs
  599: END
  600:         }
  601:         if (grep(/^selfcreation$/,@actions)) {
  602:             $js .= &selfcreate_javascript();
  603:         }
  604:         if (grep(/^contacts$/,@actions)) {
  605:             $js .= &contacts_javascript();
  606:         }
  607:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  608:     } else {
  609: # check if domconfig user exists for the domain.
  610:         my $servadm = $r->dir_config('lonAdmEMail');
  611:         my ($configuserok,$author_ok,$switchserver) =
  612:             &config_check($dom,$confname,$servadm);
  613:         unless ($configuserok eq 'ok') {
  614:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  615:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  616:                           $confname).
  617:                       '<br />'
  618:             );
  619:             if ($switchserver) {
  620:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  621:                           '<br />'.
  622:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  623:                           '<br />'.
  624:                           &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).
  625:                           '<br />'.
  626:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  627:                 );
  628:             } else {
  629:                 $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.').
  630:                           '<br />'.
  631:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  632:                 );
  633:             }
  634:             $r->print(&Apache::loncommon::end_page());
  635:             return OK;
  636:         }
  637:         if (keys(%domconfig) == 0) {
  638:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  639:             my @ids=&Apache::lonnet::current_machine_ids();
  640:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  641:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  642:                 my @loginimages = ('img','logo','domlogo','login');
  643:                 my $custom_img_count = 0;
  644:                 foreach my $img (@loginimages) {
  645:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  646:                         $custom_img_count ++;
  647:                     }
  648:                 }
  649:                 foreach my $role (@roles) {
  650:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  651:                         $custom_img_count ++;
  652:                     }
  653:                 }
  654:                 if ($custom_img_count > 0) {
  655:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  656:                     my $switch_server = &check_switchserver($dom,$confname);
  657:                     $r->print(
  658:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  659:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  660:     &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 />'.
  661:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  662:                     if ($switch_server) {
  663:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  664:                     }
  665:                     $r->print(&Apache::loncommon::end_page());
  666:                     return OK;
  667:                 }
  668:             }
  669:         }
  670:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  671:     }
  672:     return OK;
  673: }
  674: 
  675: sub process_changes {
  676:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  677:     my %domconfig;
  678:     if (ref($values) eq 'HASH') {
  679:         %domconfig = %{$values};
  680:     }
  681:     my $output;
  682:     if ($action eq 'login') {
  683:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  684:     } elsif ($action eq 'rolecolors') {
  685:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  686:                                      $lastactref,%domconfig);
  687:     } elsif ($action eq 'quotas') {
  688:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  689:     } elsif ($action eq 'autoenroll') {
  690:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  691:     } elsif ($action eq 'autoupdate') {
  692:         $output = &modify_autoupdate($dom,%domconfig);
  693:     } elsif ($action eq 'autocreate') {
  694:         $output = &modify_autocreate($dom,%domconfig);
  695:     } elsif ($action eq 'directorysrch') {
  696:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  697:     } elsif ($action eq 'usercreation') {
  698:         $output = &modify_usercreation($dom,%domconfig);
  699:     } elsif ($action eq 'selfcreation') {
  700:         $output = &modify_selfcreation($dom,$lastactref,%domconfig);
  701:     } elsif ($action eq 'usermodification') {
  702:         $output = &modify_usermodification($dom,%domconfig);
  703:     } elsif ($action eq 'contacts') {
  704:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  705:     } elsif ($action eq 'defaults') {
  706:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  707:     } elsif ($action eq 'scantron') {
  708:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  709:     } elsif ($action eq 'coursecategories') {
  710:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  711:     } elsif ($action eq 'serverstatuses') {
  712:         $output = &modify_serverstatuses($dom,%domconfig);
  713:     } elsif ($action eq 'requestcourses') {
  714:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  715:     } elsif ($action eq 'requestauthor') {
  716:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  717:     } elsif ($action eq 'helpsettings') {
  718:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  719:     } elsif ($action eq 'coursedefaults') {
  720:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  721:     } elsif ($action eq 'selfenrollment') {
  722:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  723:     } elsif ($action eq 'usersessions') {
  724:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  725:     } elsif ($action eq 'loadbalancing') {
  726:         $output = &modify_loadbalancing($dom,%domconfig);
  727:     } elsif ($action eq 'ltitools') {
  728:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  729:     } elsif ($action eq 'ssl') {
  730:         $output = &modify_ssl($dom,$lastactref,%domconfig);
  731:     } elsif ($action eq 'trust') {
  732:         $output = &modify_trust($dom,$lastactref,%domconfig);
  733:     }
  734:     return $output;
  735: }
  736: 
  737: sub print_config_box {
  738:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  739:     my $rowtotal = 0;
  740:     my $output;
  741:     if ($action eq 'coursecategories') {
  742:         $output = &coursecategories_javascript($settings);
  743:     } elsif ($action eq 'defaults') {
  744:         $output = &defaults_javascript($settings); 
  745:     } elsif ($action eq 'helpsettings') {
  746:         my (%privs,%levelscurrent);
  747:         my %full=();
  748:         my %levels=(
  749:                      course => {},
  750:                      domain => {},
  751:                      system => {},
  752:                    );
  753:         my $context = 'domain';
  754:         my $crstype = 'Course';
  755:         my $formname = 'display';
  756:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  757:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  758:         $output =
  759:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full, 
  760:                                                       \@templateroles);
  761:     }
  762:     $output .=
  763:          '<table class="LC_nested_outer">
  764:           <tr>
  765:            <th class="LC_left_item LC_middle"><span class="LC_nobreak">'.
  766:            &mt($item->{text}).'&nbsp;'.
  767:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  768:           '</tr>';
  769:     $rowtotal ++;
  770:     my $numheaders = 1;
  771:     if (ref($item->{'header'}) eq 'ARRAY') {
  772:         $numheaders = scalar(@{$item->{'header'}});
  773:     }
  774:     if ($numheaders > 1) {
  775:         my $colspan = '';
  776:         my $rightcolspan = '';
  777:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  778:             ($action eq 'directorysrch') ||
  779:             (($action eq 'login') && ($numheaders < 4))) {
  780:             $colspan = ' colspan="2"';
  781:         }
  782:         if ($action eq 'usersessions') {
  783:             $rightcolspan = ' colspan="3"'; 
  784:         }
  785:         $output .= '
  786:           <tr>
  787:            <td>
  788:             <table class="LC_nested">
  789:              <tr class="LC_info_row">
  790:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  791:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  792:              </tr>';
  793:         $rowtotal ++;
  794:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  795:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  796:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'ssl') ||
  797:             ($action eq 'directorysrch') || ($action eq 'trust') || ($action eq 'helpsettings') ||
  798:             ($action eq 'contacts')) {
  799:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  800:         } elsif ($action eq 'coursecategories') {
  801:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  802:         } elsif ($action eq 'login') {
  803:             if ($numheaders == 4) {
  804:                 $colspan = ' colspan="2"';
  805:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  806:             } else {
  807:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  808:             }
  809:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  810:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  811:         } elsif ($action eq 'rolecolors') {
  812:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  813:         }
  814:         $output .= '
  815:            </table>
  816:           </td>
  817:          </tr>
  818:          <tr>
  819:            <td>
  820:             <table class="LC_nested">
  821:              <tr class="LC_info_row">
  822:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  823:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  824:              </tr>';
  825:             $rowtotal ++;
  826:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  827:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  828:             ($action eq 'usersessions') || ($action eq 'coursecategories') || 
  829:             ($action eq 'trust') || ($action eq 'contacts') || ($action eq 'defaults')) {
  830:             if ($action eq 'coursecategories') {
  831:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  832:                 $colspan = ' colspan="2"';
  833:             } elsif ($action eq 'trust') {
  834:                 $output .= $item->{'print'}->('shared',$dom,$settings,\$rowtotal);
  835:             } else {
  836:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  837:             }
  838:             if ($action eq 'trust') {
  839:                 $output .= '
  840:             </table>
  841:           </td>
  842:          </tr>';
  843:                 my @trusthdrs = qw(2 3 4 5 6 7);
  844:                 my @prefixes = qw(enroll othcoau coaurem domroles catalog reqcrs);
  845:                 for (my $i=0; $i<@trusthdrs; $i++) {
  846:                     $output .= '
  847:          <tr>
  848:            <td>
  849:             <table class="LC_nested">
  850:              <tr class="LC_info_row">
  851:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col1'}).'</td>
  852:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col2'}).'</td></tr>'.
  853:                            $item->{'print'}->($prefixes[$i],$dom,$settings,\$rowtotal).'
  854:             </table>
  855:           </td>
  856:          </tr>';
  857:                 }
  858:                 $output .= '
  859:          <tr>
  860:            <td>
  861:             <table class="LC_nested">
  862:              <tr class="LC_info_row">
  863:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col1'}).'</td>
  864:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col2'}).'</td></tr>'.
  865:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  866:             } else {
  867:                 $output .= '
  868:            </table>
  869:           </td>
  870:          </tr>
  871:          <tr>
  872:            <td>
  873:             <table class="LC_nested">
  874:              <tr class="LC_info_row">
  875:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  876:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  877:              </tr>'."\n";
  878:                 if ($action eq 'coursecategories') {
  879:                     $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  880:                 } else {
  881:                     $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  882:                 }
  883:             }
  884:             $rowtotal ++;
  885:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  886:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
  887:                  ($action eq 'helpsettings')) {
  888:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  889:         } elsif ($action eq 'ssl') {
  890:             $output .= $item->{'print'}->('connto',$dom,$settings,\$rowtotal).'
  891:             </table>
  892:           </td>
  893:          </tr>
  894:          <tr>
  895:            <td>
  896:             <table class="LC_nested">
  897:              <tr class="LC_info_row">
  898:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  899:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  900:                            $item->{'print'}->('connfrom',$dom,$settings,\$rowtotal).'
  901:             </table>
  902:           </td>
  903:          </tr>
  904:          <tr>
  905:            <td>
  906:             <table class="LC_nested">
  907:              <tr class="LC_info_row">
  908:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  909:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'.
  910:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  911:         } elsif ($action eq 'login') {
  912:             if ($numheaders == 4) {
  913:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  914:            </table>
  915:           </td>
  916:          </tr>
  917:          <tr>
  918:            <td>
  919:             <table class="LC_nested">
  920:              <tr class="LC_info_row">
  921:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  922:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  923:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  924:                 $rowtotal ++;
  925:             } else {
  926:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  927:             }
  928:             $output .= '
  929:            </table>
  930:           </td>
  931:          </tr>
  932:          <tr>
  933:            <td>
  934:             <table class="LC_nested">
  935:              <tr class="LC_info_row">';
  936:             if ($numheaders == 4) {
  937:                 $output .= '
  938:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  939:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  940:              </tr>';
  941:             } else {
  942:                 $output .= '
  943:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  944:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  945:              </tr>';
  946:             }
  947:             $rowtotal ++;
  948:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
  949:         } elsif ($action eq 'requestcourses') {
  950:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  951:             $rowtotal ++;
  952:             $output .= &print_studentcode($settings,\$rowtotal).'
  953:            </table>
  954:           </td>
  955:          </tr>
  956:          <tr>
  957:            <td>
  958:             <table class="LC_nested">
  959:              <tr class="LC_info_row">
  960:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  961:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  962:                        &textbookcourses_javascript($settings).
  963:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
  964:             </table>
  965:            </td>
  966:           </tr>
  967:          <tr>
  968:            <td>
  969:             <table class="LC_nested">
  970:              <tr class="LC_info_row">
  971:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  972:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
  973:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
  974:             </table>
  975:            </td>
  976:           </tr>
  977:           <tr>
  978:            <td>
  979:             <table class="LC_nested">
  980:              <tr class="LC_info_row">
  981:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
  982:               <td class="LC_right_item" style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
  983:              </tr>'.
  984:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  985:         } elsif ($action eq 'requestauthor') {
  986:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  987:             $rowtotal ++;
  988:         } elsif ($action eq 'rolecolors') {
  989:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  990:            </table>
  991:           </td>
  992:          </tr>
  993:          <tr>
  994:            <td>
  995:             <table class="LC_nested">
  996:              <tr class="LC_info_row">
  997:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.
  998:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  999:               <td class="LC_right_item" style="vertical-align: top">'.
 1000:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
 1001:              </tr>'.
 1002:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
 1003:            </table>
 1004:           </td>
 1005:          </tr>
 1006:          <tr>
 1007:            <td>
 1008:             <table class="LC_nested">
 1009:              <tr class="LC_info_row">
 1010:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1011:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1012:              </tr>'.
 1013:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
 1014:             $rowtotal += 2;
 1015:         }
 1016:     } else {
 1017:         $output .= '
 1018:           <tr>
 1019:            <td>
 1020:             <table class="LC_nested">
 1021:              <tr class="LC_info_row">';
 1022:         if ($action eq 'login') {
 1023:             $output .= '  
 1024:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1025:         } elsif ($action eq 'serverstatuses') {
 1026:             $output .= '
 1027:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).
 1028:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
 1029: 
 1030:         } else {
 1031:             $output .= '
 1032:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1033:         }
 1034:         if (defined($item->{'header'}->[0]->{'col3'})) {
 1035:             $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1036:                        &mt($item->{'header'}->[0]->{'col2'});
 1037:             if ($action eq 'serverstatuses') {
 1038:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
 1039:             } 
 1040:         } else {
 1041:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1042:                        &mt($item->{'header'}->[0]->{'col2'});
 1043:         }
 1044:         $output .= '</td>';
 1045:         if ($item->{'header'}->[0]->{'col3'}) {
 1046:             if (defined($item->{'header'}->[0]->{'col4'})) {
 1047:                 $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1048:                             &mt($item->{'header'}->[0]->{'col3'});
 1049:             } else {
 1050:                 $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1051:                            &mt($item->{'header'}->[0]->{'col3'});
 1052:             }
 1053:             if ($action eq 'serverstatuses') {
 1054:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
 1055:             }
 1056:             $output .= '</td>';
 1057:         }
 1058:         if ($item->{'header'}->[0]->{'col4'}) {
 1059:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1060:                        &mt($item->{'header'}->[0]->{'col4'});
 1061:         }
 1062:         $output .= '</tr>';
 1063:         $rowtotal ++;
 1064:         if ($action eq 'quotas') {
 1065:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1066:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1067:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') || 
 1068:                  ($action eq 'ltitools')) {
 1069:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1070:         } elsif ($action eq 'scantron') {
 1071:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
 1072:         }
 1073:     }
 1074:     $output .= '
 1075:    </table>
 1076:   </td>
 1077:  </tr>
 1078: </table><br />';
 1079:     return ($output,$rowtotal);
 1080: }
 1081: 
 1082: sub print_login {
 1083:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1084:     my ($css_class,$datatable);
 1085:     my %choices = &login_choices();
 1086: 
 1087:     if ($caller eq 'service') {
 1088:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1089:         my $choice = $choices{'disallowlogin'};
 1090:         $css_class = ' class="LC_odd_row"';
 1091:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1092:                       '<td style="text-align: right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1093:                       '<th>'.$choices{'server'}.'</th>'.
 1094:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1095:                       '<th>'.$choices{'custompath'}.'</th>'.
 1096:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1097:         my %disallowed;
 1098:         if (ref($settings) eq 'HASH') {
 1099:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1100:                %disallowed = %{$settings->{'loginvia'}};
 1101:             }
 1102:         }
 1103:         foreach my $lonhost (sort(keys(%servers))) {
 1104:             my $direct = 'selected="selected"';
 1105:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1106:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1107:                     $direct = '';
 1108:                 }
 1109:             }
 1110:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1111:                           '<td><select name="'.$lonhost.'_server">'.
 1112:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1113:                           '</option>';
 1114:             foreach my $hostid (sort(keys(%servers))) {
 1115:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1116:                 my $selected = '';
 1117:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1118:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1119:                         $selected = 'selected="selected"';
 1120:                     }
 1121:                 }
 1122:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1123:                               $servers{$hostid}.'</option>';
 1124:             }
 1125:             $datatable .= '</select></td>'.
 1126:                           '<td><select name="'.$lonhost.'_serverpath">';
 1127:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1128:                 my $pathname = $path;
 1129:                 if ($path eq 'custom') {
 1130:                     $pathname = &mt('Custom Path').' ->';
 1131:                 }
 1132:                 my $selected = '';
 1133:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1134:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1135:                         $selected = 'selected="selected"';
 1136:                     }
 1137:                 } elsif ($path eq '') {
 1138:                     $selected = 'selected="selected"';
 1139:                 }
 1140:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1141:             }
 1142:             $datatable .= '</select></td>';
 1143:             my ($custom,$exempt);
 1144:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1145:                 $custom = $disallowed{$lonhost}{'custompath'};
 1146:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1147:             }
 1148:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1149:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1150:                           '</tr>';
 1151:         }
 1152:         $datatable .= '</table></td></tr>';
 1153:         return $datatable;
 1154:     } elsif ($caller eq 'page') {
 1155:         my %defaultchecked = ( 
 1156:                                'coursecatalog' => 'on',
 1157:                                'helpdesk'      => 'on',
 1158:                                'adminmail'     => 'off',
 1159:                                'newuser'       => 'off',
 1160:                              );
 1161:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1162:         my (%checkedon,%checkedoff);
 1163:         foreach my $item (@toggles) {
 1164:             if ($defaultchecked{$item} eq 'on') { 
 1165:                 $checkedon{$item} = ' checked="checked" ';
 1166:                 $checkedoff{$item} = ' ';
 1167:             } elsif ($defaultchecked{$item} eq 'off') {
 1168:                 $checkedoff{$item} = ' checked="checked" ';
 1169:                 $checkedon{$item} = ' ';
 1170:             }
 1171:         }
 1172:         my @images = ('img','logo','domlogo','login');
 1173:         my @logintext = ('textcol','bgcol');
 1174:         my @bgs = ('pgbg','mainbg','sidebg');
 1175:         my @links = ('link','alink','vlink');
 1176:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1177:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1178:         my (%is_custom,%designs);
 1179:         my %defaults = (
 1180:                        font => $defaultdesign{'login.font'},
 1181:                        );
 1182:         foreach my $item (@images) {
 1183:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1184:             $defaults{'showlogo'}{$item} = 1;
 1185:         }
 1186:         foreach my $item (@bgs) {
 1187:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1188:         }
 1189:         foreach my $item (@logintext) {
 1190:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1191:         }
 1192:         foreach my $item (@links) {
 1193:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1194:         }
 1195:         if (ref($settings) eq 'HASH') {
 1196:             foreach my $item (@toggles) {
 1197:                 if ($settings->{$item} eq '1') {
 1198:                     $checkedon{$item} =  ' checked="checked" ';
 1199:                     $checkedoff{$item} = ' ';
 1200:                 } elsif ($settings->{$item} eq '0') {
 1201:                     $checkedoff{$item} =  ' checked="checked" ';
 1202:                     $checkedon{$item} = ' ';
 1203:                 }
 1204:             }
 1205:             foreach my $item (@images) {
 1206:                 if (defined($settings->{$item})) {
 1207:                     $designs{$item} = $settings->{$item};
 1208:                     $is_custom{$item} = 1;
 1209:                 }
 1210:                 if (defined($settings->{'showlogo'}{$item})) {
 1211:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1212:                 }
 1213:             }
 1214:             foreach my $item (@logintext) {
 1215:                 if ($settings->{$item} ne '') {
 1216:                     $designs{'logintext'}{$item} = $settings->{$item};
 1217:                     $is_custom{$item} = 1;
 1218:                 }
 1219:             }
 1220:             if ($settings->{'font'} ne '') {
 1221:                 $designs{'font'} = $settings->{'font'};
 1222:                 $is_custom{'font'} = 1;
 1223:             }
 1224:             foreach my $item (@bgs) {
 1225:                 if ($settings->{$item} ne '') {
 1226:                     $designs{'bgs'}{$item} = $settings->{$item};
 1227:                     $is_custom{$item} = 1;
 1228:                 }
 1229:             }
 1230:             foreach my $item (@links) {
 1231:                 if ($settings->{$item} ne '') {
 1232:                     $designs{'links'}{$item} = $settings->{$item};
 1233:                     $is_custom{$item} = 1;
 1234:                 }
 1235:             }
 1236:         } else {
 1237:             if ($designhash{$dom.'.login.font'} ne '') {
 1238:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1239:                 $is_custom{'font'} = 1;
 1240:             }
 1241:             foreach my $item (@images) {
 1242:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1243:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1244:                     $is_custom{$item} = 1;
 1245:                 }
 1246:             }
 1247:             foreach my $item (@bgs) {
 1248:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1249:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1250:                     $is_custom{$item} = 1;
 1251:                 }
 1252:             }
 1253:             foreach my $item (@links) {
 1254:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1255:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1256:                     $is_custom{$item} = 1;
 1257:                 }
 1258:             }
 1259:         }
 1260:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1261:                                                       logo => 'Institution Logo',
 1262:                                                       domlogo => 'Domain Logo',
 1263:                                                       login => 'Login box');
 1264:         my $itemcount = 1;
 1265:         foreach my $item (@toggles) {
 1266:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1267:             $datatable .=  
 1268:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1269:                 '</td><td>'.
 1270:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1271:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1272:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1273:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1274:                 '</tr>';
 1275:             $itemcount ++;
 1276:         }
 1277:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1278:         $datatable .= '</tr></table></td></tr>';
 1279:     } elsif ($caller eq 'help') {
 1280:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1281:         my $switchserver = &check_switchserver($dom,$confname);
 1282:         my $itemcount = 1;
 1283:         $defaulturl = '/adm/loginproblems.html';
 1284:         $defaulttype = 'default';
 1285:         %lt = &Apache::lonlocal::texthash (
 1286:                      del     => 'Delete?',
 1287:                      rep     => 'Replace:',
 1288:                      upl     => 'Upload:',
 1289:                      default => 'Default',
 1290:                      custom  => 'Custom',
 1291:                                              );
 1292:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1293:         my @currlangs;
 1294:         if (ref($settings) eq 'HASH') {
 1295:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1296:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1297:                     next if ($settings->{'helpurl'}{$key} eq '');
 1298:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1299:                     $type{$key} = 'custom';
 1300:                     unless ($key eq 'nolang') {
 1301:                         push(@currlangs,$key);
 1302:                     }
 1303:                 }
 1304:             } elsif ($settings->{'helpurl'} ne '') {
 1305:                 $type{'nolang'} = 'custom';
 1306:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1307:             }
 1308:         }
 1309:         foreach my $lang ('nolang',sort(@currlangs)) {
 1310:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1311:             $datatable .= '<tr'.$css_class.'>';
 1312:             if ($url{$lang} eq '') {
 1313:                 $url{$lang} = $defaulturl;
 1314:             }
 1315:             if ($type{$lang} eq '') {
 1316:                 $type{$lang} = $defaulttype;
 1317:             }
 1318:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1319:             if ($lang eq 'nolang') {
 1320:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1321:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1322:             } else {
 1323:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1324:                                   $langchoices{$lang},
 1325:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1326:             }
 1327:             $datatable .= '</span></td>'."\n".
 1328:                           '<td class="LC_left_item">';
 1329:             if ($type{$lang} eq 'custom') {
 1330:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1331:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1332:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1333:             } else {
 1334:                 $datatable .= $lt{'upl'};
 1335:             }
 1336:             $datatable .='<br />';
 1337:             if ($switchserver) {
 1338:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1339:             } else {
 1340:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1341:             }
 1342:             $datatable .= '</td></tr>';
 1343:             $itemcount ++;
 1344:         }
 1345:         my @addlangs;
 1346:         foreach my $lang (sort(keys(%langchoices))) {
 1347:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1348:             push(@addlangs,$lang);
 1349:         }
 1350:         if (@addlangs > 0) {
 1351:             my %toadd;
 1352:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1353:             $toadd{''} = &mt('Select');
 1354:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1355:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1356:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1357:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1358:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1359:             if ($switchserver) {
 1360:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1361:             } else {
 1362:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1363:             }
 1364:             $datatable .= '</td></tr>';
 1365:             $itemcount ++;
 1366:         }
 1367:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1368:     } elsif ($caller eq 'headtag') {
 1369:         my %domservers = &Apache::lonnet::get_servers($dom);
 1370:         my $choice = $choices{'headtag'};
 1371:         $css_class = ' class="LC_odd_row"';
 1372:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1373:                       '<td style="text-align: left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1374:                       '<th>'.$choices{'current'}.'</th>'.
 1375:                       '<th>'.$choices{'action'}.'</th>'.
 1376:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1377:         my (%currurls,%currexempt);
 1378:         if (ref($settings) eq 'HASH') {
 1379:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1380:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1381:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1382:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1383:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1384:                     }
 1385:                 }
 1386:             }
 1387:         }
 1388:         my %lt = &Apache::lonlocal::texthash(
 1389:                                                del  => 'Delete?',
 1390:                                                rep  => 'Replace:',
 1391:                                                upl  => 'Upload:',
 1392:                                                curr => 'View contents',
 1393:                                                none => 'None',
 1394:         );
 1395:         my $switchserver = &check_switchserver($dom,$confname);
 1396:         foreach my $lonhost (sort(keys(%domservers))) {
 1397:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1398:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1399:             if ($currurls{$lonhost}) {
 1400:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1401:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1402:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1403:                               '">'.$lt{'curr'}.'</a></td>'.
 1404:                               '<td><span class="LC_nobreak"><label>'.
 1405:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1406:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1407:             } else {
 1408:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1409:             }
 1410:             $datatable .='<br />';
 1411:             if ($switchserver) {
 1412:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1413:             } else {
 1414:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1415:             }
 1416:             $datatable .= '</td><td><input type="textbox" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1417:         }
 1418:         $datatable .= '</table></td></tr>';
 1419:     }
 1420:     return $datatable;
 1421: }
 1422: 
 1423: sub login_choices {
 1424:     my %choices =
 1425:         &Apache::lonlocal::texthash (
 1426:             coursecatalog => 'Display Course/Community Catalog link?',
 1427:             adminmail     => "Display Administrator's E-mail Address?",
 1428:             helpdesk      => 'Display "Contact Helpdesk" link',
 1429:             disallowlogin => "Login page requests redirected",
 1430:             hostid        => "Server",
 1431:             server        => "Redirect to:",
 1432:             serverpath    => "Path",
 1433:             custompath    => "Custom", 
 1434:             exempt        => "Exempt IP(s)",
 1435:             directlogin   => "No redirect",
 1436:             newuser       => "Link to create a user account",
 1437:             img           => "Header",
 1438:             logo          => "Main Logo",
 1439:             domlogo       => "Domain Logo",
 1440:             login         => "Log-in Header", 
 1441:             textcol       => "Text color",
 1442:             bgcol         => "Box color",
 1443:             bgs           => "Background colors",
 1444:             links         => "Link colors",
 1445:             font          => "Font color",
 1446:             pgbg          => "Header",
 1447:             mainbg        => "Page",
 1448:             sidebg        => "Login box",
 1449:             link          => "Link",
 1450:             alink         => "Active link",
 1451:             vlink         => "Visited link",
 1452:             headtag       => "Custom markup",
 1453:             action        => "Action",
 1454:             current       => "Current",
 1455:         );
 1456:     return %choices;
 1457: }
 1458: 
 1459: sub print_rolecolors {
 1460:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1461:     my %choices = &color_font_choices();
 1462:     my @bgs = ('pgbg','tabbg','sidebg');
 1463:     my @links = ('link','alink','vlink');
 1464:     my @images = ('img');
 1465:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1466:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1467:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1468:     my (%is_custom,%designs);
 1469:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1470:     if (ref($settings) eq 'HASH') {
 1471:         if (ref($settings->{$role}) eq 'HASH') {
 1472:             if ($settings->{$role}->{'img'} ne '') {
 1473:                 $designs{'img'} = $settings->{$role}->{'img'};
 1474:                 $is_custom{'img'} = 1;
 1475:             }
 1476:             if ($settings->{$role}->{'font'} ne '') {
 1477:                 $designs{'font'} = $settings->{$role}->{'font'};
 1478:                 $is_custom{'font'} = 1;
 1479:             }
 1480:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1481:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1482:                 $is_custom{'fontmenu'} = 1;
 1483:             }
 1484:             foreach my $item (@bgs) {
 1485:                 if ($settings->{$role}->{$item} ne '') {
 1486:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1487:                     $is_custom{$item} = 1;
 1488:                 }
 1489:             }
 1490:             foreach my $item (@links) {
 1491:                 if ($settings->{$role}->{$item} ne '') {
 1492:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1493:                     $is_custom{$item} = 1;
 1494:                 }
 1495:             }
 1496:         }
 1497:     } else {
 1498:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1499:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1500:             $is_custom{'img'} = 1;
 1501:         }
 1502:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1503:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1504:             $is_custom{'fontmenu'} = 1; 
 1505:         }
 1506:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1507:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1508:             $is_custom{'font'} = 1;
 1509:         }
 1510:         foreach my $item (@bgs) {
 1511:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1512:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1513:                 $is_custom{$item} = 1;
 1514:             
 1515:             }
 1516:         }
 1517:         foreach my $item (@links) {
 1518:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1519:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1520:                 $is_custom{$item} = 1;
 1521:             }
 1522:         }
 1523:     }
 1524:     my $itemcount = 1;
 1525:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1526:     $datatable .= '</tr></table></td></tr>';
 1527:     return $datatable;
 1528: }
 1529: 
 1530: sub role_defaults {
 1531:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1532:     my %defaults;
 1533:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1534:         return %defaults;
 1535:     }
 1536:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1537:     if ($role eq 'login') {
 1538:         %defaults = (
 1539:                        font => $defaultdesign{$role.'.font'},
 1540:                     );
 1541:         if (ref($logintext) eq 'ARRAY') {
 1542:             foreach my $item (@{$logintext}) {
 1543:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1544:             }
 1545:         }
 1546:         foreach my $item (@{$images}) {
 1547:             $defaults{'showlogo'}{$item} = 1;
 1548:         }
 1549:     } else {
 1550:         %defaults = (
 1551:                        img => $defaultdesign{$role.'.img'},
 1552:                        font => $defaultdesign{$role.'.font'},
 1553:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1554:                     );
 1555:     }
 1556:     foreach my $item (@{$bgs}) {
 1557:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1558:     }
 1559:     foreach my $item (@{$links}) {
 1560:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1561:     }
 1562:     foreach my $item (@{$images}) {
 1563:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1564:     }
 1565:     return %defaults;
 1566: }
 1567: 
 1568: sub display_color_options {
 1569:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1570:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1571:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1572:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1573:     my $datatable = '<tr'.$css_class.'>'.
 1574:         '<td>'.$choices->{'font'}.'</td>';
 1575:     if (!$is_custom->{'font'}) {
 1576:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1577:     } else {
 1578:         $datatable .= '<td>&nbsp;</td>';
 1579:     }
 1580:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1581: 
 1582:     $datatable .= '<td><span class="LC_nobreak">'.
 1583:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1584:                   ' value="'.$current_color.'" />&nbsp;'.
 1585:                   '&nbsp;</td></tr>';
 1586:     unless ($role eq 'login') { 
 1587:         $datatable .= '<tr'.$css_class.'>'.
 1588:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1589:         if (!$is_custom->{'fontmenu'}) {
 1590:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1591:         } else {
 1592:             $datatable .= '<td>&nbsp;</td>';
 1593:         }
 1594: 	$current_color = $designs->{'fontmenu'} ?
 1595: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1596:         $datatable .= '<td><span class="LC_nobreak">'.
 1597:                       '<input class="colorchooser" type="text" size="10" name="'
 1598: 		      .$role.'_fontmenu"'.
 1599:                       ' value="'.$current_color.'" />&nbsp;'.
 1600:                       '&nbsp;</td></tr>';
 1601:     }
 1602:     my $switchserver = &check_switchserver($dom,$confname);
 1603:     foreach my $img (@{$images}) {
 1604: 	$itemcount ++;
 1605:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1606:         $datatable .= '<tr'.$css_class.'>'.
 1607:                       '<td>'.$choices->{$img};
 1608:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1609:         if ($role eq 'login') {
 1610:             if ($img eq 'login') {
 1611:                 $login_hdr_pick =
 1612:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1613:                 $logincolors =
 1614:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1615:                                        $designs,$defaults);
 1616:             } elsif ($img ne 'domlogo') {
 1617:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1618:             }
 1619:         }
 1620:         $datatable .= '</td>';
 1621:         if ($designs->{$img} ne '') {
 1622:             $imgfile = $designs->{$img};
 1623: 	    $img_import = ($imgfile =~ m{^/adm/});
 1624:         } else {
 1625:             $imgfile = $defaults->{$img};
 1626:         }
 1627:         if ($imgfile) {
 1628:             my ($showfile,$fullsize);
 1629:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1630:                 my $urldir = $1;
 1631:                 my $filename = $2;
 1632:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1633:                 if (@info) {
 1634:                     my $thumbfile = 'tn-'.$filename;
 1635:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1636:                     if (@thumb) {
 1637:                         $showfile = $urldir.'/'.$thumbfile;
 1638:                     } else {
 1639:                         $showfile = $imgfile;
 1640:                     }
 1641:                 } else {
 1642:                     $showfile = '';
 1643:                 }
 1644:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1645:                 $showfile = $imgfile;
 1646:                 my $imgdir = $1;
 1647:                 my $filename = $2;
 1648:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1649:                     $showfile = "/$imgdir/tn-".$filename;
 1650:                 } else {
 1651:                     my $input = $londocroot.$imgfile;
 1652:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1653:                     if (!-e $output) {
 1654:                         my ($width,$height) = &thumb_dimensions();
 1655:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1656:                         if ($fullwidth ne '' && $fullheight ne '') {
 1657:                             if ($fullwidth > $width && $fullheight > $height) { 
 1658:                                 my $size = $width.'x'.$height;
 1659:                                 system("convert -sample $size $input $output");
 1660:                                 $showfile = "/$imgdir/tn-".$filename;
 1661:                             }
 1662:                         }
 1663:                     }
 1664:                 }
 1665:             }
 1666:             if ($showfile) {
 1667:                 if ($showfile =~ m{^/(adm|res)/}) {
 1668:                     if ($showfile =~ m{^/res/}) {
 1669:                         my $local_showfile =
 1670:                             &Apache::lonnet::filelocation('',$showfile);
 1671:                         &Apache::lonnet::repcopy($local_showfile);
 1672:                     }
 1673:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1674:                 }
 1675:                 if ($imgfile) {
 1676:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1677:                         if ($imgfile =~ m{^/res/}) {
 1678:                             my $local_imgfile =
 1679:                                 &Apache::lonnet::filelocation('',$imgfile);
 1680:                             &Apache::lonnet::repcopy($local_imgfile);
 1681:                         }
 1682:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1683:                     } else {
 1684:                         $fullsize = $imgfile;
 1685:                     }
 1686:                 }
 1687:                 $datatable .= '<td>';
 1688:                 if ($img eq 'login') {
 1689:                     $datatable .= $login_hdr_pick;
 1690:                 } 
 1691:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1692:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1693:             } else {
 1694:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1695:                               &mt('Upload:').'<br />';
 1696:             }
 1697:         } else {
 1698:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1699:                           &mt('Upload:').'<br />';
 1700:         }
 1701:         if ($switchserver) {
 1702:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1703:         } else {
 1704:             if ($img ne 'login') { # suppress file selection for Log-in header
 1705:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1706:             }
 1707:         }
 1708:         $datatable .= '</td></tr>';
 1709:     }
 1710:     $itemcount ++;
 1711:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1712:     $datatable .= '<tr'.$css_class.'>'.
 1713:                   '<td>'.$choices->{'bgs'}.'</td>';
 1714:     my $bgs_def;
 1715:     foreach my $item (@{$bgs}) {
 1716:         if (!$is_custom->{$item}) {
 1717:             $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>';
 1718:         }
 1719:     }
 1720:     if ($bgs_def) {
 1721:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1722:     } else {
 1723:         $datatable .= '<td>&nbsp;</td>';
 1724:     }
 1725:     $datatable .= '<td class="LC_right_item">'.
 1726:                   '<table border="0"><tr>';
 1727: 
 1728:     foreach my $item (@{$bgs}) {
 1729:         $datatable .= '<td style="text-align: center">'.$choices->{$item};
 1730: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1731:         if ($designs->{'bgs'}{$item}) {
 1732:             $datatable .= '&nbsp;';
 1733:         }
 1734:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1735:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1736:     }
 1737:     $datatable .= '</tr></table></td></tr>';
 1738:     $itemcount ++;
 1739:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1740:     $datatable .= '<tr'.$css_class.'>'.
 1741:                   '<td>'.$choices->{'links'}.'</td>';
 1742:     my $links_def;
 1743:     foreach my $item (@{$links}) {
 1744:         if (!$is_custom->{$item}) {
 1745:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1746:         }
 1747:     }
 1748:     if ($links_def) {
 1749:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1750:     } else {
 1751:         $datatable .= '<td>&nbsp;</td>';
 1752:     }
 1753:     $datatable .= '<td class="LC_right_item">'.
 1754:                   '<table border="0"><tr>';
 1755:     foreach my $item (@{$links}) {
 1756: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1757:         $datatable .= '<td style="text-align: center">'.$choices->{$item}."\n";
 1758:         if ($designs->{'links'}{$item}) {
 1759:             $datatable.='&nbsp;';
 1760:         }
 1761:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1762:                       '" /></td>';
 1763:     }
 1764:     $$rowtotal += $itemcount;
 1765:     return $datatable;
 1766: }
 1767: 
 1768: sub logo_display_options {
 1769:     my ($img,$defaults,$designs) = @_;
 1770:     my $checkedon;
 1771:     if (ref($defaults) eq 'HASH') {
 1772:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1773:             if ($defaults->{'showlogo'}{$img}) {
 1774:                 $checkedon = 'checked="checked" ';     
 1775:             }
 1776:         } 
 1777:     }
 1778:     if (ref($designs) eq 'HASH') {
 1779:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1780:             if (defined($designs->{'showlogo'}{$img})) {
 1781:                 if ($designs->{'showlogo'}{$img} == 0) {
 1782:                     $checkedon = '';
 1783:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1784:                     $checkedon = 'checked="checked" ';
 1785:                 }
 1786:             }
 1787:         }
 1788:     }
 1789:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1790:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1791:            &mt('show').'</label>'."\n";
 1792: }
 1793: 
 1794: sub login_header_options  {
 1795:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1796:     my $output = '';
 1797:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1798:         $output .= &mt('Text default(s):').'<br />';
 1799:         if (!$is_custom->{'textcol'}) {
 1800:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1801:                        '&nbsp;&nbsp;&nbsp;';
 1802:         }
 1803:         if (!$is_custom->{'bgcol'}) {
 1804:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1805:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1806:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1807:         }
 1808:         $output .= '<br />';
 1809:     }
 1810:     $output .='<br />';
 1811:     return $output;
 1812: }
 1813: 
 1814: sub login_text_colors {
 1815:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1816:     my $color_menu = '<table border="0"><tr>';
 1817:     foreach my $item (@{$logintext}) {
 1818:         $color_menu .= '<td style="text-align: center">'.$choices->{$item};
 1819:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1820:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1821:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1822:     }
 1823:     $color_menu .= '</tr></table><br />';
 1824:     return $color_menu;
 1825: }
 1826: 
 1827: sub image_changes {
 1828:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1829:     my $output;
 1830:     if ($img eq 'login') {
 1831:             # suppress image for Log-in header
 1832:     } elsif (!$is_custom) {
 1833:         if ($img ne 'domlogo') {
 1834:             $output .= &mt('Default image:').'<br />';
 1835:         } else {
 1836:             $output .= &mt('Default in use:').'<br />';
 1837:         }
 1838:     }
 1839:     if ($img eq 'login') { # suppress image for Log-in header
 1840:         $output .= '<td>'.$logincolors;
 1841:     } else {
 1842:         if ($img_import) {
 1843:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1844:         }
 1845:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1846:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1847:         if ($is_custom) {
 1848:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1849:                        '<input type="checkbox" name="'.
 1850:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1851:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1852:         } else {
 1853:             $output .= '<td class="LC_middle">'.$logincolors.&mt('Upload:').'<br />';
 1854:         }
 1855:     }
 1856:     return $output;
 1857: }
 1858: 
 1859: sub print_quotas {
 1860:     my ($dom,$settings,$rowtotal,$action) = @_;
 1861:     my $context;
 1862:     if ($action eq 'quotas') {
 1863:         $context = 'tools';
 1864:     } else {
 1865:         $context = $action;
 1866:     }
 1867:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1868:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1869:     my $typecount = 0;
 1870:     my ($css_class,%titles);
 1871:     if ($context eq 'requestcourses') {
 1872:         @usertools = ('official','unofficial','community','textbook','placement');
 1873:         @options =('norequest','approval','validate','autolimit');
 1874:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1875:         %titles = &courserequest_titles();
 1876:     } elsif ($context eq 'requestauthor') {
 1877:         @usertools = ('author');
 1878:         @options = ('norequest','approval','automatic');
 1879:         %titles = &authorrequest_titles();
 1880:     } else {
 1881:         @usertools = ('aboutme','blog','webdav','portfolio');
 1882:         %titles = &tool_titles();
 1883:     }
 1884:     if (ref($types) eq 'ARRAY') {
 1885:         foreach my $type (@{$types}) {
 1886:             my ($currdefquota,$currauthorquota);
 1887:             unless (($context eq 'requestcourses') ||
 1888:                     ($context eq 'requestauthor')) {
 1889:                 if (ref($settings) eq 'HASH') {
 1890:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1891:                         $currdefquota = $settings->{defaultquota}->{$type};
 1892:                     } else {
 1893:                         $currdefquota = $settings->{$type};
 1894:                     }
 1895:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1896:                         $currauthorquota = $settings->{authorquota}->{$type};
 1897:                     }
 1898:                 }
 1899:             }
 1900:             if (defined($usertypes->{$type})) {
 1901:                 $typecount ++;
 1902:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1903:                 $datatable .= '<tr'.$css_class.'>'.
 1904:                               '<td>'.$usertypes->{$type}.'</td>'.
 1905:                               '<td class="LC_left_item">';
 1906:                 if ($context eq 'requestcourses') {
 1907:                     $datatable .= '<table><tr>';
 1908:                 }
 1909:                 my %cell;  
 1910:                 foreach my $item (@usertools) {
 1911:                     if ($context eq 'requestcourses') {
 1912:                         my ($curroption,$currlimit);
 1913:                         if (ref($settings) eq 'HASH') {
 1914:                             if (ref($settings->{$item}) eq 'HASH') {
 1915:                                 $curroption = $settings->{$item}->{$type};
 1916:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1917:                                     $currlimit = $1; 
 1918:                                 }
 1919:                             }
 1920:                         }
 1921:                         if (!$curroption) {
 1922:                             $curroption = 'norequest';
 1923:                         }
 1924:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1925:                         foreach my $option (@options) {
 1926:                             my $val = $option;
 1927:                             if ($option eq 'norequest') {
 1928:                                 $val = 0;  
 1929:                             }
 1930:                             if ($option eq 'validate') {
 1931:                                 my $canvalidate = 0;
 1932:                                 if (ref($validations{$item}) eq 'HASH') { 
 1933:                                     if ($validations{$item}{$type}) {
 1934:                                         $canvalidate = 1;
 1935:                                     }
 1936:                                 }
 1937:                                 next if (!$canvalidate);
 1938:                             }
 1939:                             my $checked = '';
 1940:                             if ($option eq $curroption) {
 1941:                                 $checked = ' checked="checked"';
 1942:                             } elsif ($option eq 'autolimit') {
 1943:                                 if ($curroption =~ /^autolimit/) {
 1944:                                     $checked = ' checked="checked"';
 1945:                                 }                       
 1946:                             } 
 1947:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1948:                                   '<input type="radio" name="crsreq_'.$item.
 1949:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1950:                                   $titles{$option}.'</label>';
 1951:                             if ($option eq 'autolimit') {
 1952:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1953:                                                 $item.'_limit_'.$type.'" size="1" '.
 1954:                                                 'value="'.$currlimit.'" />';
 1955:                             }
 1956:                             $cell{$item} .= '</span> ';
 1957:                             if ($option eq 'autolimit') {
 1958:                                 $cell{$item} .= $titles{'unlimited'};
 1959:                             }
 1960:                         }
 1961:                     } elsif ($context eq 'requestauthor') {
 1962:                         my $curroption;
 1963:                         if (ref($settings) eq 'HASH') {
 1964:                             $curroption = $settings->{$type};
 1965:                         }
 1966:                         if (!$curroption) {
 1967:                             $curroption = 'norequest';
 1968:                         }
 1969:                         foreach my $option (@options) {
 1970:                             my $val = $option;
 1971:                             if ($option eq 'norequest') {
 1972:                                 $val = 0;
 1973:                             }
 1974:                             my $checked = '';
 1975:                             if ($option eq $curroption) {
 1976:                                 $checked = ' checked="checked"';
 1977:                             }
 1978:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1979:                                   '<input type="radio" name="authorreq_'.$type.
 1980:                                   '" value="'.$val.'"'.$checked.' />'.
 1981:                                   $titles{$option}.'</label></span>&nbsp; ';
 1982:                         }
 1983:                     } else {
 1984:                         my $checked = 'checked="checked" ';
 1985:                         if (ref($settings) eq 'HASH') {
 1986:                             if (ref($settings->{$item}) eq 'HASH') {
 1987:                                 if ($settings->{$item}->{$type} == 0) {
 1988:                                     $checked = '';
 1989:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1990:                                     $checked =  'checked="checked" ';
 1991:                                 }
 1992:                             }
 1993:                         }
 1994:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1995:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1996:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1997:                                       '</label></span>&nbsp; ';
 1998:                     }
 1999:                 }
 2000:                 if ($context eq 'requestcourses') {
 2001:                     $datatable .= '</tr><tr>';
 2002:                     foreach my $item (@usertools) {
 2003:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 2004:                     }
 2005:                     $datatable .= '</tr></table>';
 2006:                 }
 2007:                 $datatable .= '</td>';
 2008:                 unless (($context eq 'requestcourses') ||
 2009:                         ($context eq 'requestauthor')) {
 2010:                     $datatable .= 
 2011:                               '<td class="LC_right_item">'.
 2012:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2013:                               '<input type="text" name="quota_'.$type.
 2014:                               '" value="'.$currdefquota.
 2015:                               '" size="5" /></span>'.('&nbsp;' x 2).
 2016:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2017:                               '<input type="text" name="authorquota_'.$type.
 2018:                               '" value="'.$currauthorquota.
 2019:                               '" size="5" /></span></td>';
 2020:                 }
 2021:                 $datatable .= '</tr>';
 2022:             }
 2023:         }
 2024:     }
 2025:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2026:         $defaultquota = '20';
 2027:         $authorquota = '500';
 2028:         if (ref($settings) eq 'HASH') {
 2029:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 2030:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 2031:             } elsif (defined($settings->{'default'})) {
 2032:                 $defaultquota = $settings->{'default'};
 2033:             }
 2034:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 2035:                 $authorquota = $settings->{'authorquota'}->{'default'};
 2036:             }
 2037:         }
 2038:     }
 2039:     $typecount ++;
 2040:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2041:     $datatable .= '<tr'.$css_class.'>'.
 2042:                   '<td>'.$othertitle.'</td>'.
 2043:                   '<td class="LC_left_item">';
 2044:     if ($context eq 'requestcourses') {
 2045:         $datatable .= '<table><tr>';
 2046:     }
 2047:     my %defcell;
 2048:     foreach my $item (@usertools) {
 2049:         if ($context eq 'requestcourses') {
 2050:             my ($curroption,$currlimit);
 2051:             if (ref($settings) eq 'HASH') {
 2052:                 if (ref($settings->{$item}) eq 'HASH') {
 2053:                     $curroption = $settings->{$item}->{'default'};
 2054:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2055:                         $currlimit = $1;
 2056:                     }
 2057:                 }
 2058:             }
 2059:             if (!$curroption) {
 2060:                 $curroption = 'norequest';
 2061:             }
 2062:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2063:             foreach my $option (@options) {
 2064:                 my $val = $option;
 2065:                 if ($option eq 'norequest') {
 2066:                     $val = 0;
 2067:                 }
 2068:                 if ($option eq 'validate') {
 2069:                     my $canvalidate = 0;
 2070:                     if (ref($validations{$item}) eq 'HASH') {
 2071:                         if ($validations{$item}{'default'}) {
 2072:                             $canvalidate = 1;
 2073:                         }
 2074:                     }
 2075:                     next if (!$canvalidate);
 2076:                 }
 2077:                 my $checked = '';
 2078:                 if ($option eq $curroption) {
 2079:                     $checked = ' checked="checked"';
 2080:                 } elsif ($option eq 'autolimit') {
 2081:                     if ($curroption =~ /^autolimit/) {
 2082:                         $checked = ' checked="checked"';
 2083:                     }
 2084:                 }
 2085:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2086:                                   '<input type="radio" name="crsreq_'.$item.
 2087:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2088:                                   $titles{$option}.'</label>';
 2089:                 if ($option eq 'autolimit') {
 2090:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2091:                                        $item.'_limit_default" size="1" '.
 2092:                                        'value="'.$currlimit.'" />';
 2093:                 }
 2094:                 $defcell{$item} .= '</span> ';
 2095:                 if ($option eq 'autolimit') {
 2096:                     $defcell{$item} .= $titles{'unlimited'};
 2097:                 }
 2098:             }
 2099:         } elsif ($context eq 'requestauthor') {
 2100:             my $curroption;
 2101:             if (ref($settings) eq 'HASH') {
 2102:                 $curroption = $settings->{'default'};
 2103:             }
 2104:             if (!$curroption) {
 2105:                 $curroption = 'norequest';
 2106:             }
 2107:             foreach my $option (@options) {
 2108:                 my $val = $option;
 2109:                 if ($option eq 'norequest') {
 2110:                     $val = 0;
 2111:                 }
 2112:                 my $checked = '';
 2113:                 if ($option eq $curroption) {
 2114:                     $checked = ' checked="checked"';
 2115:                 }
 2116:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2117:                               '<input type="radio" name="authorreq_default"'.
 2118:                               ' value="'.$val.'"'.$checked.' />'.
 2119:                               $titles{$option}.'</label></span>&nbsp; ';
 2120:             }
 2121:         } else {
 2122:             my $checked = 'checked="checked" ';
 2123:             if (ref($settings) eq 'HASH') {
 2124:                 if (ref($settings->{$item}) eq 'HASH') {
 2125:                     if ($settings->{$item}->{'default'} == 0) {
 2126:                         $checked = '';
 2127:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2128:                         $checked = 'checked="checked" ';
 2129:                     }
 2130:                 }
 2131:             }
 2132:             $datatable .= '<span class="LC_nobreak"><label>'.
 2133:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2134:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2135:                           '</label></span>&nbsp; ';
 2136:         }
 2137:     }
 2138:     if ($context eq 'requestcourses') {
 2139:         $datatable .= '</tr><tr>';
 2140:         foreach my $item (@usertools) {
 2141:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2142:         }
 2143:         $datatable .= '</tr></table>';
 2144:     }
 2145:     $datatable .= '</td>';
 2146:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2147:         $datatable .= '<td class="LC_right_item">'.
 2148:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2149:                       '<input type="text" name="defaultquota" value="'.
 2150:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2151:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2152:                       '<input type="text" name="authorquota" value="'.
 2153:                       $authorquota.'" size="5" /></span></td>';
 2154:     }
 2155:     $datatable .= '</tr>';
 2156:     $typecount ++;
 2157:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2158:     $datatable .= '<tr'.$css_class.'>'.
 2159:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2160:     if ($context eq 'requestcourses') {
 2161:         $datatable .= &mt('(overrides affiliation, if set)').
 2162:                       '</td>'.
 2163:                       '<td class="LC_left_item">'.
 2164:                       '<table><tr>';
 2165:     } else {
 2166:         $datatable .= &mt('(overrides affiliation, if checked)').
 2167:                       '</td>'.
 2168:                       '<td class="LC_left_item" colspan="2">'.
 2169:                       '<br />';
 2170:     }
 2171:     my %advcell;
 2172:     foreach my $item (@usertools) {
 2173:         if ($context eq 'requestcourses') {
 2174:             my ($curroption,$currlimit);
 2175:             if (ref($settings) eq 'HASH') {
 2176:                 if (ref($settings->{$item}) eq 'HASH') {
 2177:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2178:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2179:                         $currlimit = $1;
 2180:                     }
 2181:                 }
 2182:             }
 2183:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2184:             my $checked = '';
 2185:             if ($curroption eq '') {
 2186:                 $checked = ' checked="checked"';
 2187:             }
 2188:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2189:                                '<input type="radio" name="crsreq_'.$item.
 2190:                                '__LC_adv" value=""'.$checked.' />'.
 2191:                                &mt('No override set').'</label></span>&nbsp; ';
 2192:             foreach my $option (@options) {
 2193:                 my $val = $option;
 2194:                 if ($option eq 'norequest') {
 2195:                     $val = 0;
 2196:                 }
 2197:                 if ($option eq 'validate') {
 2198:                     my $canvalidate = 0;
 2199:                     if (ref($validations{$item}) eq 'HASH') {
 2200:                         if ($validations{$item}{'_LC_adv'}) {
 2201:                             $canvalidate = 1;
 2202:                         }
 2203:                     }
 2204:                     next if (!$canvalidate);
 2205:                 }
 2206:                 my $checked = '';
 2207:                 if ($val eq $curroption) {
 2208:                     $checked = ' checked="checked"';
 2209:                 } elsif ($option eq 'autolimit') {
 2210:                     if ($curroption =~ /^autolimit/) {
 2211:                         $checked = ' checked="checked"';
 2212:                     }
 2213:                 }
 2214:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2215:                                   '<input type="radio" name="crsreq_'.$item.
 2216:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2217:                                   $titles{$option}.'</label>';
 2218:                 if ($option eq 'autolimit') {
 2219:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2220:                                        $item.'_limit__LC_adv" size="1" '.
 2221:                                        'value="'.$currlimit.'" />';
 2222:                 }
 2223:                 $advcell{$item} .= '</span> ';
 2224:                 if ($option eq 'autolimit') {
 2225:                     $advcell{$item} .= $titles{'unlimited'};
 2226:                 }
 2227:             }
 2228:         } elsif ($context eq 'requestauthor') {
 2229:             my $curroption;
 2230:             if (ref($settings) eq 'HASH') {
 2231:                 $curroption = $settings->{'_LC_adv'};
 2232:             }
 2233:             my $checked = '';
 2234:             if ($curroption eq '') {
 2235:                 $checked = ' checked="checked"';
 2236:             }
 2237:             $datatable .= '<span class="LC_nobreak"><label>'.
 2238:                           '<input type="radio" name="authorreq__LC_adv"'.
 2239:                           ' value=""'.$checked.' />'.
 2240:                           &mt('No override set').'</label></span>&nbsp; ';
 2241:             foreach my $option (@options) {
 2242:                 my $val = $option;
 2243:                 if ($option eq 'norequest') {
 2244:                     $val = 0;
 2245:                 }
 2246:                 my $checked = '';
 2247:                 if ($val eq $curroption) {
 2248:                     $checked = ' checked="checked"';
 2249:                 }
 2250:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2251:                               '<input type="radio" name="authorreq__LC_adv"'.
 2252:                               ' value="'.$val.'"'.$checked.' />'.
 2253:                               $titles{$option}.'</label></span>&nbsp; ';
 2254:             }
 2255:         } else {
 2256:             my $checked = 'checked="checked" ';
 2257:             if (ref($settings) eq 'HASH') {
 2258:                 if (ref($settings->{$item}) eq 'HASH') {
 2259:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2260:                         $checked = '';
 2261:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2262:                         $checked = 'checked="checked" ';
 2263:                     }
 2264:                 }
 2265:             }
 2266:             $datatable .= '<span class="LC_nobreak"><label>'.
 2267:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2268:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2269:                           '</label></span>&nbsp; ';
 2270:         }
 2271:     }
 2272:     if ($context eq 'requestcourses') {
 2273:         $datatable .= '</tr><tr>';
 2274:         foreach my $item (@usertools) {
 2275:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2276:         }
 2277:         $datatable .= '</tr></table>';
 2278:     }
 2279:     $datatable .= '</td></tr>';
 2280:     $$rowtotal += $typecount;
 2281:     return $datatable;
 2282: }
 2283: 
 2284: sub print_requestmail {
 2285:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2286:     my ($now,$datatable,%currapp);
 2287:     $now = time;
 2288:     if (ref($settings) eq 'HASH') {
 2289:         if (ref($settings->{'notify'}) eq 'HASH') {
 2290:             if ($settings->{'notify'}{'approval'} ne '') {
 2291:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2292:             }
 2293:         }
 2294:     }
 2295:     my $numinrow = 2;
 2296:     my $css_class;
 2297:     if ($$rowtotal%2) {
 2298:         $css_class = 'LC_odd_row';
 2299:     }
 2300:     if ($customcss) {
 2301:         $css_class .= " $customcss";
 2302:     }
 2303:     $css_class =~ s/^\s+//;
 2304:     if ($css_class) {
 2305:         $css_class = ' class="'.$css_class.'"';
 2306:     }
 2307:     if ($rowstyle) {
 2308:         $css_class .= ' style="'.$rowstyle.'"';
 2309:     }
 2310:     my $text;
 2311:     if ($action eq 'requestcourses') {
 2312:         $text = &mt('Receive notification of course requests requiring approval');
 2313:     } elsif ($action eq 'requestauthor') {
 2314:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2315:     } else {
 2316:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2317:     }
 2318:     $datatable = '<tr'.$css_class.'>'.
 2319:                  ' <td>'.$text.'</td>'.
 2320:                  ' <td class="LC_left_item">';
 2321:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2322:                                                  $action.'notifyapproval',%currapp);
 2323:     if ($numdc > 0) {
 2324:         $datatable .= $table;
 2325:     } else {
 2326:         $datatable .= &mt('There are no active Domain Coordinators');
 2327:     }
 2328:     $datatable .='</td></tr>';
 2329:     return $datatable;
 2330: }
 2331: 
 2332: sub print_studentcode {
 2333:     my ($settings,$rowtotal) = @_;
 2334:     my $rownum = 0; 
 2335:     my ($output,%current);
 2336:     my @crstypes = ('official','unofficial','community','textbook','placement');
 2337:     if (ref($settings) eq 'HASH') {
 2338:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2339:             foreach my $type (@crstypes) {
 2340:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2341:             }
 2342:         }
 2343:     }
 2344:     $output .= '<tr>'.
 2345:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2346:                '<td class="LC_left_item">';
 2347:     foreach my $type (@crstypes) {
 2348:         my $check = ' ';
 2349:         if ($current{$type}) {
 2350:             $check = ' checked="checked" ';
 2351:         }
 2352:         $output .= '<span class="LC_nobreak"><label>'.
 2353:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2354:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2355:     }
 2356:     $output .= '</td></tr>';
 2357:     $$rowtotal ++;
 2358:     return $output;
 2359: }
 2360: 
 2361: sub print_textbookcourses {
 2362:     my ($dom,$type,$settings,$rowtotal) = @_;
 2363:     my $rownum = 0;
 2364:     my $css_class;
 2365:     my $itemcount = 1;
 2366:     my $maxnum = 0;
 2367:     my $bookshash;
 2368:     if (ref($settings) eq 'HASH') {
 2369:         $bookshash = $settings->{$type};
 2370:     }
 2371:     my %ordered;
 2372:     if (ref($bookshash) eq 'HASH') {
 2373:         foreach my $item (keys(%{$bookshash})) {
 2374:             if (ref($bookshash->{$item}) eq 'HASH') {
 2375:                 my $num = $bookshash->{$item}{'order'};
 2376:                 $ordered{$num} = $item;
 2377:             }
 2378:         }
 2379:     }
 2380:     my $confname = $dom.'-domainconfig';
 2381:     my $switchserver = &check_switchserver($dom,$confname);
 2382:     my $maxnum = scalar(keys(%ordered));
 2383:     my $datatable;
 2384:     if (keys(%ordered)) {
 2385:         my @items = sort { $a <=> $b } keys(%ordered);
 2386:         for (my $i=0; $i<@items; $i++) {
 2387:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2388:             my $key = $ordered{$items[$i]};
 2389:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2390:             my $coursetitle = $coursehash{'description'};
 2391:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2392:             if (ref($bookshash->{$key}) eq 'HASH') {
 2393:                 $subject = $bookshash->{$key}->{'subject'};
 2394:                 $title = $bookshash->{$key}->{'title'};
 2395:                 if ($type eq 'textbooks') {
 2396:                     $publisher = $bookshash->{$key}->{'publisher'};
 2397:                     $author = $bookshash->{$key}->{'author'};
 2398:                     $image = $bookshash->{$key}->{'image'};
 2399:                     if ($image ne '') {
 2400:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2401:                         my $imagethumb = "$path/tn-".$imagefile;
 2402:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2403:                     }
 2404:                 }
 2405:             }
 2406:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2407:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2408:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2409:             for (my $k=0; $k<=$maxnum; $k++) {
 2410:                 my $vpos = $k+1;
 2411:                 my $selstr;
 2412:                 if ($k == $i) {
 2413:                     $selstr = ' selected="selected" ';
 2414:                 }
 2415:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2416:             }
 2417:             $datatable .= '</select>'.('&nbsp;'x2).
 2418:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2419:                 &mt('Delete?').'</label></span></td>'.
 2420:                 '<td colspan="2">'.
 2421:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2422:                 ('&nbsp;'x2).
 2423:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2424:             if ($type eq 'textbooks') {
 2425:                 $datatable .= ('&nbsp;'x2).
 2426:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2427:                               ('&nbsp;'x2).
 2428:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2429:                               ('&nbsp;'x2).
 2430:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2431:                 if ($image) {
 2432:                     $datatable .= $imgsrc.
 2433:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2434:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2435:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2436:                 }
 2437:                 if ($switchserver) {
 2438:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2439:                 } else {
 2440:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2441:                 }
 2442:             }
 2443:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2444:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2445:                           $coursetitle.'</span></td></tr>'."\n";
 2446:             $itemcount ++;
 2447:         }
 2448:     }
 2449:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2450:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2451:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2452:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2453:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2454:     for (my $k=0; $k<$maxnum+1; $k++) {
 2455:         my $vpos = $k+1;
 2456:         my $selstr;
 2457:         if ($k == $maxnum) {
 2458:             $selstr = ' selected="selected" ';
 2459:         }
 2460:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2461:     }
 2462:     $datatable .= '</select>&nbsp;'."\n".
 2463:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2464:                   '<td colspan="2">'.
 2465:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2466:                   ('&nbsp;'x2).
 2467:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2468:                   ('&nbsp;'x2);
 2469:     if ($type eq 'textbooks') {
 2470:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2471:                       ('&nbsp;'x2).
 2472:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2473:                       ('&nbsp;'x2).
 2474:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2475:         if ($switchserver) {
 2476:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2477:         } else {
 2478:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2479:         }
 2480:     }
 2481:     $datatable .= '</span>'."\n".
 2482:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2483:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2484:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2485:                   &Apache::loncommon::selectcourse_link
 2486:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course');
 2487:                   '</span></td>'."\n".
 2488:                   '</tr>'."\n";
 2489:     $itemcount ++;
 2490:     return $datatable;
 2491: }
 2492: 
 2493: sub textbookcourses_javascript {
 2494:     my ($settings) = @_;
 2495:     return unless(ref($settings) eq 'HASH');
 2496:     my (%ordered,%total,%jstext);
 2497:     foreach my $type ('textbooks','templates') {
 2498:         $total{$type} = 0;
 2499:         if (ref($settings->{$type}) eq 'HASH') {
 2500:             foreach my $item (keys(%{$settings->{$type}})) {
 2501:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2502:                     my $num = $settings->{$type}->{$item}{'order'};
 2503:                     $ordered{$type}{$num} = $item;
 2504:                 }
 2505:             }
 2506:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2507:         }
 2508:         my @jsarray = ();
 2509:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2510:             push(@jsarray,$ordered{$type}{$item});
 2511:         }
 2512:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2513:     }
 2514:     return <<"ENDSCRIPT";
 2515: <script type="text/javascript">
 2516: // <![CDATA[
 2517: function reorderBooks(form,item,caller) {
 2518:     var changedVal;
 2519: $jstext{'textbooks'};
 2520: $jstext{'templates'};
 2521:     var newpos;
 2522:     var maxh;
 2523:     if (caller == 'textbooks') {  
 2524:         newpos = 'textbooks_addbook_pos';
 2525:         maxh = 1 + $total{'textbooks'};
 2526:     } else {
 2527:         newpos = 'templates_addbook_pos';
 2528:         maxh = 1 + $total{'templates'};
 2529:     }
 2530:     var current = new Array;
 2531:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2532:     if (item == newpos) {
 2533:         changedVal = newitemVal;
 2534:     } else {
 2535:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2536:         current[newitemVal] = newpos;
 2537:     }
 2538:     if (caller == 'textbooks') {
 2539:         for (var i=0; i<textbooks.length; i++) {
 2540:             var elementName = 'textbooks_'+textbooks[i];
 2541:             if (elementName != item) {
 2542:                 if (form.elements[elementName]) {
 2543:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2544:                     current[currVal] = elementName;
 2545:                 }
 2546:             }
 2547:         }
 2548:     }
 2549:     if (caller == 'templates') {
 2550:         for (var i=0; i<templates.length; i++) {
 2551:             var elementName = 'templates_'+templates[i];
 2552:             if (elementName != item) {
 2553:                 if (form.elements[elementName]) {
 2554:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2555:                     current[currVal] = elementName;
 2556:                 }
 2557:             }
 2558:         }
 2559:     }
 2560:     var oldVal;
 2561:     for (var j=0; j<maxh; j++) {
 2562:         if (current[j] == undefined) {
 2563:             oldVal = j;
 2564:         }
 2565:     }
 2566:     if (oldVal < changedVal) {
 2567:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2568:            var elementName = current[k];
 2569:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2570:         }
 2571:     } else {
 2572:         for (var k=changedVal; k<oldVal; k++) {
 2573:             var elementName = current[k];
 2574:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2575:         }
 2576:     }
 2577:     return;
 2578: }
 2579: 
 2580: // ]]>
 2581: </script>
 2582: 
 2583: ENDSCRIPT
 2584: }
 2585: 
 2586: sub ltitools_javascript {
 2587:     my ($settings) = @_;
 2588:     return unless(ref($settings) eq 'HASH');
 2589:     my (%ordered,$total,%jstext);
 2590:     $total = 0;
 2591:     foreach my $item (keys(%{$settings})) {
 2592:         if (ref($settings->{$item}) eq 'HASH') {
 2593:             my $num = $settings->{$item}{'order'};
 2594:             $ordered{$num} = $item;
 2595:         }
 2596:     }
 2597:     $total = scalar(keys(%{$settings}));
 2598:     my @jsarray = ();
 2599:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2600:         push(@jsarray,$ordered{$item});
 2601:     }
 2602:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 2603:     return <<"ENDSCRIPT";
 2604: <script type="text/javascript">
 2605: // <![CDATA[
 2606: function reorderLTI(form,item) {
 2607:     var changedVal;
 2608: $jstext
 2609:     var newpos = 'ltitools_add_pos';
 2610:     var maxh = 1 + $total;
 2611:     var current = new Array;
 2612:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2613:     if (item == newpos) {
 2614:         changedVal = newitemVal;
 2615:     } else {
 2616:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2617:         current[newitemVal] = newpos;
 2618:     }
 2619:     for (var i=0; i<ltitools.length; i++) {
 2620:         var elementName = 'ltitools_'+ltitools[i];
 2621:         if (elementName != item) {
 2622:             if (form.elements[elementName]) {
 2623:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2624:                 current[currVal] = elementName;
 2625:             }
 2626:         }
 2627:     }
 2628:     var oldVal;
 2629:     for (var j=0; j<maxh; j++) {
 2630:         if (current[j] == undefined) {
 2631:             oldVal = j;
 2632:         }
 2633:     }
 2634:     if (oldVal < changedVal) {
 2635:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2636:            var elementName = current[k];
 2637:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2638:         }
 2639:     } else {
 2640:         for (var k=changedVal; k<oldVal; k++) {
 2641:             var elementName = current[k];
 2642:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2643:         }
 2644:     }
 2645:     return;
 2646: }
 2647: 
 2648: // ]]>
 2649: </script>
 2650: 
 2651: ENDSCRIPT
 2652: }
 2653: 
 2654: sub print_autoenroll {
 2655:     my ($dom,$settings,$rowtotal) = @_;
 2656:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2657:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 2658:     if (ref($settings) eq 'HASH') {
 2659:         if (exists($settings->{'run'})) {
 2660:             if ($settings->{'run'} eq '0') {
 2661:                 $runoff = ' checked="checked" ';
 2662:                 $runon = ' ';
 2663:             } else {
 2664:                 $runon = ' checked="checked" ';
 2665:                 $runoff = ' ';
 2666:             }
 2667:         } else {
 2668:             if ($autorun) {
 2669:                 $runon = ' checked="checked" ';
 2670:                 $runoff = ' ';
 2671:             } else {
 2672:                 $runoff = ' checked="checked" ';
 2673:                 $runon = ' ';
 2674:             }
 2675:         }
 2676:         if (exists($settings->{'co-owners'})) {
 2677:             if ($settings->{'co-owners'} eq '0') {
 2678:                 $coownersoff = ' checked="checked" ';
 2679:                 $coownerson = ' ';
 2680:             } else {
 2681:                 $coownerson = ' checked="checked" ';
 2682:                 $coownersoff = ' ';
 2683:             }
 2684:         } else {
 2685:             $coownersoff = ' checked="checked" ';
 2686:             $coownerson = ' ';
 2687:         }
 2688:         if (exists($settings->{'sender_domain'})) {
 2689:             $defdom = $settings->{'sender_domain'};
 2690:         }
 2691:         if (exists($settings->{'autofailsafe'})) {
 2692:             $failsafe = $settings->{'autofailsafe'};
 2693:         }
 2694:     } else {
 2695:         if ($autorun) {
 2696:             $runon = ' checked="checked" ';
 2697:             $runoff = ' ';
 2698:         } else {
 2699:             $runoff = ' checked="checked" ';
 2700:             $runon = ' ';
 2701:         }
 2702:     }
 2703:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2704:     my $notif_sender;
 2705:     if (ref($settings) eq 'HASH') {
 2706:         $notif_sender = $settings->{'sender_uname'};
 2707:     }
 2708:     my $datatable='<tr class="LC_odd_row">'.
 2709:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2710:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2711:                   '<input type="radio" name="autoenroll_run"'.
 2712:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2713:                   '<label><input type="radio" name="autoenroll_run"'.
 2714:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2715:                   '</tr><tr>'.
 2716:                   '<td>'.&mt('Notification messages - sender').
 2717:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2718:                   &mt('username').':&nbsp;'.
 2719:                   '<input type="text" name="sender_uname" value="'.
 2720:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2721:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2722:                   '<tr class="LC_odd_row">'.
 2723:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2724:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2725:                   '<input type="radio" name="autoassign_coowners"'.
 2726:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2727:                   '<label><input type="radio" name="autoassign_coowners"'.
 2728:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2729:                   '</tr><tr>'.
 2730:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 2731:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2732:                   '<input type="text" name="autoenroll_failsafe"'.
 2733:                   ' value="'.$failsafe.'" size="4" /></td></tr>';
 2734:     $$rowtotal += 4;
 2735:     return $datatable;
 2736: }
 2737: 
 2738: sub print_autoupdate {
 2739:     my ($position,$dom,$settings,$rowtotal) = @_;
 2740:     my $datatable;
 2741:     if ($position eq 'top') {
 2742:         my $updateon = ' ';
 2743:         my $updateoff = ' checked="checked" ';
 2744:         my $classlistson = ' ';
 2745:         my $classlistsoff = ' checked="checked" ';
 2746:         if (ref($settings) eq 'HASH') {
 2747:             if ($settings->{'run'} eq '1') {
 2748:                 $updateon = $updateoff;
 2749:                 $updateoff = ' ';
 2750:             }
 2751:             if ($settings->{'classlists'} eq '1') {
 2752:                 $classlistson = $classlistsoff;
 2753:                 $classlistsoff = ' ';
 2754:             }
 2755:         }
 2756:         my %title = (
 2757:                    run => 'Auto-update active?',
 2758:                    classlists => 'Update information in classlists?',
 2759:                     );
 2760:         $datatable = '<tr class="LC_odd_row">'. 
 2761:                   '<td>'.&mt($title{'run'}).'</td>'.
 2762:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2763:                   '<input type="radio" name="autoupdate_run"'.
 2764:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2765:                   '<label><input type="radio" name="autoupdate_run"'.
 2766:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2767:                   '</tr><tr>'.
 2768:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2769:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2770:                   '<label><input type="radio" name="classlists"'.
 2771:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2772:                   '<label><input type="radio" name="classlists"'.
 2773:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2774:                   '</tr>';
 2775:         $$rowtotal += 2;
 2776:     } elsif ($position eq 'middle') {
 2777:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2778:         my $numinrow = 3;
 2779:         my $locknamesettings;
 2780:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2781:                                      $dom,$numinrow,$othertitle,
 2782:                                     'lockablenames',$rowtotal);
 2783:         $$rowtotal ++;
 2784:     } else {
 2785:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2786:         my @fields = ('lastname','firstname','middlename','generation',
 2787:                       'permanentemail','id');
 2788:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2789:         my $numrows = 0;
 2790:         if (ref($types) eq 'ARRAY') {
 2791:             if (@{$types} > 0) {
 2792:                 $datatable = 
 2793:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2794:                                          \@fields,$types,\$numrows);
 2795:                     $$rowtotal += @{$types}; 
 2796:             }
 2797:         }
 2798:         $datatable .= 
 2799:             &usertype_update_row($settings,{'default' => $othertitle},
 2800:                                  \%fieldtitles,\@fields,['default'],
 2801:                                  \$numrows);
 2802:         $$rowtotal ++;     
 2803:     }
 2804:     return $datatable;
 2805: }
 2806: 
 2807: sub print_autocreate {
 2808:     my ($dom,$settings,$rowtotal) = @_;
 2809:     my (%createon,%createoff,%currhash);
 2810:     my @types = ('xml','req');
 2811:     if (ref($settings) eq 'HASH') {
 2812:         foreach my $item (@types) {
 2813:             $createoff{$item} = ' checked="checked" ';
 2814:             $createon{$item} = ' ';
 2815:             if (exists($settings->{$item})) {
 2816:                 if ($settings->{$item}) {
 2817:                     $createon{$item} = ' checked="checked" ';
 2818:                     $createoff{$item} = ' ';
 2819:                 }
 2820:             }
 2821:         }
 2822:         if ($settings->{'xmldc'} ne '') {
 2823:             $currhash{$settings->{'xmldc'}} = 1;
 2824:         }
 2825:     } else {
 2826:         foreach my $item (@types) {
 2827:             $createoff{$item} = ' checked="checked" ';
 2828:             $createon{$item} = ' ';
 2829:         }
 2830:     }
 2831:     $$rowtotal += 2;
 2832:     my $numinrow = 2;
 2833:     my $datatable='<tr class="LC_odd_row">'.
 2834:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2835:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2836:                   '<input type="radio" name="autocreate_xml"'.
 2837:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2838:                   '<label><input type="radio" name="autocreate_xml"'.
 2839:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2840:                   '</td></tr><tr>'.
 2841:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2842:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2843:                   '<input type="radio" name="autocreate_req"'.
 2844:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2845:                   '<label><input type="radio" name="autocreate_req"'.
 2846:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2847:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2848:                                                    'autocreate_xmldc',%currhash);
 2849:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 2850:     if ($numdc > 1) {
 2851:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 2852:                       '</td><td class="LC_left_item">';
 2853:     } else {
 2854:         $datatable .= &mt('Course creation processed as:').
 2855:                       '</td><td class="LC_right_item">';
 2856:     }
 2857:     $datatable .= $dctable.'</td></tr>';
 2858:     $$rowtotal += $rows;
 2859:     return $datatable;
 2860: }
 2861: 
 2862: sub print_directorysrch {
 2863:     my ($position,$dom,$settings,$rowtotal) = @_;
 2864:     my $datatable;
 2865:     if ($position eq 'top') {
 2866:         my $instsrchon = ' ';
 2867:         my $instsrchoff = ' checked="checked" ';
 2868:         my ($exacton,$containson,$beginson);
 2869:         my $instlocalon = ' ';
 2870:         my $instlocaloff = ' checked="checked" ';
 2871:         if (ref($settings) eq 'HASH') {
 2872:             if ($settings->{'available'} eq '1') {
 2873:                 $instsrchon = $instsrchoff;
 2874:                 $instsrchoff = ' ';
 2875:             }
 2876:             if ($settings->{'localonly'} eq '1') {
 2877:                 $instlocalon = $instlocaloff;
 2878:                 $instlocaloff = ' ';
 2879:             }
 2880:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2881:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 2882:                     if ($type eq 'exact') {
 2883:                         $exacton = ' checked="checked" ';
 2884:                     } elsif ($type eq 'contains') {
 2885:                         $containson = ' checked="checked" ';
 2886:                     } elsif ($type eq 'begins') {
 2887:                         $beginson = ' checked="checked" ';
 2888:                     }
 2889:                 }
 2890:             } else {
 2891:                 if ($settings->{'searchtypes'} eq 'exact') {
 2892:                     $exacton = ' checked="checked" ';
 2893:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 2894:                     $containson = ' checked="checked" ';
 2895:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 2896:                     $exacton = ' checked="checked" ';
 2897:                     $containson = ' checked="checked" ';
 2898:                 }
 2899:             }
 2900:         }
 2901:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2902:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2903: 
 2904:         my $numinrow = 4;
 2905:         my $cansrchrow = 0;
 2906:         $datatable='<tr class="LC_odd_row">'.
 2907:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 2908:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2909:                    '<input type="radio" name="dirsrch_available"'.
 2910:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2911:                    '<label><input type="radio" name="dirsrch_available"'.
 2912:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2913:                    '</tr><tr>'.
 2914:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 2915:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2916:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 2917:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2918:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 2919:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2920:                    '</tr>';
 2921:         $$rowtotal += 2;
 2922:         if (ref($usertypes) eq 'HASH') {
 2923:             if (keys(%{$usertypes}) > 0) {
 2924:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2925:                                              $numinrow,$othertitle,'cansearch',
 2926:                                              $rowtotal);
 2927:                 $cansrchrow = 1;
 2928:             }
 2929:         }
 2930:         if ($cansrchrow) {
 2931:             $$rowtotal ++;
 2932:             $datatable .= '<tr>';
 2933:         } else {
 2934:             $datatable .= '<tr class="LC_odd_row">';
 2935:         }
 2936:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2937:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2938:         foreach my $title (@{$titleorder}) {
 2939:             if (defined($searchtitles->{$title})) {
 2940:                 my $check = ' ';
 2941:                 if (ref($settings) eq 'HASH') {
 2942:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2943:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2944:                             $check = ' checked="checked" ';
 2945:                         }
 2946:                     }
 2947:                 }
 2948:                 $datatable .= '<td class="LC_left_item">'.
 2949:                               '<span class="LC_nobreak"><label>'.
 2950:                               '<input type="checkbox" name="searchby" '.
 2951:                               'value="'.$title.'"'.$check.'/>'.
 2952:                               $searchtitles->{$title}.'</label></span></td>';
 2953:             }
 2954:         }
 2955:         $datatable .= '</tr></table></td></tr>';
 2956:         $$rowtotal ++;
 2957:         if ($cansrchrow) {
 2958:             $datatable .= '<tr class="LC_odd_row">';
 2959:         } else {
 2960:             $datatable .= '<tr>';
 2961:         }
 2962:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2963:                       '<td class="LC_left_item" colspan="2">'.
 2964:                       '<span class="LC_nobreak"><label>'.
 2965:                       '<input type="checkbox" name="searchtypes" '.
 2966:                       $exacton.' value="exact" />'.&mt('Exact match').
 2967:                       '</label>&nbsp;'.
 2968:                       '<label><input type="checkbox" name="searchtypes" '.
 2969:                       $beginson.' value="begins" />'.&mt('Begins with').
 2970:                       '</label>&nbsp;'.
 2971:                       '<label><input type="checkbox" name="searchtypes" '.
 2972:                       $containson.' value="contains" />'.&mt('Contains').
 2973:                       '</label></span></td></tr>';
 2974:         $$rowtotal ++;
 2975:     } else {
 2976:         my $domsrchon = ' checked="checked" ';
 2977:         my $domsrchoff = ' ';
 2978:         my $domlocalon = ' ';
 2979:         my $domlocaloff = ' checked="checked" ';
 2980:         if (ref($settings) eq 'HASH') {
 2981:             if ($settings->{'lclocalonly'} eq '1') {
 2982:                 $domlocalon = $domlocaloff;
 2983:                 $domlocaloff = ' ';
 2984:             }
 2985:             if ($settings->{'lcavailable'} eq '0') {
 2986:                 $domsrchoff = $domsrchon;
 2987:                 $domsrchon = ' ';
 2988:             }
 2989:         }
 2990:         $datatable='<tr class="LC_odd_row">'.
 2991:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 2992:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2993:                       '<input type="radio" name="dirsrch_domavailable"'.
 2994:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2995:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 2996:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2997:                       '</tr><tr>'.
 2998:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 2999:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3000:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 3001:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 3002:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 3003:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 3004:                       '</tr>';
 3005:         $$rowtotal += 2;
 3006:     }
 3007:     return $datatable;
 3008: }
 3009: 
 3010: sub print_contacts {
 3011:     my ($position,$dom,$settings,$rowtotal) = @_;
 3012:     my $datatable;
 3013:     my @contacts = ('adminemail','supportemail');
 3014:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 3015:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings);
 3016:     if ($position eq 'top') {
 3017:         if (ref($settings) eq 'HASH') {
 3018:             foreach my $item (@contacts) {
 3019:                 if (exists($settings->{$item})) {
 3020:                     $to{$item} = $settings->{$item};
 3021:                 }
 3022:             }
 3023:         }
 3024:     } elsif ($position eq 'middle') {
 3025:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 3026:                      'updatesmail','idconflictsmail');
 3027:         foreach my $type (@mailings) {
 3028:             $otheremails{$type} = '';
 3029:         }
 3030:     } else {
 3031:         @mailings = ('helpdeskmail','otherdomsmail');
 3032:         foreach my $type (@mailings) {
 3033:             $otheremails{$type} = '';
 3034:         }
 3035:         $bccemails{'helpdeskmail'} = '';
 3036:         $bccemails{'otherdomsmail'} = '';
 3037:         $includestr{'helpdeskmail'} = '';
 3038:         $includestr{'otherdomsmail'} = '';
 3039:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 3040:     }
 3041:     if (ref($settings) eq 'HASH') {
 3042:         unless ($position eq 'top') {
 3043:             foreach my $type (@mailings) {
 3044:                 if (exists($settings->{$type})) {
 3045:                     if (ref($settings->{$type}) eq 'HASH') {
 3046:                         foreach my $item (@contacts) {
 3047:                             if ($settings->{$type}{$item}) {
 3048:                                 $checked{$type}{$item} = ' checked="checked" ';
 3049:                             }
 3050:                         }
 3051:                         $otheremails{$type} = $settings->{$type}{'others'};
 3052:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3053:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 3054:                             if ($settings->{$type}{'include'} ne '') {
 3055:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3056:                                 $includestr{$type} = &unescape($includestr{$type});
 3057:                             }
 3058:                         }
 3059:                     }
 3060:                 } elsif ($type eq 'lonstatusmail') {
 3061:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3062:                 }
 3063:             }
 3064:         }
 3065:         if ($position eq 'bottom') {
 3066:             foreach my $type (@mailings) {
 3067:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 3068:                 if ($settings->{$type}{'include'} ne '') {
 3069:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3070:                     $includestr{$type} = &unescape($includestr{$type});
 3071:                 }
 3072:             }
 3073:             if (ref($settings->{'helpform'}) eq 'HASH') {
 3074:                 if (ref($fields) eq 'ARRAY') {
 3075:                     foreach my $field (@{$fields}) {
 3076:                         $currfield{$field} = $settings->{'helpform'}{$field};
 3077:                     }
 3078:                 }
 3079:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 3080:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 3081:                 } else {
 3082:                     $maxsize = '1.0';
 3083:                 }
 3084:             } else {
 3085:                 if (ref($fields) eq 'ARRAY') {
 3086:                     foreach my $field (@{$fields}) {
 3087:                         $currfield{$field} = 'yes';
 3088:                     }
 3089:                 }
 3090:                 $maxsize = '1.0';
 3091:             }
 3092:         }
 3093:     } else {
 3094:         if ($position eq 'top') {
 3095:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 3096:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 3097:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 3098:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 3099:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3100:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 3101:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 3102:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 3103:         } elsif ($position eq 'bottom') {
 3104:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 3105:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 3106:             if (ref($fields) eq 'ARRAY') {
 3107:                 foreach my $field (@{$fields}) {
 3108:                     $currfield{$field} = 'yes';
 3109:                 }
 3110:             }
 3111:             $maxsize = '1.0';
 3112:         }
 3113:     }
 3114:     my ($titles,$short_titles) = &contact_titles();
 3115:     my $rownum = 0;
 3116:     my $css_class;
 3117:     if ($position eq 'top') {
 3118:         foreach my $item (@contacts) {
 3119:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3120:             $datatable .= '<tr'.$css_class.'>'. 
 3121:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 3122:                           '</span></td><td class="LC_right_item">'.
 3123:                           '<input type="text" name="'.$item.'" value="'.
 3124:                           $to{$item}.'" /></td></tr>';
 3125:             $rownum ++;
 3126:         }
 3127:     } else {
 3128:         foreach my $type (@mailings) {
 3129:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3130:             $datatable .= '<tr'.$css_class.'>'.
 3131:                           '<td><span class="LC_nobreak">'.
 3132:                           $titles->{$type}.': </span></td>'.
 3133:                           '<td class="LC_left_item">';
 3134:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3135:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 3136:             }
 3137:             $datatable .= '<span class="LC_nobreak">';
 3138:             foreach my $item (@contacts) {
 3139:                 $datatable .= '<label>'.
 3140:                               '<input type="checkbox" name="'.$type.'"'.
 3141:                               $checked{$type}{$item}.
 3142:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 3143:                               '</label>&nbsp;';
 3144:             }
 3145:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3146:                           '<input type="text" name="'.$type.'_others" '.
 3147:                           'value="'.$otheremails{$type}.'"  />';
 3148:             my %locchecked;
 3149:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3150:                 foreach my $loc ('s','b') {
 3151:                     if ($includeloc{$type} eq $loc) {
 3152:                         $locchecked{$loc} = ' checked="checked"';
 3153:                         last;
 3154:                     }
 3155:                 }
 3156:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3157:                               '<input type="text" name="'.$type.'_bcc" '.
 3158:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 3159:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3160:                               &mt('Text automatically added to e-mail:').' '.
 3161:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br >'.
 3162:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3163:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3164:                               ('&nbsp;'x2).
 3165:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3166:                               '</span></fieldset>';
 3167:             }
 3168:             $datatable .= '</td></tr>'."\n";
 3169:             $rownum ++;
 3170:         }
 3171:     }
 3172:     if ($position eq 'middle') {
 3173:         my %choices;
 3174:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 3175:                                        &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3176:                                        &mt('LON-CAPA core group - MSU'),600,500));
 3177:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 3178:                                         &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3179:                                         &mt('LON-CAPA core group - MSU'),600,500));
 3180:         my @toggles = ('reporterrors','reportupdates');
 3181:         my %defaultchecked = ('reporterrors'  => 'on',
 3182:                               'reportupdates' => 'on');
 3183:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3184:                                                    \%choices,$rownum);
 3185:         $datatable .= $reports;
 3186:     } elsif ($position eq 'bottom') {
 3187:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3188:         $datatable .= '<tr'.$css_class.'>'.
 3189:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 3190:                       &mt('(e-mail, subject, and description always shown)').
 3191:                       '</td><td class="LC_left_item">';
 3192:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 3193:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 3194:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 3195:             foreach my $field (@{$fields}) {
 3196:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 3197:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 3198:                     $datatable .= ' '.&mt('(logged-in users)');
 3199:                 }
 3200:                 $datatable .='</td><td>';
 3201:                 my $clickaction;
 3202:                 if ($field eq 'screenshot') {
 3203:                     $clickaction = ' onclick="screenshotSize(this);"';
 3204:                 }
 3205:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 3206:                     foreach my $option (@{$possoptions->{$field}}) {
 3207:                         my $checked;
 3208:                         if ($currfield{$field} eq $option) {
 3209:                             $checked = ' checked="checked"';
 3210:                         }
 3211:                         $datatable .= '<span class="LC_nobreak"><label>'.
 3212:                                       '<input type="radio" name="helpform_'.$field.'" '.
 3213:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 3214:                                       '</label></span>'.('&nbsp;'x2);
 3215:                     }
 3216:                 }
 3217:                 if ($field eq 'screenshot') {
 3218:                     my $display;
 3219:                     if ($currfield{$field} eq 'no') {
 3220:                         $display = ' style="display:none"';
 3221:                     }
 3222:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.' />'.
 3223:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 3224:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 3225:                 }
 3226:                 $datatable .= '</td></tr>';
 3227:             }
 3228:             $datatable .= '</table>';
 3229:         }
 3230:         $datatable .= '</td></tr>'."\n";
 3231:         $rownum ++;
 3232:     }
 3233:     $$rowtotal += $rownum;
 3234:     return $datatable;
 3235: }
 3236: 
 3237: sub contacts_javascript {
 3238:     return <<"ENDSCRIPT";
 3239: 
 3240: <script type="text/javascript">
 3241: // <![CDATA[
 3242: 
 3243: function screenshotSize(field) {
 3244:     if (document.getElementById('help_screenshotsize')) {
 3245:         if (field.value == 'no') {
 3246:             document.getElementById('help_screenshotsize').style.display="none";
 3247:         } else {
 3248:             document.getElementById('help_screenshotsize').style.display="";
 3249:         }
 3250:     }
 3251:     return;
 3252: }
 3253: 
 3254: // ]]>
 3255: </script>
 3256: 
 3257: ENDSCRIPT
 3258: }
 3259: 
 3260: sub print_helpsettings {
 3261:     my ($position,$dom,$settings,$rowtotal) = @_;
 3262:     my $confname = $dom.'-domainconfig';
 3263:     my $formname = 'display';
 3264:     my ($datatable,$itemcount);
 3265:     if ($position eq 'top') {
 3266:         $itemcount = 1;
 3267:         my (%choices,%defaultchecked,@toggles);
 3268:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 3269:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 3270:                                      &mt('LON-CAPA bug tracker'),600,500));
 3271:         %defaultchecked = ('submitbugs' => 'on');
 3272:         @toggles = ('submitbugs');
 3273:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3274:                                                      \%choices,$itemcount);
 3275:         $$rowtotal ++;
 3276:     } else {
 3277:         my $css_class;
 3278:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 3279:         my (%customroles,%ordered,%current);
 3280:         if (ref($settings) eq 'HASH') {
 3281:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 3282:                 %current = %{$settings->{'adhoc'}};
 3283:             }
 3284:         }
 3285:         my $count = 0;
 3286:         foreach my $key (sort(keys(%existing))) {
 3287:             if ($key=~/^rolesdef\_(\w+)$/) {
 3288:                 my $rolename = $1;
 3289:                 my (%privs,$order);
 3290:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 3291:                 $customroles{$rolename} = \%privs;
 3292:                 if (ref($current{$rolename}) eq 'HASH') {
 3293:                     $order = $current{$rolename}{'order'};
 3294:                 }
 3295:                 if ($order eq '') {
 3296:                     $order = $count;
 3297:                 }
 3298:                 $ordered{$order} = $rolename;
 3299:                 $count++;
 3300:             }
 3301:         }
 3302:         my $maxnum = scalar(keys(%ordered));
 3303:         my @roles_by_num = ();
 3304:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3305:             push(@roles_by_num,$item);
 3306:         }
 3307:         my $context = 'domprefs';
 3308:         my $crstype = 'Course';
 3309:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3310:         my @accesstypes = ('all','dh','da','none');
 3311:         my ($numstatustypes,@jsarray);
 3312:         if (ref($types) eq 'ARRAY') {
 3313:             if (@{$types} > 0) {
 3314:                 $numstatustypes = scalar(@{$types});
 3315:                 push(@accesstypes,'status');
 3316:                 @jsarray = ('bystatus');
 3317:             }
 3318:         }
 3319:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 3320:         if (keys(%domhelpdesk)) {
 3321:             push(@accesstypes,('inc','exc'));
 3322:             push(@jsarray,('notinc','notexc'));
 3323:         }
 3324:         my $hiddenstr = join("','",@jsarray);
 3325:         $datatable .= &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname);
 3326:         my $context = 'domprefs';
 3327:         my $crstype = 'Course';
 3328:         my $prefix = 'helproles_';
 3329:         my $add_class = 'LC_hidden';
 3330:         foreach my $num (@roles_by_num) {
 3331:             my $role = $ordered{$num};
 3332:             my ($desc,$access,@statuses);
 3333:             if (ref($current{$role}) eq 'HASH') {
 3334:                 $desc = $current{$role}{'desc'};
 3335:                 $access = $current{$role}{'access'};
 3336:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 3337:                     @statuses = @{$current{$role}{'insttypes'}};
 3338:                 }
 3339:             }
 3340:             if ($desc eq '') {
 3341:                 $desc = $role;
 3342:             }
 3343:             my $identifier = 'custhelp'.$num;
 3344:             my %full=();
 3345:             my %levels= (
 3346:                          course => {},
 3347:                          domain => {},
 3348:                          system => {},
 3349:                         );
 3350:             my %levelscurrent=(
 3351:                                course => {},
 3352:                                domain => {},
 3353:                                system => {},
 3354:                               );
 3355:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 3356:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3357:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3358:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 3359:             $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><b>'.$role.'</b><br />'.
 3360:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 3361:             for (my $k=0; $k<=$maxnum; $k++) {
 3362:                 my $vpos = $k+1;
 3363:                 my $selstr;
 3364:                 if ($k == $num) {
 3365:                     $selstr = ' selected="selected" ';
 3366:                 }
 3367:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3368:             }
 3369:             $datatable .= '</select>'.('&nbsp;'x2).
 3370:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 3371:                           '</td>'.
 3372:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3373:                           &mt('Name shown to users:').
 3374:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 3375:                           '</fieldset>'.
 3376:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 3377:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 3378:                           '<fieldset>'.
 3379:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 3380:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 3381:                                                                    \%levelscurrent,$identifier,
 3382:                                                                    'LC_hidden',$prefix.$num.'_privs').
 3383:                           '</fieldset></td>';
 3384:             $itemcount ++;
 3385:         }
 3386:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3387:         my $newcust = 'custhelp'.$count;
 3388:         my (%privs,%levelscurrent);
 3389:         my %full=();
 3390:         my %levels= (
 3391:                      course => {},
 3392:                      domain => {},
 3393:                      system => {},
 3394:                     );
 3395:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 3396:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3397:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 3398:         $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><span class="LC_nobreak"><label>'.
 3399:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 3400:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 3401:         for (my $k=0; $k<$maxnum+1; $k++) {
 3402:             my $vpos = $k+1;
 3403:             my $selstr;
 3404:             if ($k == $maxnum) {
 3405:                 $selstr = ' selected="selected" ';
 3406:             }
 3407:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3408:         }
 3409:         $datatable .= '</select>&nbsp;'."\n".
 3410:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 3411:                       '</label></span></td>'.
 3412:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3413:                       '<span class="LC_nobreak">'.
 3414:                       &mt('Internal name:').
 3415:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 3416:                       '</span>'.('&nbsp;'x4).
 3417:                       '<span class="LC_nobreak">'.
 3418:                       &mt('Name shown to users:').
 3419:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 3420:                       '</span></fieldset>'.
 3421:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 3422:                                              $usertypes,$types,\%domhelpdesk).
 3423:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 3424:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 3425:                                                                 \@templateroles,$newcust).
 3426:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 3427:                                                                \%levelscurrent,$newcust).
 3428:                       '</fieldset></td></tr>';
 3429:         $count ++;
 3430:         $$rowtotal += $count;
 3431:     }
 3432:     return $datatable;
 3433: }
 3434: 
 3435: sub adhocbutton {
 3436:     my ($prefix,$num,$field,$visibility) = @_;
 3437:     my %lt = &Apache::lonlocal::texthash(
 3438:                                           show => 'Show details',
 3439:                                           hide => 'Hide details',
 3440:                                         );
 3441:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 3442:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 3443:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 3444:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 3445: }
 3446: 
 3447: sub helpsettings_javascript {
 3448:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 3449:     return unless(ref($roles_by_num) eq 'ARRAY');
 3450:     my %html_js_lt = &Apache::lonlocal::texthash(
 3451:                                           show => 'Show details',
 3452:                                           hide => 'Hide details',
 3453:                                         );
 3454:     &html_escape(\%html_js_lt);
 3455:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 3456:     return <<"ENDSCRIPT";
 3457: <script type="text/javascript">
 3458: // <![CDATA[
 3459: 
 3460: function reorderHelpRoles(form,item) {
 3461:     var changedVal;
 3462: $jstext
 3463:     var newpos = 'helproles_${total}_pos';
 3464:     var maxh = 1 + $total;
 3465:     var current = new Array();
 3466:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3467:     if (item == newpos) {
 3468:         changedVal = newitemVal;
 3469:     } else {
 3470:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3471:         current[newitemVal] = newpos;
 3472:     }
 3473:     for (var i=0; i<helproles.length; i++) {
 3474:         var elementName = 'helproles_'+helproles[i]+'_pos';
 3475:         if (elementName != item) {
 3476:             if (form.elements[elementName]) {
 3477:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3478:                 current[currVal] = elementName;
 3479:             }
 3480:         }
 3481:     }
 3482:     var oldVal;
 3483:     for (var j=0; j<maxh; j++) {
 3484:         if (current[j] == undefined) {
 3485:             oldVal = j;
 3486:         }
 3487:     }
 3488:     if (oldVal < changedVal) {
 3489:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3490:            var elementName = current[k];
 3491:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3492:         }
 3493:     } else {
 3494:         for (var k=changedVal; k<oldVal; k++) {
 3495:             var elementName = current[k];
 3496:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3497:         }
 3498:     }
 3499:     return;
 3500: }
 3501: 
 3502: function helpdeskAccess(num) {
 3503:     var curraccess = null;
 3504:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 3505:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 3506:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 3507:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 3508:             }
 3509:         }
 3510:     }
 3511:     var shown = Array();
 3512:     var hidden = Array();
 3513:     if (curraccess == 'none') {
 3514:         hidden = Array('$hiddenstr');
 3515:     } else {
 3516:         if (curraccess == 'status') {
 3517:             shown = Array('bystatus');
 3518:             hidden = Array('notinc','notexc');
 3519:         } else {
 3520:             if (curraccess == 'exc') {
 3521:                 shown = Array('notexc');
 3522:                 hidden = Array('notinc','bystatus');
 3523:             }
 3524:             if (curraccess == 'inc') {
 3525:                 shown = Array('notinc');
 3526:                 hidden = Array('notexc','bystatus');
 3527:             }
 3528:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 3529:                 hidden = Array('notinc','notexc','bystatus');
 3530:             }
 3531:         }
 3532:     }
 3533:     if (hidden.length > 0) {
 3534:         for (var i=0; i<hidden.length; i++) {
 3535:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 3536:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 3537:             }
 3538:         }
 3539:     }
 3540:     if (shown.length > 0) {
 3541:         for (var i=0; i<shown.length; i++) {
 3542:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 3543:                 if (shown[i] == 'privs') {
 3544:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 3545:                 } else {
 3546:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 3547:                 }
 3548:             }
 3549:         }
 3550:     }
 3551:     return;
 3552: }
 3553: 
 3554: function toggleHelpdeskItem(num,field) {
 3555:     if (document.getElementById('helproles_'+num+'_'+field)) {
 3556:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 3557:             document.getElementById('helproles_'+num+'_'+field).className =
 3558:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 3559:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3560:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 3561:             }
 3562:         } else {
 3563:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 3564:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3565:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 3566:             }
 3567:         }
 3568:     }
 3569:     return;
 3570: }
 3571: 
 3572: // ]]>
 3573: </script>
 3574: 
 3575: ENDSCRIPT
 3576: }
 3577: 
 3578: sub helpdeskroles_access {
 3579:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 3580:         $usertypes,$types,$domhelpdesk) = @_;
 3581:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 3582:     my %lt = &Apache::lonlocal::texthash(
 3583:                     'rou'    => 'Role usage',
 3584:                     'whi'    => 'Which helpdesk personnel may use this role?',
 3585:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 3586:                     'dh'     => 'All with domain helpdesk role',
 3587:                     'da'     => 'All with domain helpdesk assistant role',
 3588:                     'none'   => 'None',
 3589:                     'status' => 'Determined based on institutional status',
 3590:                     'inc'    => 'Include all, but exclude specific personnel',
 3591:                     'exc'    => 'Exclude all, but include specific personnel',
 3592:                   );
 3593:     my %usecheck = (
 3594:                      all => ' checked="checked"',
 3595:                    );
 3596:     my %displaydiv = (
 3597:                       status => 'none',
 3598:                       inc    => 'none',
 3599:                       exc    => 'none',
 3600:                       priv   => 'block',
 3601:                      );
 3602:     my $output;
 3603:     if (ref($current) eq 'HASH') {
 3604:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 3605:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 3606:                 $usecheck{$current->{access}} = $usecheck{'all'};
 3607:                 delete($usecheck{'all'});
 3608:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 3609:                     my $access = $1;
 3610:                     $displaydiv{$access} = 'inline';
 3611:                 } elsif ($current->{access} eq 'none') {
 3612:                     $displaydiv{'priv'} = 'none';
 3613:                 }
 3614:             }
 3615:         }
 3616:     }
 3617:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 3618:               '<p>'.$lt{'whi'}.'</p>';
 3619:     foreach my $access (@{$accesstypes}) {
 3620:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 3621:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 3622:                    $lt{$access}.'</label>';
 3623:         if ($access eq 'status') {
 3624:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 3625:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 3626:                                                                  $othertitle,$usertypes,$types).
 3627:                        '</div>';
 3628:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 3629:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 3630:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3631:                        '</div>';
 3632:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 3633:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 3634:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3635:                        '</div>';
 3636:         }
 3637:         $output .= '</p>';
 3638:     }
 3639:     $output .= '</fieldset>';
 3640:     return $output;
 3641: }
 3642: 
 3643: sub radiobutton_prefs {
 3644:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 3645:         $additional,$align) = @_;
 3646:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 3647:                    (ref($choices) eq 'HASH'));
 3648: 
 3649:     my (%checkedon,%checkedoff,$datatable,$css_class);
 3650: 
 3651:     foreach my $item (@{$toggles}) {
 3652:         if ($defaultchecked->{$item} eq 'on') {
 3653:             $checkedon{$item} = ' checked="checked" ';
 3654:             $checkedoff{$item} = ' ';
 3655:         } elsif ($defaultchecked->{$item} eq 'off') {
 3656:             $checkedoff{$item} = ' checked="checked" ';
 3657:             $checkedon{$item} = ' ';
 3658:         }
 3659:     }
 3660:     if (ref($settings) eq 'HASH') {
 3661:         foreach my $item (@{$toggles}) {
 3662:             if ($settings->{$item} eq '1') {
 3663:                 $checkedon{$item} =  ' checked="checked" ';
 3664:                 $checkedoff{$item} = ' ';
 3665:             } elsif ($settings->{$item} eq '0') {
 3666:                 $checkedoff{$item} =  ' checked="checked" ';
 3667:                 $checkedon{$item} = ' ';
 3668:             }
 3669:         }
 3670:     }
 3671:     if ($onclick) {
 3672:         $onclick = ' onclick="'.$onclick.'"';
 3673:     }
 3674:     foreach my $item (@{$toggles}) {
 3675:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3676:         $datatable .=
 3677:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 3678:             '<span class="LC_nobreak">'.$choices->{$item}.
 3679:             '</span></td>';
 3680:         if ($align eq 'left') {
 3681:             $datatable .= '<td class="LC_left_item">';
 3682:         } else {
 3683:             $datatable .= '<td class="LC_right_item">';
 3684:         }
 3685:         $datatable .=
 3686:             '<span class="LC_nobreak">'.
 3687:             '<label><input type="radio" name="'.
 3688:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 3689:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 3690:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 3691:             '</span>'.$additional.
 3692:             '</td>'.
 3693:             '</tr>';
 3694:         $itemcount ++;
 3695:     }
 3696:     return ($datatable,$itemcount);
 3697: }
 3698: 
 3699: sub print_ltitools {
 3700:     my ($dom,$settings,$rowtotal) = @_;
 3701:     my $rownum = 0;
 3702:     my $css_class;
 3703:     my $itemcount = 1;
 3704:     my $maxnum = 0;
 3705:     my %ordered;
 3706:     if (ref($settings) eq 'HASH') {
 3707:         foreach my $item (keys(%{$settings})) {
 3708:             if (ref($settings->{$item}) eq 'HASH') {
 3709:                 my $num = $settings->{$item}{'order'};
 3710:                 $ordered{$num} = $item;
 3711:             }
 3712:         }
 3713:     }
 3714:     my $confname = $dom.'-domainconfig';
 3715:     my $switchserver = &check_switchserver($dom,$confname);
 3716:     my $maxnum = scalar(keys(%ordered));
 3717:     my $datatable = &ltitools_javascript($settings);
 3718:     my %lt = &ltitools_names();
 3719:     my @courseroles = ('cc','in','ta','ep','st');
 3720:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 3721:     my @fields = ('fullname','firstname','lastname','email','user','roles');
 3722:     if (keys(%ordered)) {
 3723:         my @items = sort { $a <=> $b } keys(%ordered);
 3724:         for (my $i=0; $i<@items; $i++) {
 3725:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3726:             my $item = $ordered{$items[$i]};
 3727:             my ($title,$key,$secret,$url,$imgsrc,$version);
 3728:             if (ref($settings->{$item}) eq 'HASH') {
 3729:                 $title = $settings->{$item}->{'title'};
 3730:                 $url = $settings->{$item}->{'url'};
 3731:                 $key = $settings->{$item}->{'key'};
 3732:                 $secret = $settings->{$item}->{'secret'};
 3733:                 my $image = $settings->{$item}->{'image'};
 3734:                 if ($image ne '') {
 3735:                     $imgsrc = '<img src="'.$image.'" alt="'.&mt('Tool Provider icon').'" />';
 3736:                 }
 3737:             }
 3738:             my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_".$item."'".');"';
 3739:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 3740:                          .'<select name="ltitools_'.$item.'"'.$chgstr.'>';
 3741:             for (my $k=0; $k<=$maxnum; $k++) {
 3742:                 my $vpos = $k+1;
 3743:                 my $selstr;
 3744:                 if ($k == $i) {
 3745:                     $selstr = ' selected="selected" ';
 3746:                 }
 3747:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3748:             }
 3749:             $datatable .= '</select>'.('&nbsp;'x2).
 3750:                 '<label><input type="checkbox" name="ltitools_del" value="'.$item.'" />'.
 3751:                 &mt('Delete?').'</label></span></td>'.
 3752:                 '<td colspan="2">'.
 3753:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 3754:                 '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="30" name="ltitools_title_'.$i.'" value="'.$title.'" /></span> '.
 3755:                 ('&nbsp;'x2).
 3756:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_version_'.$i.'">'.
 3757:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 3758:                 ('&nbsp;'x2).
 3759:                 '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_msgtype_'.$i.'">'.
 3760:                 '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 3761:                 '<br /><br />'.
 3762:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="30" name="ltitools_url_'.$i.'"'.
 3763:                 ' value="'.$url.'" /></span>'.
 3764:                 ('&nbsp;'x2).
 3765:                 '<span class="LC_nobreak">'.$lt{'key'}.
 3766:                 '<input type="text" size="25" name="ltitools_key_'.$i.'" value="'.$key.'" /></span> '.
 3767:                 ('&nbsp;'x2).
 3768:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 3769:                 '<input type="password" size="20" name="ltitools_secret_'.$i.'" value="'.$secret.'" />'.
 3770:                 '<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>'.
 3771:                 '<input type="hidden" name="ltitools_id_'.$i.'" value="'.$item.'" /></span>'.
 3772:                 '</fieldset>'.
 3773:                 '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 3774:                 '<span class="LC_nobreak">'.&mt('Display target:');
 3775:             my %currdisp;
 3776:             if (ref($settings->{$item}->{'display'}) eq 'HASH') {
 3777:                 if ($settings->{$item}->{'display'}->{'target'} eq 'window') {
 3778:                     $currdisp{'window'} = ' checked="checked"';
 3779:                 } elsif ($settings->{$item}->{'display'}->{'target'} eq 'tab') {
 3780:                     $currdisp{'tab'} = ' checked="checked"';
 3781:                 } else {
 3782:                     $currdisp{'iframe'} = ' checked="checked"';
 3783:                 }
 3784:                 if ($settings->{$item}->{'display'}->{'width'} =~ /^(\d+)$/) {
 3785:                     $currdisp{'width'} = $1;
 3786:                 }
 3787:                 if ($settings->{$item}->{'display'}->{'height'} =~ /^(\d+)$/) {
 3788:                      $currdisp{'height'} = $1;
 3789:                 }
 3790:                 $currdisp{'linktext'} = $settings->{$item}->{'display'}->{'linktext'};
 3791:                 $currdisp{'explanation'} = $settings->{$item}->{'display'}->{'explanation'};
 3792:             } else {
 3793:                 $currdisp{'iframe'} = ' checked="checked"';
 3794:             }
 3795:             foreach my $disp ('iframe','tab','window') {
 3796:                 $datatable .= '<label><input type="radio" name="ltitools_target_'.$i.'" value="'.$disp.'"'.$currdisp{$disp}.' />'.
 3797:                               $lt{$disp}.'</label>'.('&nbsp;'x2);
 3798:             }
 3799:             $datatable .= ('&nbsp;'x4);
 3800:             foreach my $dimen ('width','height') {
 3801:                 $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 3802:                               '<input type="text" name="ltitools_'.$dimen.'_'.$i.'" size="5" value="'.$currdisp{$dimen}.'" /></label>'.
 3803:                               ('&nbsp;'x2);
 3804:             }
 3805:             $datatable .= '<br />'.
 3806:                           '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 3807:                           '<input type="text" name="ltitools_linktext_'.$i.'" size="25" value="'.$currdisp{'linktext'}.'" /></label></div>'.
 3808:                           '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 3809:                           '<textarea name="ltitools_explanation_'.$i.'" rows="5" cols="40">'.$currdisp{'explanation'}.
 3810:                           '</textarea></div><div style=""></div><br />';
 3811:             $datatable .= '<br />';
 3812:             foreach my $extra ('passback','roster') {
 3813:                 my $checkedon = '';
 3814:                 my $checkedoff = ' checked="checked"';
 3815:                 if ($settings->{$item}->{$extra}) {
 3816:                     $checkedon = $checkedoff;
 3817:                     $checkedoff = '';
 3818:                 }
 3819:                 $datatable .= $lt{$extra}.'&nbsp;'.
 3820:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="1"'.$checkedon.' />'.
 3821:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 3822:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="0"'.$checkedoff.' />'.
 3823:                               &mt('No').'</label>'.('&nbsp;'x4);
 3824:             }
 3825:             $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 3826:             if ($imgsrc) {
 3827:                 $datatable .= $imgsrc.
 3828:                               '<label><input type="checkbox" name="ltitools_image_del"'.
 3829:                               ' value="'.$item.'" />'.&mt('Delete?').'</label></span> '.
 3830:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 3831:             } else {
 3832:                 $datatable .= '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 3833:             }
 3834:             if ($switchserver) {
 3835:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3836:             } else {
 3837:                 $datatable .= '<input type="file" name="ltitools_image_'.$i.'" value="" />';
 3838:             }
 3839:             $datatable .= '</span></fieldset>';
 3840:             my (%checkedfields,%rolemaps);
 3841:             if (ref($settings->{$item}) eq 'HASH') {
 3842:                 if (ref($settings->{$item}->{'fields'}) eq 'HASH') {
 3843:                     %checkedfields = %{$settings->{$item}->{'fields'}};
 3844:                 }
 3845:                 if (ref($settings->{$item}->{'roles'}) eq 'HASH') {
 3846:                     %rolemaps = %{$settings->{$item}->{'roles'}};
 3847:                     $checkedfields{'roles'} = 1;
 3848:                 }
 3849:             }
 3850:             $datatable .= '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 3851:                           '<span class="LC_nobreak">';
 3852:             foreach my $field (@fields) {
 3853:                 my $checked;
 3854:                 if ($checkedfields{$field}) {
 3855:                     $checked = ' checked="checked"';
 3856:                 }
 3857:                 $datatable .= '<label>'.
 3858:                               '<input type="checkbox" name="ltitools_fields_'.$i.'" value="'.$field.'"'.$checked.' />'.
 3859:                               $lt{$field}.'</label>'.('&nbsp;' x2);
 3860:             }
 3861:             $datatable .= '</span></fieldset>'.
 3862:                           '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 3863:             foreach my $role (@courseroles) {
 3864:                 my ($selected,$selectnone);
 3865:                 if (!$rolemaps{$role}) {
 3866:                     $selectnone = ' selected="selected"';
 3867:                 }
 3868:                 $datatable .= '<td style="text-align: center">'. 
 3869:                               &Apache::lonnet::plaintext($role,'Course').'<br />'.
 3870:                               '<select name="ltitools_roles_'.$role.'_'.$i.'">'.
 3871:                               '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 3872:                 foreach my $ltirole (@ltiroles) {
 3873:                     unless ($selectnone) {
 3874:                         if ($rolemaps{$role} eq $ltirole) {
 3875:                             $selected = ' selected="selected"';
 3876:                         } else {
 3877:                             $selected = '';
 3878:                         }
 3879:                     }
 3880:                     $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 3881:                 }
 3882:                 $datatable .= '</select></td>';
 3883:             }
 3884:             $datatable .= '</tr></table></fieldset>';
 3885:             my %courseconfig;
 3886:             if (ref($settings->{$item}) eq 'HASH') {
 3887:                 if (ref($settings->{$item}->{'crsconf'}) eq 'HASH') {
 3888:                     %courseconfig = %{$settings->{$item}->{'crsconf'}};
 3889:                 }
 3890:             }
 3891:             $datatable .= '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 3892:             foreach my $item ('label','title','target','linktext','explanation') {
 3893:                 my $checked;
 3894:                 if ($courseconfig{$item}) {
 3895:                     $checked = ' checked="checked"';
 3896:                 }
 3897:                 $datatable .= '<label>'.
 3898:                        '<input type="checkbox" name="ltitools_courseconfig_'.$i.'" value="'.$item.'"'.$checked.' />'.
 3899:                        $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 3900:             }
 3901:             $datatable .= '</span></fieldset>'.
 3902:                           '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 3903:                           '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>';
 3904:             if (ref($settings->{$item}->{'custom'}) eq 'HASH') {
 3905:                 my %custom = %{$settings->{$item}->{'custom'}};
 3906:                 if (keys(%custom) > 0) {
 3907:                     foreach my $key (sort(keys(%custom))) {
 3908:                         $datatable .= '<tr><td><span class="LC_nobreak">'.
 3909:                                       '<label><input type="checkbox" name="ltitools_customdel_'.$i.'" value="'.
 3910:                                       $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 3911:                                       '<td><input type="text" name="ltitools_customval_'.$key.'_'.$i.'"'.
 3912:                                       ' value="'.$custom{$key}.'" /></td></tr>';
 3913:                     }
 3914:                 }
 3915:             }
 3916:             $datatable .= '<tr><td><span class="LC_nobreak">'.
 3917:                           '<label><input type="checkbox" name="ltitools_customadd" value="'.$i.'" />'.
 3918:                           &mt('Add').'</label></span></td><td><input type="text" name="ltitools_custom_name_'.$i.'" />'.
 3919:                           '</td><td><input type="text" name="ltitools_custom_value_'.$i.'" /></td></tr>';
 3920:             $datatable .= '</table></fieldset></td></tr>'."\n";
 3921:             $itemcount ++;
 3922:         }
 3923:     }
 3924:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3925:     my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_add_pos'".');"';
 3926:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 3927:                   '<input type="hidden" name="ltitools_maxnum" value="'.$maxnum.'" />'."\n".
 3928:                   '<select name="ltitools_add_pos"'.$chgstr.'>';
 3929:     for (my $k=0; $k<$maxnum+1; $k++) {
 3930:         my $vpos = $k+1;
 3931:         my $selstr;
 3932:         if ($k == $maxnum) {
 3933:             $selstr = ' selected="selected" ';
 3934:         }
 3935:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3936:     }
 3937:     $datatable .= '</select>&nbsp;'."\n".
 3938:                   '<input type="checkbox" name="ltitools_add" value="1" />'.&mt('Add').'</td>'."\n".
 3939:                   '<td colspan="2">'.
 3940:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 3941:                   '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="30" name="ltitools_add_title" value="" /></span> '."\n".
 3942:                   ('&nbsp;'x2).
 3943:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_add_version">'.
 3944:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 3945:                   ('&nbsp;'x2).
 3946:                   '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_add_msgtype">'.
 3947:                   '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 3948:                   '<br />'.
 3949:                   '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="30" name="ltitools_add_url" value="" /></span> '."\n".
 3950:                   ('&nbsp;'x2).
 3951:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="ltitools_add_key" value="" /></span> '."\n".
 3952:                   ('&nbsp;'x2).
 3953:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="ltitools_add_secret" value="" />'.
 3954:                   '<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".
 3955:                   '</fieldset>'.
 3956:                   '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 3957:                   '<span class="LC_nobreak">'.&mt('Display target:');
 3958:     my %defaultdisp;
 3959:     $defaultdisp{'iframe'} = ' checked="checked"';
 3960:     foreach my $disp ('iframe','tab','window') {
 3961:         $datatable .= '<label><input type="radio" name="ltitools_add_target" value="'.$disp.'"'.$defaultdisp{$disp}.' />'.
 3962:                       $lt{$disp}.'</label>'.('&nbsp;'x2);
 3963:     }
 3964:     $datatable .= ('&nbsp;'x4);
 3965:     foreach my $dimen ('width','height') {
 3966:         $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 3967:                       '<input type="text" name="ltitools_add_'.$dimen.'" size="5" /></label>'.
 3968:                       ('&nbsp;'x2);
 3969:     }
 3970:     $datatable .= '<br />'.
 3971:                   '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 3972:                   '<input type="text" name="ltitools_add_linktext" size="5" /></label></div>'.
 3973:                   '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 3974:                   '<textarea name=ltitools_add_explanation" rows="5" cols="40"></textarea>'.
 3975:                   '</div><div style=""></div><br />';
 3976:     foreach my $extra ('passback','roster') {
 3977:         $datatable .= $lt{$extra}.'&nbsp;'.
 3978:                       '<label><input type="radio" name="ltitools_add_'.$extra.'" value="1" />'.
 3979:                       &mt('Yes').'</label>'.('&nbsp;'x2).
 3980:                       '<label><input type="radio" name="ltitools_add_'.$extra.'" value="0" checked="checked" />'.
 3981:                       &mt('No').'</label>'.('&nbsp;'x4);
 3982:     }
 3983:     $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;'.
 3984:                   '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 3985:     if ($switchserver) {
 3986:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3987:     } else {
 3988:         $datatable .= '<input type="file" name="ltitools_add_image" value="" />';
 3989:     }
 3990:     $datatable .= '</span></fieldset>'.
 3991:                   '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 3992:                   '<span class="LC_nobreak">';
 3993:     foreach my $field (@fields) {
 3994:         $datatable .= '<label>'.
 3995:                       '<input type="checkbox" name="ltitools_add_fields" value="'.$field.'" />'.
 3996:                       $lt{$field}.'</label>'.('&nbsp;' x2);
 3997:     }
 3998:     $datatable .= '</span></fieldset>'.
 3999:                   '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 4000:     foreach my $role (@courseroles) {
 4001:         my ($checked,$checkednone);
 4002:         $datatable .= '<td style="text-align: center">'.
 4003:                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 4004:                       '<select name="ltitools_add_roles_'.$role.'">'.
 4005:                       '<option value="" selected="selected">'.&mt('Select').'</option>';
 4006:         foreach my $ltirole (@ltiroles) {
 4007:             $datatable .= '<option value="'.$ltirole.'">'.$ltirole.'</option>';
 4008:         }
 4009:         $datatable .= '</select></td>';
 4010:     }
 4011:     $datatable .= '</tr></table></fieldset>'.
 4012:                   '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 4013:     foreach my $item ('label','title','target','linktext','explanation') {
 4014:         $datatable .= '<label>'.
 4015:                       '<input type="checkbox" name="ltitools_courseconfig" value="'.$item.'" checked="checked" />'.
 4016:                       $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 4017:     }
 4018:     $datatable .= '</span></fieldset>'.
 4019:                   '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4020:                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 4021:                   '<tr><td><span class="LC_nobreak">'.
 4022:                   '<label><input type="checkbox" name="ltitools_add_custom" value="1" />'.
 4023:                   &mt('Add').'</label></span></td><td><input type="text" name="ltitools_add_custom_name" />'.
 4024:                   '</td><td><input type="text" name="ltitools_add_custom_value" /></td></tr>'.
 4025:                   '</table></fieldset></td></tr>'."\n".
 4026:                   '</td>'."\n".
 4027:                   '</tr>'."\n";
 4028:     $itemcount ++;
 4029:     return $datatable;
 4030: }
 4031: 
 4032: sub ltitools_names {
 4033:     my %lt = &Apache::lonlocal::texthash(
 4034:                                           'title'          => 'Title',
 4035:                                           'version'        => 'Version',
 4036:                                           'msgtype'        => 'Message Type',
 4037:                                           'url'            => 'URL',
 4038:                                           'key'            => 'Key',
 4039:                                           'secret'         => 'Secret',
 4040:                                           'icon'           => 'Icon',   
 4041:                                           'user'           => 'Username:domain',
 4042:                                           'fullname'       => 'Full Name',
 4043:                                           'firstname'      => 'First Name',
 4044:                                           'lastname'       => 'Last Name',
 4045:                                           'email'          => 'E-mail',
 4046:                                           'roles'          => 'Role',
 4047:                                           'window'         => 'Window',
 4048:                                           'tab'            => 'Tab',
 4049:                                           'iframe'         => 'iFrame',
 4050:                                           'height'         => 'Height',
 4051:                                           'width'          => 'Width',
 4052:                                           'linktext'       => 'Default Link Text',
 4053:                                           'explanation'    => 'Default Explanation',
 4054:                                           'passback'       => 'Tool can return grades:',
 4055:                                           'roster'         => 'Tool can retrieve roster:',
 4056:                                           'crstarget'      => 'Display target',
 4057:                                           'crslabel'       => 'Course label',
 4058:                                           'crstitle'       => 'Course title', 
 4059:                                           'crslinktext'    => 'Link Text',
 4060:                                           'crsexplanation' => 'Explanation',
 4061:                                         );
 4062:     return %lt;
 4063: }
 4064: 
 4065: sub print_coursedefaults {
 4066:     my ($position,$dom,$settings,$rowtotal) = @_;
 4067:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 4068:     my $itemcount = 1;
 4069:     my %choices =  &Apache::lonlocal::texthash (
 4070:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 4071:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 4072:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 4073:         coursecredits        => 'Credits can be specified for courses',
 4074:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 4075:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 4076:         texengine            => 'Default method to display mathematics',
 4077:         postsubmit           => 'Disable submit button/keypress following student submission',
 4078:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 4079:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 4080:     );
 4081:     my %staticdefaults = (
 4082:                            texengine            => 'MathJax',
 4083:                            anonsurvey_threshold => 10,
 4084:                            uploadquota          => 500,
 4085:                            postsubmit           => 60,
 4086:                            mysqltables          => 172800,
 4087:                          );
 4088:     if ($position eq 'top') {
 4089:         %defaultchecked = (
 4090:                             'canuse_pdfforms' => 'off',
 4091:                             'uselcmath'       => 'on',
 4092:                             'usejsme'         => 'on',
 4093:                             'canclone'        => 'none',
 4094:                           );
 4095:         @toggles = ('canuse_pdfforms','uselcmath','usejsme');
 4096:         my $deftex = $staticdefaults{'texengine'};
 4097:         if (ref($settings) eq 'HASH') {
 4098:             if ($settings->{'texengine'}) {
 4099:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 4100:                     $deftex = $settings->{'texengine'};
 4101:                 }
 4102:             }
 4103:         }
 4104:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4105:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 4106:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 4107:                        '</span></td><td class="LC_right_item">'.
 4108:                        '<select name="texengine">'."\n";
 4109:         my %texoptions = (
 4110:                             MathJax  => 'MathJax',
 4111:                             mimetex  => &mt('Convert to Images'),
 4112:                             tth      => &mt('TeX to HTML'),
 4113:                          );
 4114:         foreach my $renderer ('MathJax','mimetex','tth') {
 4115:             my $selected = '';
 4116:             if ($renderer eq $deftex) {
 4117:                 $selected = ' selected="selected"';
 4118:             }
 4119:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 4120:         }
 4121:         $mathdisp .= '</select></td></tr>'."\n";
 4122:         $itemcount ++;
 4123:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4124:                                                      \%choices,$itemcount);
 4125:         $datatable = $mathdisp.$datatable;
 4126:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4127:         $datatable .=
 4128:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 4129:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 4130:             '</span></td><td class="LC_left_item">';
 4131:         my $currcanclone = 'none';
 4132:         my $onclick;
 4133:         my @cloneoptions = ('none','domain');
 4134:         my %clonetitles = (
 4135:                              none     => 'No additional course requesters',
 4136:                              domain   => "Any course requester in course's domain",
 4137:                              instcode => 'Course requests for official courses ...',
 4138:                           );
 4139:         my (%codedefaults,@code_order,@posscodes);
 4140:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 4141:                                                     \@code_order) eq 'ok') {
 4142:             if (@code_order > 0) {
 4143:                 push(@cloneoptions,'instcode');
 4144:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 4145:             }
 4146:         }
 4147:         if (ref($settings) eq 'HASH') {
 4148:             if ($settings->{'canclone'}) {
 4149:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 4150:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 4151:                         if (@code_order > 0) {
 4152:                             $currcanclone = 'instcode';
 4153:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 4154:                         }
 4155:                     }
 4156:                 } elsif ($settings->{'canclone'} eq 'domain') {
 4157:                     $currcanclone = $settings->{'canclone'};
 4158:                 }
 4159:             }
 4160:         }
 4161:         foreach my $option (@cloneoptions) {
 4162:             my ($checked,$additional);
 4163:             if ($currcanclone eq $option) {
 4164:                 $checked = ' checked="checked"';
 4165:             }
 4166:             if ($option eq 'instcode') {
 4167:                 if (@code_order) {
 4168:                     my $show = 'none';
 4169:                     if ($checked) {
 4170:                         $show = 'block';
 4171:                     }
 4172:                     $additional = '<div id="cloneinstcode" style="display:'.$show.'" />'.
 4173:                                   &mt('Institutional codes for new and cloned course have identical:').
 4174:                                   '<br />';
 4175:                     foreach my $item (@code_order) {
 4176:                         my $codechk;
 4177:                         if ($checked) {
 4178:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 4179:                                 $codechk = ' checked="checked"';
 4180:                             }
 4181:                         }
 4182:                         $additional .= '<label>'.
 4183:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 4184:                                        $item.'</label>';
 4185:                     }
 4186:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 4187:                 }
 4188:             }
 4189:             $datatable .=
 4190:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 4191:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 4192:                 '</label>&nbsp;'.$additional.'</span><br />';
 4193:         }
 4194:         $datatable .= '</td>'.
 4195:                       '</tr>';
 4196:         $itemcount ++;
 4197:     } else {
 4198:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4199:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 4200:         my $currusecredits = 0;
 4201:         my $postsubmitclient = 1;
 4202:         my @types = ('official','unofficial','community','textbook','placement');
 4203:         if (ref($settings) eq 'HASH') {
 4204:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 4205:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 4206:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 4207:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 4208:                 }
 4209:             }
 4210:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 4211:                 foreach my $type (@types) {
 4212:                     next if ($type eq 'community');
 4213:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 4214:                     if ($defcredits{$type} ne '') {
 4215:                         $currusecredits = 1;
 4216:                     }
 4217:                 }
 4218:             }
 4219:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 4220:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 4221:                     $postsubmitclient = 0;
 4222:                     foreach my $type (@types) {
 4223:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4224:                     }
 4225:                 } else {
 4226:                     foreach my $type (@types) {
 4227:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 4228:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 4229:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 4230:                             } else {
 4231:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4232:                             }
 4233:                         } else {
 4234:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4235:                         }
 4236:                     }
 4237:                 }
 4238:             } else {
 4239:                 foreach my $type (@types) {
 4240:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4241:                 }
 4242:             }
 4243:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 4244:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 4245:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 4246:                 }
 4247:             } else {
 4248:                 foreach my $type (@types) {
 4249:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 4250:                 }
 4251:             }
 4252:         } else {
 4253:             foreach my $type (@types) {
 4254:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4255:             }
 4256:         }
 4257:         if (!$currdefresponder) {
 4258:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 4259:         } elsif ($currdefresponder < 1) {
 4260:             $currdefresponder = 1;
 4261:         }
 4262:         foreach my $type (@types) {
 4263:             if ($curruploadquota{$type} eq '') {
 4264:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 4265:             }
 4266:         }
 4267:         $datatable .=
 4268:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4269:                 $choices{'anonsurvey_threshold'}.
 4270:                 '</span></td>'.
 4271:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 4272:                 '<input type="text" name="anonsurvey_threshold"'.
 4273:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 4274:                 '</td></tr>'."\n";
 4275:         $itemcount ++;
 4276:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4277:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4278:                       $choices{'uploadquota'}.
 4279:                       '</span></td>'.
 4280:                       '<td style="text-align: right" class="LC_right_item">'.
 4281:                       '<table><tr>';
 4282:         foreach my $type (@types) {
 4283:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 4284:                            '<input type="text" name="uploadquota_'.$type.'"'.
 4285:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 4286:         }
 4287:         $datatable .= '</tr></table></td></tr>'."\n";
 4288:         $itemcount ++;
 4289:         my $onclick = "toggleDisplay(this.form,'credits');";
 4290:         my $display = 'none';
 4291:         if ($currusecredits) {
 4292:             $display = 'block';
 4293:         }
 4294:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 4295:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 4296:         foreach my $type (@types) {
 4297:             next if ($type eq 'community');
 4298:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 4299:                            '<input type="text" name="'.$type.'_credits"'.
 4300:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 4301:         }
 4302:         $additional .= '</tr></table></div>'."\n";
 4303:         %defaultchecked = ('coursecredits' => 'off');
 4304:         @toggles = ('coursecredits');
 4305:         my $current = {
 4306:                         'coursecredits' => $currusecredits,
 4307:                       };
 4308:         (my $table,$itemcount) =
 4309:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4310:                                \%choices,$itemcount,$onclick,$additional,'left');
 4311:         $datatable .= $table;
 4312:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 4313:         my $display = 'none';
 4314:         if ($postsubmitclient) {
 4315:             $display = 'block';
 4316:         }
 4317:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 4318:                       &mt('Number of seconds submit is disabled').'<br />'.
 4319:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 4320:                       '<table><tr>';
 4321:         foreach my $type (@types) {
 4322:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 4323:                            '<input type="text" name="'.$type.'_timeout" value="'.
 4324:                            $deftimeout{$type}.'" size="5" /></td>';
 4325:         }
 4326:         $additional .= '</tr></table></div>'."\n";
 4327:         %defaultchecked = ('postsubmit' => 'on');
 4328:         @toggles = ('postsubmit');
 4329:         $current = {
 4330:                        'postsubmit' => $postsubmitclient,
 4331:                    };
 4332:         ($table,$itemcount) =
 4333:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4334:                                \%choices,$itemcount,$onclick,$additional,'left');
 4335:         $datatable .= $table;
 4336:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4337:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4338:                       $choices{'mysqltables'}.
 4339:                       '</span></td>'.
 4340:                       '<td style="text-align: right" class="LC_right_item">'.
 4341:                       '<table><tr>';
 4342:         foreach my $type (@types) {
 4343:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 4344:                            '<input type="text" name="mysqltables_'.$type.'"'.
 4345:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 4346:         }
 4347:         $datatable .= '</tr></table></td></tr>'."\n";
 4348:         $itemcount ++;
 4349: 
 4350:     }
 4351:     $$rowtotal += $itemcount;
 4352:     return $datatable;
 4353: }
 4354: 
 4355: sub print_selfenrollment {
 4356:     my ($position,$dom,$settings,$rowtotal) = @_;
 4357:     my ($css_class,$datatable);
 4358:     my $itemcount = 1;
 4359:     my @types = ('official','unofficial','community','textbook','placement');
 4360:     if (($position eq 'top') || ($position eq 'middle')) {
 4361:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 4362:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 4363:         my @rows;
 4364:         my $key;
 4365:         if ($position eq 'top') {
 4366:             $key = 'admin'; 
 4367:             if (ref($rowsref) eq 'ARRAY') {
 4368:                 @rows = @{$rowsref};
 4369:             }
 4370:         } elsif ($position eq 'middle') {
 4371:             $key = 'default';
 4372:             @rows = ('types','registered','approval','limit');
 4373:         }
 4374:         foreach my $row (@rows) {
 4375:             if (defined($titlesref->{$row})) {
 4376:                 $itemcount ++;
 4377:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4378:                 $datatable .= '<tr'.$css_class.'>'.
 4379:                               '<td>'.$titlesref->{$row}.'</td>'.
 4380:                               '<td class="LC_left_item">'.
 4381:                               '<table><tr>';
 4382:                 my (%current,%currentcap);
 4383:                 if (ref($settings) eq 'HASH') {
 4384:                     if (ref($settings->{$key}) eq 'HASH') {
 4385:                         foreach my $type (@types) {
 4386:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4387:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 4388:                             }
 4389:                             if (($row eq 'limit') && ($key eq 'default')) {
 4390:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4391:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 4392:                                 }
 4393:                             }
 4394:                         }
 4395:                     }
 4396:                 }
 4397:                 my %roles = (
 4398:                              '0' => &Apache::lonnet::plaintext('dc'),
 4399:                             ); 
 4400:             
 4401:                 foreach my $type (@types) {
 4402:                     unless (($row eq 'registered') && ($key eq 'default')) {
 4403:                         $datatable .= '<th>'.&mt($type).'</th>';
 4404:                     }
 4405:                 }
 4406:                 unless (($row eq 'registered') && ($key eq 'default')) {
 4407:                     $datatable .= '</tr><tr>';
 4408:                 }
 4409:                 foreach my $type (@types) {
 4410:                     if ($type eq 'community') {
 4411:                         $roles{'1'} = &mt('Community personnel');
 4412:                     } else {
 4413:                         $roles{'1'} = &mt('Course personnel');
 4414:                     }
 4415:                     $datatable .= '<td style="vertical-align: top">';
 4416:                     if ($position eq 'top') {
 4417:                         my %checked;
 4418:                         if ($current{$type} eq '0') {
 4419:                             $checked{'0'} = ' checked="checked"';
 4420:                         } else {
 4421:                             $checked{'1'} = ' checked="checked"';
 4422:                         }
 4423:                         foreach my $role ('1','0') {
 4424:                             $datatable .= '<span class="LC_nobreak"><label>'.
 4425:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 4426:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 4427:                                           $roles{$role}.'</label></span> ';
 4428:                         }
 4429:                     } else {
 4430:                         if ($row eq 'types') {
 4431:                             my %checked;
 4432:                             if ($current{$type} =~ /^(all|dom)$/) {
 4433:                                 $checked{$1} = ' checked="checked"';
 4434:                             } else {
 4435:                                 $checked{''} = ' checked="checked"';
 4436:                             }
 4437:                             foreach my $val ('','dom','all') {
 4438:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4439:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4440:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4441:                             }
 4442:                         } elsif ($row eq 'registered') {
 4443:                             my %checked;
 4444:                             if ($current{$type} eq '1') {
 4445:                                 $checked{'1'} = ' checked="checked"';
 4446:                             } else {
 4447:                                 $checked{'0'} = ' checked="checked"';
 4448:                             }
 4449:                             foreach my $val ('0','1') {
 4450:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4451:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4452:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4453:                             }
 4454:                         } elsif ($row eq 'approval') {
 4455:                             my %checked;
 4456:                             if ($current{$type} =~ /^([12])$/) {
 4457:                                 $checked{$1} = ' checked="checked"';
 4458:                             } else {
 4459:                                 $checked{'0'} = ' checked="checked"';
 4460:                             }
 4461:                             for my $val (0..2) {
 4462:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4463:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4464:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4465:                             }
 4466:                         } elsif ($row eq 'limit') {
 4467:                             my %checked;
 4468:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 4469:                                 $checked{$1} = ' checked="checked"';
 4470:                             } else {
 4471:                                 $checked{'none'} = ' checked="checked"';
 4472:                             }
 4473:                             my $cap;
 4474:                             if ($currentcap{$type} =~ /^\d+$/) {
 4475:                                 $cap = $currentcap{$type};
 4476:                             }
 4477:                             foreach my $val ('none','allstudents','selfenrolled') {
 4478:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4479:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4480:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4481:                             }
 4482:                             $datatable .= '<br />'.
 4483:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 4484:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 4485:                                           '</span>'; 
 4486:                         }
 4487:                     }
 4488:                     $datatable .= '</td>';
 4489:                 }
 4490:                 $datatable .= '</tr>';
 4491:             }
 4492:             $datatable .= '</table></td></tr>';
 4493:         }
 4494:     } elsif ($position eq 'bottom') {
 4495:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 4496:     }
 4497:     $$rowtotal += $itemcount;
 4498:     return $datatable;
 4499: }
 4500: 
 4501: sub print_validation_rows {
 4502:     my ($caller,$dom,$settings,$rowtotal) = @_;
 4503:     my ($itemsref,$namesref,$fieldsref);
 4504:     if ($caller eq 'selfenroll') { 
 4505:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 4506:     } elsif ($caller eq 'requestcourses') {
 4507:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 4508:     }
 4509:     my %currvalidation;
 4510:     if (ref($settings) eq 'HASH') {
 4511:         if (ref($settings->{'validation'}) eq 'HASH') {
 4512:             %currvalidation = %{$settings->{'validation'}};
 4513:         }
 4514:     }
 4515:     my $datatable;
 4516:     my $itemcount = 0;
 4517:     foreach my $item (@{$itemsref}) {
 4518:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4519:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4520:                       $namesref->{$item}.
 4521:                       '</span></td>'.
 4522:                       '<td class="LC_left_item">';
 4523:         if (($item eq 'url') || ($item eq 'button')) {
 4524:             $datatable .= '<span class="LC_nobreak">'.
 4525:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 4526:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 4527:         } elsif ($item eq 'fields') {
 4528:             my @currfields;
 4529:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 4530:                 @currfields = @{$currvalidation{$item}};
 4531:             }
 4532:             foreach my $field (@{$fieldsref}) {
 4533:                 my $check = '';
 4534:                 if (grep(/^\Q$field\E$/,@currfields)) {
 4535:                     $check = ' checked="checked"';
 4536:                 }
 4537:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4538:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 4539:                               ' value="'.$field.'"'.$check.' />'.$field.
 4540:                               '</label></span> ';
 4541:             }
 4542:         } elsif ($item eq 'markup') {
 4543:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 4544:                            $currvalidation{$item}.
 4545:                               '</textarea>';
 4546:         }
 4547:         $datatable .= '</td></tr>'."\n";
 4548:         if (ref($rowtotal)) {
 4549:             $itemcount ++;
 4550:         }
 4551:     }
 4552:     if ($caller eq 'requestcourses') {
 4553:         my %currhash;
 4554:         if (ref($settings) eq 'HASH') {
 4555:             if (ref($settings->{'validation'}) eq 'HASH') {
 4556:                 if ($settings->{'validation'}{'dc'} ne '') {
 4557:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 4558:                 }
 4559:             }
 4560:         }
 4561:         my $numinrow = 2;
 4562:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 4563:                                                        'validationdc',%currhash);
 4564:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4565:         $datatable .= '</td></tr><tr'.$css_class.'><td>';
 4566:         if ($numdc > 1) {
 4567:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 4568:         } else {
 4569:             $datatable .=  &mt('Course creation processed as: ');
 4570:         }
 4571:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 4572:         $itemcount ++;
 4573:     }
 4574:     if (ref($rowtotal)) {
 4575:         $$rowtotal += $itemcount;
 4576:     }
 4577:     return $datatable;
 4578: }
 4579: 
 4580: sub print_usersessions {
 4581:     my ($position,$dom,$settings,$rowtotal) = @_;
 4582:     my ($css_class,$datatable,$itemcount,%checked,%choices);
 4583:     my (%by_ip,%by_location,@intdoms,@instdoms);
 4584:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 4585: 
 4586:     my @alldoms = &Apache::lonnet::all_domains();
 4587:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 4588:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4589:     my %altids = &id_for_thisdom(%servers);
 4590:     if ($position eq 'top') {
 4591:         if (keys(%serverhomes) > 1) {
 4592:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 4593:             my $curroffloadnow;
 4594:             if (ref($settings) eq 'HASH') {
 4595:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 4596:                     $curroffloadnow = $settings->{'offloadnow'};
 4597:                 }
 4598:             }
 4599:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$curroffloadnow,$rowtotal);
 4600:         } else {
 4601:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 4602:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.').
 4603:                           '</td></tr>';
 4604:         }
 4605:     } else {
 4606:         my %titles = &usersession_titles();
 4607:         my ($prefix,@types);
 4608:         if ($position eq 'bottom') {
 4609:             $prefix = 'remote';
 4610:             @types = ('version','excludedomain','includedomain');
 4611:         } else {
 4612:             $prefix = 'hosted';
 4613:             @types = ('excludedomain','includedomain');
 4614:         }
 4615:         ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 4616:     }
 4617:     $$rowtotal += $itemcount;
 4618:     return $datatable;
 4619: }
 4620: 
 4621: sub rules_by_location {
 4622:     my ($settings,$prefix,$by_location,$by_ip,$types,$titles) = @_; 
 4623:     my ($datatable,$itemcount,$css_class);
 4624:     if (keys(%{$by_location}) == 0) {
 4625:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4626:         $datatable = '<tr'.$css_class.'><td colspan="2">'.
 4627:                      &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.').
 4628:                      '</td></tr>';
 4629:         $itemcount = 1;
 4630:     } else {
 4631:         $itemcount = 0;
 4632:         my $numinrow = 5;
 4633:         my (%current,%checkedon,%checkedoff);
 4634:         my @locations = sort(keys(%{$by_location}));
 4635:         foreach my $type (@{$types}) {
 4636:             $checkedon{$type} = '';
 4637:             $checkedoff{$type} = ' checked="checked"';
 4638:         }
 4639:         if (ref($settings) eq 'HASH') {
 4640:             if (ref($settings->{$prefix}) eq 'HASH') {
 4641:                 foreach my $key (keys(%{$settings->{$prefix}})) {
 4642:                     $current{$key} = $settings->{$prefix}{$key};
 4643:                     if ($key eq 'version') {
 4644:                         if ($current{$key} ne '') {
 4645:                             $checkedon{$key} = ' checked="checked"';
 4646:                             $checkedoff{$key} = '';
 4647:                         }
 4648:                     } elsif (ref($current{$key}) eq 'ARRAY') {
 4649:                         $checkedon{$key} = ' checked="checked"';
 4650:                         $checkedoff{$key} = '';
 4651:                     }
 4652:                 }
 4653:             }
 4654:         }
 4655:         foreach my $type (@{$types}) {
 4656:             next if ($type ne 'version' && !@locations);
 4657:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4658:             $datatable .= '<tr'.$css_class.'>
 4659:                            <td><span class="LC_nobreak">'.$titles->{$type}.'</span><br />
 4660:                            <span class="LC_nobreak">&nbsp;
 4661:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 4662:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 4663:             if ($type eq 'version') {
 4664:                 my @lcversions = &Apache::lonnet::all_loncaparevs();
 4665:                 my $selector = '<select name="'.$prefix.'_version">';
 4666:                 foreach my $version (@lcversions) {
 4667:                     my $selected = '';
 4668:                     if ($current{'version'} eq $version) {
 4669:                         $selected = ' selected="selected"';
 4670:                     }
 4671:                     $selector .= ' <option value="'.$version.'"'.
 4672:                                  $selected.'>'.$version.'</option>';
 4673:                 }
 4674:                 $selector .= '</select> ';
 4675:                 $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 4676:             } else {
 4677:                 $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 4678:                              'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 4679:                              ' />'.('&nbsp;'x2).
 4680:                              '<input type="button" value="'.&mt('uncheck all').'" '.
 4681:                              'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 4682:                              "\n".
 4683:                              '</div><div><table>';
 4684:                 my $rem;
 4685:                 for (my $i=0; $i<@locations; $i++) {
 4686:                     my ($showloc,$value,$checkedtype);
 4687:                     if (ref($by_location->{$locations[$i]}) eq 'ARRAY') {
 4688:                         my $ip = $by_location->{$locations[$i]}->[0];
 4689:                         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4690:                             $value = join(':',@{$by_ip->{$ip}});
 4691:                             $showloc = join(', ',@{$by_ip->{$ip}});
 4692:                             if (ref($current{$type}) eq 'ARRAY') {
 4693:                                 foreach my $loc (@{$by_ip->{$ip}}) {
 4694:                                     if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 4695:                                         $checkedtype = ' checked="checked"';
 4696:                                         last;
 4697:                                     }
 4698:                                 }
 4699:                             }
 4700:                         }
 4701:                     }
 4702:                     $rem = $i%($numinrow);
 4703:                     if ($rem == 0) {
 4704:                         if ($i > 0) {
 4705:                             $datatable .= '</tr>';
 4706:                         }
 4707:                         $datatable .= '<tr>';
 4708:                     }
 4709:                     $datatable .= '<td class="LC_left_item">'.
 4710:                                   '<span class="LC_nobreak"><label>'.
 4711:                                   '<input type="checkbox" name="'.$prefix.'_'.$type.
 4712:                                   '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 4713:                                   '</label></span></td>';
 4714:                 }
 4715:                 $rem = @locations%($numinrow);
 4716:                 my $colsleft = $numinrow - $rem;
 4717:                 if ($colsleft > 1 ) {
 4718:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4719:                                   '&nbsp;</td>';
 4720:                 } elsif ($colsleft == 1) {
 4721:                     $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4722:                 }
 4723:                 $datatable .= '</tr></table>';
 4724:             }
 4725:             $datatable .= '</td></tr>';
 4726:             $itemcount ++;
 4727:         }
 4728:     }
 4729:     return ($datatable,$itemcount);
 4730: }
 4731: 
 4732: sub print_ssl {
 4733:     my ($position,$dom,$settings,$rowtotal) = @_;
 4734:     my ($css_class,$datatable);
 4735:     my $itemcount = 1;
 4736:     if ($position eq 'top') {
 4737:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 4738:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 4739:         my $same_institution;
 4740:         if ($intdom ne '') {
 4741:             my $internet_names = &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
 4742:             if (ref($internet_names) eq 'ARRAY') {
 4743:                 if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
 4744:                     $same_institution = 1;
 4745:                 }
 4746:             }
 4747:         }
 4748:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4749:         $datatable = '<tr'.$css_class.'><td colspan="2">';
 4750:         if ($same_institution) {
 4751:             my %domservers = &Apache::lonnet::get_servers($dom);
 4752:             $datatable .= &LONCAPA::SSL::print_certstatus(\%domservers,'web','domprefs');
 4753:         } else {
 4754:             $datatable .= &mt("You need to be logged into one of your own domain's servers to display information about the status of LON-CAPA SSL certificates.");
 4755:         }
 4756:         $datatable .= '</td></tr>';
 4757:         $itemcount ++;
 4758:     } else {
 4759:         my %titles = &ssl_titles();
 4760:         my (%by_ip,%by_location,@intdoms,@instdoms);
 4761:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 4762:         my @alldoms = &Apache::lonnet::all_domains();
 4763:         my %serverhomes = %Apache::lonnet::serverhomeIDs;
 4764:         my @domservers = &Apache::lonnet::get_servers($dom);
 4765:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4766:         my %altids = &id_for_thisdom(%servers);
 4767:         if (($position eq 'connto') || ($position eq 'connfrom')) {
 4768:             my $legacy;
 4769:             unless (ref($settings) eq 'HASH') {
 4770:                 my $name;
 4771:                 if ($position eq 'connto') {
 4772:                     $name = 'loncAllowInsecure';
 4773:                 } else {
 4774:                     $name = 'londAllowInsecure';
 4775:                 }
 4776:                 my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
 4777:                 my @ids=&Apache::lonnet::current_machine_ids();
 4778:                 if (($primarylibserv ne '') && (!grep(/^\Q$primarylibserv\E$/,@ids))) {
 4779:                     my %what = (
 4780:                                    $name => 1,
 4781:                                );
 4782:                     my ($result,$returnhash) =
 4783:                         &Apache::lonnet::get_remote_globals($primarylibserv,\%what);
 4784:                     if ($result eq 'ok') {
 4785:                         if (ref($returnhash) eq 'HASH') {
 4786:                             $legacy = $returnhash->{$name};
 4787:                         }
 4788:                     }
 4789:                 } else {
 4790:                     $legacy = $Apache::lonnet::perlvar{$name};
 4791:                 }
 4792:             }
 4793:             foreach my $type ('dom','intdom','other') {
 4794:                 my %checked;
 4795:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4796:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$type}.'</td>'.
 4797:                               '<td class="LC_right_item">';
 4798:                 my $skip; 
 4799:                 if ($type eq 'dom') {
 4800:                     unless (keys(%servers) > 1) {
 4801:                         $datatable .= &mt('Nothing to set here, as there are no other servers/VMs');    
 4802:                         $skip = 1;
 4803:                     }
 4804:                 }
 4805:                 if ($type eq 'intdom') {
 4806:                     unless (@instdoms > 1) {
 4807:                         $datatable .= &mt('Nothing to set here, as there are no other domains for this institution');
 4808:                         $skip = 1;
 4809:                     } 
 4810:                 } elsif ($type eq 'other') {
 4811:                     if (keys(%by_location) == 0) {
 4812:                         $datatable .= &mt('Nothing to set here, as there are no other institutions');
 4813:                         $skip = 1;
 4814:                     }
 4815:                 }
 4816:                 unless ($skip) {
 4817:                     $checked{'yes'} = ' checked="checked"'; 
 4818:                     if (ref($settings) eq 'HASH') {
 4819:                         if (ref($settings->{$position}) eq 'HASH') {
 4820:                             if ($settings->{$position}->{$type} =~ /^(no|req)$/) {
 4821:                                 $checked{$1} = $checked{'yes'};
 4822:                                 delete($checked{'yes'}); 
 4823:                             }
 4824:                         }
 4825:                     } else {
 4826:                         if ($legacy == 0) {
 4827:                             $checked{'req'} = $checked{'yes'};
 4828:                             delete($checked{'yes'});    
 4829:                         }
 4830:                     }
 4831:                     foreach my $option ('no','yes','req') {
 4832:                         $datatable .= '<span class="LC_nobreak"><label>'.
 4833:                                       '<input type="radio" name="'.$position.'_'.$type.'" '.
 4834:                                       'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 4835:                                       '</label></span>'.('&nbsp;'x2);
 4836:                     }
 4837:                 }
 4838:                 $datatable .= '</td></tr>';
 4839:                 $itemcount ++; 
 4840:             }
 4841:         } else {
 4842:             my $prefix = 'replication';
 4843:             my @types = ('certreq','nocertreq');
 4844:             if (keys(%by_location) == 0) {
 4845:                 $datatable .= '<tr'.$css_class.'><td>'.
 4846:                               &mt('Nothing to set here, as there are no other institutions').
 4847:                               '</td></tr>';
 4848:                 $itemcount ++;
 4849:             } else {
 4850:                 ($datatable,$itemcount) = 
 4851:                     &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 4852:             }
 4853:         }
 4854:     }
 4855:     $$rowtotal += $itemcount;
 4856:     return $datatable;
 4857: }
 4858: 
 4859: sub ssl_titles {
 4860:     return &Apache::lonlocal::texthash (
 4861:                dom           => 'LON-CAPA servers/VMs from same domain',
 4862:                intdom        => 'LON-CAPA servers/VMs from same "internet" domain',
 4863:                other         => 'External LON-CAPA servers/VMs',
 4864:                connto        => 'Connections to other servers',
 4865:                connfrom      => 'Connections from other servers',
 4866:                replication   => 'Replicating content to other institutions',
 4867:                certreq       => 'Client certificate required, but specific domains exempt',
 4868:                nocertreq     => 'No client certificate required, except for specific domains',
 4869:                no            => 'SSL not used',
 4870:                yes           => 'SSL Optional (used if available)',
 4871:                req           => 'SSL Required',
 4872:     );
 4873: }
 4874: 
 4875: sub print_trust {
 4876:     my ($prefix,$dom,$settings,$rowtotal) = @_;
 4877:     my ($css_class,$datatable,%checked,%choices);
 4878:     my (%by_ip,%by_location,@intdoms,@instdoms);
 4879:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 4880:     my $itemcount = 1;
 4881:     my %titles = &trust_titles();
 4882:     my @types = ('exc','inc');
 4883:     if ($prefix eq 'top') {
 4884:         $prefix = 'content';
 4885:     } elsif ($prefix eq 'bottom') {
 4886:         $prefix = 'msg';
 4887:     }
 4888:     ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 4889:     $$rowtotal += $itemcount;
 4890:     return $datatable;
 4891: }
 4892: 
 4893: sub trust_titles {
 4894:     return &Apache::lonlocal::texthash(
 4895:                content  => "Access to this domain's content by others",
 4896:                shared   => "Access to other domain's content by this domain",
 4897:                enroll   => "Enrollment in this domain's courses by others", 
 4898:                othcoau  => "Co-author roles in this domain for others",
 4899:                coaurem  => "Co-author roles for this domain's users elsewhere", 
 4900:                domroles => "Domain roles in this domain assignable to others",
 4901:                catalog  => "Course Catalog for this domain displayed elsewhere",
 4902:                reqcrs   => "Requests for creation of courses in this domain by others",
 4903:                msg      => "Users in other domains can send messages to this domain",
 4904:                exc      => "Allow all, but exclude specific domains",
 4905:                inc      => "Deny all, but include specific domains",
 4906:            );
 4907: } 
 4908: 
 4909: sub build_location_hashes {
 4910:     my ($intdoms,$by_ip,$by_location,$instdoms) = @_;
 4911:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 4912:                   (ref($by_location) eq 'HASH') && (ref($instdoms) eq 'ARRAY'));
 4913:     my %iphost = &Apache::lonnet::get_iphost();
 4914:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 4915:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 4916:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 4917:         foreach my $id (@{$iphost{$primary_ip}}) {
 4918:             my $intdom = &Apache::lonnet::internet_dom($id);
 4919:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 4920:                 push(@{$intdoms},$intdom);
 4921:             }
 4922:         }
 4923:     }
 4924:     foreach my $ip (keys(%iphost)) {
 4925:         if (ref($iphost{$ip}) eq 'ARRAY') {
 4926:             foreach my $id (@{$iphost{$ip}}) {
 4927:                 my $location = &Apache::lonnet::internet_dom($id);
 4928:                 if ($location) {
 4929:                     if (grep(/^\Q$location\E$/,@{$intdoms})) {
 4930:                         my $dom = &Apache::lonnet::host_domain($id);
 4931:                         unless (grep(/^\Q$dom\E/,@{$instdoms})) {
 4932:                             push(@{$instdoms},$dom);
 4933:                         }
 4934:                         next;
 4935:                     }
 4936:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4937:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 4938:                             push(@{$by_ip->{$ip}},$location);
 4939:                         }
 4940:                     } else {
 4941:                         $by_ip->{$ip} = [$location];
 4942:                     }
 4943:                 }
 4944:             }
 4945:         }
 4946:     }
 4947:     foreach my $ip (sort(keys(%{$by_ip}))) {
 4948:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4949:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 4950:             my $first = $by_ip->{$ip}->[0];
 4951:             if (ref($by_location->{$first}) eq 'ARRAY') {
 4952:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 4953:                     push(@{$by_location->{$first}},$ip);
 4954:                 }
 4955:             } else {
 4956:                 $by_location->{$first} = [$ip];
 4957:             }
 4958:         }
 4959:     }
 4960:     return;
 4961: }
 4962: 
 4963: sub current_offloads_to {
 4964:     my ($dom,$settings,$servers) = @_;
 4965:     my (%spareid,%otherdomconfigs);
 4966:     if (ref($servers) eq 'HASH') {
 4967:         foreach my $lonhost (sort(keys(%{$servers}))) {
 4968:             my $gotspares;
 4969:             if (ref($settings) eq 'HASH') {
 4970:                 if (ref($settings->{'spares'}) eq 'HASH') {
 4971:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 4972:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 4973:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 4974:                         $gotspares = 1;
 4975:                     }
 4976:                 }
 4977:             }
 4978:             unless ($gotspares) {
 4979:                 my $gotspares;
 4980:                 my $serverhomeID =
 4981:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 4982:                 my $serverhomedom =
 4983:                     &Apache::lonnet::host_domain($serverhomeID);
 4984:                 if ($serverhomedom ne $dom) {
 4985:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 4986:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4987:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4988:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4989:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4990:                                 $gotspares = 1;
 4991:                             }
 4992:                         }
 4993:                     } else {
 4994:                         $otherdomconfigs{$serverhomedom} =
 4995:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 4996:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 4997:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4998:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4999:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 5000:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 5001:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 5002:                                         $gotspares = 1;
 5003:                                     }
 5004:                                 }
 5005:                             }
 5006:                         }
 5007:                     }
 5008:                 }
 5009:             }
 5010:             unless ($gotspares) {
 5011:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 5012:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 5013:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 5014:                } else {
 5015:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 5016:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 5017:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 5018:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 5019:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 5020:                     } else {
 5021:                         my %what = (
 5022:                              spareid => 1,
 5023:                         );
 5024:                         my ($result,$returnhash) = 
 5025:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 5026:                         if ($result eq 'ok') { 
 5027:                             if (ref($returnhash) eq 'HASH') {
 5028:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 5029:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 5030:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 5031:                                 }
 5032:                             }
 5033:                         }
 5034:                     }
 5035:                 }
 5036:             }
 5037:         }
 5038:     }
 5039:     return %spareid;
 5040: }
 5041: 
 5042: sub spares_row {
 5043:     my ($dom,$servers,$spareid,$serverhomes,$altids,$curroffloadnow,$rowtotal) = @_;
 5044:     my $css_class;
 5045:     my $numinrow = 4;
 5046:     my $itemcount = 1;
 5047:     my $datatable;
 5048:     my %typetitles = &sparestype_titles();
 5049:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 5050:         foreach my $server (sort(keys(%{$servers}))) {
 5051:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 5052:             my ($othercontrol,$serverdom);
 5053:             if ($serverhome ne $server) {
 5054:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 5055:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 5056:             } else {
 5057:                 $serverdom = &Apache::lonnet::host_domain($server);
 5058:                 if ($serverdom ne $dom) {
 5059:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 5060:                 }
 5061:             }
 5062:             next unless (ref($spareid->{$server}) eq 'HASH');
 5063:             my $checkednow;
 5064:             if (ref($curroffloadnow) eq 'HASH') {
 5065:                 if ($curroffloadnow->{$server}) {
 5066:                     $checkednow = ' checked="checked"';
 5067:                 }
 5068:             }
 5069:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5070:             $datatable .= '<tr'.$css_class.'>
 5071:                            <td rowspan="2">
 5072:                             <span class="LC_nobreak">'.
 5073:                           &mt('[_1] when busy, offloads to:'
 5074:                               ,'<b>'.$server.'</b>').'</span><br />'.
 5075:                           '<span class="LC_nobreak">'."\n".
 5076:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 5077:                           '&nbsp;'.&mt('Switch active users on next access').'</label></span>'.
 5078:                           "\n";
 5079:             my (%current,%canselect);
 5080:             my @choices = 
 5081:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 5082:             foreach my $type ('primary','default') {
 5083:                 if (ref($spareid->{$server}) eq 'HASH') {
 5084:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 5085:                         my @spares = @{$spareid->{$server}{$type}};
 5086:                         if (@spares > 0) {
 5087:                             if ($othercontrol) {
 5088:                                 $current{$type} = join(', ',@spares);
 5089:                             } else {
 5090:                                 $current{$type} .= '<table>';
 5091:                                 my $numspares = scalar(@spares);
 5092:                                 for (my $i=0;  $i<@spares; $i++) {
 5093:                                     my $rem = $i%($numinrow);
 5094:                                     if ($rem == 0) {
 5095:                                         if ($i > 0) {
 5096:                                             $current{$type} .= '</tr>';
 5097:                                         }
 5098:                                         $current{$type} .= '<tr>';
 5099:                                     }
 5100:                                     $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;'.
 5101:                                                        $spareid->{$server}{$type}[$i].
 5102:                                                        '</label></td>'."\n";
 5103:                                 }
 5104:                                 my $rem = @spares%($numinrow);
 5105:                                 my $colsleft = $numinrow - $rem;
 5106:                                 if ($colsleft > 1 ) {
 5107:                                     $current{$type} .= '<td colspan="'.$colsleft.
 5108:                                                        '" class="LC_left_item">'.
 5109:                                                        '&nbsp;</td>';
 5110:                                 } elsif ($colsleft == 1) {
 5111:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 5112:                                 }
 5113:                                 $current{$type} .= '</tr></table>';
 5114:                             }
 5115:                         }
 5116:                     }
 5117:                     if ($current{$type} eq '') {
 5118:                         $current{$type} = &mt('None specified');
 5119:                     }
 5120:                     if ($othercontrol) {
 5121:                         if ($type eq 'primary') {
 5122:                             $canselect{$type} = $othercontrol;
 5123:                         }
 5124:                     } else {
 5125:                         $canselect{$type} = 
 5126:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 5127:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 5128:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 5129:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 5130:                         if (@choices > 0) {
 5131:                             foreach my $lonhost (@choices) {
 5132:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 5133:                             }
 5134:                         }
 5135:                         $canselect{$type} .= '</select>'."\n";
 5136:                     }
 5137:                 } else {
 5138:                     $current{$type} = &mt('Could not be determined');
 5139:                     if ($type eq 'primary') {
 5140:                         $canselect{$type} =  $othercontrol;
 5141:                     }
 5142:                 }
 5143:                 if ($type eq 'default') {
 5144:                     $datatable .= '<tr'.$css_class.'>';
 5145:                 }
 5146:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 5147:                               '<td>'.$current{$type}.'</td>'."\n".
 5148:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 5149:             }
 5150:             $itemcount ++;
 5151:         }
 5152:     }
 5153:     $$rowtotal += $itemcount;
 5154:     return $datatable;
 5155: }
 5156: 
 5157: sub possible_newspares {
 5158:     my ($server,$currspares,$serverhomes,$altids) = @_;
 5159:     my $serverhostname = &Apache::lonnet::hostname($server);
 5160:     my %excluded;
 5161:     if ($serverhostname ne '') {
 5162:         %excluded = (
 5163:                        $serverhostname => 1,
 5164:                     );
 5165:     }
 5166:     if (ref($currspares) eq 'HASH') {
 5167:         foreach my $type (keys(%{$currspares})) {
 5168:             if (ref($currspares->{$type}) eq 'ARRAY') {
 5169:                 if (@{$currspares->{$type}} > 0) {
 5170:                     foreach my $curr (@{$currspares->{$type}}) {
 5171:                         my $hostname = &Apache::lonnet::hostname($curr);
 5172:                         $excluded{$hostname} = 1;
 5173:                     }
 5174:                 }
 5175:             }
 5176:         }
 5177:     }
 5178:     my @choices;
 5179:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 5180:         if (keys(%{$serverhomes}) > 1) {
 5181:             foreach my $name (sort(keys(%{$serverhomes}))) {
 5182:                 unless ($excluded{$name}) {
 5183:                     if (exists($altids->{$serverhomes->{$name}})) {
 5184:                         push(@choices,$altids->{$serverhomes->{$name}});
 5185:                     } else {
 5186:                         push(@choices,$serverhomes->{$name});
 5187:                     }
 5188:                 }
 5189:             }
 5190:         }
 5191:     }
 5192:     return sort(@choices);
 5193: }
 5194: 
 5195: sub print_loadbalancing {
 5196:     my ($dom,$settings,$rowtotal) = @_;
 5197:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 5198:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 5199:     my $numinrow = 1;
 5200:     my $datatable;
 5201:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5202:     my (%currbalancer,%currtargets,%currrules,%existing);
 5203:     if (ref($settings) eq 'HASH') {
 5204:         %existing = %{$settings};
 5205:     }
 5206:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 5207:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 5208:                                   \%currtargets,\%currrules);
 5209:     } else {
 5210:         return;
 5211:     }
 5212:     my ($othertitle,$usertypes,$types) =
 5213:         &Apache::loncommon::sorted_inst_types($dom);
 5214:     my $rownum = 8;
 5215:     if (ref($types) eq 'ARRAY') {
 5216:         $rownum += scalar(@{$types});
 5217:     }
 5218:     my @css_class = ('LC_odd_row','LC_even_row');
 5219:     my $balnum = 0;
 5220:     my $islast;
 5221:     my (@toshow,$disabledtext);
 5222:     if (keys(%currbalancer) > 0) {
 5223:         @toshow = sort(keys(%currbalancer));
 5224:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 5225:             push(@toshow,'');
 5226:         }
 5227:     } else {
 5228:         @toshow = ('');
 5229:         $disabledtext = &mt('No existing load balancer');
 5230:     }
 5231:     foreach my $lonhost (@toshow) {
 5232:         if ($balnum == scalar(@toshow)-1) {
 5233:             $islast = 1;
 5234:         } else {
 5235:             $islast = 0;
 5236:         }
 5237:         my $cssidx = $balnum%2;
 5238:         my $targets_div_style = 'display: none';
 5239:         my $disabled_div_style = 'display: block';
 5240:         my $homedom_div_style = 'display: none';
 5241:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 5242:                       '<td rowspan="'.$rownum.'" style="vertical-align: top">'.
 5243:                       '<p>';
 5244:         if ($lonhost eq '') {
 5245:             $datatable .= '<span class="LC_nobreak">';
 5246:             if (keys(%currbalancer) > 0) {
 5247:                 $datatable .= &mt('Add balancer:');
 5248:             } else {
 5249:                 $datatable .= &mt('Enable balancer:');
 5250:             }
 5251:             $datatable .= '&nbsp;'.
 5252:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 5253:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 5254:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 5255:                           '<option value="" selected="selected">'.&mt('None').
 5256:                           '</option>'."\n";
 5257:             foreach my $server (sort(keys(%servers))) {
 5258:                 next if ($currbalancer{$server});
 5259:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 5260:             }
 5261:             $datatable .=
 5262:                 '</select>'."\n".
 5263:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 5264:         } else {
 5265:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 5266:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 5267:                            &mt('Stop balancing').'</label>'.
 5268:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 5269:             $targets_div_style = 'display: block';
 5270:             $disabled_div_style = 'display: none';
 5271:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 5272:                 $homedom_div_style = 'display: block';
 5273:             }
 5274:         }
 5275:         $datatable .= '</p></td><td rowspan="'.$rownum.'" style="vertical-align: top">'.
 5276:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 5277:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 5278:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 5279:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 5280:         my @sparestypes = ('primary','default');
 5281:         my %typetitles = &sparestype_titles();
 5282:         my %hostherechecked = (
 5283:                                   no => ' checked="checked"',
 5284:                               );
 5285:         foreach my $sparetype (@sparestypes) {
 5286:             my $targettable;
 5287:             for (my $i=0; $i<$numspares; $i++) {
 5288:                 my $checked;
 5289:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 5290:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5291:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5292:                             $checked = ' checked="checked"';
 5293:                         }
 5294:                     }
 5295:                 }
 5296:                 my ($chkboxval,$disabled);
 5297:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 5298:                     $chkboxval = $spares[$i];
 5299:                 }
 5300:                 if (exists($currbalancer{$spares[$i]})) {
 5301:                     $disabled = ' disabled="disabled"';
 5302:                 }
 5303:                 $targettable .=
 5304:                     '<td><span class="LC_nobreak"><label>'.
 5305:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 5306:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 5307:                     '</span></label></span></td>';
 5308:                 my $rem = $i%($numinrow);
 5309:                 if ($rem == 0) {
 5310:                     if (($i > 0) && ($i < $numspares-1)) {
 5311:                         $targettable .= '</tr>';
 5312:                     }
 5313:                     if ($i < $numspares-1) {
 5314:                         $targettable .= '<tr>';
 5315:                     }
 5316:                 }
 5317:             }
 5318:             if ($targettable ne '') {
 5319:                 my $rem = $numspares%($numinrow);
 5320:                 my $colsleft = $numinrow - $rem;
 5321:                 if ($colsleft > 1 ) {
 5322:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5323:                                     '&nbsp;</td>';
 5324:                 } elsif ($colsleft == 1) {
 5325:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 5326:                 }
 5327:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 5328:                                '<table><tr>'.$targettable.'</tr></table><br />';
 5329:             }
 5330:             $hostherechecked{$sparetype} = '';
 5331:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 5332:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5333:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5334:                         $hostherechecked{$sparetype} = ' checked="checked"';
 5335:                         $hostherechecked{'no'} = '';
 5336:                     }
 5337:                 }
 5338:             }
 5339:         }
 5340:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 5341:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 5342:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 5343:         foreach my $sparetype (@sparestypes) {
 5344:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 5345:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 5346:                           '</i></label><br />';
 5347:         }
 5348:         $datatable .= '</div></td></tr>'.
 5349:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 5350:                                            $othertitle,$usertypes,$types,\%servers,
 5351:                                            \%currbalancer,$lonhost,
 5352:                                            $targets_div_style,$homedom_div_style,
 5353:                                            $css_class[$cssidx],$balnum,$islast);
 5354:         $$rowtotal += $rownum;
 5355:         $balnum ++;
 5356:     }
 5357:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 5358:     return $datatable;
 5359: }
 5360: 
 5361: sub get_loadbalancers_config {
 5362:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 5363:     return unless ((ref($servers) eq 'HASH') &&
 5364:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 5365:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 5366:     if (keys(%{$existing}) > 0) {
 5367:         my $oldlonhost;
 5368:         foreach my $key (sort(keys(%{$existing}))) {
 5369:             if ($key eq 'lonhost') {
 5370:                 $oldlonhost = $existing->{'lonhost'};
 5371:                 $currbalancer->{$oldlonhost} = 1;
 5372:             } elsif ($key eq 'targets') {
 5373:                 if ($oldlonhost) {
 5374:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 5375:                 }
 5376:             } elsif ($key eq 'rules') {
 5377:                 if ($oldlonhost) {
 5378:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 5379:                 }
 5380:             } elsif (ref($existing->{$key}) eq 'HASH') {
 5381:                 $currbalancer->{$key} = 1;
 5382:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 5383:                 $currrules->{$key} = $existing->{$key}{'rules'};
 5384:             }
 5385:         }
 5386:     } else {
 5387:         my ($balancerref,$targetsref) =
 5388:                 &Apache::lonnet::get_lonbalancer_config($servers);
 5389:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 5390:             foreach my $server (sort(keys(%{$balancerref}))) {
 5391:                 $currbalancer->{$server} = 1;
 5392:                 $currtargets->{$server} = $targetsref->{$server};
 5393:             }
 5394:         }
 5395:     }
 5396:     return;
 5397: }
 5398: 
 5399: sub loadbalancing_rules {
 5400:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 5401:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 5402:         $css_class,$balnum,$islast) = @_;
 5403:     my $output;
 5404:     my $num = 0;
 5405:     my ($alltypes,$othertypes,$titles) =
 5406:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 5407:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 5408:         foreach my $type (@{$alltypes}) {
 5409:             $num ++;
 5410:             my $current;
 5411:             if (ref($currrules) eq 'HASH') {
 5412:                 $current = $currrules->{$type};
 5413:             }
 5414:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5415:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 5416:                     $current = '';
 5417:                 }
 5418:             }
 5419:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 5420:                                              $servers,$currbalancer,$lonhost,$dom,
 5421:                                              $targets_div_style,$homedom_div_style,
 5422:                                              $css_class,$balnum,$num,$islast);
 5423:         }
 5424:     }
 5425:     return $output;
 5426: }
 5427: 
 5428: sub loadbalancing_titles {
 5429:     my ($dom,$intdom,$usertypes,$types) = @_;
 5430:     my %othertypes = (
 5431:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 5432:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 5433:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 5434:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 5435:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 5436:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 5437:                      );
 5438:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 5439:     my @available;
 5440:     if (ref($types) eq 'ARRAY') {
 5441:         @available = @{$types};
 5442:     }
 5443:     unless (grep(/^default$/,@available)) {
 5444:         push(@available,'default');
 5445:     }
 5446:     unshift(@alltypes,@available);
 5447:     my %titles;
 5448:     foreach my $type (@alltypes) {
 5449:         if ($type =~ /^_LC_/) {
 5450:             $titles{$type} = $othertypes{$type};
 5451:         } elsif ($type eq 'default') {
 5452:             $titles{$type} = &mt('All users from [_1]',$dom);
 5453:             if (ref($types) eq 'ARRAY') {
 5454:                 if (@{$types} > 0) {
 5455:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 5456:                 }
 5457:             }
 5458:         } elsif (ref($usertypes) eq 'HASH') {
 5459:             $titles{$type} = $usertypes->{$type};
 5460:         }
 5461:     }
 5462:     return (\@alltypes,\%othertypes,\%titles);
 5463: }
 5464: 
 5465: sub loadbalance_rule_row {
 5466:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 5467:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 5468:     my @rulenames;
 5469:     my %ruletitles = &offloadtype_text();
 5470:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 5471:         @rulenames = ('balancer','offloadedto','specific');
 5472:     } else {
 5473:         @rulenames = ('default','homeserver');
 5474:         if ($type eq '_LC_external') {
 5475:             push(@rulenames,'externalbalancer');
 5476:         } else {
 5477:             push(@rulenames,'specific');
 5478:         }
 5479:         push(@rulenames,'none');
 5480:     }
 5481:     my $style = $targets_div_style;
 5482:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5483:         $style = $homedom_div_style;
 5484:     }
 5485:     my $space;
 5486:     if ($islast && $num == 1) {
 5487:         $space = '<div display="inline-block">&nbsp;</div>';
 5488:     }
 5489:     my $output =
 5490:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td style="vertical-align: top">'.$space.
 5491:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 5492:         '<td valaign="top">'.$space.
 5493:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 5494:     for (my $i=0; $i<@rulenames; $i++) {
 5495:         my $rule = $rulenames[$i];
 5496:         my ($checked,$extra);
 5497:         if ($rulenames[$i] eq 'default') {
 5498:             $rule = '';
 5499:         }
 5500:         if ($rulenames[$i] eq 'specific') {
 5501:             if (ref($servers) eq 'HASH') {
 5502:                 my $default;
 5503:                 if (($current ne '') && (exists($servers->{$current}))) {
 5504:                     $checked = ' checked="checked"';
 5505:                 }
 5506:                 unless ($checked) {
 5507:                     $default = ' selected="selected"';
 5508:                 }
 5509:                 $extra =
 5510:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5511:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5512:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 5513:                     '<option value=""'.$default.'></option>'."\n";
 5514:                 foreach my $server (sort(keys(%{$servers}))) {
 5515:                     if (ref($currbalancer) eq 'HASH') {
 5516:                         next if (exists($currbalancer->{$server}));
 5517:                     }
 5518:                     my $selected;
 5519:                     if ($server eq $current) {
 5520:                         $selected = ' selected="selected"';
 5521:                     }
 5522:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 5523:                 }
 5524:                 $extra .= '</select>';
 5525:             }
 5526:         } elsif ($rule eq $current) {
 5527:             $checked = ' checked="checked"';
 5528:         }
 5529:         $output .= '<span class="LC_nobreak"><label>'.
 5530:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 5531:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 5532:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 5533:                    ')"'.$checked.' />&nbsp;';
 5534:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 5535:             $output .= $ruletitles{'particular'};
 5536:         } else {
 5537:             $output .= $ruletitles{$rulenames[$i]};
 5538:         }
 5539:         $output .= '</label>'.$extra.'</span><br />'."\n";
 5540:     }
 5541:     $output .= '</div></td></tr>'."\n";
 5542:     return $output;
 5543: }
 5544: 
 5545: sub offloadtype_text {
 5546:     my %ruletitles = &Apache::lonlocal::texthash (
 5547:            'default'          => 'Offloads to default destinations',
 5548:            'homeserver'       => "Offloads to user's home server",
 5549:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 5550:            'specific'         => 'Offloads to specific server',
 5551:            'none'             => 'No offload',
 5552:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 5553:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 5554:            'particular'       => 'Session hosted (after re-auth) on server:',
 5555:     );
 5556:     return %ruletitles;
 5557: }
 5558: 
 5559: sub sparestype_titles {
 5560:     my %typestitles = &Apache::lonlocal::texthash (
 5561:                           'primary' => 'primary',
 5562:                           'default' => 'default',
 5563:                       );
 5564:     return %typestitles;
 5565: }
 5566: 
 5567: sub contact_titles {
 5568:     my %titles = &Apache::lonlocal::texthash (
 5569:                    'supportemail'    => 'Support E-mail address',
 5570:                    'adminemail'      => 'Default Server Admin E-mail address',
 5571:                    'errormail'       => 'Error reports to be e-mailed to',
 5572:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 5573:                    'helpdeskmail'    => "Helpdesk requests for this domain's users",
 5574:                    'otherdomsmail'   => 'Helpdesk requests for other (unconfigured) domains',
 5575:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 5576:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 5577:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 5578:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 5579:                  );
 5580:     my %short_titles = &Apache::lonlocal::texthash (
 5581:                            adminemail   => 'Admin E-mail address',
 5582:                            supportemail => 'Support E-mail',
 5583:                        );   
 5584:     return (\%titles,\%short_titles);
 5585: }
 5586: 
 5587: sub helpform_fields {
 5588:     my %titles =  &Apache::lonlocal::texthash (
 5589:                        'username'   => 'Name',
 5590:                        'user'       => 'Username/domain',
 5591:                        'phone'      => 'Phone',
 5592:                        'cc'         => 'Cc e-mail',
 5593:                        'course'     => 'Course Details',
 5594:                        'section'    => 'Sections',
 5595:                        'screenshot' => 'File upload',
 5596:     );
 5597:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 5598:     my %possoptions = (
 5599:                         username     => ['yes','no','req'],
 5600:                         phone        => ['yes','no','req'],
 5601:                         user         => ['yes','no'],
 5602:                         cc           => ['yes','no'],
 5603:                         course       => ['yes','no'],
 5604:                         section      => ['yes','no'],
 5605:                         screenshot   => ['yes','no'],
 5606:                       );
 5607:     my %fieldoptions = &Apache::lonlocal::texthash (
 5608:                          'yes'  => 'Optional',
 5609:                          'req'  => 'Required',
 5610:                          'no'   => "Not shown",
 5611:     );
 5612:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 5613: }
 5614: 
 5615: sub tool_titles {
 5616:     my %titles = &Apache::lonlocal::texthash (
 5617:                      aboutme    => 'Personal web page',
 5618:                      blog       => 'Blog',
 5619:                      webdav     => 'WebDAV',
 5620:                      portfolio  => 'Portfolio',
 5621:                      official   => 'Official courses (with institutional codes)',
 5622:                      unofficial => 'Unofficial courses',
 5623:                      community  => 'Communities',
 5624:                      textbook   => 'Textbook courses',
 5625:                      placement  => 'Placement tests',
 5626:                  );
 5627:     return %titles;
 5628: }
 5629: 
 5630: sub courserequest_titles {
 5631:     my %titles = &Apache::lonlocal::texthash (
 5632:                                    official   => 'Official',
 5633:                                    unofficial => 'Unofficial',
 5634:                                    community  => 'Communities',
 5635:                                    textbook   => 'Textbook',
 5636:                                    placement  => 'Placement tests',
 5637:                                    norequest  => 'Not allowed',
 5638:                                    approval   => 'Approval by Dom. Coord.',
 5639:                                    validate   => 'With validation',
 5640:                                    autolimit  => 'Numerical limit',
 5641:                                    unlimited  => '(blank for unlimited)',
 5642:                  );
 5643:     return %titles;
 5644: }
 5645: 
 5646: sub authorrequest_titles {
 5647:     my %titles = &Apache::lonlocal::texthash (
 5648:                                    norequest  => 'Not allowed',
 5649:                                    approval   => 'Approval by Dom. Coord.',
 5650:                                    automatic  => 'Automatic approval',
 5651:                  );
 5652:     return %titles;
 5653: }
 5654: 
 5655: sub courserequest_conditions {
 5656:     my %conditions = &Apache::lonlocal::texthash (
 5657:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 5658:        validate   => '(Processing of request subject to institutional validation).',
 5659:                  );
 5660:     return %conditions;
 5661: }
 5662: 
 5663: 
 5664: sub print_usercreation {
 5665:     my ($position,$dom,$settings,$rowtotal) = @_;
 5666:     my $numinrow = 4;
 5667:     my $datatable;
 5668:     if ($position eq 'top') {
 5669:         $$rowtotal ++;
 5670:         my $rowcount = 0;
 5671:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 5672:         if (ref($rules) eq 'HASH') {
 5673:             if (keys(%{$rules}) > 0) {
 5674:                 $datatable .= &user_formats_row('username',$settings,$rules,
 5675:                                                 $ruleorder,$numinrow,$rowcount);
 5676:                 $$rowtotal ++;
 5677:                 $rowcount ++;
 5678:             }
 5679:         }
 5680:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 5681:         if (ref($idrules) eq 'HASH') {
 5682:             if (keys(%{$idrules}) > 0) {
 5683:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 5684:                                                 $idruleorder,$numinrow,$rowcount);
 5685:                 $$rowtotal ++;
 5686:                 $rowcount ++;
 5687:             }
 5688:         }
 5689:         if ($rowcount == 0) {
 5690:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 5691:             $$rowtotal ++;
 5692:             $rowcount ++;
 5693:         }
 5694:     } elsif ($position eq 'middle') {
 5695:         my @creators = ('author','course','requestcrs');
 5696:         my ($rules,$ruleorder) =
 5697:             &Apache::lonnet::inst_userrules($dom,'username');
 5698:         my %lt = &usercreation_types();
 5699:         my %checked;
 5700:         if (ref($settings) eq 'HASH') {
 5701:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 5702:                 foreach my $item (@creators) {
 5703:                     $checked{$item} = $settings->{'cancreate'}{$item};
 5704:                 }
 5705:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 5706:                 foreach my $item (@creators) {
 5707:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 5708:                         $checked{$item} = 'none';
 5709:                     }
 5710:                 }
 5711:             }
 5712:         }
 5713:         my $rownum = 0;
 5714:         foreach my $item (@creators) {
 5715:             $rownum ++;
 5716:             if ($checked{$item} eq '') {
 5717:                 $checked{$item} = 'any';
 5718:             }
 5719:             my $css_class;
 5720:             if ($rownum%2) {
 5721:                 $css_class = '';
 5722:             } else {
 5723:                 $css_class = ' class="LC_odd_row" ';
 5724:             }
 5725:             $datatable .= '<tr'.$css_class.'>'.
 5726:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 5727:                          '</span></td><td style="text-align: right">';
 5728:             my @options = ('any');
 5729:             if (ref($rules) eq 'HASH') {
 5730:                 if (keys(%{$rules}) > 0) {
 5731:                     push(@options,('official','unofficial'));
 5732:                 }
 5733:             }
 5734:             push(@options,'none');
 5735:             foreach my $option (@options) {
 5736:                 my $type = 'radio';
 5737:                 my $check = ' ';
 5738:                 if ($checked{$item} eq $option) {
 5739:                     $check = ' checked="checked" ';
 5740:                 } 
 5741:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5742:                               '<input type="'.$type.'" name="can_createuser_'.
 5743:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 5744:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 5745:             }
 5746:             $datatable .= '</td></tr>';
 5747:         }
 5748:     } else {
 5749:         my @contexts = ('author','course','domain');
 5750:         my @authtypes = ('int','krb4','krb5','loc');
 5751:         my %checked;
 5752:         if (ref($settings) eq 'HASH') {
 5753:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 5754:                 foreach my $item (@contexts) {
 5755:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 5756:                         foreach my $auth (@authtypes) {
 5757:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 5758:                                 $checked{$item}{$auth} = ' checked="checked" ';
 5759:                             }
 5760:                         }
 5761:                     }
 5762:                 }
 5763:             }
 5764:         } else {
 5765:             foreach my $item (@contexts) {
 5766:                 foreach my $auth (@authtypes) {
 5767:                     $checked{$item}{$auth} = ' checked="checked" ';
 5768:                 }
 5769:             }
 5770:         }
 5771:         my %title = &context_names();
 5772:         my %authname = &authtype_names();
 5773:         my $rownum = 0;
 5774:         my $css_class; 
 5775:         foreach my $item (@contexts) {
 5776:             if ($rownum%2) {
 5777:                 $css_class = '';
 5778:             } else {
 5779:                 $css_class = ' class="LC_odd_row" ';
 5780:             }
 5781:             $datatable .=   '<tr'.$css_class.'>'.
 5782:                             '<td>'.$title{$item}.
 5783:                             '</td><td class="LC_left_item">'.
 5784:                             '<span class="LC_nobreak">';
 5785:             foreach my $auth (@authtypes) {
 5786:                 $datatable .= '<label>'. 
 5787:                               '<input type="checkbox" name="'.$item.'_auth" '.
 5788:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 5789:                               $authname{$auth}.'</label>&nbsp;';
 5790:             }
 5791:             $datatable .= '</span></td></tr>';
 5792:             $rownum ++;
 5793:         }
 5794:         $$rowtotal += $rownum;
 5795:     }
 5796:     return $datatable;
 5797: }
 5798: 
 5799: sub print_selfcreation {
 5800:     my ($position,$dom,$settings,$rowtotal) = @_;
 5801:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 5802:         $emaildomain,$datatable);
 5803:     if (ref($settings) eq 'HASH') {
 5804:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 5805:             $createsettings = $settings->{'cancreate'};
 5806:             if (ref($createsettings) eq 'HASH') {
 5807:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 5808:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 5809:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 5810:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 5811:                         @selfcreate = ('email','login','sso');
 5812:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 5813:                         @selfcreate = ($createsettings->{'selfcreate'});
 5814:                     }
 5815:                 }
 5816:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 5817:                     $processing = $createsettings->{'selfcreateprocessing'};
 5818:                 }
 5819:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
 5820:                     $emailoptions = $createsettings->{'emailoptions'};
 5821:                 }
 5822:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
 5823:                     $emailverified = $createsettings->{'emailverified'};
 5824:                 }
 5825:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
 5826:                     $emaildomain = $createsettings->{'emaildomain'};
 5827:                 }
 5828:             }
 5829:         }
 5830:     }
 5831:     my %radiohash;
 5832:     my $numinrow = 4;
 5833:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 5834:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5835:     if ($position eq 'top') {
 5836:         my %choices = &Apache::lonlocal::texthash (
 5837:                                                       cancreate_login      => 'Institutional Login',
 5838:                                                       cancreate_sso        => 'Institutional Single Sign On',
 5839:                                                   );
 5840:         my @toggles = sort(keys(%choices));
 5841:         my %defaultchecked = (
 5842:                                'cancreate_login' => 'off',
 5843:                                'cancreate_sso'   => 'off',
 5844:                              );
 5845:         my ($onclick,$itemcount);
 5846:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5847:                                                      \%choices,$itemcount,$onclick);
 5848:         $$rowtotal += $itemcount;
 5849:         
 5850:         if (ref($usertypes) eq 'HASH') {
 5851:             if (keys(%{$usertypes}) > 0) {
 5852:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 5853:                                              $dom,$numinrow,$othertitle,
 5854:                                              'statustocreate',$rowtotal);
 5855:                 $$rowtotal ++;
 5856:             }
 5857:         }
 5858:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 5859:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5860:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 5861:         my $rem;
 5862:         my $numperrow = 2;
 5863:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 5864:         $datatable .= '<tr'.$css_class.'>'.
 5865:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 5866:                      '<td class="LC_left_item">'."\n".
 5867:                      '<table><tr><td>'."\n";
 5868:         for (my $i=0; $i<@fields; $i++) {
 5869:             $rem = $i%($numperrow);
 5870:             if ($rem == 0) {
 5871:                 if ($i > 0) {
 5872:                     $datatable .= '</tr>';
 5873:                 }
 5874:                 $datatable .= '<tr>';
 5875:             }
 5876:             my $currval;
 5877:             if (ref($createsettings) eq 'HASH') {
 5878:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 5879:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 5880:                 }
 5881:             }
 5882:             $datatable .= '<td class="LC_left_item">'.
 5883:                           '<span class="LC_nobreak">'.
 5884:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 5885:                           'value="'.$currval.'" size="10" />&nbsp;'.
 5886:                           $fieldtitles{$fields[$i]}.'</span></td>';
 5887:         }
 5888:         my $colsleft = $numperrow - $rem;
 5889:         if ($colsleft > 1 ) {
 5890:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5891:                          '&nbsp;</td>';
 5892:         } elsif ($colsleft == 1) {
 5893:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 5894:         }
 5895:         $datatable .= '</tr></table></td></tr>';
 5896:         $$rowtotal ++;
 5897:     } elsif ($position eq 'middle') {
 5898:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 5899:         my @posstypes;
 5900:         if (ref($types) eq 'ARRAY') {
 5901:             @posstypes = @{$types};
 5902:         }
 5903:         unless (grep(/^default$/,@posstypes)) {
 5904:             push(@posstypes,'default');
 5905:         }
 5906:         my %usertypeshash;
 5907:         if (ref($usertypes) eq 'HASH') {
 5908:             %usertypeshash = %{$usertypes};
 5909:         }
 5910:         $usertypeshash{'default'} = $othertitle;
 5911:         foreach my $status (@posstypes) {
 5912:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 5913:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 5914:             $$rowtotal ++;
 5915:         }
 5916:     } else {
 5917:         my %choices = &Apache::lonlocal::texthash (
 5918:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
 5919:                                                   );
 5920:         my @toggles = sort(keys(%choices));
 5921:         my %defaultchecked = (
 5922:                                'cancreate_email' => 'off',
 5923:                              );
 5924:         my $customclass = 'LC_selfcreate_email';
 5925:         my $classprefix = 'LC_canmodify_emailusername_';
 5926:         my $optionsprefix = 'LC_options_emailusername_';
 5927:         my $display = 'none';
 5928:         my $rowstyle = 'display:none';
 5929:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 5930:             $display = 'block';
 5931:             $rowstyle = 'display:table-row';
 5932:         }
 5933:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
 5934:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5935:                                                      \%choices,$$rowtotal,$onclick);
 5936:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
 5937:                                          $rowstyle);
 5938:         $$rowtotal ++;
 5939:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
 5940:                                       $rowstyle);
 5941:         $$rowtotal ++;
 5942:         my (@ordered,@posstypes,%usertypeshash);
 5943:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5944:         my ($emailrules,$emailruleorder) =
 5945:             &Apache::lonnet::inst_userrules($dom,'email');
 5946:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 5947:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 5948:         if (ref($types) eq 'ARRAY') {
 5949:             @posstypes = @{$types};
 5950:         }
 5951:         if (@posstypes) {
 5952:             unless (grep(/^default$/,@posstypes)) {
 5953:                 push(@posstypes,'default');
 5954:             }
 5955:             if (ref($usertypes) eq 'HASH') {
 5956:                 %usertypeshash = %{$usertypes};
 5957:             }
 5958:             my $currassign;
 5959:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 5960:                 $currassign = {
 5961:                                   selfassign => $domdefaults{'inststatusguest'},
 5962:                               };
 5963:                 @ordered = @{$domdefaults{'inststatusguest'}};
 5964:             } else {
 5965:                 $currassign = { selfassign => [] };
 5966:             }
 5967:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
 5968:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
 5969:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
 5970:                                          $numinrow,$othertitle,'selfassign',
 5971:                                          $rowtotal,$onclicktypes,$customclass,
 5972:                                          $rowstyle);
 5973:             $$rowtotal ++;
 5974:             $usertypeshash{'default'} = $othertitle;
 5975:             foreach my $status (@posstypes) {
 5976:                 my $css_class;
 5977:                 if ($$rowtotal%2) {
 5978:                     $css_class = 'LC_odd_row ';
 5979:                 }
 5980:                 $css_class .= $customclass;
 5981:                 my $rowid = $optionsprefix.$status;
 5982:                 my $hidden = 1;
 5983:                 my $currstyle = 'display:none';
 5984:                 if (grep(/^\Q$status\E$/,@ordered)) {
 5985:                     $currstyle = $rowstyle;
 5986:                     $hidden = 0; 
 5987:                 }
 5988:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 5989:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
 5990:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
 5991:                 unless ($hidden) {
 5992:                     $$rowtotal ++;
 5993:                 }
 5994:             }
 5995:         } else {
 5996:             my $css_class;
 5997:             if ($$rowtotal%2) {
 5998:                 $css_class = 'LC_odd_row ';
 5999:             }
 6000:             $css_class .= $customclass;
 6001:             $usertypeshash{'default'} = $othertitle;
 6002:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 6003:                                          $emailrules,$emailruleorder,$settings,'default','',
 6004:                                          $othertitle,$css_class,$rowstyle,$intdom);
 6005:             $$rowtotal ++;
 6006:         }
 6007:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 6008:         $numinrow = 1;
 6009:         if (@posstypes) {
 6010:             foreach my $status (@posstypes) {
 6011:                 my $rowid = $classprefix.$status;
 6012:                 my $datarowstyle = 'display:none';
 6013:                 if (grep(/^\Q$status\E$/,@ordered)) { 
 6014:                     $datarowstyle = $rowstyle; 
 6015:                 }
 6016:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 6017:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 6018:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
 6019:                 unless ($datarowstyle eq 'display:none') {
 6020:                     $$rowtotal ++;
 6021:                 }
 6022:             }
 6023:         } else {
 6024:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
 6025:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 6026:                                                    $infotitles,'',$customclass,$rowstyle);
 6027:         }
 6028:     }
 6029:     return $datatable;
 6030: }
 6031: 
 6032: sub selfcreate_javascript {
 6033:     return <<"ENDSCRIPT";
 6034: 
 6035: <script type="text/javascript">
 6036: // <![CDATA[
 6037: 
 6038: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
 6039:     var x = document.getElementsByClassName(target);
 6040:     var insttypes = 0;
 6041:     var insttypeRegExp = new RegExp(prefix);
 6042:     if ((x.length != undefined) && (x.length > 0)) {
 6043:         if (form.elements[radio].length != undefined) {
 6044:             for (var i=0; i<form.elements[radio].length; i++) {
 6045:                 if (form.elements[radio][i].checked) {
 6046:                     if (form.elements[radio][i].value == 1) {
 6047:                         for (var j=0; j<x.length; j++) {
 6048:                             if (x[j].id == 'undefined') {
 6049:                                 x[j].style.display = 'table-row';
 6050:                             } else if (insttypeRegExp.test(x[j].id)) {
 6051:                                 insttypes ++;
 6052:                             } else {
 6053:                                 x[j].style.display = 'table-row';
 6054:                             }
 6055:                         }
 6056:                     } else {
 6057:                         for (var j=0; j<x.length; j++) {
 6058:                             x[j].style.display = 'none';
 6059:                         }
 6060:                     }
 6061:                     break;
 6062:                 }
 6063:             }
 6064:             if (insttypes > 0) {
 6065:                 toggleDataRow(form,checkbox,target,altprefix);
 6066:                 toggleDataRow(form,checkbox,target,prefix,1);
 6067:             }
 6068:         }
 6069:     }
 6070:     return;
 6071: }
 6072: 
 6073: function toggleDataRow(form,checkbox,target,prefix,docount) {
 6074:     if (form.elements[checkbox].length != undefined) {
 6075:         var count = 0;
 6076:         if (docount) {
 6077:             for (var i=0; i<form.elements[checkbox].length; i++) {
 6078:                 if (form.elements[checkbox][i].checked) {
 6079:                     count ++;
 6080:                 }
 6081:             }
 6082:         }
 6083:         for (var i=0; i<form.elements[checkbox].length; i++) {
 6084:             var type = form.elements[checkbox][i].value;
 6085:             if (document.getElementById(prefix+type)) {
 6086:                 if (form.elements[checkbox][i].checked) {
 6087:                     document.getElementById(prefix+type).style.display = 'table-row';
 6088:                     if (count % 2 == 1) {
 6089:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 6090:                     } else {
 6091:                         document.getElementById(prefix+type).className = target;
 6092:                     }
 6093:                     count ++;
 6094:                 } else {
 6095:                     document.getElementById(prefix+type).style.display = 'none';
 6096:                 }
 6097:             }
 6098:         }
 6099:     }
 6100:     return;
 6101: }
 6102: 
 6103: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
 6104:     var caller = radio+'_'+status;
 6105:     if (form.elements[caller].length != undefined) {
 6106:         for (var i=0; i<form.elements[caller].length; i++) {
 6107:             if (form.elements[caller][i].checked) {
 6108:                 if (document.getElementById(altprefix+'_inst_'+status)) {
 6109:                     var curr = form.elements[caller][i].value;
 6110:                     if (prefix) {
 6111:                         document.getElementById(prefix+'_'+status).style.display = 'none';
 6112:                     }
 6113:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
 6114:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
 6115:                     if (curr == 'custom') {
 6116:                         if (prefix) { 
 6117:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
 6118:                         }
 6119:                     } else if (curr == 'inst') {
 6120:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
 6121:                     } else if (curr == 'noninst') {
 6122:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
 6123:                     }
 6124:                     break;
 6125:                 }
 6126:             }
 6127:         }
 6128:     }
 6129: }
 6130: 
 6131: // ]]>
 6132: </script>
 6133: 
 6134: ENDSCRIPT
 6135: }
 6136: 
 6137: sub noninst_users {
 6138:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
 6139:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_; 
 6140:     my $class = 'LC_left_item';
 6141:     if ($css_class) {
 6142:         $css_class = ' class="'.$css_class.'"'; 
 6143:     }
 6144:     if ($rowid) {
 6145:         $rowid = ' id="'.$rowid.'"';
 6146:     }
 6147:     if ($rowstyle) {
 6148:         $rowstyle = ' style="'.$rowstyle.'"';
 6149:     }
 6150:     my ($output,$description);
 6151:     if ($type eq 'default') {
 6152:         $description = &mt('Requests for: [_1]',$typetitle);
 6153:     } else {
 6154:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
 6155:     }
 6156:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 6157:               "<td>$description</td>\n".           
 6158:               '<td class="'.$class.'" colspan="2">'.
 6159:               '<table><tr>';
 6160:     my %headers = &Apache::lonlocal::texthash( 
 6161:               approve  => 'Processing',
 6162:               email    => 'E-mail',
 6163:               username => 'Username',
 6164:     );
 6165:     foreach my $item ('approve','email','username') {
 6166:         $output .= '<th>'.$headers{$item}.'</th>';
 6167:     }
 6168:     $output .= '</tr><tr>';
 6169:     foreach my $item ('approve','email','username') {
 6170:         $output .= '<td style="vertical-align: top">';
 6171:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
 6172:         if ($item eq 'approve') {
 6173:             %choices = &Apache::lonlocal::texthash (
 6174:                                                      automatic => 'Automatically approved',
 6175:                                                      approval  => 'Queued for approval',
 6176:                                                    );
 6177:             @options = ('automatic','approval');
 6178:             $hashref = $processing;
 6179:             $defoption = 'automatic';
 6180:             $name = 'cancreate_emailprocess_'.$type;
 6181:         } elsif ($item eq 'email') {
 6182:             %choices = &Apache::lonlocal::texthash (
 6183:                                                      any     => 'Any e-mail',
 6184:                                                      inst    => 'Institutional only',
 6185:                                                      noninst => 'Non-institutional only',
 6186:                                                      custom  => 'Custom restrictions',
 6187:                                                    );
 6188:             @options = ('any','inst','noninst');
 6189:             my $showcustom;
 6190:             if (ref($emailrules) eq 'HASH') {
 6191:                 if (keys(%{$emailrules}) > 0) {
 6192:                     push(@options,'custom');
 6193:                     $showcustom = 'cancreate_emailrule';
 6194:                     if (ref($settings) eq 'HASH') {
 6195:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
 6196:                             foreach my $rule (@{$settings->{'email_rule'}}) {
 6197:                                 if (exists($emailrules->{$rule})) {
 6198:                                     $hascustom ++;
 6199:                                 }
 6200:                             }
 6201:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
 6202:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
 6203:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
 6204:                                     if (exists($emailrules->{$rule})) {
 6205:                                         $hascustom ++;
 6206:                                     }
 6207:                                 }
 6208:                             }
 6209:                         }
 6210:                     }
 6211:                 }
 6212:             }
 6213:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
 6214:                                                      "'cancreate_emaildomain','$type'".');"';
 6215:             $hashref = $emailoptions;
 6216:             $defoption = 'any';
 6217:             $name = 'cancreate_emailoptions_'.$type;
 6218:         } elsif ($item eq 'username') {
 6219:             %choices = &Apache::lonlocal::texthash (
 6220:                                                      all    => 'Same as e-mail',
 6221:                                                      first  => 'Omit @domain',
 6222:                                                      free   => 'Free to choose',
 6223:                                                    );
 6224:             @options = ('all','first','free');
 6225:             $hashref = $emailverified;
 6226:             $defoption = 'all';
 6227:             $name = 'cancreate_usernameoptions_'.$type;
 6228:         }
 6229:         foreach my $option (@options) {
 6230:             my $checked;
 6231:             if (ref($hashref) eq 'HASH') {
 6232:                 if ($type eq '') {
 6233:                     if (!exists($hashref->{'default'})) {
 6234:                         if ($option eq $defoption) {
 6235:                             $checked = ' checked="checked"';
 6236:                         }
 6237:                     } else {
 6238:                         if ($hashref->{'default'} eq $option) {
 6239:                             $checked = ' checked="checked"';
 6240:                         }
 6241:                     }
 6242:                 } else {
 6243:                     if (!exists($hashref->{$type})) {
 6244:                         if ($option eq $defoption) {
 6245:                             $checked = ' checked="checked"';
 6246:                         }
 6247:                     } else {
 6248:                         if ($hashref->{$type} eq $option) {
 6249:                             $checked = ' checked="checked"';
 6250:                         }
 6251:                     }
 6252:                 }
 6253:             } elsif (($item eq 'email') && ($hascustom)) {
 6254:                 if ($option eq 'custom') {
 6255:                     $checked = ' checked="checked"';
 6256:                 }
 6257:             } elsif ($option eq $defoption) {
 6258:                 $checked = ' checked="checked"';
 6259:             }
 6260:             $output .= '<span class="LC_nobreak"><label>'.
 6261:                        '<input type="radio" name="'.$name.'"'.
 6262:                        $checked.' value="'.$option.'"'.$onclick.' />'.
 6263:                        $choices{$option}.'</label></span><br />';
 6264:             if ($item eq 'email') {
 6265:                 if ($option eq 'custom') {
 6266:                     my $id = 'cancreate_emailrule_'.$type;
 6267:                     my $display = 'none';
 6268:                     if ($checked) {
 6269:                         $display = 'inline';
 6270:                     }
 6271:                     my $numinrow = 2;
 6272:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
 6273:                                '<legend>'.&mt('Disallow').'</legend><table>'.
 6274:                                &user_formats_row('email',$settings,$emailrules,
 6275:                                                  $emailruleorder,$numinrow,'',$type);
 6276:                               '</table></fieldset>';
 6277:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
 6278:                     my %text = &Apache::lonlocal::texthash (
 6279:                                                              inst    => 'must end:',
 6280:                                                              noninst => 'cannot end:',
 6281:                                                            );
 6282:                     my $value;
 6283:                     if (ref($emaildomain) eq 'HASH') {
 6284:                         if (ref($emaildomain->{$type}) eq 'HASH') {
 6285:                             $value = $emaildomain->{$type}->{$option}; 
 6286:                         }
 6287:                     }
 6288:                     if ($value eq '') {
 6289:                         $value = '@'.$intdom;
 6290:                     }
 6291:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
 6292:                     my $display = 'none';
 6293:                     if ($checked) {
 6294:                         $display = 'inline';
 6295:                     }
 6296:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
 6297:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
 6298:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
 6299:                                '</div>';
 6300:                 }
 6301:             }
 6302:         }
 6303:         $output .= '</td>'."\n";
 6304:     }
 6305:     $output .= "</tr></table></td></tr>\n";
 6306:     return $output;
 6307: }
 6308: 
 6309: sub captcha_choice {
 6310:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
 6311:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 6312:         $vertext,$currver);
 6313:     my %lt = &captcha_phrases();
 6314:     $keyentry = 'hidden';
 6315:     if ($context eq 'cancreate') {
 6316:         $rowname = &mt('CAPTCHA validation');
 6317:     } elsif ($context eq 'login') {
 6318:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 6319:     }
 6320:     if (ref($settings) eq 'HASH') {
 6321:         if ($settings->{'captcha'}) {
 6322:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 6323:         } else {
 6324:             $checked{'original'} = ' checked="checked"';
 6325:         }
 6326:         if ($settings->{'captcha'} eq 'recaptcha') {
 6327:             $pubtext = $lt{'pub'};
 6328:             $privtext = $lt{'priv'};
 6329:             $keyentry = 'text';
 6330:             $vertext = $lt{'ver'};
 6331:             $currver = $settings->{'recaptchaversion'};
 6332:             if ($currver ne '2') {
 6333:                 $currver = 1;
 6334:             }
 6335:         }
 6336:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 6337:             $currpub = $settings->{'recaptchakeys'}{'public'};
 6338:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 6339:         }
 6340:     } else {
 6341:         $checked{'original'} = ' checked="checked"';
 6342:     }
 6343:     my $css_class;
 6344:     if ($itemcount%2) {
 6345:         $css_class = 'LC_odd_row';
 6346:     }
 6347:     if ($customcss) {
 6348:         $css_class .= " $customcss";
 6349:     }
 6350:     $css_class =~ s/^\s+//;
 6351:     if ($css_class) {
 6352:         $css_class = ' class="'.$css_class.'"';
 6353:     }
 6354:     if ($rowstyle) {
 6355:         $css_class .= ' style="'.$rowstyle.'"';
 6356:     }
 6357:     my $output = '<tr'.$css_class.'>'.
 6358:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 6359:                  '<table><tr><td>'."\n";
 6360:     foreach my $option ('original','recaptcha','notused') {
 6361:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 6362:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 6363:                    $lt{$option}.'</label></span>';
 6364:         unless ($option eq 'notused') {
 6365:             $output .= ('&nbsp;'x2)."\n";
 6366:         }
 6367:     }
 6368: #
 6369: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 6370: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 6371: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 6372: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 6373: #
 6374:     $output .= '</td></tr>'."\n".
 6375:                '<tr><td class="LC_zero_height">'."\n".
 6376:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 6377:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 6378:                $currpub.'" size="40" /></span><br />'."\n".
 6379:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 6380:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 6381:                $currpriv.'" size="40" /></span><br />'.
 6382:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 6383:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 6384:                $currver.'" size="3" /></span><br />'.
 6385:                '</td></tr></table>'."\n".
 6386:                '</td></tr>';
 6387:     return $output;
 6388: }
 6389: 
 6390: sub user_formats_row {
 6391:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
 6392:     my $output;
 6393:     my %text = (
 6394:                    'username' => 'new usernames',
 6395:                    'id'       => 'IDs',
 6396:                );
 6397:     unless ($type eq 'email') {
 6398:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 6399:         $output = '<tr '.$css_class.'>'.
 6400:                   '<td><span class="LC_nobreak">'.
 6401:                   &mt("Format rules to check for $text{$type}: ").
 6402:                   '</td><td class="LC_left_item" colspan="2"><table>';
 6403:     }
 6404:     my $rem;
 6405:     if (ref($ruleorder) eq 'ARRAY') {
 6406:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 6407:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 6408:                 my $rem = $i%($numinrow);
 6409:                 if ($rem == 0) {
 6410:                     if ($i > 0) {
 6411:                         $output .= '</tr>';
 6412:                     }
 6413:                     $output .= '<tr>';
 6414:                 }
 6415:                 my $check = ' ';
 6416:                 if (ref($settings) eq 'HASH') {
 6417:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 6418:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 6419:                             $check = ' checked="checked" ';
 6420:                         }
 6421:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
 6422:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
 6423:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
 6424:                                 $check = ' checked="checked" ';
 6425:                             }
 6426:                         }
 6427:                     }
 6428:                 }
 6429:                 my $name = $type.'_rule';
 6430:                 if ($type eq 'email') {
 6431:                     $name .= '_'.$status;
 6432:                 }
 6433:                 $output .= '<td class="LC_left_item">'.
 6434:                            '<span class="LC_nobreak"><label>'.
 6435:                            '<input type="checkbox" name="'.$name.'" '.
 6436:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 6437:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 6438:             }
 6439:         }
 6440:         $rem = @{$ruleorder}%($numinrow);
 6441:     }
 6442:     my $colsleft;
 6443:     if ($rem) {
 6444:         $colsleft = $numinrow - $rem;
 6445:     }
 6446:     if ($colsleft > 1 ) {
 6447:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6448:                    '&nbsp;</td>';
 6449:     } elsif ($colsleft == 1) {
 6450:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 6451:     }
 6452:     $output .= '</tr></table>';
 6453:     unless ($type eq 'email') {
 6454:         $output .= '</td></tr>';
 6455:     }
 6456:     return $output;
 6457: }
 6458: 
 6459: sub usercreation_types {
 6460:     my %lt = &Apache::lonlocal::texthash (
 6461:                     author     => 'When adding a co-author',
 6462:                     course     => 'When adding a user to a course',
 6463:                     requestcrs => 'When requesting a course',
 6464:                     any        => 'Any',
 6465:                     official   => 'Institutional only ',
 6466:                     unofficial => 'Non-institutional only',
 6467:                     none       => 'None',
 6468:     );
 6469:     return %lt;
 6470: }
 6471: 
 6472: sub selfcreation_types {
 6473:     my %lt = &Apache::lonlocal::texthash (
 6474:                     selfcreate => 'User creates own account',
 6475:                     any        => 'Any',
 6476:                     official   => 'Institutional only ',
 6477:                     unofficial => 'Non-institutional only',
 6478:                     email      => 'E-mail address',
 6479:                     login      => 'Institutional Login',
 6480:                     sso        => 'SSO',
 6481:              );
 6482: }
 6483: 
 6484: sub authtype_names {
 6485:     my %lt = &Apache::lonlocal::texthash(
 6486:                       int    => 'Internal',
 6487:                       krb4   => 'Kerberos 4',
 6488:                       krb5   => 'Kerberos 5',
 6489:                       loc    => 'Local',
 6490:                   );
 6491:     return %lt;
 6492: }
 6493: 
 6494: sub context_names {
 6495:     my %context_title = &Apache::lonlocal::texthash(
 6496:        author => 'Creating users when an Author',
 6497:        course => 'Creating users when in a course',
 6498:        domain => 'Creating users when a Domain Coordinator',
 6499:     );
 6500:     return %context_title;
 6501: }
 6502: 
 6503: sub print_usermodification {
 6504:     my ($position,$dom,$settings,$rowtotal) = @_;
 6505:     my $numinrow = 4;
 6506:     my ($context,$datatable,$rowcount);
 6507:     if ($position eq 'top') {
 6508:         $rowcount = 0;
 6509:         $context = 'author'; 
 6510:         foreach my $role ('ca','aa') {
 6511:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6512:                                                    $numinrow,$rowcount);
 6513:             $$rowtotal ++;
 6514:             $rowcount ++;
 6515:         }
 6516:     } elsif ($position eq 'bottom') {
 6517:         $context = 'course';
 6518:         $rowcount = 0;
 6519:         foreach my $role ('st','ep','ta','in','cr') {
 6520:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6521:                                                    $numinrow,$rowcount);
 6522:             $$rowtotal ++;
 6523:             $rowcount ++;
 6524:         }
 6525:     }
 6526:     return $datatable;
 6527: }
 6528: 
 6529: sub print_defaults {
 6530:     my ($position,$dom,$settings,$rowtotal) = @_;
 6531:     my $rownum = 0;
 6532:     my ($datatable,$css_class,$titles);
 6533:     unless ($position eq 'bottom') {
 6534:         $titles = &defaults_titles($dom);
 6535:     }
 6536:     if ($position eq 'top') {
 6537:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 6538:                      'datelocale_def','portal_def');
 6539:         my %defaults;
 6540:         if (ref($settings) eq 'HASH') {
 6541:             %defaults = %{$settings};
 6542:         } else {
 6543:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6544:             foreach my $item (@items) {
 6545:                 $defaults{$item} = $domdefaults{$item};
 6546:             }
 6547:         }
 6548:         foreach my $item (@items) {
 6549:             if ($rownum%2) {
 6550:                 $css_class = '';
 6551:             } else {
 6552:                 $css_class = ' class="LC_odd_row" ';
 6553:             }
 6554:             $datatable .= '<tr'.$css_class.'>'.
 6555:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 6556:                           '</span></td><td class="LC_right_item" colspan="3">';
 6557:             if ($item eq 'auth_def') {
 6558:                 my @authtypes = ('internal','krb4','krb5','localauth');
 6559:                 my %shortauth = (
 6560:                                  internal => 'int',
 6561:                                  krb4 => 'krb4',
 6562:                                  krb5 => 'krb5',
 6563:                                  localauth  => 'loc'
 6564:                                 );
 6565:                 my %authnames = &authtype_names();
 6566:                 foreach my $auth (@authtypes) {
 6567:                     my $checked = ' ';
 6568:                     if ($defaults{$item} eq $auth) {
 6569:                         $checked = ' checked="checked" ';
 6570:                     }
 6571:                     $datatable .= '<label><input type="radio" name="'.$item.
 6572:                                   '" value="'.$auth.'"'.$checked.'/>'.
 6573:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 6574:                 }
 6575:             } elsif ($item eq 'timezone_def') {
 6576:                 my $includeempty = 1;
 6577:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 6578:             } elsif ($item eq 'datelocale_def') {
 6579:                 my $includeempty = 1;
 6580:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 6581:             } elsif ($item eq 'lang_def') {
 6582:                 my $includeempty = 1;
 6583:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 6584:             } else {
 6585:                 my $size;
 6586:                 if ($item eq 'portal_def') {
 6587:                     $size = ' size="25"';
 6588:                 }
 6589:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6590:                               $defaults{$item}.'"'.$size.' />';
 6591:             }
 6592:             $datatable .= '</td></tr>';
 6593:             $rownum ++;
 6594:         }
 6595:     } elsif ($position eq 'middle') {
 6596:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 6597:         my %defaults;
 6598:         if (ref($settings) eq 'HASH') {
 6599:             %defaults = %{$settings};
 6600:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 6601:                 $defaults{'intauth_cost'} = 10;
 6602:             }
 6603:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 6604:                 $defaults{'intauth_check'} = 0;
 6605:             }
 6606:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 6607:                 $defaults{'intauth_switch'} = 0;
 6608:             }
 6609:         } else {
 6610:             %defaults = (
 6611:                           'intauth_cost'   => 10,
 6612:                           'intauth_check'  => 0,
 6613:                           'intauth_switch' => 0,
 6614:                         );
 6615:         }
 6616:         foreach my $item (@items) {
 6617:             if ($rownum%2) {
 6618:                 $css_class = '';
 6619:             } else {
 6620:                 $css_class = ' class="LC_odd_row" ';
 6621:             }
 6622:             $datatable .= '<tr'.$css_class.'>'.
 6623:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 6624:                           '</span></td><td class="LC_left_item" colspan="3">';
 6625:             if ($item eq 'intauth_switch') {
 6626:                 my @options = (0,1,2);
 6627:                 my %optiondesc = &Apache::lonlocal::texthash (
 6628:                                    0 => 'No',
 6629:                                    1 => 'Yes',
 6630:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 6631:                                  );
 6632:                 $datatable .= '<table width="100%">';
 6633:                 foreach my $option (@options) {
 6634:                     my $checked = ' ';
 6635:                     if ($defaults{$item} eq $option) {
 6636:                         $checked = ' checked="checked"';
 6637:                     }
 6638:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 6639:                                   '<label><input type="radio" name="'.$item.
 6640:                                   '" value="'.$option.'"'.$checked.' />'.
 6641:                                   $optiondesc{$option}.'</label></span></td></tr>';
 6642:                 }
 6643:                 $datatable .= '</table>';
 6644:             } elsif ($item eq 'intauth_check') {
 6645:                 my @options = (0,1,2);
 6646:                 my %optiondesc = &Apache::lonlocal::texthash (
 6647:                                    0 => 'No',
 6648:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 6649:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 6650:                                  );
 6651:                 $datatable .= '<table wisth="100%">';
 6652:                 foreach my $option (@options) {
 6653:                     my $checked = ' ';
 6654:                     my $onclick;
 6655:                     if ($defaults{$item} eq $option) {
 6656:                         $checked = ' checked="checked"';
 6657:                     }
 6658:                     if ($option == 2) {
 6659:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 6660:                     }
 6661:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 6662:                                   '<label><input type="radio" name="'.$item.
 6663:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 6664:                                   $optiondesc{$option}.'</label></span></td></tr>';
 6665:                 }
 6666:                 $datatable .= '</table>';
 6667:             } else {
 6668:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6669:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />'; 
 6670:             }
 6671:             $datatable .= '</td></tr>';
 6672:             $rownum ++;
 6673:         }
 6674:     } else {
 6675:         my %defaults;
 6676:         if (ref($settings) eq 'HASH') {
 6677:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 6678:                 my $maxnum = @{$settings->{'inststatusorder'}};
 6679:                 for (my $i=0; $i<$maxnum; $i++) {
 6680:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 6681:                     my $item = $settings->{'inststatusorder'}->[$i];
 6682:                     my $title = $settings->{'inststatustypes'}->{$item};
 6683:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 6684:                     $datatable .= '<tr'.$css_class.'>'.
 6685:                                   '<td><span class="LC_nobreak">'.
 6686:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 6687:                     for (my $k=0; $k<=$maxnum; $k++) {
 6688:                         my $vpos = $k+1;
 6689:                         my $selstr;
 6690:                         if ($k == $i) {
 6691:                             $selstr = ' selected="selected" ';
 6692:                         }
 6693:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6694:                     }
 6695:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 6696:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 6697:                                   &mt('delete').'</span></td>'.
 6698:                                   '<td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.&mt('Name displayed:').
 6699:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 6700:                                   '</span></td></tr>';
 6701:                 }
 6702:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 6703:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 6704:                 $datatable .= '<tr '.$css_class.'>'.
 6705:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 6706:                 for (my $k=0; $k<=$maxnum; $k++) {
 6707:                     my $vpos = $k+1;
 6708:                     my $selstr;
 6709:                     if ($k == $maxnum) {
 6710:                         $selstr = ' selected="selected" ';
 6711:                     }
 6712:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6713:                 }
 6714:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 6715:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 6716:                               '&nbsp;'.&mt('(new)').
 6717:                               '</span></td><td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.
 6718:                               &mt('Name displayed:').
 6719:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 6720:                               '</tr>'."\n";
 6721:                 $rownum ++;
 6722:             }
 6723:         }
 6724:     }
 6725:     $$rowtotal += $rownum;
 6726:     return $datatable;
 6727: }
 6728: 
 6729: sub get_languages_hash {
 6730:     my %langchoices;
 6731:     foreach my $id (&Apache::loncommon::languageids()) {
 6732:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 6733:         if ($code ne '') {
 6734:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 6735:         }
 6736:     }
 6737:     return %langchoices;
 6738: }
 6739: 
 6740: sub defaults_titles {
 6741:     my ($dom) = @_;
 6742:     my %titles = &Apache::lonlocal::texthash (
 6743:                    'auth_def'      => 'Default authentication type',
 6744:                    'auth_arg_def'  => 'Default authentication argument',
 6745:                    'lang_def'      => 'Default language',
 6746:                    'timezone_def'  => 'Default timezone',
 6747:                    'datelocale_def' => 'Default locale for dates',
 6748:                    'portal_def'     => 'Portal/Default URL',
 6749:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 6750:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 6751:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 6752:                  );
 6753:     if ($dom) {
 6754:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 6755:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 6756:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 6757:         $protocol = 'http' if ($protocol ne 'https');
 6758:         if ($uint_dom) {
 6759:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 6760:                                          $uint_dom);
 6761:         }
 6762:     }
 6763:     return (\%titles);
 6764: }
 6765: 
 6766: sub print_scantronformat {
 6767:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 6768:     my $itemcount = 1;
 6769:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 6770:         %confhash);
 6771:     my $switchserver = &check_switchserver($dom,$confname);
 6772:     my %lt = &Apache::lonlocal::texthash (
 6773:                 default => 'Default bubblesheet format file error',
 6774:                 custom  => 'Custom bubblesheet format file error',
 6775:              );
 6776:     my %scantronfiles = (
 6777:         default => 'default.tab',
 6778:         custom => 'custom.tab',
 6779:     );
 6780:     foreach my $key (keys(%scantronfiles)) {
 6781:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 6782:                               .$scantronfiles{$key};
 6783:     }
 6784:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 6785:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 6786:         if (!$switchserver) {
 6787:             my $servadm = $r->dir_config('lonAdmEMail');
 6788:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 6789:             if ($configuserok eq 'ok') {
 6790:                 if ($author_ok eq 'ok') {
 6791:                     my %legacyfile = (
 6792:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 6793:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 6794:                     );
 6795:                     my %md5chk;
 6796:                     foreach my $type (keys(%legacyfile)) {
 6797:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 6798:                         chomp($md5chk{$type});
 6799:                     }
 6800:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 6801:                         foreach my $type (keys(%legacyfile)) {
 6802:                             ($scantronurls{$type},my $error) = 
 6803:                                 &legacy_scantronformat($r,$dom,$confname,
 6804:                                                  $type,$legacyfile{$type},
 6805:                                                  $scantronurls{$type},
 6806:                                                  $scantronfiles{$type});
 6807:                             if ($error ne '') {
 6808:                                 $error{$type} = $error;
 6809:                             }
 6810:                         }
 6811:                         if (keys(%error) == 0) {
 6812:                             $is_custom = 1;
 6813:                             $confhash{'scantron'}{'scantronformat'} = 
 6814:                                 $scantronurls{'custom'};
 6815:                             my $putresult = 
 6816:                                 &Apache::lonnet::put_dom('configuration',
 6817:                                                          \%confhash,$dom);
 6818:                             if ($putresult ne 'ok') {
 6819:                                 $error{'custom'} = 
 6820:                                     '<span class="LC_error">'.
 6821:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6822:                             }
 6823:                         }
 6824:                     } else {
 6825:                         ($scantronurls{'default'},my $error) =
 6826:                             &legacy_scantronformat($r,$dom,$confname,
 6827:                                           'default',$legacyfile{'default'},
 6828:                                           $scantronurls{'default'},
 6829:                                           $scantronfiles{'default'});
 6830:                         if ($error eq '') {
 6831:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 6832:                             my $putresult =
 6833:                                 &Apache::lonnet::put_dom('configuration',
 6834:                                                          \%confhash,$dom);
 6835:                             if ($putresult ne 'ok') {
 6836:                                 $error{'default'} =
 6837:                                     '<span class="LC_error">'.
 6838:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6839:                             }
 6840:                         } else {
 6841:                             $error{'default'} = $error;
 6842:                         }
 6843:                     }
 6844:                 }
 6845:             }
 6846:         } else {
 6847:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 6848:         }
 6849:     }
 6850:     if (ref($settings) eq 'HASH') {
 6851:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 6852:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 6853:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 6854:                 $scantronurl = '';
 6855:             } else {
 6856:                 $scantronurl = $settings->{'scantronformat'};
 6857:             }
 6858:             $is_custom = 1;
 6859:         } else {
 6860:             $scantronurl = $scantronurls{'default'};
 6861:         }
 6862:     } else {
 6863:         if ($is_custom) {
 6864:             $scantronurl = $scantronurls{'custom'};
 6865:         } else {
 6866:             $scantronurl = $scantronurls{'default'};
 6867:         }
 6868:     }
 6869:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6870:     $datatable .= '<tr'.$css_class.'>';
 6871:     if (!$is_custom) {
 6872:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 6873:                       '<span class="LC_nobreak">';
 6874:         if ($scantronurl) {
 6875:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 6876:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 6877:         } else {
 6878:             $datatable = &mt('File unavailable for display');
 6879:         }
 6880:         $datatable .= '</span></td>';
 6881:         if (keys(%error) == 0) { 
 6882:             $datatable .= '<td style="vertical-align: bottom">';
 6883:             if (!$switchserver) {
 6884:                 $datatable .= &mt('Upload:').'<br />';
 6885:             }
 6886:         } else {
 6887:             my $errorstr;
 6888:             foreach my $key (sort(keys(%error))) {
 6889:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6890:             }
 6891:             $datatable .= '<td>'.$errorstr;
 6892:         }
 6893:     } else {
 6894:         if (keys(%error) > 0) {
 6895:             my $errorstr;
 6896:             foreach my $key (sort(keys(%error))) {
 6897:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6898:             } 
 6899:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 6900:         } elsif ($scantronurl) {
 6901:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 6902:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 6903:             $datatable .= '<td><span class="LC_nobreak">'.
 6904:                           $link.
 6905:                           '<label><input type="checkbox" name="scantronformat_del"'.
 6906:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 6907:                           '<td><span class="LC_nobreak">&nbsp;'.
 6908:                           &mt('Replace:').'</span><br />';
 6909:         }
 6910:     }
 6911:     if (keys(%error) == 0) {
 6912:         if ($switchserver) {
 6913:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 6914:         } else {
 6915:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 6916:                          '<input type="file" name="scantronformat" /></span>';
 6917:         }
 6918:     }
 6919:     $datatable .= '</td></tr>';
 6920:     $$rowtotal ++;
 6921:     return $datatable;
 6922: }
 6923: 
 6924: sub legacy_scantronformat {
 6925:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 6926:     my ($url,$error);
 6927:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 6928:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 6929:         (my $result,$url) =
 6930:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 6931:                          '','',$newfile);
 6932:         if ($result ne 'ok') {
 6933:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 6934:         }
 6935:     }
 6936:     return ($url,$error);
 6937: }
 6938: 
 6939: sub print_coursecategories {
 6940:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 6941:     my $datatable;
 6942:     if ($position eq 'top') {
 6943:         my (%checked);
 6944:         my @catitems = ('unauth','auth');
 6945:         my @cattypes = ('std','domonly','codesrch','none');
 6946:         $checked{'unauth'} = 'std';
 6947:         $checked{'auth'} = 'std';
 6948:         if (ref($settings) eq 'HASH') {
 6949:             foreach my $type (@cattypes) {
 6950:                 if ($type eq $settings->{'unauth'}) {
 6951:                     $checked{'unauth'} = $type;
 6952:                 }
 6953:                 if ($type eq $settings->{'auth'}) {
 6954:                     $checked{'auth'} = $type;
 6955:                 }
 6956:             }
 6957:         }
 6958:         my %lt = &Apache::lonlocal::texthash (
 6959:                                                unauth   => 'Catalog type for unauthenticated users',
 6960:                                                auth     => 'Catalog type for authenticated users',
 6961:                                                none     => 'No catalog',
 6962:                                                std      => 'Standard catalog',
 6963:                                                domonly  => 'Domain-only catalog',
 6964:                                                codesrch => "Code search form",
 6965:                                              );
 6966:        my $itemcount = 0;
 6967:        foreach my $item (@catitems) {
 6968:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 6969:            $datatable .= '<tr '.$css_class.'>'.
 6970:                          '<td>'.$lt{$item}.'</td>'.
 6971:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 6972:            foreach my $type (@cattypes) {
 6973:                my $ischecked;
 6974:                if ($checked{$item} eq $type) {
 6975:                    $ischecked=' checked="checked"';
 6976:                }
 6977:                $datatable .= '<label>'.
 6978:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 6979:                              ' />'.$lt{$type}.'</label>&nbsp;';
 6980:            }
 6981:            $datatable .= '</td></tr>';
 6982:            $itemcount ++;
 6983:         }
 6984:         $$rowtotal += $itemcount;
 6985:     } elsif ($position eq 'middle') {
 6986:         my $toggle_cats_crs = ' ';
 6987:         my $toggle_cats_dom = ' checked="checked" ';
 6988:         my $can_cat_crs = ' ';
 6989:         my $can_cat_dom = ' checked="checked" ';
 6990:         my $toggle_catscomm_comm = ' ';
 6991:         my $toggle_catscomm_dom = ' checked="checked" ';
 6992:         my $can_catcomm_comm = ' ';
 6993:         my $can_catcomm_dom = ' checked="checked" ';
 6994:         my $toggle_catsplace_place = ' ';
 6995:         my $toggle_catsplace_dom = ' checked="checked" ';
 6996:         my $can_catplace_place = ' ';
 6997:         my $can_catplace_dom = ' checked="checked" ';
 6998: 
 6999:         if (ref($settings) eq 'HASH') {
 7000:             if ($settings->{'togglecats'} eq 'crs') {
 7001:                 $toggle_cats_crs = $toggle_cats_dom;
 7002:                 $toggle_cats_dom = ' ';
 7003:             }
 7004:             if ($settings->{'categorize'} eq 'crs') {
 7005:                 $can_cat_crs = $can_cat_dom;
 7006:                 $can_cat_dom = ' ';
 7007:             }
 7008:             if ($settings->{'togglecatscomm'} eq 'comm') {
 7009:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 7010:                 $toggle_catscomm_dom = ' ';
 7011:             }
 7012:             if ($settings->{'categorizecomm'} eq 'comm') {
 7013:                 $can_catcomm_comm = $can_catcomm_dom;
 7014:                 $can_catcomm_dom = ' ';
 7015:             }
 7016:             if ($settings->{'togglecatsplace'} eq 'place') {
 7017:                 $toggle_catsplace_place = $toggle_catsplace_dom;
 7018:                 $toggle_catsplace_dom = ' ';
 7019:             }
 7020:             if ($settings->{'categorizeplace'} eq 'place') {
 7021:                 $can_catplace_place = $can_catplace_dom;
 7022:                 $can_catplace_dom = ' ';
 7023:             }
 7024:         }
 7025:         my %title = &Apache::lonlocal::texthash (
 7026:                      togglecats      => 'Show/Hide a course in catalog',
 7027:                      togglecatscomm  => 'Show/Hide a community in catalog',
 7028:                      togglecatsplace => 'Show/Hide a placement test in catalog',
 7029:                      categorize      => 'Assign a category to a course',
 7030:                      categorizecomm  => 'Assign a category to a community',
 7031:                      categorizeplace => 'Assign a category to a placement test',
 7032:                     );
 7033:         my %level = &Apache::lonlocal::texthash (
 7034:                      dom   => 'Set in Domain',
 7035:                      crs   => 'Set in Course',
 7036:                      comm  => 'Set in Community',
 7037:                      place => 'Set in Placement Test',
 7038:                     );
 7039:         $datatable = '<tr class="LC_odd_row">'.
 7040:                   '<td>'.$title{'togglecats'}.'</td>'.
 7041:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 7042:                   '<input type="radio" name="togglecats"'.
 7043:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7044:                   '<label><input type="radio" name="togglecats"'.
 7045:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 7046:                   '</tr><tr>'.
 7047:                   '<td>'.$title{'categorize'}.'</td>'.
 7048:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 7049:                   '<label><input type="radio" name="categorize"'.
 7050:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7051:                   '<label><input type="radio" name="categorize"'.
 7052:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 7053:                   '</tr><tr class="LC_odd_row">'.
 7054:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 7055:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 7056:                   '<input type="radio" name="togglecatscomm"'.
 7057:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7058:                   '<label><input type="radio" name="togglecatscomm"'.
 7059:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 7060:                   '</tr><tr>'.
 7061:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 7062:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 7063:                   '<label><input type="radio" name="categorizecomm"'.
 7064:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7065:                   '<label><input type="radio" name="categorizecomm"'.
 7066:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 7067:                   '</tr><tr>'.
 7068:                   '<td>'.$title{'togglecatsplace'}.'</td>'.
 7069:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 7070:                   '<input type="radio" name="togglecatsplace"'.
 7071:                   $toggle_catsplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7072:                   '<label><input type="radio" name="togglecatscomm"'.
 7073:                   $toggle_catsplace_place.' value="comm" />'.$level{'place'}.'</label></span></td>'.
 7074:                   '</tr><tr>'.
 7075:                   '<td>'.$title{'categorizeplace'}.'</td>'.
 7076:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 7077:                   '<label><input type="radio" name="categorizeplace"'.
 7078:                   $can_catplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7079:                   '<label><input type="radio" name="categorizeplace"'.
 7080:                   $can_catplace_place.'value="place" />'.$level{'place'}.'</label></span></td>'.
 7081:                   '</tr>';
 7082:         $$rowtotal += 6;
 7083:     } else {
 7084:         my $css_class;
 7085:         my $itemcount = 1;
 7086:         my $cathash; 
 7087:         if (ref($settings) eq 'HASH') {
 7088:             $cathash = $settings->{'cats'};
 7089:         }
 7090:         if (ref($cathash) eq 'HASH') {
 7091:             my (@cats,@trails,%allitems,%idx,@jsarray);
 7092:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 7093:                                                    \%allitems,\%idx,\@jsarray);
 7094:             my $maxdepth = scalar(@cats);
 7095:             my $colattrib = '';
 7096:             if ($maxdepth > 2) {
 7097:                 $colattrib = ' colspan="2" ';
 7098:             }
 7099:             my @path;
 7100:             if (@cats > 0) {
 7101:                 if (ref($cats[0]) eq 'ARRAY') {
 7102:                     my $numtop = @{$cats[0]};
 7103:                     my $maxnum = $numtop;
 7104:                     my %default_names = (
 7105:                           instcode    => &mt('Official courses'),
 7106:                           communities => &mt('Communities'),
 7107:                           placement   => &mt('Placement Tests'),
 7108:                     );
 7109: 
 7110:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 7111:                         ($cathash->{'instcode::0'} eq '') ||
 7112:                         (!grep(/^communities$/,@{$cats[0]})) || 
 7113:                         ($cathash->{'communities::0'} eq '') ||
 7114:                         (!grep(/^placement$/,@{$cats[0]})) ||
 7115:                         ($cathash->{'placement::0'} eq '')) {
 7116:                         $maxnum ++;
 7117:                     }
 7118:                     my $lastidx;
 7119:                     for (my $i=0; $i<$numtop; $i++) {
 7120:                         my $parent = $cats[0][$i];
 7121:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7122:                         my $item = &escape($parent).'::0';
 7123:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 7124:                         $lastidx = $idx{$item};
 7125:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7126:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 7127:                         for (my $k=0; $k<=$maxnum; $k++) {
 7128:                             my $vpos = $k+1;
 7129:                             my $selstr;
 7130:                             if ($k == $i) {
 7131:                                 $selstr = ' selected="selected" ';
 7132:                             }
 7133:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7134:                         }
 7135:                         $datatable .= '</select></span></td><td>';
 7136:                         if ($parent eq 'instcode' || $parent eq 'communities' || $parent eq 'placement') {
 7137:                             $datatable .=  '<span class="LC_nobreak">'
 7138:                                            .$default_names{$parent}.'</span>';
 7139:                             if ($parent eq 'instcode') {
 7140:                                 $datatable .= '<br /><span class="LC_nobreak">('
 7141:                                               .&mt('with institutional codes')
 7142:                                               .')</span></td><td'.$colattrib.'>';
 7143:                             } else {
 7144:                                 $datatable .= '<table><tr><td>';
 7145:                             }
 7146:                             $datatable .= '<span class="LC_nobreak">'
 7147:                                           .'<label><input type="radio" name="'
 7148:                                           .$parent.'" value="1" checked="checked" />'
 7149:                                           .&mt('Display').'</label>';
 7150:                             if ($parent eq 'instcode') {
 7151:                                 $datatable .= '&nbsp;';
 7152:                             } else {
 7153:                                 $datatable .= '</span></td></tr><tr><td>'
 7154:                                               .'<span class="LC_nobreak">';
 7155:                             }
 7156:                             $datatable .= '<label><input type="radio" name="'
 7157:                                           .$parent.'" value="0" />'
 7158:                                           .&mt('Do not display').'</label></span>';
 7159:                             if (($parent eq 'communities') || ($parent eq 'placement')) {
 7160:                                 $datatable .= '</td></tr></table>';
 7161:                             }
 7162:                             $datatable .= '</td>';
 7163:                         } else {
 7164:                             $datatable .= $parent
 7165:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 7166:                                           .'<input type="checkbox" name="deletecategory" '
 7167:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 7168:                         }
 7169:                         my $depth = 1;
 7170:                         push(@path,$parent);
 7171:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 7172:                         pop(@path);
 7173:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 7174:                         $itemcount ++;
 7175:                     }
 7176:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7177:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 7178:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 7179:                     for (my $k=0; $k<=$maxnum; $k++) {
 7180:                         my $vpos = $k+1;
 7181:                         my $selstr;
 7182:                         if ($k == $numtop) {
 7183:                             $selstr = ' selected="selected" ';
 7184:                         }
 7185:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7186:                     }
 7187:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 7188:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 7189:                                   .'</tr>'."\n";
 7190:                     $itemcount ++;
 7191:                     foreach my $default ('instcode','communities','placement') {
 7192:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 7193:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7194:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 7195:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 7196:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 7197:                             for (my $k=0; $k<=$maxnum; $k++) {
 7198:                                 my $vpos = $k+1;
 7199:                                 my $selstr;
 7200:                                 if ($k == $maxnum) {
 7201:                                     $selstr = ' selected="selected" ';
 7202:                                 }
 7203:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7204:                             }
 7205:                             $datatable .= '</select></span></td>'.
 7206:                                           '<td><span class="LC_nobreak">'.
 7207:                                           $default_names{$default}.'</span>';
 7208:                             if ($default eq 'instcode') {
 7209:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 7210:                                               .&mt('with institutional codes').')</span>';
 7211:                             }
 7212:                             $datatable .= '</td>'
 7213:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 7214:                                           .&mt('Display').'</label>&nbsp;'
 7215:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 7216:                                           .&mt('Do not display').'</label></span></td></tr>';
 7217:                         }
 7218:                     }
 7219:                 }
 7220:             } else {
 7221:                 $datatable .= &initialize_categories($itemcount);
 7222:             }
 7223:         } else {
 7224:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
 7225:                           .&initialize_categories($itemcount);
 7226:         }
 7227:         $$rowtotal += $itemcount;
 7228:     }
 7229:     return $datatable;
 7230: }
 7231: 
 7232: sub print_serverstatuses {
 7233:     my ($dom,$settings,$rowtotal) = @_;
 7234:     my $datatable;
 7235:     my @pages = &serverstatus_pages();
 7236:     my (%namedaccess,%machineaccess);
 7237:     foreach my $type (@pages) {
 7238:         $namedaccess{$type} = '';
 7239:         $machineaccess{$type}= '';
 7240:     }
 7241:     if (ref($settings) eq 'HASH') {
 7242:         foreach my $type (@pages) {
 7243:             if (exists($settings->{$type})) {
 7244:                 if (ref($settings->{$type}) eq 'HASH') {
 7245:                     foreach my $key (keys(%{$settings->{$type}})) {
 7246:                         if ($key eq 'namedusers') {
 7247:                             $namedaccess{$type} = $settings->{$type}->{$key};
 7248:                         } elsif ($key eq 'machines') {
 7249:                             $machineaccess{$type} = $settings->{$type}->{$key};
 7250:                         }
 7251:                     }
 7252:                 }
 7253:             }
 7254:         }
 7255:     }
 7256:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 7257:     my $rownum = 0;
 7258:     my $css_class;
 7259:     foreach my $type (@pages) {
 7260:         $rownum ++;
 7261:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 7262:         $datatable .= '<tr'.$css_class.'>'.
 7263:                       '<td><span class="LC_nobreak">'.
 7264:                       $titles->{$type}.'</span></td>'.
 7265:                       '<td class="LC_left_item">'.
 7266:                       '<input type="text" name="'.$type.'_namedusers" '.
 7267:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 7268:                       '<td class="LC_right_item">'.
 7269:                       '<span class="LC_nobreak">'.
 7270:                       '<input type="text" name="'.$type.'_machines" '.
 7271:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 7272:                       '</td></tr>'."\n";
 7273:     }
 7274:     $$rowtotal += $rownum;
 7275:     return $datatable;
 7276: }
 7277: 
 7278: sub serverstatus_pages {
 7279:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 7280:             'checksums','clusterstatus','certstatus','metadata_keywords',
 7281:             'metadata_harvest','takeoffline','takeonline','showenv','toggledebug',
 7282:             'ping','domconf','uniquecodes','diskusage','coursecatalog');
 7283: }
 7284: 
 7285: sub defaults_javascript {
 7286:     my ($settings) = @_;
 7287:     my $intauthcheck = &mt('Warning: disallowing login for an authenticated user if the stored cost is less than the default will require a password reset by/for the user.');
 7288:     my $intauthcost = &mt('Warning: bcrypt encryption cost for internal authentication must be an integer.');
 7289:     &js_escape(\$intauthcheck);
 7290:     &js_escape(\$intauthcost);
 7291:     my $intauthjs = <<"ENDSCRIPT";
 7292: 
 7293: function warnIntAuth(field) {
 7294:     if (field.name == 'intauth_check') {
 7295:         if (field.value == '2') {
 7296:             alert('$intauthcheck');
 7297:         }
 7298:     }
 7299:     if (field.name == 'intauth_cost') {
 7300:         field.value.replace(/\s/g,'');
 7301:         if (field.value != '') {
 7302:             var regexdigit=/^\\d+\$/;
 7303:             if (!regexdigit.test(field.value)) {
 7304:                 alert('$intauthcost');
 7305:             }
 7306:         }
 7307:     }
 7308:     return;
 7309: }
 7310: 
 7311: ENDSCRIPT
 7312: 
 7313:     if (ref($settings) ne 'HASH') {
 7314:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 7315:     }
 7316:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 7317:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 7318:         if ($maxnum eq '') {
 7319:             $maxnum = 0;
 7320:         }
 7321:         $maxnum ++;
 7322:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 7323:         return <<"ENDSCRIPT";
 7324: <script type="text/javascript">
 7325: // <![CDATA[
 7326: function reorderTypes(form,caller) {
 7327:     var changedVal;
 7328: $jstext 
 7329:     var newpos = 'addinststatus_pos';
 7330:     var current = new Array;
 7331:     var maxh = $maxnum;
 7332:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 7333:     var oldVal;
 7334:     if (caller == newpos) {
 7335:         changedVal = newitemVal;
 7336:     } else {
 7337:         var curritem = 'inststatus_pos_'+caller;
 7338:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 7339:         current[newitemVal] = newpos;
 7340:     }
 7341:     for (var i=0; i<inststatuses.length; i++) {
 7342:         if (inststatuses[i] != caller) {
 7343:             var elementName = 'inststatus_pos_'+inststatuses[i];
 7344:             if (form.elements[elementName]) {
 7345:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 7346:                 current[currVal] = elementName;
 7347:             }
 7348:         }
 7349:     }
 7350:     for (var j=0; j<maxh; j++) {
 7351:         if (current[j] == undefined) {
 7352:             oldVal = j;
 7353:         }
 7354:     }
 7355:     if (oldVal < changedVal) {
 7356:         for (var k=oldVal+1; k<=changedVal ; k++) {
 7357:            var elementName = current[k];
 7358:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 7359:         }
 7360:     } else {
 7361:         for (var k=changedVal; k<oldVal; k++) {
 7362:             var elementName = current[k];
 7363:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 7364:         }
 7365:     }
 7366:     return;
 7367: }
 7368: 
 7369: $intauthjs
 7370: 
 7371: // ]]>
 7372: </script>
 7373: 
 7374: ENDSCRIPT
 7375:     } else {
 7376:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 7377:     }
 7378: }
 7379: 
 7380: sub coursecategories_javascript {
 7381:     my ($settings) = @_;
 7382:     my ($output,$jstext,$cathash);
 7383:     if (ref($settings) eq 'HASH') {
 7384:         $cathash = $settings->{'cats'};
 7385:     }
 7386:     if (ref($cathash) eq 'HASH') {
 7387:         my (@cats,@jsarray,%idx);
 7388:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 7389:         if (@jsarray > 0) {
 7390:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 7391:             for (my $i=0; $i<@jsarray; $i++) {
 7392:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 7393:                     my $catstr = join('","',@{$jsarray[$i]});
 7394:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 7395:                 }
 7396:             }
 7397:         }
 7398:     } else {
 7399:         $jstext  = '    var categories = Array(1);'."\n".
 7400:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 7401:     }
 7402:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 7403:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 7404:     my $placement_reserved = &mt('The name: [_1] is a reserved category.','"placement"');
 7405:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 7406:     &js_escape(\$instcode_reserved);
 7407:     &js_escape(\$communities_reserved);
 7408:     &js_escape(\$placement_reserved);
 7409:     &js_escape(\$choose_again);
 7410:     $output = <<"ENDSCRIPT";
 7411: <script type="text/javascript">
 7412: // <![CDATA[
 7413: function reorderCats(form,parent,item,idx) {
 7414:     var changedVal;
 7415: $jstext
 7416:     var newpos = 'addcategory_pos';
 7417:     if (parent == '') {
 7418:         var has_instcode = 0;
 7419:         var maxtop = categories[idx].length;
 7420:         for (var j=0; j<maxtop; j++) {
 7421:             if (categories[idx][j] == 'instcode::0') {
 7422:                 has_instcode == 1;
 7423:             }
 7424:         }
 7425:         if (has_instcode == 0) {
 7426:             categories[idx][maxtop] = 'instcode_pos';
 7427:         }
 7428:     } else {
 7429:         newpos += '_'+parent;
 7430:     }
 7431:     var maxh = 1 + categories[idx].length;
 7432:     var current = new Array;
 7433:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 7434:     if (item == newpos) {
 7435:         changedVal = newitemVal;
 7436:     } else {
 7437:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 7438:         current[newitemVal] = newpos;
 7439:     }
 7440:     for (var i=0; i<categories[idx].length; i++) {
 7441:         var elementName = categories[idx][i];
 7442:         if (elementName != item) {
 7443:             if (form.elements[elementName]) {
 7444:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 7445:                 current[currVal] = elementName;
 7446:             }
 7447:         }
 7448:     }
 7449:     var oldVal;
 7450:     for (var j=0; j<maxh; j++) {
 7451:         if (current[j] == undefined) {
 7452:             oldVal = j;
 7453:         }
 7454:     }
 7455:     if (oldVal < changedVal) {
 7456:         for (var k=oldVal+1; k<=changedVal ; k++) {
 7457:            var elementName = current[k];
 7458:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 7459:         }
 7460:     } else {
 7461:         for (var k=changedVal; k<oldVal; k++) {
 7462:             var elementName = current[k];
 7463:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 7464:         }
 7465:     }
 7466:     return;
 7467: }
 7468: 
 7469: function categoryCheck(form) {
 7470:     if (form.elements['addcategory_name'].value == 'instcode') {
 7471:         alert('$instcode_reserved\\n$choose_again');
 7472:         return false;
 7473:     }
 7474:     if (form.elements['addcategory_name'].value == 'communities') {
 7475:         alert('$communities_reserved\\n$choose_again');
 7476:         return false;
 7477:     }
 7478:     if (form.elements['addcategory_name'].value == 'placement') {
 7479:         alert('$placement_reserved\\n$choose_again');
 7480:         return false;
 7481:     }
 7482:     return true;
 7483: }
 7484: 
 7485: // ]]>
 7486: </script>
 7487: 
 7488: ENDSCRIPT
 7489:     return $output;
 7490: }
 7491: 
 7492: sub initialize_categories {
 7493:     my ($itemcount) = @_;
 7494:     my ($datatable,$css_class,$chgstr);
 7495:     my %default_names = (
 7496:                       instcode    => 'Official courses (with institutional codes)',
 7497:                       communities => 'Communities',
 7498:                       placement   => 'Placement Tests',
 7499:                         );
 7500:     my $select0 = ' selected="selected"';
 7501:     my $select1 = '';
 7502:     foreach my $default ('instcode','communities','placement') {
 7503:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7504:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 7505:         if (($default eq 'communities') || ($default eq 'placement')) {
 7506:             $select1 = $select0;
 7507:             $select0 = '';
 7508:         }
 7509:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7510:                      .'<select name="'.$default.'_pos">'
 7511:                      .'<option value="0"'.$select0.'>1</option>'
 7512:                      .'<option value="1"'.$select1.'>2</option>'
 7513:                      .'<option value="2">3</option></select>&nbsp;'
 7514:                      .$default_names{$default}
 7515:                      .'</span></td><td><span class="LC_nobreak">'
 7516:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 7517:                      .&mt('Display').'</label>&nbsp;<label>'
 7518:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 7519:                  .'</label></span></td></tr>';
 7520:         $itemcount ++;
 7521:     }
 7522:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7523:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 7524:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7525:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 7526:                   .'<option value="0">1</option>'
 7527:                   .'<option value="1">2</option>'
 7528:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 7529:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 7530:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 7531:     return $datatable;
 7532: }
 7533: 
 7534: sub build_category_rows {
 7535:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 7536:     my ($text,$name,$item,$chgstr);
 7537:     if (ref($cats) eq 'ARRAY') {
 7538:         my $maxdepth = scalar(@{$cats});
 7539:         if (ref($cats->[$depth]) eq 'HASH') {
 7540:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 7541:                 my $numchildren = @{$cats->[$depth]{$parent}};
 7542:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7543:                 $text .= '<td><table class="LC_data_table">';
 7544:                 my ($idxnum,$parent_name,$parent_item);
 7545:                 my $higher = $depth - 1;
 7546:                 if ($higher == 0) {
 7547:                     $parent_name = &escape($parent).'::'.$higher;
 7548:                 } else {
 7549:                     if (ref($path) eq 'ARRAY') {
 7550:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 7551:                     }
 7552:                 }
 7553:                 $parent_item = 'addcategory_pos_'.$parent_name;
 7554:                 for (my $j=0; $j<=$numchildren; $j++) {
 7555:                     if ($j < $numchildren) {
 7556:                         $name = $cats->[$depth]{$parent}[$j];
 7557:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 7558:                         $idxnum = $idx->{$item};
 7559:                     } else {
 7560:                         $name = $parent_name;
 7561:                         $item = $parent_item;
 7562:                     }
 7563:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 7564:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 7565:                     for (my $i=0; $i<=$numchildren; $i++) {
 7566:                         my $vpos = $i+1;
 7567:                         my $selstr;
 7568:                         if ($j == $i) {
 7569:                             $selstr = ' selected="selected" ';
 7570:                         }
 7571:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 7572:                     }
 7573:                     $text .= '</select>&nbsp;';
 7574:                     if ($j < $numchildren) {
 7575:                         my $deeper = $depth+1;
 7576:                         $text .= $name.'&nbsp;'
 7577:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 7578:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 7579:                         if(ref($path) eq 'ARRAY') {
 7580:                             push(@{$path},$name);
 7581:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 7582:                             pop(@{$path});
 7583:                         }
 7584:                     } else {
 7585:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 7586:                         if ($j == $numchildren) {
 7587:                             $text .= $name;
 7588:                         } else {
 7589:                             $text .= $item;
 7590:                         }
 7591:                         $text .= '" value="" />';
 7592:                     }
 7593:                     $text .= '</td></tr>';
 7594:                 }
 7595:                 $text .= '</table></td>';
 7596:             } else {
 7597:                 my $higher = $depth-1;
 7598:                 if ($higher == 0) {
 7599:                     $name = &escape($parent).'::'.$higher;
 7600:                 } else {
 7601:                     if (ref($path) eq 'ARRAY') {
 7602:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 7603:                     }
 7604:                 }
 7605:                 my $colspan;
 7606:                 if ($parent ne 'instcode') {
 7607:                     $colspan = $maxdepth - $depth - 1;
 7608:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 7609:                 }
 7610:             }
 7611:         }
 7612:     }
 7613:     return $text;
 7614: }
 7615: 
 7616: sub modifiable_userdata_row {
 7617:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
 7618:         $rowid,$customcss,$rowstyle) = @_;
 7619:     my ($role,$rolename,$statustype);
 7620:     $role = $item;
 7621:     if ($context eq 'cancreate') {
 7622:         if ($item =~ /^(emailusername)_(.+)$/) {
 7623:             $role = $1;
 7624:             $statustype = $2;
 7625:             if (ref($usertypes) eq 'HASH') {
 7626:                 if ($usertypes->{$statustype}) {
 7627:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 7628:                 } else {
 7629:                     $rolename = &mt('Data provided by user');
 7630:                 }
 7631:             }
 7632:         }
 7633:     } elsif ($context eq 'selfcreate') {
 7634:         if (ref($usertypes) eq 'HASH') {
 7635:             $rolename = $usertypes->{$role};
 7636:         } else {
 7637:             $rolename = $role;
 7638:         }
 7639:     } else {
 7640:         if ($role eq 'cr') {
 7641:             $rolename = &mt('Custom role');
 7642:         } else {
 7643:             $rolename = &Apache::lonnet::plaintext($role);
 7644:         }
 7645:     }
 7646:     my (@fields,%fieldtitles);
 7647:     if (ref($fieldsref) eq 'ARRAY') {
 7648:         @fields = @{$fieldsref};
 7649:     } else {
 7650:         @fields = ('lastname','firstname','middlename','generation',
 7651:                    'permanentemail','id');
 7652:     }
 7653:     if ((ref($titlesref) eq 'HASH')) {
 7654:         %fieldtitles = %{$titlesref};
 7655:     } else {
 7656:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7657:     }
 7658:     my $output;
 7659:     my $css_class;
 7660:     if ($rowcount%2) {
 7661:         $css_class = 'LC_odd_row';
 7662:     }
 7663:     if ($customcss) {
 7664:         $css_class .= " $customcss";
 7665:     }
 7666:     $css_class =~ s/^\s+//;
 7667:     if ($css_class) {
 7668:         $css_class = ' class="'.$css_class.'"';
 7669:     }
 7670:     if ($rowstyle) {
 7671:         $css_class .= ' style="'.$rowstyle.'"';
 7672:     }
 7673:     if ($rowid) {
 7674:         $rowid = ' id="'.$rowid.'"';
 7675:     }
 7676:     $output = '<tr '.$css_class.$rowid.'>'.
 7677:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 7678:               '<td class="LC_left_item" colspan="2"><table>';
 7679:     my $rem;
 7680:     my %checks;
 7681:     if (ref($settings) eq 'HASH') {
 7682:         if (ref($settings->{$context}) eq 'HASH') {
 7683:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 7684:                 my $hashref = $settings->{$context}->{$role};
 7685:                 if ($role eq 'emailusername') {
 7686:                     if ($statustype) {
 7687:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 7688:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 7689:                             if (ref($hashref) eq 'HASH') { 
 7690:                                 foreach my $field (@fields) {
 7691:                                     if ($hashref->{$field}) {
 7692:                                         $checks{$field} = $hashref->{$field};
 7693:                                     }
 7694:                                 }
 7695:                             }
 7696:                         }
 7697:                     }
 7698:                 } else {
 7699:                     if (ref($hashref) eq 'HASH') {
 7700:                         foreach my $field (@fields) {
 7701:                             if ($hashref->{$field}) {
 7702:                                 $checks{$field} = ' checked="checked" ';
 7703:                             }
 7704:                         }
 7705:                     }
 7706:                 }
 7707:             }
 7708:         }
 7709:     }
 7710:  
 7711:     my $total = scalar(@fields);
 7712:     for (my $i=0; $i<$total; $i++) {
 7713:         $rem = $i%($numinrow);
 7714:         if ($rem == 0) {
 7715:             if ($i > 0) {
 7716:                 $output .= '</tr>';
 7717:             }
 7718:             $output .= '<tr>';
 7719:         }
 7720:         my $check = ' ';
 7721:         unless ($role eq 'emailusername') {
 7722:             if (exists($checks{$fields[$i]})) {
 7723:                 $check = $checks{$fields[$i]}
 7724:             } else {
 7725:                 if ($role eq 'st') {
 7726:                     if (ref($settings) ne 'HASH') {
 7727:                         $check = ' checked="checked" '; 
 7728:                     }
 7729:                 }
 7730:             }
 7731:         }
 7732:         $output .= '<td class="LC_left_item">'.
 7733:                    '<span class="LC_nobreak">';
 7734:         if ($role eq 'emailusername') {
 7735:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 7736:                 $checks{$fields[$i]} = 'omit';
 7737:             }
 7738:             foreach my $option ('required','optional','omit') {
 7739:                 my $checked='';
 7740:                 if ($checks{$fields[$i]} eq $option) {
 7741:                     $checked='checked="checked" ';
 7742:                 }
 7743:                 $output .= '<label>'.
 7744:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 7745:                            &mt($option).'</label>'.('&nbsp;' x2);
 7746:             }
 7747:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 7748:         } else {
 7749:             $output .= '<label>'.
 7750:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 7751:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 7752:                        '</label>';
 7753:         }
 7754:         $output .= '</span></td>';
 7755:     }
 7756:     $rem = $total%$numinrow;
 7757:     my $colsleft;
 7758:     if ($rem) {
 7759:         $colsleft = $numinrow - $rem;
 7760:     }
 7761:     if ($colsleft > 1) {
 7762:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7763:                    '&nbsp;</td>';
 7764:     } elsif ($colsleft == 1) {
 7765:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 7766:     }
 7767:     $output .= '</tr></table></td></tr>';
 7768:     return $output;
 7769: }
 7770: 
 7771: sub insttypes_row {
 7772:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
 7773:         $customcss,$rowstyle) = @_;
 7774:     my %lt = &Apache::lonlocal::texthash (
 7775:                       cansearch => 'Users allowed to search',
 7776:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 7777:                       lockablenames => 'User preference to lock name',
 7778:                       selfassign    => 'Self-reportable affiliations',
 7779:              );
 7780:     my $showdom;
 7781:     if ($context eq 'cansearch') {
 7782:         $showdom = ' ('.$dom.')';
 7783:     }
 7784:     my $class = 'LC_left_item';
 7785:     if ($context eq 'statustocreate') {
 7786:         $class = 'LC_right_item';
 7787:     }
 7788:     my $css_class;
 7789:     if ($$rowtotal%2) {
 7790:         $css_class = 'LC_odd_row';
 7791:     }
 7792:     if ($customcss) {
 7793:         $css_class .= ' '.$customcss;
 7794:     }
 7795:     $css_class =~ s/^\s+//;
 7796:     if ($css_class) {
 7797:         $css_class = ' class="'.$css_class.'"';
 7798:     }
 7799:     if ($rowstyle) {
 7800:         $css_class .= ' style="'.$rowstyle.'"';
 7801:     }
 7802:     if ($onclick) {
 7803:         $onclick = 'onclick="'.$onclick.'" ';
 7804:     }
 7805:     my $output = '<tr'.$css_class.'>'.
 7806:                  '<td>'.$lt{$context}.$showdom.
 7807:                  '</td><td class="'.$class.'" colspan="2"><table>';
 7808:     my $rem;
 7809:     if (ref($types) eq 'ARRAY') {
 7810:         for (my $i=0; $i<@{$types}; $i++) {
 7811:             if (defined($usertypes->{$types->[$i]})) {
 7812:                 my $rem = $i%($numinrow);
 7813:                 if ($rem == 0) {
 7814:                     if ($i > 0) {
 7815:                         $output .= '</tr>';
 7816:                     }
 7817:                     $output .= '<tr>';
 7818:                 }
 7819:                 my $check = ' ';
 7820:                 if (ref($settings) eq 'HASH') {
 7821:                     if (ref($settings->{$context}) eq 'ARRAY') {
 7822:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 7823:                             $check = ' checked="checked" ';
 7824:                         }
 7825:                     } elsif ($context eq 'statustocreate') {
 7826:                         $check = ' checked="checked" ';
 7827:                     }
 7828:                 }
 7829:                 $output .= '<td class="LC_left_item">'.
 7830:                            '<span class="LC_nobreak"><label>'.
 7831:                            '<input type="checkbox" name="'.$context.'" '.
 7832:                            'value="'.$types->[$i].'"'.$check.$onclick.' />'.
 7833:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 7834:             }
 7835:         }
 7836:         $rem = @{$types}%($numinrow);
 7837:     }
 7838:     my $colsleft = $numinrow - $rem;
 7839:     if (($rem == 0) && (@{$types} > 0)) {
 7840:         $output .= '<tr>';
 7841:     }
 7842:     if ($colsleft > 1) {
 7843:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 7844:     } else {
 7845:         $output .= '<td class="LC_left_item">';
 7846:     }
 7847:     my $defcheck = ' ';
 7848:     if (ref($settings) eq 'HASH') {  
 7849:         if (ref($settings->{$context}) eq 'ARRAY') {
 7850:             if (grep(/^default$/,@{$settings->{$context}})) {
 7851:                 $defcheck = ' checked="checked" ';
 7852:             }
 7853:         } elsif ($context eq 'statustocreate') {
 7854:             $defcheck = ' checked="checked" ';
 7855:         }
 7856:     }
 7857:     $output .= '<span class="LC_nobreak"><label>'.
 7858:                '<input type="checkbox" name="'.$context.'" '.
 7859:                'value="default"'.$defcheck.$onclick.' />'.
 7860:                $othertitle.'</label></span></td>'.
 7861:                '</tr></table></td></tr>';
 7862:     return $output;
 7863: }
 7864: 
 7865: sub sorted_searchtitles {
 7866:     my %searchtitles = &Apache::lonlocal::texthash(
 7867:                          'uname' => 'username',
 7868:                          'lastname' => 'last name',
 7869:                          'lastfirst' => 'last name, first name',
 7870:                      );
 7871:     my @titleorder = ('uname','lastname','lastfirst');
 7872:     return (\%searchtitles,\@titleorder);
 7873: }
 7874: 
 7875: sub sorted_searchtypes {
 7876:     my %srchtypes_desc = (
 7877:                            exact    => 'is exact match',
 7878:                            contains => 'contains ..',
 7879:                            begins   => 'begins with ..',
 7880:                          );
 7881:     my @srchtypeorder = ('exact','begins','contains');
 7882:     return (\%srchtypes_desc,\@srchtypeorder);
 7883: }
 7884: 
 7885: sub usertype_update_row {
 7886:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 7887:     my $datatable;
 7888:     my $numinrow = 4;
 7889:     foreach my $type (@{$types}) {
 7890:         if (defined($usertypes->{$type})) {
 7891:             $$rownums ++;
 7892:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 7893:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 7894:                           '</td><td class="LC_left_item"><table>';
 7895:             for (my $i=0; $i<@{$fields}; $i++) {
 7896:                 my $rem = $i%($numinrow);
 7897:                 if ($rem == 0) {
 7898:                     if ($i > 0) {
 7899:                         $datatable .= '</tr>';
 7900:                     }
 7901:                     $datatable .= '<tr>';
 7902:                 }
 7903:                 my $check = ' ';
 7904:                 if (ref($settings) eq 'HASH') {
 7905:                     if (ref($settings->{'fields'}) eq 'HASH') {
 7906:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 7907:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 7908:                                 $check = ' checked="checked" ';
 7909:                             }
 7910:                         }
 7911:                     }
 7912:                 }
 7913: 
 7914:                 if ($i == @{$fields}-1) {
 7915:                     my $colsleft = $numinrow - $rem;
 7916:                     if ($colsleft > 1) {
 7917:                         $datatable .= '<td colspan="'.$colsleft.'">';
 7918:                     } else {
 7919:                         $datatable .= '<td>';
 7920:                     }
 7921:                 } else {
 7922:                     $datatable .= '<td>';
 7923:                 }
 7924:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7925:                               '<input type="checkbox" name="updateable_'.$type.
 7926:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 7927:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 7928:             }
 7929:             $datatable .= '</tr></table></td></tr>';
 7930:         }
 7931:     }
 7932:     return $datatable;
 7933: }
 7934: 
 7935: sub modify_login {
 7936:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 7937:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 7938:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 7939:     %title = ( coursecatalog => 'Display course catalog',
 7940:                adminmail => 'Display administrator E-mail address',
 7941:                helpdesk  => 'Display "Contact Helpdesk" link',
 7942:                newuser => 'Link for visitors to create a user account',
 7943:                loginheader => 'Log-in box header');
 7944:     @offon = ('off','on');
 7945:     if (ref($domconfig{login}) eq 'HASH') {
 7946:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 7947:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 7948:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 7949:             }
 7950:         }
 7951:     }
 7952:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 7953:                                            \%domconfig,\%loginhash);
 7954:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 7955:     foreach my $item (@toggles) {
 7956:         $loginhash{login}{$item} = $env{'form.'.$item};
 7957:     }
 7958:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 7959:     if (ref($colchanges{'login'}) eq 'HASH') {  
 7960:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 7961:                                          \%loginhash);
 7962:     }
 7963: 
 7964:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7965:     my %domservers = &Apache::lonnet::get_servers($dom);
 7966:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 7967:     if (keys(%servers) > 1) {
 7968:         foreach my $lonhost (keys(%servers)) {
 7969:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 7970:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 7971:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 7972:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 7973:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 7974:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7975:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7976:                         $changes{'loginvia'}{$lonhost} = 1;
 7977:                     } else {
 7978:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 7979:                         $changes{'loginvia'}{$lonhost} = 1;
 7980:                     }
 7981:                 } else {
 7982:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7983:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7984:                         $changes{'loginvia'}{$lonhost} = 1;
 7985:                     }
 7986:                 }
 7987:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 7988:                     foreach my $item (@loginvia_attribs) {
 7989:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 7990:                     }
 7991:                 } else {
 7992:                     foreach my $item (@loginvia_attribs) {
 7993:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 7994:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 7995:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 7996:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 7997:                                 $new = '/';
 7998:                             }
 7999:                         }
 8000:                         if (($item eq 'custompath') && 
 8001:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 8002:                             $new = '';
 8003:                         }
 8004:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 8005:                             $changes{'loginvia'}{$lonhost} = 1;
 8006:                         }
 8007:                         if ($item eq 'exempt') {
 8008:                             $new = &check_exempt_addresses($new);
 8009:                         }
 8010:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 8011:                     }
 8012:                 }
 8013:             } else {
 8014:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 8015:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 8016:                     $changes{'loginvia'}{$lonhost} = 1;
 8017:                     foreach my $item (@loginvia_attribs) {
 8018:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 8019:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 8020:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 8021:                                 $new = '/';
 8022:                             }
 8023:                         }
 8024:                         if (($item eq 'custompath') && 
 8025:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 8026:                             $new = '';
 8027:                         }
 8028:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 8029:                     }
 8030:                 }
 8031:             }
 8032:         }
 8033:     }
 8034: 
 8035:     my $servadm = $r->dir_config('lonAdmEMail');
 8036:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 8037:     if (ref($domconfig{'login'}) eq 'HASH') {
 8038:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 8039:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 8040:                 if ($lang eq 'nolang') {
 8041:                     push(@currlangs,$lang);
 8042:                 } elsif (defined($langchoices{$lang})) {
 8043:                     push(@currlangs,$lang);
 8044:                 } else {
 8045:                     next;
 8046:                 }
 8047:             }
 8048:         }
 8049:     }
 8050:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 8051:     if (@currlangs > 0) {
 8052:         foreach my $lang (@currlangs) {
 8053:             if (grep(/^\Q$lang\E$/,@delurls)) {
 8054:                 $changes{'helpurl'}{$lang} = 1;
 8055:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 8056:                 $changes{'helpurl'}{$lang} = 1;
 8057:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 8058:                 push(@newlangs,$lang);
 8059:             } else {
 8060:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 8061:             }
 8062:         }
 8063:     }
 8064:     unless (grep(/^nolang$/,@currlangs)) {
 8065:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 8066:             $changes{'helpurl'}{'nolang'} = 1;
 8067:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 8068:             push(@newlangs,'nolang');
 8069:         }
 8070:     }
 8071:     if ($env{'form.loginhelpurl_add_lang'}) {
 8072:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 8073:             ($env{'form.loginhelpurl_add_file.filename'})) {
 8074:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 8075:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 8076:         }
 8077:     }
 8078:     if ((@newlangs > 0) || ($addedfile)) {
 8079:         my $error;
 8080:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8081:         if ($configuserok eq 'ok') {
 8082:             if ($switchserver) {
 8083:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 8084:             } elsif ($author_ok eq 'ok') {
 8085:                 my @allnew = @newlangs;
 8086:                 if ($addedfile ne '') {
 8087:                     push(@allnew,$addedfile);
 8088:                 }
 8089:                 foreach my $lang (@allnew) {
 8090:                     my $formelem = 'loginhelpurl_'.$lang;
 8091:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 8092:                         $formelem = 'loginhelpurl_add_file';
 8093:                     }
 8094:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 8095:                                                                "help/$lang",'','',$newfile{$lang});
 8096:                     if ($result eq 'ok') {
 8097:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 8098:                         $changes{'helpurl'}{$lang} = 1;
 8099:                     } else {
 8100:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 8101:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 8102:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 8103:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 8104:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 8105:                         }
 8106:                     }
 8107:                 }
 8108:             } else {
 8109:                 $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);
 8110:             }
 8111:         } else {
 8112:             $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);
 8113:         }
 8114:         if ($error) {
 8115:             &Apache::lonnet::logthis($error);
 8116:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8117:         }
 8118:     }
 8119: 
 8120:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 8121:     if (ref($domconfig{'login'}) eq 'HASH') {
 8122:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 8123:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 8124:                 if ($domservers{$lonhost}) {
 8125:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 8126:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 8127:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
 8128:                     }
 8129:                 }
 8130:             }
 8131:         }
 8132:     }
 8133:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 8134:     foreach my $lonhost (sort(keys(%domservers))) {
 8135:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 8136:             $changes{'headtag'}{$lonhost} = 1;
 8137:         } else {
 8138:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 8139:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 8140:             }
 8141:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 8142:                 push(@newhosts,$lonhost);
 8143:             } elsif ($currheadtagurls{$lonhost}) {
 8144:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 8145:                 if ($currexempt{$lonhost}) {
 8146:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 8147:                         $changes{'headtag'}{$lonhost} = 1;
 8148:                     }
 8149:                 } elsif ($possexempt{$lonhost}) {
 8150:                     $changes{'headtag'}{$lonhost} = 1;
 8151:                 }
 8152:                 if ($possexempt{$lonhost}) {
 8153:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 8154:                 }
 8155:             }
 8156:         }
 8157:     }
 8158:     if (@newhosts) {
 8159:         my $error;
 8160:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8161:         if ($configuserok eq 'ok') {
 8162:             if ($switchserver) {
 8163:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 8164:             } elsif ($author_ok eq 'ok') {
 8165:                 foreach my $lonhost (@newhosts) {
 8166:                     my $formelem = 'loginheadtag_'.$lonhost;
 8167:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 8168:                                                                           "login/headtag/$lonhost",'','',
 8169:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 8170:                     if ($result eq 'ok') {
 8171:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 8172:                         $changes{'headtag'}{$lonhost} = 1;
 8173:                         if ($possexempt{$lonhost}) {
 8174:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 8175:                         }
 8176:                     } else {
 8177:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 8178:                                            $newheadtagurls{$lonhost},$result);
 8179:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 8180:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 8181:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 8182:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 8183:                         }
 8184:                     }
 8185:                 }
 8186:             } else {
 8187:                 $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);
 8188:             }
 8189:         } else {
 8190:             $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);
 8191:         }
 8192:         if ($error) {
 8193:             &Apache::lonnet::logthis($error);
 8194:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8195:         }
 8196:     }
 8197:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 8198: 
 8199:     my $defaulthelpfile = '/adm/loginproblems.html';
 8200:     my $defaulttext = &mt('Default in use');
 8201: 
 8202:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 8203:                                              $dom);
 8204:     if ($putresult eq 'ok') {
 8205:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 8206:         my %defaultchecked = (
 8207:                     'coursecatalog' => 'on',
 8208:                     'helpdesk'      => 'on',
 8209:                     'adminmail'     => 'off',
 8210:                     'newuser'       => 'off',
 8211:         );
 8212:         if (ref($domconfig{'login'}) eq 'HASH') {
 8213:             foreach my $item (@toggles) {
 8214:                 if ($defaultchecked{$item} eq 'on') { 
 8215:                     if (($domconfig{'login'}{$item} eq '0') &&
 8216:                         ($env{'form.'.$item} eq '1')) {
 8217:                         $changes{$item} = 1;
 8218:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 8219:                               $domconfig{'login'}{$item} eq '1') &&
 8220:                              ($env{'form.'.$item} eq '0')) {
 8221:                         $changes{$item} = 1;
 8222:                     }
 8223:                 } elsif ($defaultchecked{$item} eq 'off') {
 8224:                     if (($domconfig{'login'}{$item} eq '1') &&
 8225:                         ($env{'form.'.$item} eq '0')) {
 8226:                         $changes{$item} = 1;
 8227:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 8228:                               $domconfig{'login'}{$item} eq '0') &&
 8229:                              ($env{'form.'.$item} eq '1')) {
 8230:                         $changes{$item} = 1;
 8231:                     }
 8232:                 }
 8233:             }
 8234:         }
 8235:         if (keys(%changes) > 0 || $colchgtext) {
 8236:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8237:             if (ref($lastactref) eq 'HASH') {
 8238:                 $lastactref->{'domainconfig'} = 1;
 8239:             }
 8240:             $resulttext = &mt('Changes made:').'<ul>';
 8241:             foreach my $item (sort(keys(%changes))) {
 8242:                 if ($item eq 'loginvia') {
 8243:                     if (ref($changes{$item}) eq 'HASH') {
 8244:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 8245:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 8246:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 8247:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 8248:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 8249:                                     $protocol = 'http' if ($protocol ne 'https');
 8250:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 8251: 
 8252:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 8253:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 8254:                                     } else {
 8255:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 8256:                                     }
 8257:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 8258:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 8259:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 8260:                                     }
 8261:                                     $resulttext .= '</li>';
 8262:                                 } else {
 8263:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 8264:                                 }
 8265:                             } else {
 8266:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 8267:                             }
 8268:                         }
 8269:                         $resulttext .= '</ul></li>';
 8270:                     }
 8271:                 } elsif ($item eq 'helpurl') {
 8272:                     if (ref($changes{$item}) eq 'HASH') {
 8273:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 8274:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 8275:                                 my ($chg,$link);
 8276:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 8277:                                 if ($lang eq 'nolang') {
 8278:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 8279:                                 } else {
 8280:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 8281:                                 }
 8282:                                 $resulttext .= '<li>'.$chg.'</li>';
 8283:                             } else {
 8284:                                 my $chg;
 8285:                                 if ($lang eq 'nolang') {
 8286:                                     $chg = &mt('custom log-in help file for no preferred language');
 8287:                                 } else {
 8288:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 8289:                                 }
 8290:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 8291:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 8292:                                                       '?inhibitmenu=yes',$chg,600,500).
 8293:                                                '</li>';
 8294:                             }
 8295:                         }
 8296:                     }
 8297:                 } elsif ($item eq 'headtag') {
 8298:                     if (ref($changes{$item}) eq 'HASH') {
 8299:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 8300:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 8301:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 8302:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 8303:                                 $resulttext .= '<li><a href="'.
 8304:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 8305:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 8306:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 8307:                                 if ($possexempt{$lonhost}) {
 8308:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 8309:                                 } else {
 8310:                                     $resulttext .= &mt('included for any client IP');
 8311:                                 }
 8312:                                 $resulttext .= '</li>';
 8313:                             }
 8314:                         }
 8315:                     }
 8316:                 } elsif ($item eq 'captcha') {
 8317:                     if (ref($loginhash{'login'}) eq 'HASH') {
 8318:                         my $chgtxt;
 8319:                         if ($loginhash{'login'}{$item} eq 'notused') {
 8320:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 8321:                         } else {
 8322:                             my %captchas = &captcha_phrases();
 8323:                             if ($captchas{$loginhash{'login'}{$item}}) {
 8324:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 8325:                             } else {
 8326:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 8327:                             }
 8328:                         }
 8329:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 8330:                     }
 8331:                 } elsif ($item eq 'recaptchakeys') {
 8332:                     if (ref($loginhash{'login'}) eq 'HASH') {
 8333:                         my ($privkey,$pubkey);
 8334:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 8335:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 8336:                             $privkey = $loginhash{'login'}{$item}{'private'};
 8337:                         }
 8338:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 8339:                         if (!$pubkey) {
 8340:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 8341:                         } else {
 8342:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 8343:                         }
 8344:                         if (!$privkey) {
 8345:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 8346:                         } else {
 8347:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 8348:                         }
 8349:                         $chgtxt .= '</ul>';
 8350:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 8351:                     }
 8352:                 } elsif ($item eq 'recaptchaversion') {
 8353:                     if (ref($loginhash{'login'}) eq 'HASH') {
 8354:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
 8355:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
 8356:                                            '</li>';
 8357:                         }
 8358:                     }
 8359:                 } else {
 8360:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 8361:                 }
 8362:             }
 8363:             $resulttext .= $colchgtext.'</ul>';
 8364:         } else {
 8365:             $resulttext = &mt('No changes made to log-in page settings');
 8366:         }
 8367:     } else {
 8368:         $resulttext = '<span class="LC_error">'.
 8369: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8370:     }
 8371:     if ($errors) {
 8372:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 8373:                        $errors.'</ul>';
 8374:     }
 8375:     return $resulttext;
 8376: }
 8377: 
 8378: sub check_exempt_addresses {
 8379:     my ($iplist) = @_;
 8380:     $iplist =~ s/^\s+//;
 8381:     $iplist =~ s/\s+$//;
 8382:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 8383:     my (@okips,$new);
 8384:     foreach my $ip (@poss_ips) {
 8385:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 8386:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 8387:                 push(@okips,$ip);
 8388:             }
 8389:         }
 8390:     }
 8391:     if (@okips > 0) {
 8392:         $new = join(',',@okips);
 8393:     } else {
 8394:         $new = '';
 8395:     }
 8396:     return $new;
 8397: }
 8398: 
 8399: sub color_font_choices {
 8400:     my %choices =
 8401:         &Apache::lonlocal::texthash (
 8402:             img => "Header",
 8403:             bgs => "Background colors",
 8404:             links => "Link colors",
 8405:             images => "Images",
 8406:             font => "Font color",
 8407:             fontmenu => "Font menu",
 8408:             pgbg => "Page",
 8409:             tabbg => "Header",
 8410:             sidebg => "Border",
 8411:             link => "Link",
 8412:             alink => "Active link",
 8413:             vlink => "Visited link",
 8414:         );
 8415:     return %choices;
 8416: }
 8417: 
 8418: sub modify_rolecolors {
 8419:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 8420:     my ($resulttext,%rolehash);
 8421:     $rolehash{'rolecolors'} = {};
 8422:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 8423:         if ($domconfig{'rolecolors'} eq '') {
 8424:             $domconfig{'rolecolors'} = {};
 8425:         }
 8426:     }
 8427:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 8428:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 8429:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 8430:                                              $dom);
 8431:     if ($putresult eq 'ok') {
 8432:         if (keys(%changes) > 0) {
 8433:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8434:             if (ref($lastactref) eq 'HASH') {
 8435:                 $lastactref->{'domainconfig'} = 1;
 8436:             }
 8437:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 8438:                                              $rolehash{'rolecolors'});
 8439:         } else {
 8440:             $resulttext = &mt('No changes made to default color schemes');
 8441:         }
 8442:     } else {
 8443:         $resulttext = '<span class="LC_error">'.
 8444: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8445:     }
 8446:     if ($errors) {
 8447:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 8448:                        $errors.'</ul>';
 8449:     }
 8450:     return $resulttext;
 8451: }
 8452: 
 8453: sub modify_colors {
 8454:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 8455:     my (%changes,%choices);
 8456:     my @bgs;
 8457:     my @links = ('link','alink','vlink');
 8458:     my @logintext;
 8459:     my @images;
 8460:     my $servadm = $r->dir_config('lonAdmEMail');
 8461:     my $errors;
 8462:     my %defaults;
 8463:     foreach my $role (@{$roles}) {
 8464:         if ($role eq 'login') {
 8465:             %choices = &login_choices();
 8466:             @logintext = ('textcol','bgcol');
 8467:         } else {
 8468:             %choices = &color_font_choices();
 8469:         }
 8470:         if ($role eq 'login') {
 8471:             @images = ('img','logo','domlogo','login');
 8472:             @bgs = ('pgbg','mainbg','sidebg');
 8473:         } else {
 8474:             @images = ('img');
 8475:             @bgs = ('pgbg','tabbg','sidebg');
 8476:         }
 8477:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 8478:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 8479:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 8480:         }
 8481:         if ($role eq 'login') {
 8482:             foreach my $item (@logintext) {
 8483:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8484:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8485:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8486:                 }
 8487:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 8488:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8489:                 }
 8490:             }
 8491:         } else {
 8492:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 8493:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 8494:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 8495:             }
 8496:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 8497:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 8498:             }
 8499:         }
 8500:         foreach my $item (@bgs) {
 8501:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8502:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8503:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8504:             }
 8505:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 8506:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8507:             }
 8508:         }
 8509:         foreach my $item (@links) {
 8510:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8511:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8512:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8513:             }
 8514:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 8515:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8516:             }
 8517:         }
 8518:         my ($configuserok,$author_ok,$switchserver) = 
 8519:             &config_check($dom,$confname,$servadm);
 8520:         my ($width,$height) = &thumb_dimensions();
 8521:         if (ref($domconfig->{$role}) ne 'HASH') {
 8522:             $domconfig->{$role} = {};
 8523:         }
 8524:         foreach my $img (@images) {
 8525:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 8526:                 if (defined($env{'form.login_showlogo_'.$img})) {
 8527:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 8528:                 } else { 
 8529:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 8530:                 }
 8531:             } 
 8532: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 8533: 		 && !defined($domconfig->{$role}{$img})
 8534: 		 && !$env{'form.'.$role.'_del_'.$img}
 8535: 		 && $env{'form.'.$role.'_import_'.$img}) {
 8536: 		# import the old configured image from the .tab setting
 8537: 		# if they haven't provided a new one 
 8538: 		$domconfig->{$role}{$img} = 
 8539: 		    $env{'form.'.$role.'_import_'.$img};
 8540: 	    }
 8541:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 8542:                 my $error;
 8543:                 if ($configuserok eq 'ok') {
 8544:                     if ($switchserver) {
 8545:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 8546:                     } else {
 8547:                         if ($author_ok eq 'ok') {
 8548:                             my ($result,$logourl) = 
 8549:                                 &publishlogo($r,'upload',$role.'_'.$img,
 8550:                                            $dom,$confname,$img,$width,$height);
 8551:                             if ($result eq 'ok') {
 8552:                                 $confhash->{$role}{$img} = $logourl;
 8553:                                 $changes{$role}{'images'}{$img} = 1;
 8554:                             } else {
 8555:                                 $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);
 8556:                             }
 8557:                         } else {
 8558:                             $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);
 8559:                         }
 8560:                     }
 8561:                 } else {
 8562:                     $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);
 8563:                 }
 8564:                 if ($error) {
 8565:                     &Apache::lonnet::logthis($error);
 8566:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8567:                 }
 8568:             } elsif ($domconfig->{$role}{$img} ne '') {
 8569:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 8570:                     my $error;
 8571:                     if ($configuserok eq 'ok') {
 8572: # is confname an author?
 8573:                         if ($switchserver eq '') {
 8574:                             if ($author_ok eq 'ok') {
 8575:                                 my ($result,$logourl) = 
 8576:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 8577:                                             $dom,$confname,$img,$width,$height);
 8578:                                 if ($result eq 'ok') {
 8579:                                     $confhash->{$role}{$img} = $logourl;
 8580: 				    $changes{$role}{'images'}{$img} = 1;
 8581:                                 }
 8582:                             }
 8583:                         }
 8584:                     }
 8585:                 }
 8586:             }
 8587:         }
 8588:         if (ref($domconfig) eq 'HASH') {
 8589:             if (ref($domconfig->{$role}) eq 'HASH') {
 8590:                 foreach my $img (@images) {
 8591:                     if ($domconfig->{$role}{$img} ne '') {
 8592:                         if ($env{'form.'.$role.'_del_'.$img}) {
 8593:                             $confhash->{$role}{$img} = '';
 8594:                             $changes{$role}{'images'}{$img} = 1;
 8595:                         } else {
 8596:                             if ($confhash->{$role}{$img} eq '') {
 8597:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 8598:                             }
 8599:                         }
 8600:                     } else {
 8601:                         if ($env{'form.'.$role.'_del_'.$img}) {
 8602:                             $confhash->{$role}{$img} = '';
 8603:                             $changes{$role}{'images'}{$img} = 1;
 8604:                         } 
 8605:                     }
 8606:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 8607:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 8608:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 8609:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 8610:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 8611:                             }
 8612:                         } else {
 8613:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 8614:                                 $changes{$role}{'showlogo'}{$img} = 1;
 8615:                             }
 8616:                         }
 8617:                     }
 8618:                 }
 8619:                 if ($domconfig->{$role}{'font'} ne '') {
 8620:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 8621:                         $changes{$role}{'font'} = 1;
 8622:                     }
 8623:                 } else {
 8624:                     if ($confhash->{$role}{'font'}) {
 8625:                         $changes{$role}{'font'} = 1;
 8626:                     }
 8627:                 }
 8628:                 if ($role ne 'login') {
 8629:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 8630:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 8631:                             $changes{$role}{'fontmenu'} = 1;
 8632:                         }
 8633:                     } else {
 8634:                         if ($confhash->{$role}{'fontmenu'}) {
 8635:                             $changes{$role}{'fontmenu'} = 1;
 8636:                         }
 8637:                     }
 8638:                 }
 8639:                 foreach my $item (@bgs) {
 8640:                     if ($domconfig->{$role}{$item} ne '') {
 8641:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8642:                             $changes{$role}{'bgs'}{$item} = 1;
 8643:                         } 
 8644:                     } else {
 8645:                         if ($confhash->{$role}{$item}) {
 8646:                             $changes{$role}{'bgs'}{$item} = 1;
 8647:                         }
 8648:                     }
 8649:                 }
 8650:                 foreach my $item (@links) {
 8651:                     if ($domconfig->{$role}{$item} ne '') {
 8652:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8653:                             $changes{$role}{'links'}{$item} = 1;
 8654:                         }
 8655:                     } else {
 8656:                         if ($confhash->{$role}{$item}) {
 8657:                             $changes{$role}{'links'}{$item} = 1;
 8658:                         }
 8659:                     }
 8660:                 }
 8661:                 foreach my $item (@logintext) {
 8662:                     if ($domconfig->{$role}{$item} ne '') {
 8663:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8664:                             $changes{$role}{'logintext'}{$item} = 1;
 8665:                         }
 8666:                     } else {
 8667:                         if ($confhash->{$role}{$item}) {
 8668:                             $changes{$role}{'logintext'}{$item} = 1;
 8669:                         }
 8670:                     }
 8671:                 }
 8672:             } else {
 8673:                 &default_change_checker($role,\@images,\@links,\@bgs,
 8674:                                         \@logintext,$confhash,\%changes); 
 8675:             }
 8676:         } else {
 8677:             &default_change_checker($role,\@images,\@links,\@bgs,
 8678:                                     \@logintext,$confhash,\%changes); 
 8679:         }
 8680:     }
 8681:     return ($errors,%changes);
 8682: }
 8683: 
 8684: sub config_check {
 8685:     my ($dom,$confname,$servadm) = @_;
 8686:     my ($configuserok,$author_ok,$switchserver,%currroles);
 8687:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 8688:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 8689:                                                    $confname,$servadm);
 8690:     if ($configuserok eq 'ok') {
 8691:         $switchserver = &check_switchserver($dom,$confname);
 8692:         if ($switchserver eq '') {
 8693:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 8694:         }
 8695:     }
 8696:     return ($configuserok,$author_ok,$switchserver);
 8697: }
 8698: 
 8699: sub default_change_checker {
 8700:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 8701:     foreach my $item (@{$links}) {
 8702:         if ($confhash->{$role}{$item}) {
 8703:             $changes->{$role}{'links'}{$item} = 1;
 8704:         }
 8705:     }
 8706:     foreach my $item (@{$bgs}) {
 8707:         if ($confhash->{$role}{$item}) {
 8708:             $changes->{$role}{'bgs'}{$item} = 1;
 8709:         }
 8710:     }
 8711:     foreach my $item (@{$logintext}) {
 8712:         if ($confhash->{$role}{$item}) {
 8713:             $changes->{$role}{'logintext'}{$item} = 1;
 8714:         }
 8715:     }
 8716:     foreach my $img (@{$images}) {
 8717:         if ($env{'form.'.$role.'_del_'.$img}) {
 8718:             $confhash->{$role}{$img} = '';
 8719:             $changes->{$role}{'images'}{$img} = 1;
 8720:         }
 8721:         if ($role eq 'login') {
 8722:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 8723:                 $changes->{$role}{'showlogo'}{$img} = 1;
 8724:             }
 8725:         }
 8726:     }
 8727:     if ($confhash->{$role}{'font'}) {
 8728:         $changes->{$role}{'font'} = 1;
 8729:     }
 8730: }
 8731: 
 8732: sub display_colorchgs {
 8733:     my ($dom,$changes,$roles,$confhash) = @_;
 8734:     my (%choices,$resulttext);
 8735:     if (!grep(/^login$/,@{$roles})) {
 8736:         $resulttext = &mt('Changes made:').'<br />';
 8737:     }
 8738:     foreach my $role (@{$roles}) {
 8739:         if ($role eq 'login') {
 8740:             %choices = &login_choices();
 8741:         } else {
 8742:             %choices = &color_font_choices();
 8743:         }
 8744:         if (ref($changes->{$role}) eq 'HASH') {
 8745:             if ($role ne 'login') {
 8746:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 8747:             }
 8748:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 8749:                 if ($role ne 'login') {
 8750:                     $resulttext .= '<ul>';
 8751:                 }
 8752:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 8753:                     if ($role ne 'login') {
 8754:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 8755:                     }
 8756:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 8757:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 8758:                             if ($confhash->{$role}{$key}{$item}) {
 8759:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 8760:                             } else {
 8761:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 8762:                             }
 8763:                         } elsif ($confhash->{$role}{$item} eq '') {
 8764:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 8765:                         } else {
 8766:                             my $newitem = $confhash->{$role}{$item};
 8767:                             if ($key eq 'images') {
 8768:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" style="vertical-align: bottom" />';
 8769:                             }
 8770:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 8771:                         }
 8772:                     }
 8773:                     if ($role ne 'login') {
 8774:                         $resulttext .= '</ul></li>';
 8775:                     }
 8776:                 } else {
 8777:                     if ($confhash->{$role}{$key} eq '') {
 8778:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 8779:                     } else {
 8780:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 8781:                     }
 8782:                 }
 8783:                 if ($role ne 'login') {
 8784:                     $resulttext .= '</ul>';
 8785:                 }
 8786:             }
 8787:         }
 8788:     }
 8789:     return $resulttext;
 8790: }
 8791: 
 8792: sub thumb_dimensions {
 8793:     return ('200','50');
 8794: }
 8795: 
 8796: sub check_dimensions {
 8797:     my ($inputfile) = @_;
 8798:     my ($fullwidth,$fullheight);
 8799:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 8800:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 8801:             my $imageinfo = <PIPE>;
 8802:             if (!close(PIPE)) {
 8803:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 8804:             }
 8805:             chomp($imageinfo);
 8806:             my ($fullsize) = 
 8807:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 8808:             if ($fullsize) {
 8809:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 8810:             }
 8811:         }
 8812:     }
 8813:     return ($fullwidth,$fullheight);
 8814: }
 8815: 
 8816: sub check_configuser {
 8817:     my ($uhome,$dom,$confname,$servadm) = @_;
 8818:     my ($configuserok,%currroles);
 8819:     if ($uhome eq 'no_host') {
 8820:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 8821:         my $configpass = &LONCAPA::Enrollment::create_password();
 8822:         $configuserok = 
 8823:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 8824:                              $configpass,'','','','','',undef,$servadm);
 8825:     } else {
 8826:         $configuserok = 'ok';
 8827:         %currroles = 
 8828:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 8829:     }
 8830:     return ($configuserok,%currroles);
 8831: }
 8832: 
 8833: sub check_authorstatus {
 8834:     my ($dom,$confname,%currroles) = @_;
 8835:     my $author_ok;
 8836:     if (!$currroles{':'.$dom.':au'}) {
 8837:         my $start = time;
 8838:         my $end = 0;
 8839:         $author_ok = 
 8840:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 8841:                                         'au',$end,$start,'','','domconfig');
 8842:     } else {
 8843:         $author_ok = 'ok';
 8844:     }
 8845:     return $author_ok;
 8846: }
 8847: 
 8848: sub publishlogo {
 8849:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 8850:     my ($output,$fname,$logourl,$madethumb);
 8851:     if ($action eq 'upload') {
 8852:         $fname=$env{'form.'.$formname.'.filename'};
 8853:         chop($env{'form.'.$formname});
 8854:     } else {
 8855:         ($fname) = ($formname =~ /([^\/]+)$/);
 8856:     }
 8857:     if ($savefileas ne '') {
 8858:         $fname = $savefileas;
 8859:     }
 8860:     $fname=&Apache::lonnet::clean_filename($fname);
 8861: # See if there is anything left
 8862:     unless ($fname) { return ('error: no uploaded file'); }
 8863:     $fname="$subdir/$fname";
 8864:     my $docroot=$r->dir_config('lonDocRoot');
 8865:     my $filepath="$docroot/priv";
 8866:     my $relpath = "$dom/$confname";
 8867:     my ($fnamepath,$file,$fetchthumb);
 8868:     $file=$fname;
 8869:     if ($fname=~m|/|) {
 8870:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 8871:     }
 8872:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 8873:     my $count;
 8874:     for ($count=5;$count<=$#parts;$count++) {
 8875:         $filepath.="/$parts[$count]";
 8876:         if ((-e $filepath)!=1) {
 8877:             mkdir($filepath,02770);
 8878:         }
 8879:     }
 8880:     # Check for bad extension and disallow upload
 8881:     if ($file=~/\.(\w+)$/ &&
 8882:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 8883:         $output = 
 8884:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 8885:     } elsif ($file=~/\.(\w+)$/ &&
 8886:         !defined(&Apache::loncommon::fileembstyle($1))) {
 8887:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 8888:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 8889:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 8890:     } elsif (-d "$filepath/$file") {
 8891:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 8892:     } else {
 8893:         my $source = $filepath.'/'.$file;
 8894:         my $logfile;
 8895:         if (!open($logfile,">>$source".'.log')) {
 8896:             return (&mt('No write permission to Authoring Space'));
 8897:         }
 8898:         print $logfile
 8899: "\n================= Publish ".localtime()." ================\n".
 8900: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 8901: # Save the file
 8902:         if (!open(FH,'>'.$source)) {
 8903:             &Apache::lonnet::logthis('Failed to create '.$source);
 8904:             return (&mt('Failed to create file'));
 8905:         }
 8906:         if ($action eq 'upload') {
 8907:             if (!print FH ($env{'form.'.$formname})) {
 8908:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 8909:                 return (&mt('Failed to write file'));
 8910:             }
 8911:         } else {
 8912:             my $original = &Apache::lonnet::filelocation('',$formname);
 8913:             if(!copy($original,$source)) {
 8914:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 8915:                 return (&mt('Failed to write file'));
 8916:             }
 8917:         }
 8918:         close(FH);
 8919:         chmod(0660, $source); # Permissions to rw-rw---.
 8920: 
 8921:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 8922:         my $copyfile=$targetdir.'/'.$file;
 8923: 
 8924:         my @parts=split(/\//,$targetdir);
 8925:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 8926:         for (my $count=5;$count<=$#parts;$count++) {
 8927:             $path.="/$parts[$count]";
 8928:             if (!-e $path) {
 8929:                 print $logfile "\nCreating directory ".$path;
 8930:                 mkdir($path,02770);
 8931:             }
 8932:         }
 8933:         my $versionresult;
 8934:         if (-e $copyfile) {
 8935:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 8936:         } else {
 8937:             $versionresult = 'ok';
 8938:         }
 8939:         if ($versionresult eq 'ok') {
 8940:             if (copy($source,$copyfile)) {
 8941:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 8942:                 $output = 'ok';
 8943:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 8944:                 push(@{$modified_urls},[$copyfile,$source]);
 8945:                 my $metaoutput = 
 8946:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 8947:                 unless ($registered_cleanup) {
 8948:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8949:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8950:                     $registered_cleanup=1;
 8951:                 }
 8952:             } else {
 8953:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 8954:                 $output = &mt('Failed to copy file to RES space').", $!";
 8955:             }
 8956:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 8957:                 my $inputfile = $filepath.'/'.$file;
 8958:                 my $outfile = $filepath.'/'.'tn-'.$file;
 8959:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 8960:                 if ($fullwidth ne '' && $fullheight ne '') { 
 8961:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 8962:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 8963:                         system("convert -sample $thumbsize $inputfile $outfile");
 8964:                         chmod(0660, $filepath.'/tn-'.$file);
 8965:                         if (-e $outfile) {
 8966:                             my $copyfile=$targetdir.'/tn-'.$file;
 8967:                             if (copy($outfile,$copyfile)) {
 8968:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 8969:                                 my $thumb_metaoutput = 
 8970:                                     &write_metadata($dom,$confname,$formname,
 8971:                                                     $targetdir,'tn-'.$file,$logfile);
 8972:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 8973:                                 unless ($registered_cleanup) {
 8974:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8975:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8976:                                     $registered_cleanup=1;
 8977:                                 }
 8978:                                 $madethumb = 1;
 8979:                             } else {
 8980:                                 print $logfile "\nUnable to write ".$copyfile.
 8981:                                                ':'.$!."\n";
 8982:                             }
 8983:                         }
 8984:                     }
 8985:                 }
 8986:             }
 8987:         } else {
 8988:             $output = $versionresult;
 8989:         }
 8990:     }
 8991:     return ($output,$logourl,$madethumb);
 8992: }
 8993: 
 8994: sub logo_versioning {
 8995:     my ($targetdir,$file,$logfile) = @_;
 8996:     my $target = $targetdir.'/'.$file;
 8997:     my ($maxversion,$fn,$extn,$output);
 8998:     $maxversion = 0;
 8999:     if ($file =~ /^(.+)\.(\w+)$/) {
 9000:         $fn=$1;
 9001:         $extn=$2;
 9002:     }
 9003:     opendir(DIR,$targetdir);
 9004:     while (my $filename=readdir(DIR)) {
 9005:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 9006:             $maxversion=($1>$maxversion)?$1:$maxversion;
 9007:         }
 9008:     }
 9009:     $maxversion++;
 9010:     print $logfile "\nCreating old version ".$maxversion."\n";
 9011:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 9012:     if (copy($target,$copyfile)) {
 9013:         print $logfile "Copied old target to ".$copyfile."\n";
 9014:         $copyfile=$copyfile.'.meta';
 9015:         if (copy($target.'.meta',$copyfile)) {
 9016:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 9017:             $output = 'ok';
 9018:         } else {
 9019:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 9020:             $output = &mt('Failed to copy old meta').", $!, ";
 9021:         }
 9022:     } else {
 9023:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 9024:         $output = &mt('Failed to copy old target').", $!, ";
 9025:     }
 9026:     return $output;
 9027: }
 9028: 
 9029: sub write_metadata {
 9030:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 9031:     my (%metadatafields,%metadatakeys,$output);
 9032:     $metadatafields{'title'}=$formname;
 9033:     $metadatafields{'creationdate'}=time;
 9034:     $metadatafields{'lastrevisiondate'}=time;
 9035:     $metadatafields{'copyright'}='public';
 9036:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 9037:                                          $env{'user.domain'};
 9038:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 9039:     $metadatafields{'domain'}=$dom;
 9040:     {
 9041:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 9042:         my $mfh;
 9043:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 9044:             foreach (sort(keys(%metadatafields))) {
 9045:                 unless ($_=~/\./) {
 9046:                     my $unikey=$_;
 9047:                     $unikey=~/^([A-Za-z]+)/;
 9048:                     my $tag=$1;
 9049:                     $tag=~tr/A-Z/a-z/;
 9050:                     print $mfh "\n\<$tag";
 9051:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 9052:                         my $value=$metadatafields{$unikey.'.'.$_};
 9053:                         $value=~s/\"/\'\'/g;
 9054:                         print $mfh ' '.$_.'="'.$value.'"';
 9055:                     }
 9056:                     print $mfh '>'.
 9057:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 9058:                             .'</'.$tag.'>';
 9059:                 }
 9060:             }
 9061:             $output = 'ok';
 9062:             print $logfile "\nWrote metadata";
 9063:             close($mfh);
 9064:         } else {
 9065:             print $logfile "\nFailed to open metadata file";
 9066:             $output = &mt('Could not write metadata');
 9067:         }
 9068:     }
 9069:     return $output;
 9070: }
 9071: 
 9072: sub notifysubscribed {
 9073:     foreach my $targetsource (@{$modified_urls}){
 9074:         next unless (ref($targetsource) eq 'ARRAY');
 9075:         my ($target,$source)=@{$targetsource};
 9076:         if ($source ne '') {
 9077:             if (open(my $logfh,'>>'.$source.'.log')) {
 9078:                 print $logfh "\nCleanup phase: Notifications\n";
 9079:                 my @subscribed=&subscribed_hosts($target);
 9080:                 foreach my $subhost (@subscribed) {
 9081:                     print $logfh "\nNotifying host ".$subhost.':';
 9082:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 9083:                     print $logfh $reply;
 9084:                 }
 9085:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 9086:                 foreach my $subhost (@subscribedmeta) {
 9087:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 9088:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 9089:                                                         $subhost);
 9090:                     print $logfh $reply;
 9091:                 }
 9092:                 print $logfh "\n============ Done ============\n";
 9093:                 close($logfh);
 9094:             }
 9095:         }
 9096:     }
 9097:     return OK;
 9098: }
 9099: 
 9100: sub subscribed_hosts {
 9101:     my ($target) = @_;
 9102:     my @subscribed;
 9103:     if (open(my $fh,"<$target.subscription")) {
 9104:         while (my $subline=<$fh>) {
 9105:             if ($subline =~ /^($match_lonid):/) {
 9106:                 my $host = $1;
 9107:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 9108:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 9109:                         push(@subscribed,$host);
 9110:                     }
 9111:                 }
 9112:             }
 9113:         }
 9114:     }
 9115:     return @subscribed;
 9116: }
 9117: 
 9118: sub check_switchserver {
 9119:     my ($dom,$confname) = @_;
 9120:     my ($allowed,$switchserver);
 9121:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 9122:     if ($home eq 'no_host') {
 9123:         $home = &Apache::lonnet::domain($dom,'primary');
 9124:     }
 9125:     my @ids=&Apache::lonnet::current_machine_ids();
 9126:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 9127:     if (!$allowed) {
 9128: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 9129:     }
 9130:     return $switchserver;
 9131: }
 9132: 
 9133: sub modify_quotas {
 9134:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 9135:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 9136:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 9137:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 9138:         $validationfieldsref);
 9139:     if ($action eq 'quotas') {
 9140:         $context = 'tools'; 
 9141:     } else {
 9142:         $context = $action;
 9143:     }
 9144:     if ($context eq 'requestcourses') {
 9145:         @usertools = ('official','unofficial','community','textbook','placement');
 9146:         @options =('norequest','approval','validate','autolimit');
 9147:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 9148:         %titles = &courserequest_titles();
 9149:         $toolregexp = join('|',@usertools);
 9150:         %conditions = &courserequest_conditions();
 9151:         $confname = $dom.'-domainconfig';
 9152:         my $servadm = $r->dir_config('lonAdmEMail');
 9153:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9154:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 9155:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 9156:     } elsif ($context eq 'requestauthor') {
 9157:         @usertools = ('author');
 9158:         %titles = &authorrequest_titles();
 9159:     } else {
 9160:         @usertools = ('aboutme','blog','webdav','portfolio');
 9161:         %titles = &tool_titles();
 9162:     }
 9163:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9164:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9165:     foreach my $key (keys(%env)) {
 9166:         if ($context eq 'requestcourses') {
 9167:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 9168:                 my $item = $1;
 9169:                 my $type = $2;
 9170:                 if ($type =~ /^limit_(.+)/) {
 9171:                     $limithash{$item}{$1} = $env{$key};
 9172:                 } else {
 9173:                     $confhash{$item}{$type} = $env{$key};
 9174:                 }
 9175:             }
 9176:         } elsif ($context eq 'requestauthor') {
 9177:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 9178:                 $confhash{$1} = $env{$key};
 9179:             }
 9180:         } else {
 9181:             if ($key =~ /^form\.quota_(.+)$/) {
 9182:                 $confhash{'defaultquota'}{$1} = $env{$key};
 9183:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 9184:                 $confhash{'authorquota'}{$1} = $env{$key};
 9185:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 9186:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 9187:             }
 9188:         }
 9189:     }
 9190:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 9191:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 9192:         @approvalnotify = sort(@approvalnotify);
 9193:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 9194:         my @crstypes = ('official','unofficial','community','textbook','placement');
 9195:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 9196:         foreach my $type (@hasuniquecode) {
 9197:             if (grep(/^\Q$type\E$/,@crstypes)) {
 9198:                 $confhash{'uniquecode'}{$type} = 1;
 9199:             }
 9200:         }
 9201:         my (%newbook,%allpos);
 9202:         if ($context eq 'requestcourses') {
 9203:             foreach my $type ('textbooks','templates') {
 9204:                 @{$allpos{$type}} = (); 
 9205:                 my $invalid;
 9206:                 if ($type eq 'textbooks') {
 9207:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 9208:                 } else {
 9209:                     $invalid = &mt('Invalid LON-CAPA course for template');
 9210:                 }
 9211:                 if ($env{'form.'.$type.'_addbook'}) {
 9212:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 9213:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 9214:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 9215:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 9216:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 9217:                         } else {
 9218:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 9219:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 9220:                             $position =~ s/\D+//g;
 9221:                             if ($position ne '') {
 9222:                                 $allpos{$type}[$position] = $newbook{$type};
 9223:                             }
 9224:                         }
 9225:                     } else {
 9226:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 9227:                     }
 9228:                 }
 9229:             } 
 9230:         }
 9231:         if (ref($domconfig{$action}) eq 'HASH') {
 9232:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 9233:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 9234:                     $changes{'notify'}{'approval'} = 1;
 9235:                 }
 9236:             } else {
 9237:                 if ($confhash{'notify'}{'approval'}) {
 9238:                     $changes{'notify'}{'approval'} = 1;
 9239:                 }
 9240:             }
 9241:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 9242:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 9243:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 9244:                         unless ($confhash{'uniquecode'}{$crstype}) {
 9245:                             $changes{'uniquecode'} = 1;
 9246:                         }
 9247:                     }
 9248:                     unless ($changes{'uniquecode'}) {
 9249:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 9250:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 9251:                                 $changes{'uniquecode'} = 1;
 9252:                             }
 9253:                         }
 9254:                     }
 9255:                } else {
 9256:                    $changes{'uniquecode'} = 1;
 9257:                }
 9258:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 9259:                 $changes{'uniquecode'} = 1;
 9260:             }
 9261:             if ($context eq 'requestcourses') {
 9262:                 foreach my $type ('textbooks','templates') {
 9263:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 9264:                         my %deletions;
 9265:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 9266:                         if (@todelete) {
 9267:                             map { $deletions{$_} = 1; } @todelete;
 9268:                         }
 9269:                         my %imgdeletions;
 9270:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 9271:                         if (@todeleteimages) {
 9272:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 9273:                         }
 9274:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 9275:                         for (my $i=0; $i<=$maxnum; $i++) {
 9276:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 9277:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 9278:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 9279:                                 if ($deletions{$key}) {
 9280:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 9281:                                         #FIXME need to obsolete item in RES space
 9282:                                     }
 9283:                                     next;
 9284:                                 } else {
 9285:                                     my $newpos = $env{'form.'.$itemid};
 9286:                                     $newpos =~ s/\D+//g;
 9287:                                     foreach my $item ('subject','title','publisher','author') {
 9288:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9289:                                                  ($type eq 'templates'));
 9290:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 9291:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 9292:                                             $changes{$type}{$key} = 1;
 9293:                                         }
 9294:                                     }
 9295:                                     $allpos{$type}[$newpos] = $key;
 9296:                                 }
 9297:                                 if ($imgdeletions{$key}) {
 9298:                                     $changes{$type}{$key} = 1;
 9299:                                     #FIXME need to obsolete item in RES space
 9300:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 9301:                                     my ($cdom,$cnum) = split(/_/,$key);
 9302:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
 9303:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
 9304:                                     } else {
 9305:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 9306:                                                                                       $cdom,$cnum,$type,$configuserok,
 9307:                                                                                       $switchserver,$author_ok);
 9308:                                         if ($imgurl) {
 9309:                                             $confhash{$type}{$key}{'image'} = $imgurl;
 9310:                                             $changes{$type}{$key} = 1; 
 9311:                                         }
 9312:                                         if ($error) {
 9313:                                             &Apache::lonnet::logthis($error);
 9314:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9315:                                         }
 9316:                                     }
 9317:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 9318:                                     $confhash{$type}{$key}{'image'} = 
 9319:                                         $domconfig{$action}{$type}{$key}{'image'};
 9320:                                 }
 9321:                             }
 9322:                         }
 9323:                     }
 9324:                 }
 9325:             }
 9326:         } else {
 9327:             if ($confhash{'notify'}{'approval'}) {
 9328:                 $changes{'notify'}{'approval'} = 1;
 9329:             }
 9330:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 9331:                 $changes{'uniquecode'} = 1;
 9332:             }
 9333:         }
 9334:         if ($context eq 'requestcourses') {
 9335:             foreach my $type ('textbooks','templates') {
 9336:                 if ($newbook{$type}) {
 9337:                     $changes{$type}{$newbook{$type}} = 1;
 9338:                     foreach my $item ('subject','title','publisher','author') {
 9339:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9340:                                  ($type eq 'template'));
 9341:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 9342:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 9343:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 9344:                         }
 9345:                     }
 9346:                     if ($type eq 'textbooks') {
 9347:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 9348:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 9349:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
 9350:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
 9351:                             } else {
 9352:                                 my ($imageurl,$error) =
 9353:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 9354:                                                             $configuserok,$switchserver,$author_ok);
 9355:                                 if ($imageurl) {
 9356:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 9357:                                 }
 9358:                                 if ($error) {
 9359:                                     &Apache::lonnet::logthis($error);
 9360:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9361:                                 }
 9362:                             }
 9363:                         }
 9364:                     }
 9365:                 }
 9366:                 if (@{$allpos{$type}} > 0) {
 9367:                     my $idx = 0;
 9368:                     foreach my $item (@{$allpos{$type}}) {
 9369:                         if ($item ne '') {
 9370:                             $confhash{$type}{$item}{'order'} = $idx;
 9371:                             if (ref($domconfig{$action}) eq 'HASH') {
 9372:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 9373:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 9374:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 9375:                                             $changes{$type}{$item} = 1;
 9376:                                         }
 9377:                                     }
 9378:                                 }
 9379:                             }
 9380:                             $idx ++;
 9381:                         }
 9382:                     }
 9383:                 }
 9384:             }
 9385:             if (ref($validationitemsref) eq 'ARRAY') {
 9386:                 foreach my $item (@{$validationitemsref}) {
 9387:                     if ($item eq 'fields') {
 9388:                         my @changed;
 9389:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 9390:                         if (@{$confhash{'validation'}{$item}} > 0) {
 9391:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 9392:                         }
 9393:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9394:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9395:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 9396:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 9397:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
 9398:                                 } else {
 9399:                                     @changed = @{$confhash{'validation'}{$item}};
 9400:                                 }
 9401:                             } else {
 9402:                                 @changed = @{$confhash{'validation'}{$item}};
 9403:                             }
 9404:                         } else {
 9405:                             @changed = @{$confhash{'validation'}{$item}};
 9406:                         }
 9407:                         if (@changed) {
 9408:                             if ($confhash{'validation'}{$item}) {
 9409:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 9410:                             } else {
 9411:                                 $changes{'validation'}{$item} = &mt('None');
 9412:                             }
 9413:                         }
 9414:                     } else {
 9415:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 9416:                         if ($item eq 'markup') {
 9417:                             if ($env{'form.requestcourses_validation_'.$item}) {
 9418:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 9419:                             }
 9420:                         }
 9421:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9422:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9423:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 9424:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9425:                                 }
 9426:                             } else {
 9427:                                 if ($confhash{'validation'}{$item} ne '') {
 9428:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9429:                                 }
 9430:                             }
 9431:                         } else {
 9432:                             if ($confhash{'validation'}{$item} ne '') {
 9433:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9434:                             }
 9435:                         }
 9436:                     }
 9437:                 }
 9438:             }
 9439:             if ($env{'form.validationdc'}) {
 9440:                 my $newval = $env{'form.validationdc'};
 9441:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
 9442:                 if (exists($domcoords{$newval})) {
 9443:                     $confhash{'validation'}{'dc'} = $newval;
 9444:                 }
 9445:             }
 9446:             if (ref($confhash{'validation'}) eq 'HASH') {
 9447:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9448:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9449:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9450:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9451:                                 if ($confhash{'validation'}{'dc'} eq '') {
 9452:                                     $changes{'validation'}{'dc'} = &mt('None');
 9453:                                 } else {
 9454:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9455:                                 }
 9456:                             }
 9457:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
 9458:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9459:                         }
 9460:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 9461:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9462:                     }
 9463:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 9464:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9465:                 }
 9466:             } else {
 9467:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9468:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9469:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9470:                             $changes{'validation'}{'dc'} = &mt('None');
 9471:                         }
 9472:                     }
 9473:                 }
 9474:             }
 9475:         }
 9476:     } else {
 9477:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 9478:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 9479:     }
 9480:     foreach my $item (@usertools) {
 9481:         foreach my $type (@{$types},'default','_LC_adv') {
 9482:             my $unset; 
 9483:             if ($context eq 'requestcourses') {
 9484:                 $unset = '0';
 9485:                 if ($type eq '_LC_adv') {
 9486:                     $unset = '';
 9487:                 }
 9488:                 if ($confhash{$item}{$type} eq 'autolimit') {
 9489:                     $confhash{$item}{$type} .= '=';
 9490:                     unless ($limithash{$item}{$type} =~ /\D/) {
 9491:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 9492:                     }
 9493:                 }
 9494:             } elsif ($context eq 'requestauthor') {
 9495:                 $unset = '0';
 9496:                 if ($type eq '_LC_adv') {
 9497:                     $unset = '';
 9498:                 }
 9499:             } else {
 9500:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 9501:                     $confhash{$item}{$type} = 1;
 9502:                 } else {
 9503:                     $confhash{$item}{$type} = 0;
 9504:                 }
 9505:             }
 9506:             if (ref($domconfig{$action}) eq 'HASH') {
 9507:                 if ($action eq 'requestauthor') {
 9508:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 9509:                         $changes{$type} = 1;
 9510:                     }
 9511:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 9512:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 9513:                         $changes{$item}{$type} = 1;
 9514:                     }
 9515:                 } else {
 9516:                     if ($context eq 'requestcourses') {
 9517:                         if ($confhash{$item}{$type} ne $unset) {
 9518:                             $changes{$item}{$type} = 1;
 9519:                         }
 9520:                     } else {
 9521:                         if (!$confhash{$item}{$type}) {
 9522:                             $changes{$item}{$type} = 1;
 9523:                         }
 9524:                     }
 9525:                 }
 9526:             } else {
 9527:                 if ($context eq 'requestcourses') {
 9528:                     if ($confhash{$item}{$type} ne $unset) {
 9529:                         $changes{$item}{$type} = 1;
 9530:                     }
 9531:                 } elsif ($context eq 'requestauthor') {
 9532:                     if ($confhash{$type} ne $unset) {
 9533:                         $changes{$type} = 1;
 9534:                     }
 9535:                 } else {
 9536:                     if (!$confhash{$item}{$type}) {
 9537:                         $changes{$item}{$type} = 1;
 9538:                     }
 9539:                 }
 9540:             }
 9541:         }
 9542:     }
 9543:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 9544:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 9545:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 9546:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 9547:                     if (exists($confhash{'defaultquota'}{$key})) {
 9548:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 9549:                             $changes{'defaultquota'}{$key} = 1;
 9550:                         }
 9551:                     } else {
 9552:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 9553:                     }
 9554:                 }
 9555:             } else {
 9556:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 9557:                     if (exists($confhash{'defaultquota'}{$key})) {
 9558:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 9559:                             $changes{'defaultquota'}{$key} = 1;
 9560:                         }
 9561:                     } else {
 9562:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 9563:                     }
 9564:                 }
 9565:             }
 9566:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 9567:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 9568:                     if (exists($confhash{'authorquota'}{$key})) {
 9569:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 9570:                             $changes{'authorquota'}{$key} = 1;
 9571:                         }
 9572:                     } else {
 9573:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 9574:                     }
 9575:                 }
 9576:             }
 9577:         }
 9578:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 9579:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 9580:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 9581:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 9582:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 9583:                             $changes{'defaultquota'}{$key} = 1;
 9584:                         }
 9585:                     } else {
 9586:                         if (!exists($domconfig{'quotas'}{$key})) {
 9587:                             $changes{'defaultquota'}{$key} = 1;
 9588:                         }
 9589:                     }
 9590:                 } else {
 9591:                     $changes{'defaultquota'}{$key} = 1;
 9592:                 }
 9593:             }
 9594:         }
 9595:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 9596:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 9597:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 9598:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 9599:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 9600:                             $changes{'authorquota'}{$key} = 1;
 9601:                         }
 9602:                     } else {
 9603:                         $changes{'authorquota'}{$key} = 1;
 9604:                     }
 9605:                 } else {
 9606:                     $changes{'authorquota'}{$key} = 1;
 9607:                 }
 9608:             }
 9609:         }
 9610:     }
 9611: 
 9612:     if ($context eq 'requestauthor') {
 9613:         $domdefaults{'requestauthor'} = \%confhash;
 9614:     } else {
 9615:         foreach my $key (keys(%confhash)) {
 9616:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 9617:                 $domdefaults{$key} = $confhash{$key};
 9618:             }
 9619:         }
 9620:     }
 9621: 
 9622:     my %quotahash = (
 9623:                       $action => { %confhash }
 9624:                     );
 9625:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 9626:                                              $dom);
 9627:     if ($putresult eq 'ok') {
 9628:         if (keys(%changes) > 0) {
 9629:             my $cachetime = 24*60*60;
 9630:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9631:             if (ref($lastactref) eq 'HASH') {
 9632:                 $lastactref->{'domdefaults'} = 1;
 9633:             }
 9634:             $resulttext = &mt('Changes made:').'<ul>';
 9635:             unless (($context eq 'requestcourses') ||
 9636:                     ($context eq 'requestauthor')) {
 9637:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 9638:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 9639:                     foreach my $type (@{$types},'default') {
 9640:                         if (defined($changes{'defaultquota'}{$type})) {
 9641:                             my $typetitle = $usertypes->{$type};
 9642:                             if ($type eq 'default') {
 9643:                                 $typetitle = $othertitle;
 9644:                             }
 9645:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 9646:                         }
 9647:                     }
 9648:                     $resulttext .= '</ul></li>';
 9649:                 }
 9650:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 9651:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 9652:                     foreach my $type (@{$types},'default') {
 9653:                         if (defined($changes{'authorquota'}{$type})) {
 9654:                             my $typetitle = $usertypes->{$type};
 9655:                             if ($type eq 'default') {
 9656:                                 $typetitle = $othertitle;
 9657:                             }
 9658:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 9659:                         }
 9660:                     }
 9661:                     $resulttext .= '</ul></li>';
 9662:                 }
 9663:             }
 9664:             my %newenv;
 9665:             foreach my $item (@usertools) {
 9666:                 my (%haschgs,%inconf);
 9667:                 if ($context eq 'requestauthor') {
 9668:                     %haschgs = %changes;
 9669:                     %inconf = %confhash;
 9670:                 } else {
 9671:                     if (ref($changes{$item}) eq 'HASH') {
 9672:                         %haschgs = %{$changes{$item}};
 9673:                     }
 9674:                     if (ref($confhash{$item}) eq 'HASH') {
 9675:                         %inconf = %{$confhash{$item}};
 9676:                     }
 9677:                 }
 9678:                 if (keys(%haschgs) > 0) {
 9679:                     my $newacc = 
 9680:                         &Apache::lonnet::usertools_access($env{'user.name'},
 9681:                                                           $env{'user.domain'},
 9682:                                                           $item,'reload',$context);
 9683:                     if (($context eq 'requestcourses') ||
 9684:                         ($context eq 'requestauthor')) {
 9685:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 9686:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 9687:                         }
 9688:                     } else {
 9689:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 9690:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 9691:                         }
 9692:                     }
 9693:                     unless ($context eq 'requestauthor') {
 9694:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 9695:                     }
 9696:                     foreach my $type (@{$types},'default','_LC_adv') {
 9697:                         if ($haschgs{$type}) {
 9698:                             my $typetitle = $usertypes->{$type};
 9699:                             if ($type eq 'default') {
 9700:                                 $typetitle = $othertitle;
 9701:                             } elsif ($type eq '_LC_adv') {
 9702:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 9703:                             }
 9704:                             if ($inconf{$type}) {
 9705:                                 if ($context eq 'requestcourses') {
 9706:                                     my $cond;
 9707:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 9708:                                         if ($1 eq '') {
 9709:                                             $cond = &mt('(Automatic processing of any request).');
 9710:                                         } else {
 9711:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 9712:                                         }
 9713:                                     } else { 
 9714:                                         $cond = $conditions{$inconf{$type}};
 9715:                                     }
 9716:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 9717:                                 } elsif ($context eq 'requestauthor') {
 9718:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 9719:                                                              $titles{$inconf{$type}},$typetitle);
 9720: 
 9721:                                 } else {
 9722:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 9723:                                 }
 9724:                             } else {
 9725:                                 if ($type eq '_LC_adv') {
 9726:                                     if ($inconf{$type} eq '0') {
 9727:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9728:                                     } else { 
 9729:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 9730:                                     }
 9731:                                 } else {
 9732:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9733:                                 }
 9734:                             }
 9735:                         }
 9736:                     }
 9737:                     unless ($context eq 'requestauthor') {
 9738:                         $resulttext .= '</ul></li>';
 9739:                     }
 9740:                 }
 9741:             }
 9742:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 9743:                 if (ref($changes{'notify'}) eq 'HASH') {
 9744:                     if ($changes{'notify'}{'approval'}) {
 9745:                         if (ref($confhash{'notify'}) eq 'HASH') {
 9746:                             if ($confhash{'notify'}{'approval'}) {
 9747:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 9748:                             } else {
 9749:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 9750:                             }
 9751:                         }
 9752:                     }
 9753:                 }
 9754:             }
 9755:             if ($action eq 'requestcourses') {
 9756:                 my @offon = ('off','on');
 9757:                 if ($changes{'uniquecode'}) {
 9758:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 9759:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 9760:                         $resulttext .= '<li>'.
 9761:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 9762:                                        '</li>';
 9763:                     } else {
 9764:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 9765:                                        '</li>';
 9766:                     }
 9767:                 }
 9768:                 foreach my $type ('textbooks','templates') {
 9769:                     if (ref($changes{$type}) eq 'HASH') {
 9770:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 9771:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 9772:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 9773:                             my $coursetitle = $coursehash{'description'};
 9774:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 9775:                             $resulttext .= '<li>';
 9776:                             foreach my $item ('subject','title','publisher','author') {
 9777:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9778:                                          ($type eq 'templates'));
 9779:                                 my $name = $item.':';
 9780:                                 $name =~ s/^(\w)/\U$1/;
 9781:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 9782:                             }
 9783:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 9784:                             if ($type eq 'textbooks') {
 9785:                                 if ($confhash{$type}{$key}{'image'}) {
 9786:                                     $resulttext .= ' '.&mt('Image: [_1]',
 9787:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 9788:                                                    ' alt="Textbook cover" />').'<br />';
 9789:                                 }
 9790:                             }
 9791:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 9792:                         }
 9793:                         $resulttext .= '</ul></li>';
 9794:                     }
 9795:                 }
 9796:                 if (ref($changes{'validation'}) eq 'HASH') {
 9797:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 9798:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 9799:                         foreach my $item (@{$validationitemsref}) {
 9800:                             if (exists($changes{'validation'}{$item})) {
 9801:                                 if ($item eq 'markup') {
 9802:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9803:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 9804:                                 } else {
 9805:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9806:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 9807:                                 }
 9808:                             }
 9809:                         }
 9810:                         if (exists($changes{'validation'}{'dc'})) {
 9811:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 9812:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 9813:                         }
 9814:                     }
 9815:                 }
 9816:             }
 9817:             $resulttext .= '</ul>';
 9818:             if (keys(%newenv)) {
 9819:                 &Apache::lonnet::appenv(\%newenv);
 9820:             }
 9821:         } else {
 9822:             if ($context eq 'requestcourses') {
 9823:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 9824:             } elsif ($context eq 'requestauthor') {
 9825:                 $resulttext = &mt('No changes made to rights to request author space.');
 9826:             } else {
 9827:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 9828:             }
 9829:         }
 9830:     } else {
 9831:         $resulttext = '<span class="LC_error">'.
 9832: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9833:     }
 9834:     if ($errors) {
 9835:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 9836:                        '<ul>'.$errors.'</ul></p>';
 9837:     }
 9838:     return $resulttext;
 9839: }
 9840: 
 9841: sub process_textbook_image {
 9842:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
 9843:     my $filename = $env{'form.'.$caller.'.filename'};
 9844:     my ($error,$url);
 9845:     my ($width,$height) = (50,50);
 9846:     if ($configuserok eq 'ok') {
 9847:         if ($switchserver) {
 9848:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 9849:                          $switchserver);
 9850:         } elsif ($author_ok eq 'ok') {
 9851:             my ($result,$imageurl) =
 9852:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 9853:                              "$type/$cdom/$cnum/cover",$width,$height);
 9854:             if ($result eq 'ok') {
 9855:                 $url = $imageurl;
 9856:             } else {
 9857:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 9858:             }
 9859:         } else {
 9860:             $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);
 9861:         }
 9862:     } else {
 9863:         $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);
 9864:     }
 9865:     return ($url,$error);
 9866: }
 9867: 
 9868: sub modify_ltitools {
 9869:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 9870:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9871:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
 9872:     my $confname = $dom.'-domainconfig';
 9873:     my $servadm = $r->dir_config('lonAdmEMail');
 9874:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9875:     my (%posslti,%possfield);
 9876:     my @courseroles = ('cc','in','ta','ep','st');
 9877:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 9878:     map { $posslti{$_} = 1; } @ltiroles;
 9879:     my @allfields = ('fullname','firstname','lastname','email','user','roles');
 9880:     map { $possfield{$_} = 1; } @allfields;
 9881:     my %lt = &ltitools_names(); 
 9882:     if ($env{'form.ltitools_add'}) {
 9883:         my $title = $env{'form.ltitools_add_title'};
 9884:         $title =~ s/(`)/'/g;
 9885:         ($newid,my $error) = &get_ltitools_id($dom,$title);
 9886:         if ($newid) {
 9887:             my $position = $env{'form.ltitools_add_pos'};
 9888:             $position =~ s/\D+//g;
 9889:             if ($position ne '') {
 9890:                 $allpos[$position] = $newid;
 9891:             }
 9892:             $changes{$newid} = 1;
 9893:             foreach my $item ('title','url','key','secret') {
 9894:                 $env{'form.ltitools_add_'.$item} =~ s/(`)/'/g;
 9895:                 if ($env{'form.ltitools_add_'.$item}) {
 9896:                     if (($item eq 'key') || ($item eq 'secret')) {
 9897:                         $encconfig{$newid}{$item} = $env{'form.ltitools_add_'.$item};
 9898:                     } else {
 9899:                         $confhash{$newid}{$item} = $env{'form.ltitools_add_'.$item};
 9900:                     }
 9901:                 }
 9902:             }
 9903:             if ($env{'form.ltitools_add_version'} eq 'LTI-1p0') {
 9904:                 $confhash{$newid}{'version'} = $env{'form.ltitools_add_version'};
 9905:             }
 9906:             if ($env{'form.ltitools_add_msgtype'} eq 'basic-lti-launch-request') {
 9907:                 $confhash{$newid}{'msgtype'} = $env{'form.ltitools_add_msgtype'};
 9908:             }
 9909:             foreach my $item ('width','height','linktext','explanation') {
 9910:                 $env{'form.ltitools_add_'.$item} =~ s/^\s+//;
 9911:                 $env{'form.ltitools_add_'.$item} =~ s/\s+$//;
 9912:                 if (($item eq 'width') || ($item eq 'height')) {
 9913:                     if ($env{'form.ltitools_add_'.$item} =~ /^\d+$/) {
 9914:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
 9915:                     }
 9916:                 } else {
 9917:                     if ($env{'form.ltitools_add_'.$item} ne '') {
 9918:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item}; 
 9919:                     }
 9920:                 }
 9921:             }
 9922:             if ($env{'form.ltitools_add_target'} eq 'window') {
 9923:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
 9924:             } elsif ($env{'form.ltitools_add_target'} eq 'tab') {
 9925:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
 9926:             } else {
 9927:                 $confhash{$newid}{'display'}{'target'} = 'iframe';
 9928:             }
 9929:             foreach my $item ('passback','roster') {
 9930:                 if ($env{'form.ltitools_add_'.$item}) {
 9931:                     $confhash{$newid}{$item} = 1;
 9932:                 }
 9933:             }
 9934:             if ($env{'form.ltitools_add_image.filename'} ne '') {
 9935:                 my ($imageurl,$error) =
 9936:                     &process_ltitools_image($r,$dom,$confname,'ltitools_add_image',$newid,
 9937:                                             $configuserok,$switchserver,$author_ok);
 9938:                 if ($imageurl) {
 9939:                     $confhash{$newid}{'image'} = $imageurl;
 9940:                 }
 9941:                 if ($error) {
 9942:                     &Apache::lonnet::logthis($error);
 9943:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9944:                 }
 9945:             }
 9946:             my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_add_fields');
 9947:             foreach my $field (@fields) {
 9948:                 if ($possfield{$field}) {
 9949:                     if ($field eq 'roles') {
 9950:                         foreach my $role (@courseroles) {
 9951:                             my $choice = $env{'form.ltitools_add_roles_'.$role};
 9952:                             if (($choice ne '') && ($posslti{$choice})) {
 9953:                                 $confhash{$newid}{'roles'}{$role} = $choice;
 9954:                                 if ($role eq 'cc') {
 9955:                                     $confhash{$newid}{'roles'}{'co'} = $choice; 
 9956:                                 }
 9957:                             }
 9958:                         }
 9959:                     } else {
 9960:                         $confhash{$newid}{'fields'}{$field} = 1;
 9961:                     }
 9962:                 }
 9963:             }
 9964:             my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig');
 9965:             foreach my $item (@courseconfig) {
 9966:                 $confhash{$newid}{'crsconf'}{$item} = 1;
 9967:             }
 9968:             if ($env{'form.ltitools_add_custom'}) {
 9969:                 my $name = $env{'form.ltitools_add_custom_name'};
 9970:                 my $value = $env{'form.ltitools_add_custom_value'};
 9971:                 $value =~ s/(`)/'/g;
 9972:                 $name =~ s/(`)/'/g;
 9973:                 $confhash{$newid}{'custom'}{$name} = $value;
 9974:             }
 9975:         } else {
 9976:             my $error = &mt('Failed to acquire unique ID for new external tool');   
 9977:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9978:         }
 9979:     }
 9980:     if (ref($domconfig{$action}) eq 'HASH') {
 9981:         my %deletions;
 9982:         my @todelete = &Apache::loncommon::get_env_multiple('form.ltitools_del');
 9983:         if (@todelete) {
 9984:             map { $deletions{$_} = 1; } @todelete;
 9985:         }
 9986:         my %customadds;
 9987:         my @newcustom = &Apache::loncommon::get_env_multiple('form.ltitools_customadd');
 9988:         if (@newcustom) {
 9989:             map { $customadds{$_} = 1; } @newcustom;
 9990:         } 
 9991:         my %imgdeletions;
 9992:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.ltitools_image_del');
 9993:         if (@todeleteimages) {
 9994:             map { $imgdeletions{$_} = 1; } @todeleteimages;
 9995:         }
 9996:         my $maxnum = $env{'form.ltitools_maxnum'};
 9997:         for (my $i=0; $i<=$maxnum; $i++) {
 9998:             my $itemid = $env{'form.ltitools_id_'.$i};
 9999:             $itemid =~ s/\D+//g;
10000:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
10001:                 if ($deletions{$itemid}) {
10002:                     if ($domconfig{$action}{$itemid}{'image'}) {
10003:                         #FIXME need to obsolete item in RES space
10004:                     }
10005:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'title'};
10006:                     next;
10007:                 } else {
10008:                     my $newpos = $env{'form.ltitools_'.$itemid};
10009:                     $newpos =~ s/\D+//g;
10010:                     foreach my $item ('title','url') {
10011:                         $confhash{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
10012:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
10013:                             $changes{$itemid} = 1;
10014:                         }
10015:                     }
10016:                     foreach my $item ('key','secret') {
10017:                         $encconfig{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
10018:                         if ($domconfig{$action}{$itemid}{$item} ne $encconfig{$itemid}{$item}) {
10019:                             $changes{$itemid} = 1;
10020:                         }
10021:                     }
10022:                     if ($env{'form.ltitools_version_'.$i} eq 'LTI-1p0') {
10023:                         $confhash{$itemid}{'version'} = $env{'form.ltitools_version_'.$i};
10024:                     }
10025:                     if ($env{'form.ltitools_msgtype_'.$i} eq 'basic-lti-launch-request') {
10026:                         $confhash{$itemid}{'msgtype'} = $env{'form.ltitools_msgtype_'.$i};
10027:                     }
10028:                     foreach my $size ('width','height') {
10029:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/^\s+//;
10030:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/\s+$//;
10031:                         if ($env{'form.ltitools_'.$size.'_'.$i} =~ /^\d+$/) {
10032:                             $confhash{$itemid}{'display'}{$size} = $env{'form.ltitools_'.$size.'_'.$i};
10033:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10034:                                 if ($domconfig{$action}{$itemid}{'display'}{$size} ne $confhash{$itemid}{'display'}{$size}) {
10035:                                     $changes{$itemid} = 1;
10036:                                 }
10037:                             } else {
10038:                                 $changes{$itemid} = 1;
10039:                             }
10040:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10041:                             if ($domconfig{$action}{$itemid}{'display'}{$size} ne '') {
10042:                                 $changes{$itemid} = 1;
10043:                             }
10044:                         }
10045:                     }
10046:                     foreach my $item ('linktext','explanation') {
10047:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/^\s+//;
10048:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/\s+$//;
10049:                         if ($env{'form.ltitools_'.$item.'_'.$i} ne '') {
10050:                             $confhash{$itemid}{'display'}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
10051:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10052:                                 if ($domconfig{$action}{$itemid}{'display'}{$item} ne $confhash{$itemid}{'display'}{$item}) {
10053:                                     $changes{$itemid} = 1;
10054:                                 }
10055:                             } else {
10056:                                 $changes{$itemid} = 1;
10057:                             }
10058:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10059:                             if ($domconfig{$action}{$itemid}{'display'}{$item} ne '') {
10060:                                 $changes{$itemid} = 1;
10061:                             }
10062:                         }
10063:                     }
10064:                     if ($env{'form.ltitools_target_'.$i} eq 'window') {
10065:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
10066:                     } elsif ($env{'form.ltitools_target_'.$i} eq 'tab') {
10067:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
10068:                     } else {
10069:                         $confhash{$itemid}{'display'}{'target'} = 'iframe';
10070:                     }
10071:                     if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10072:                         if ($domconfig{$action}{$itemid}{'display'}{'target'} ne $confhash{$itemid}{'display'}{'target'}) {
10073:                             $changes{$itemid} = 1;
10074:                         }
10075:                     } else {
10076:                         $changes{$itemid} = 1;
10077:                     }
10078:                     foreach my $extra ('passback','roster') {
10079:                         if ($env{'form.ltitools_'.$extra.'_'.$i}) {
10080:                             $confhash{$itemid}{$extra} = 1;
10081:                         }
10082:                         if ($domconfig{$action}{$itemid}{$extra} ne $confhash{$itemid}{$extra}) {
10083:                             $changes{$itemid} = 1;
10084:                         }
10085:                     }
10086:                     my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig_'.$i);
10087:                     foreach my $item ('label','title','target','linktext','explanation') {
10088:                         if (grep(/^\Q$item\E$/,@courseconfig)) {
10089:                             $confhash{$itemid}{'crsconf'}{$item} = 1;
10090:                             if (ref($domconfig{$action}{$itemid}{'crsconf'}) eq 'HASH') {
10091:                                 if ($domconfig{$action}{$itemid}{'crsconf'}{$item} ne $confhash{$itemid}{'crsconf'}{$item}) {
10092:                                     $changes{$itemid} = 1;
10093:                                 }
10094:                             } else {
10095:                                 $changes{$itemid} = 1;
10096:                             }
10097:                         }
10098:                     }
10099:                     my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_fields_'.$i);
10100:                     foreach my $field (@fields) {
10101:                         if ($possfield{$field}) {
10102:                             if ($field eq 'roles') {
10103:                                 foreach my $role (@courseroles) {
10104:                                     my $choice = $env{'form.ltitools_roles_'.$role.'_'.$i};
10105:                                     if (($choice ne '') && ($posslti{$choice})) {
10106:                                         $confhash{$itemid}{'roles'}{$role} = $choice;
10107:                                         if ($role eq 'cc') {
10108:                                             $confhash{$itemid}{'roles'}{'co'} = $choice;
10109:                                         }
10110:                                     }
10111:                                     if (ref($domconfig{$action}{$itemid}{'roles'}) eq 'HASH') {
10112:                                         if ($domconfig{$action}{$itemid}{'roles'}{$role} ne $confhash{$itemid}{'roles'}{$role}) {
10113:                                             $changes{$itemid} = 1;
10114:                                         }
10115:                                     } elsif ($confhash{$itemid}{'roles'}{$role}) {
10116:                                         $changes{$itemid} = 1;
10117:                                     }
10118:                                 }
10119:                             } else {
10120:                                 $confhash{$itemid}{'fields'}{$field} = 1;
10121:                                 if (ref($domconfig{$action}{$itemid}{'fields'}) eq 'HASH') {
10122:                                     if ($domconfig{$action}{$itemid}{'fields'}{$field} ne $confhash{$itemid}{'fields'}{$field}) {
10123:                                         $changes{$itemid} = 1;
10124:                                     }
10125:                                 } else {
10126:                                     $changes{$itemid} = 1;
10127:                                 }
10128:                             }
10129:                         }
10130:                     }
10131:                     $allpos[$newpos] = $itemid;
10132:                 }
10133:                 if ($imgdeletions{$itemid}) {
10134:                     $changes{$itemid} = 1;
10135:                     #FIXME need to obsolete item in RES space
10136:                 } elsif ($env{'form.ltitools_image_'.$i.'.filename'}) {
10137:                     my ($imgurl,$error) = &process_ltitools_image($r,$dom,$confname,'ltitools_image_'.$i,
10138:                                                                  $itemid,$configuserok,$switchserver,
10139:                                                                  $author_ok);
10140:                     if ($imgurl) {
10141:                         $confhash{$itemid}{'image'} = $imgurl;
10142:                         $changes{$itemid} = 1;
10143:                     }
10144:                     if ($error) {
10145:                         &Apache::lonnet::logthis($error);
10146:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10147:                     }
10148:                 } elsif ($domconfig{$action}{$itemid}{'image'}) {
10149:                     $confhash{$itemid}{'image'} =
10150:                        $domconfig{$action}{$itemid}{'image'};
10151:                 }
10152:                 if ($customadds{$i}) {
10153:                     my $name = $env{'form.ltitools_custom_name_'.$i};
10154:                     $name =~ s/(`)/'/g;
10155:                     $name =~ s/^\s+//;
10156:                     $name =~ s/\s+$//;
10157:                     my $value = $env{'form.ltitools_custom_value_'.$i};
10158:                     $value =~ s/(`)/'/g;
10159:                     $value =~ s/^\s+//;
10160:                     $value =~ s/\s+$//;
10161:                     if ($name ne '') {
10162:                         $confhash{$itemid}{'custom'}{$name} = $value;
10163:                         $changes{$itemid} = 1;
10164:                     }
10165:                 }
10166:                 my %customdels;
10167:                 my @customdeletions = &Apache::loncommon::get_env_multiple('form.ltitools_customdel_'.$i); 
10168:                 if (@customdeletions) {
10169:                     $changes{$itemid} = 1;
10170:                 }
10171:                 map { $customdels{$_} = 1; } @customdeletions;
10172:                 if (ref($domconfig{$action}{$itemid}{'custom'}) eq 'HASH') {
10173:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}{'custom'}})) {
10174:                         unless ($customdels{$key}) {
10175:                             if ($env{'form.ltitools_customval_'.$key.'_'.$i} ne '') {
10176:                                 $confhash{$itemid}{'custom'}{$key} = $env{'form.ltitools_customval_'.$key.'_'.$i}; 
10177:                             }
10178:                             if ($domconfig{$action}{$itemid}{'custom'}{$key} ne $env{'form.ltitools_customval_'.$key.'_'.$i}) {
10179:                                 $changes{$itemid} = 1;
10180:                             }
10181:                         }
10182:                     }
10183:                 }
10184:                 unless ($changes{$itemid}) {
10185:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}})) {
10186:                         if (ref($domconfig{$action}{$itemid}{$key}) eq 'HASH') {
10187:                             if (ref($confhash{$itemid}{$key}) eq 'HASH') {
10188:                                 foreach my $innerkey (keys(%{$domconfig{$action}{$itemid}{$key}})) {
10189:                                     unless (exists($confhash{$itemid}{$key}{$innerkey})) {
10190:                                         $changes{$itemid} = 1;
10191:                                         last;
10192:                                     }
10193:                                 }
10194:                             } elsif (keys(%{$domconfig{$action}{$itemid}{$key}}) > 0) {
10195:                                 $changes{$itemid} = 1;
10196:                             }
10197:                         }
10198:                         last if ($changes{$itemid});
10199:                     }
10200:                 }
10201:             }
10202:         }
10203:     }
10204:     if (@allpos > 0) {
10205:         my $idx = 0;
10206:         foreach my $itemid (@allpos) {
10207:             if ($itemid ne '') {
10208:                 $confhash{$itemid}{'order'} = $idx;
10209:                 if (ref($domconfig{$action}) eq 'HASH') {
10210:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
10211:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
10212:                             $changes{$itemid} = 1;
10213:                         }
10214:                     }
10215:                 }
10216:                 $idx ++;
10217:             }
10218:         }
10219:     }
10220:     my %ltitoolshash = (
10221:                           $action => { %confhash }
10222:                        );
10223:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,
10224:                                              $dom);
10225:     if ($putresult eq 'ok') {
10226:         my %ltienchash = (
10227:                              $action => { %encconfig }
10228:                          );
10229:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom);
10230:         if (keys(%changes) > 0) {
10231:             my $cachetime = 24*60*60;
10232:             my %ltiall = %confhash;
10233:             foreach my $id (keys(%ltiall)) {
10234:                 if (ref($encconfig{$id}) eq 'HASH') {
10235:                     foreach my $item ('key','secret') {
10236:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
10237:                     }
10238:                 }
10239:             }
10240:             &Apache::lonnet::do_cache_new('ltitools',$dom,\%ltiall,$cachetime);
10241:             if (ref($lastactref) eq 'HASH') {
10242:                 $lastactref->{'ltitools'} = 1;
10243:             }
10244:             $resulttext = &mt('Changes made:').'<ul>';
10245:             my %bynum;
10246:             foreach my $itemid (sort(keys(%changes))) {
10247:                 my $position = $confhash{$itemid}{'order'};
10248:                 $bynum{$position} = $itemid;
10249:             }
10250:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
10251:                 my $itemid = $bynum{$pos}; 
10252:                 if (ref($confhash{$itemid}) ne 'HASH') {
10253:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
10254:                 } else {
10255:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'title'}.'</b>';
10256:                     if ($confhash{$itemid}{'image'}) {
10257:                         $resulttext .= '&nbsp;'.
10258:                                        '<img src="'.$confhash{$itemid}{'image'}.'"'.
10259:                                        ' alt="'.&mt('Tool Provider icon').'" />';
10260:                     }
10261:                     $resulttext .= '</li><ul>';
10262:                     my $position = $pos + 1;
10263:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
10264:                     foreach my $item ('version','msgtype','url') {
10265:                         if ($confhash{$itemid}{$item} ne '') {
10266:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
10267:                         }
10268:                     }
10269:                     if ($encconfig{$itemid}{'key'} ne '') {
10270:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
10271:                     }
10272:                     if ($encconfig{$itemid}{'secret'} ne '') {
10273:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
10274:                         my $num = length($encconfig{$itemid}{'secret'});
10275:                         $resulttext .= ('*'x$num).'</li>';
10276:                     }
10277:                     $resulttext .= '<li>'.&mt('Configurable in course:');
10278:                     my @possconfig = ('label','title','target','linktext','explanation');
10279:                     my $numconfig = 0; 
10280:                     if (ref($confhash{$itemid}{'crsconf'}) eq 'HASH') { 
10281:                         foreach my $item (@possconfig) {
10282:                             if ($confhash{$itemid}{'crsconf'}{$item}) {
10283:                                 $numconfig ++;
10284:                                 $resulttext .= ' "'.$lt{'crs'.$item}.'"';
10285:                             }
10286:                         }
10287:                     }
10288:                     if (!$numconfig) {
10289:                         $resulttext .= &mt('None');
10290:                     }
10291:                     $resulttext .= '</li>';
10292:                     foreach my $item ('passback','roster') {
10293:                         $resulttext .= '<li>'.$lt{$item}.'&nbsp;';
10294:                         if ($confhash{$itemid}{$item}) {
10295:                             $resulttext .= &mt('Yes');
10296:                         } else {
10297:                             $resulttext .= &mt('No');
10298:                         }
10299:                         $resulttext .= '</li>';
10300:                     }
10301:                     if (ref($confhash{$itemid}{'display'}) eq 'HASH') {
10302:                         my $displaylist;
10303:                         if ($confhash{$itemid}{'display'}{'target'}) {
10304:                             $displaylist = &mt('Display target').':&nbsp;'.
10305:                                            $confhash{$itemid}{'display'}{'target'}.',';
10306:                         }
10307:                         foreach my $size ('width','height') { 
10308:                             if ($confhash{$itemid}{'display'}{$size}) {
10309:                                 $displaylist .= ('&nbsp;'x2).$lt{$size}.':&nbsp;'.
10310:                                                 $confhash{$itemid}{'display'}{$size}.',';
10311:                             }
10312:                         }
10313:                         if ($displaylist) {
10314:                             $displaylist =~ s/,$//;
10315:                             $resulttext .= '<li>'.$displaylist.'</li>';
10316:                         }
10317:                         foreach my $item ('linktext','explanation') {
10318:                             if ($confhash{$itemid}{'display'}{$item}) {
10319:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{'display'}{$item}.'</li>';
10320:                             }
10321:                         }
10322:                     }
10323:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
10324:                         my $fieldlist;
10325:                         foreach my $field (@allfields) {
10326:                             if ($confhash{$itemid}{'fields'}{$field}) {
10327:                                 $fieldlist .= ('&nbsp;'x2).$lt{$field}.',';
10328:                             }
10329:                         }
10330:                         if ($fieldlist) {
10331:                             $fieldlist =~ s/,$//;
10332:                             $resulttext .= '<li>'.&mt('Data sent').':'.$fieldlist.'</li>';
10333:                         }
10334:                     }
10335:                     if (ref($confhash{$itemid}{'roles'}) eq 'HASH') {
10336:                         my $rolemaps;
10337:                         foreach my $role (@courseroles) {
10338:                             if ($confhash{$itemid}{'roles'}{$role}) {
10339:                                 $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
10340:                                              $confhash{$itemid}{'roles'}{$role}.',';
10341:                             }
10342:                         }
10343:                         if ($rolemaps) {
10344:                             $rolemaps =~ s/,$//; 
10345:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
10346:                         }
10347:                     }
10348:                     if (ref($confhash{$itemid}{'custom'}) eq 'HASH') {
10349:                         my $customlist;
10350:                         if (keys(%{$confhash{$itemid}{'custom'}})) {
10351:                             foreach my $key (sort(keys(%{$confhash{$itemid}{'custom'}}))) {
10352:                                 $customlist .= $key.':'.$confhash{$itemid}{'custom'}{$key}.('&nbsp;'x2);
10353:                             } 
10354:                         }
10355:                         if ($customlist) {
10356:                             $resulttext .= '<li>'.&mt('Custom items').':'.$customlist.'</li>';
10357:                         }
10358:                     } 
10359:                     $resulttext .= '</ul></li>';
10360:                 }
10361:             }
10362:             $resulttext .= '</ul>';
10363:         } else {
10364:             $resulttext = &mt('No changes made.');
10365:         }
10366:     } else {
10367:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
10368:     }
10369:     if ($errors) {
10370:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
10371:                        $errors.'</ul>';
10372:     }
10373:     return $resulttext;
10374: }
10375: 
10376: sub process_ltitools_image {
10377:     my ($r,$dom,$confname,$caller,$itemid,$configuserok,$switchserver,$author_ok) = @_;
10378:     my $filename = $env{'form.'.$caller.'.filename'};
10379:     my ($error,$url);
10380:     my ($width,$height) = (21,21);
10381:     if ($configuserok eq 'ok') {
10382:         if ($switchserver) {
10383:             $error = &mt('Upload of Tool Provider (LTI) icon is not permitted to this server: [_1]',
10384:                          $switchserver);
10385:         } elsif ($author_ok eq 'ok') {
10386:             my ($result,$imageurl,$madethumb) =
10387:                 &publishlogo($r,'upload',$caller,$dom,$confname,
10388:                              "ltitools/$itemid/icon",$width,$height);
10389:             if ($result eq 'ok') {
10390:                 if ($madethumb) {
10391:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
10392:                     my $imagethumb = "$path/tn-".$imagefile;
10393:                     $url = $imagethumb;
10394:                 } else {
10395:                     $url = $imageurl;
10396:                 }
10397:             } else {
10398:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
10399:             }
10400:         } else {
10401:             $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);
10402:         }
10403:     } else {
10404:         $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);
10405:     }
10406:     return ($url,$error);
10407: }
10408: 
10409: sub get_ltitools_id {
10410:     my ($cdom,$title) = @_;
10411:     # get lock on ltitools db
10412:     my $lockhash = {
10413:                       lock => $env{'user.name'}.
10414:                               ':'.$env{'user.domain'},
10415:                    };
10416:     my $tries = 0;
10417:     my $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
10418:     my ($id,$error);
10419:  
10420:     while (($gotlock ne 'ok') && ($tries<10)) {
10421:         $tries ++;
10422:         sleep (0.1);
10423:         $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
10424:     }
10425:     if ($gotlock eq 'ok') {
10426:         my %currids = &Apache::lonnet::dump_dom('ltitools',$cdom);
10427:         if ($currids{'lock'}) {
10428:             delete($currids{'lock'});
10429:             if (keys(%currids)) {
10430:                 my @curr = sort { $a <=> $b } keys(%currids);
10431:                 if ($curr[-1] =~ /^\d+$/) {
10432:                     $id = 1 + $curr[-1];
10433:                 }
10434:             } else {
10435:                 $id = 1;
10436:             }
10437:             if ($id) {
10438:                 unless (&Apache::lonnet::newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
10439:                     $error = 'nostore';
10440:                 }
10441:             } else {
10442:                 $error = 'nonumber';
10443:             }
10444:         }
10445:         my $dellockoutcome = &Apache::lonnet::del_dom('ltitools',['lock'],$cdom);
10446:     } else {
10447:         $error = 'nolock';
10448:     }
10449:     return ($id,$error);
10450: }
10451: 
10452: sub modify_autoenroll {
10453:     my ($dom,$lastactref,%domconfig) = @_;
10454:     my ($resulttext,%changes);
10455:     my %currautoenroll;
10456:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
10457:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
10458:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
10459:         }
10460:     }
10461:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
10462:     my %title = ( run => 'Auto-enrollment active',
10463:                   sender => 'Sender for notification messages',
10464:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
10465:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
10466:     my @offon = ('off','on');
10467:     my $sender_uname = $env{'form.sender_uname'};
10468:     my $sender_domain = $env{'form.sender_domain'};
10469:     if ($sender_domain eq '') {
10470:         $sender_uname = '';
10471:     } elsif ($sender_uname eq '') {
10472:         $sender_domain = '';
10473:     }
10474:     my $coowners = $env{'form.autoassign_coowners'};
10475:     my $failsafe = $env{'form.autoenroll_failsafe'};
10476:     $failsafe =~ s{^\s+|\s+$}{}g;
10477:     if ($failsafe =~ /\D/) {
10478:         undef($failsafe);
10479:     }
10480:     my %autoenrollhash =  (
10481:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
10482:                                        'sender_uname' => $sender_uname,
10483:                                        'sender_domain' => $sender_domain,
10484:                                        'co-owners' => $coowners,
10485:                                        'autofailsafe' => $failsafe,
10486:                                 }
10487:                      );
10488:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
10489:                                              $dom);
10490:     if ($putresult eq 'ok') {
10491:         if (exists($currautoenroll{'run'})) {
10492:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
10493:                  $changes{'run'} = 1;
10494:              }
10495:         } elsif ($autorun) {
10496:             if ($env{'form.autoenroll_run'} ne '1') {
10497:                  $changes{'run'} = 1;
10498:             }
10499:         }
10500:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
10501:             $changes{'sender'} = 1;
10502:         }
10503:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
10504:             $changes{'sender'} = 1;
10505:         }
10506:         if ($currautoenroll{'co-owners'} ne '') {
10507:             if ($currautoenroll{'co-owners'} ne $coowners) {
10508:                 $changes{'coowners'} = 1;
10509:             }
10510:         } elsif ($coowners) {
10511:             $changes{'coowners'} = 1;
10512:         }
10513:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
10514:             $changes{'autofailsafe'} = 1;
10515:         }
10516:         if (keys(%changes) > 0) {
10517:             $resulttext = &mt('Changes made:').'<ul>';
10518:             if ($changes{'run'}) {
10519:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
10520:             }
10521:             if ($changes{'sender'}) {
10522:                 if ($sender_uname eq '' || $sender_domain eq '') {
10523:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
10524:                 } else {
10525:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
10526:                 }
10527:             }
10528:             if ($changes{'coowners'}) {
10529:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
10530:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
10531:                 if (ref($lastactref) eq 'HASH') {
10532:                     $lastactref->{'domainconfig'} = 1;
10533:                 }
10534:             }
10535:             if ($changes{'autofailsafe'}) {
10536:                 if ($failsafe ne '') {
10537:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$failsafe).'</li>';
10538:                 } else {
10539:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section: deleted');
10540:                 }
10541:                 &Apache::lonnet::get_domain_defaults($dom,1);
10542:                 if (ref($lastactref) eq 'HASH') {
10543:                     $lastactref->{'domdefaults'} = 1;
10544:                 }
10545:             }
10546:             $resulttext .= '</ul>';
10547:         } else {
10548:             $resulttext = &mt('No changes made to auto-enrollment settings');
10549:         }
10550:     } else {
10551:         $resulttext = '<span class="LC_error">'.
10552: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10553:     }
10554:     return $resulttext;
10555: }
10556: 
10557: sub modify_autoupdate {
10558:     my ($dom,%domconfig) = @_;
10559:     my ($resulttext,%currautoupdate,%fields,%changes);
10560:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
10561:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
10562:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
10563:         }
10564:     }
10565:     my @offon = ('off','on');
10566:     my %title = &Apache::lonlocal::texthash (
10567:                    run => 'Auto-update:',
10568:                    classlists => 'Updates to user information in classlists?'
10569:                 );
10570:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10571:     my %fieldtitles = &Apache::lonlocal::texthash (
10572:                         id => 'Student/Employee ID',
10573:                         permanentemail => 'E-mail address',
10574:                         lastname => 'Last Name',
10575:                         firstname => 'First Name',
10576:                         middlename => 'Middle Name',
10577:                         generation => 'Generation',
10578:                       );
10579:     $othertitle = &mt('All users');
10580:     if (keys(%{$usertypes}) >  0) {
10581:         $othertitle = &mt('Other users');
10582:     }
10583:     foreach my $key (keys(%env)) {
10584:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
10585:             my ($usertype,$item) = ($1,$2);
10586:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
10587:                 if ($usertype eq 'default') {   
10588:                     push(@{$fields{$1}},$2);
10589:                 } elsif (ref($types) eq 'ARRAY') {
10590:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
10591:                         push(@{$fields{$1}},$2);
10592:                     }
10593:                 }
10594:             }
10595:         }
10596:     }
10597:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
10598:     @lockablenames = sort(@lockablenames);
10599:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
10600:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
10601:         if (@changed) {
10602:             $changes{'lockablenames'} = 1;
10603:         }
10604:     } else {
10605:         if (@lockablenames) {
10606:             $changes{'lockablenames'} = 1;
10607:         }
10608:     }
10609:     my %updatehash = (
10610:                       autoupdate => { run => $env{'form.autoupdate_run'},
10611:                                       classlists => $env{'form.classlists'},
10612:                                       fields => {%fields},
10613:                                       lockablenames => \@lockablenames,
10614:                                     }
10615:                      );
10616:     foreach my $key (keys(%currautoupdate)) {
10617:         if (($key eq 'run') || ($key eq 'classlists')) {
10618:             if (exists($updatehash{autoupdate}{$key})) {
10619:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
10620:                     $changes{$key} = 1;
10621:                 }
10622:             }
10623:         } elsif ($key eq 'fields') {
10624:             if (ref($currautoupdate{$key}) eq 'HASH') {
10625:                 foreach my $item (@{$types},'default') {
10626:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
10627:                         my $change = 0;
10628:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
10629:                             if (!exists($fields{$item})) {
10630:                                 $change = 1;
10631:                                 last;
10632:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
10633:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
10634:                                     $change = 1;
10635:                                     last;
10636:                                 }
10637:                             }
10638:                         }
10639:                         if ($change) {
10640:                             push(@{$changes{$key}},$item);
10641:                         }
10642:                     } 
10643:                 }
10644:             }
10645:         } elsif ($key eq 'lockablenames') {
10646:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
10647:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
10648:                 if (@changed) {
10649:                     $changes{'lockablenames'} = 1;
10650:                 }
10651:             } else {
10652:                 if (@lockablenames) {
10653:                     $changes{'lockablenames'} = 1;
10654:                 }
10655:             }
10656:         }
10657:     }
10658:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
10659:         if (@lockablenames) {
10660:             $changes{'lockablenames'} = 1;
10661:         }
10662:     }
10663:     foreach my $item (@{$types},'default') {
10664:         if (defined($fields{$item})) {
10665:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
10666:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
10667:                     my $change = 0;
10668:                     if (ref($fields{$item}) eq 'ARRAY') {
10669:                         foreach my $type (@{$fields{$item}}) {
10670:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
10671:                                 $change = 1;
10672:                                 last;
10673:                             }
10674:                         }
10675:                     }
10676:                     if ($change) {
10677:                         push(@{$changes{'fields'}},$item);
10678:                     }
10679:                 } else {
10680:                     push(@{$changes{'fields'}},$item);
10681:                 }
10682:             } else {
10683:                 push(@{$changes{'fields'}},$item);
10684:             }
10685:         }
10686:     }
10687:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
10688:                                              $dom);
10689:     if ($putresult eq 'ok') {
10690:         if (keys(%changes) > 0) {
10691:             $resulttext = &mt('Changes made:').'<ul>';
10692:             foreach my $key (sort(keys(%changes))) {
10693:                 if ($key eq 'lockablenames') {
10694:                     $resulttext .= '<li>';
10695:                     if (@lockablenames) {
10696:                         $usertypes->{'default'} = $othertitle;
10697:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
10698:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
10699:                     } else {
10700:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
10701:                     }
10702:                     $resulttext .= '</li>';
10703:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
10704:                     foreach my $item (@{$changes{$key}}) {
10705:                         my @newvalues;
10706:                         foreach my $type (@{$fields{$item}}) {
10707:                             push(@newvalues,$fieldtitles{$type});
10708:                         }
10709:                         my $newvaluestr;
10710:                         if (@newvalues > 0) {
10711:                             $newvaluestr = join(', ',@newvalues);
10712:                         } else {
10713:                             $newvaluestr = &mt('none');
10714:                         }
10715:                         if ($item eq 'default') {
10716:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
10717:                         } else {
10718:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
10719:                         }
10720:                     }
10721:                 } else {
10722:                     my $newvalue;
10723:                     if ($key eq 'run') {
10724:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
10725:                     } else {
10726:                         $newvalue = $offon[$env{'form.'.$key}];
10727:                     }
10728:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
10729:                 }
10730:             }
10731:             $resulttext .= '</ul>';
10732:         } else {
10733:             $resulttext = &mt('No changes made to autoupdates');
10734:         }
10735:     } else {
10736:         $resulttext = '<span class="LC_error">'.
10737: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10738:     }
10739:     return $resulttext;
10740: }
10741: 
10742: sub modify_autocreate {
10743:     my ($dom,%domconfig) = @_;
10744:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
10745:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
10746:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
10747:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
10748:         }
10749:     }
10750:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
10751:                  req => 'Auto-creation of validated requests for official courses',
10752:                  xmldc => 'Identity of course creator of courses from XML files',
10753:                );
10754:     my @types = ('xml','req');
10755:     foreach my $item (@types) {
10756:         $newvals{$item} = $env{'form.autocreate_'.$item};
10757:         $newvals{$item} =~ s/\D//g;
10758:         $newvals{$item} = 0 if ($newvals{$item} eq '');
10759:     }
10760:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
10761:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
10762:     unless (exists($domcoords{$newvals{'xmldc'}})) {
10763:         $newvals{'xmldc'} = '';
10764:     } 
10765:     %autocreatehash =  (
10766:                         autocreate => { xml => $newvals{'xml'},
10767:                                         req => $newvals{'req'},
10768:                                       }
10769:                        );
10770:     if ($newvals{'xmldc'} ne '') {
10771:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
10772:     }
10773:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
10774:                                              $dom);
10775:     if ($putresult eq 'ok') {
10776:         my @items = @types;
10777:         if ($newvals{'xml'}) {
10778:             push(@items,'xmldc');
10779:         }
10780:         foreach my $item (@items) {
10781:             if (exists($currautocreate{$item})) {
10782:                 if ($currautocreate{$item} ne $newvals{$item}) {
10783:                     $changes{$item} = 1;
10784:                 }
10785:             } elsif ($newvals{$item}) {
10786:                 $changes{$item} = 1;
10787:             }
10788:         }
10789:         if (keys(%changes) > 0) {
10790:             my @offon = ('off','on'); 
10791:             $resulttext = &mt('Changes made:').'<ul>';
10792:             foreach my $item (@types) {
10793:                 if ($changes{$item}) {
10794:                     my $newtxt = $offon[$newvals{$item}];
10795:                     $resulttext .= '<li>'.
10796:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
10797:                                        '<b>','</b>').
10798:                                    '</li>';
10799:                 }
10800:             }
10801:             if ($changes{'xmldc'}) {
10802:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
10803:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
10804:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
10805:             }
10806:             $resulttext .= '</ul>';
10807:         } else {
10808:             $resulttext = &mt('No changes made to auto-creation settings');
10809:         }
10810:     } else {
10811:         $resulttext = '<span class="LC_error">'.
10812:             &mt('An error occurred: [_1]',$putresult).'</span>';
10813:     }
10814:     return $resulttext;
10815: }
10816: 
10817: sub modify_directorysrch {
10818:     my ($dom,$lastactref,%domconfig) = @_;
10819:     my ($resulttext,%changes);
10820:     my %currdirsrch;
10821:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
10822:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
10823:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
10824:         }
10825:     }
10826:     my %title = ( available => 'Institutional directory search available',
10827:                   localonly => 'Other domains can search institution',
10828:                   lcavailable => 'LON-CAPA directory search available',
10829:                   lclocalonly => 'Other domains can search LON-CAPA domain',
10830:                   searchby => 'Search types',
10831:                   searchtypes => 'Search latitude');
10832:     my @offon = ('off','on');
10833:     my @otherdoms = ('Yes','No');
10834: 
10835:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
10836:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
10837:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
10838: 
10839:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10840:     if (keys(%{$usertypes}) == 0) {
10841:         @cansearch = ('default');
10842:     } else {
10843:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
10844:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
10845:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
10846:                     push(@{$changes{'cansearch'}},$type);
10847:                 }
10848:             }
10849:             foreach my $type (@cansearch) {
10850:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
10851:                     push(@{$changes{'cansearch'}},$type);
10852:                 }
10853:             }
10854:         } else {
10855:             push(@{$changes{'cansearch'}},@cansearch);
10856:         }
10857:     }
10858: 
10859:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
10860:         foreach my $by (@{$currdirsrch{'searchby'}}) {
10861:             if (!grep(/^\Q$by\E$/,@searchby)) {
10862:                 push(@{$changes{'searchby'}},$by);
10863:             }
10864:         }
10865:         foreach my $by (@searchby) {
10866:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
10867:                 push(@{$changes{'searchby'}},$by);
10868:             }
10869:         }
10870:     } else {
10871:         push(@{$changes{'searchby'}},@searchby);
10872:     }
10873: 
10874:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
10875:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
10876:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
10877:                 push(@{$changes{'searchtypes'}},$type);
10878:             }
10879:         }
10880:         foreach my $type (@searchtypes) {
10881:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
10882:                 push(@{$changes{'searchtypes'}},$type);
10883:             }
10884:         }
10885:     } else {
10886:         if (exists($currdirsrch{'searchtypes'})) {
10887:             foreach my $type (@searchtypes) {  
10888:                 if ($type ne $currdirsrch{'searchtypes'}) { 
10889:                     push(@{$changes{'searchtypes'}},$type);
10890:                 }
10891:             }
10892:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
10893:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
10894:             }   
10895:         } else {
10896:             push(@{$changes{'searchtypes'}},@searchtypes); 
10897:         }
10898:     }
10899: 
10900:     my %dirsrch_hash =  (
10901:             directorysrch => { available => $env{'form.dirsrch_available'},
10902:                                cansearch => \@cansearch,
10903:                                localonly => $env{'form.dirsrch_instlocalonly'},
10904:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
10905:                                lcavailable => $env{'form.dirsrch_domavailable'},
10906:                                searchby => \@searchby,
10907:                                searchtypes => \@searchtypes,
10908:                              }
10909:             );
10910:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
10911:                                              $dom);
10912:     if ($putresult eq 'ok') {
10913:         if (exists($currdirsrch{'available'})) {
10914:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
10915:                  $changes{'available'} = 1;
10916:              }
10917:         } else {
10918:             if ($env{'form.dirsrch_available'} eq '1') {
10919:                 $changes{'available'} = 1;
10920:             }
10921:         }
10922:         if (exists($currdirsrch{'lcavailable'})) {
10923:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
10924:                 $changes{'lcavailable'} = 1;
10925:             }
10926:         } else {
10927:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
10928:                 $changes{'lcavailable'} = 1;
10929:             }
10930:         }
10931:         if (exists($currdirsrch{'localonly'})) {
10932:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
10933:                 $changes{'localonly'} = 1;
10934:             }
10935:         } else {
10936:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
10937:                 $changes{'localonly'} = 1;
10938:             }
10939:         }
10940:         if (exists($currdirsrch{'lclocalonly'})) {
10941:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
10942:                 $changes{'lclocalonly'} = 1;
10943:             }
10944:         } else {
10945:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
10946:                 $changes{'lclocalonly'} = 1;
10947:             }
10948:         }
10949:         if (keys(%changes) > 0) {
10950:             $resulttext = &mt('Changes made:').'<ul>';
10951:             if ($changes{'available'}) {
10952:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
10953:             }
10954:             if ($changes{'lcavailable'}) {
10955:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
10956:             }
10957:             if ($changes{'localonly'}) {
10958:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
10959:             }
10960:             if ($changes{'lclocalonly'}) {
10961:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
10962:             }
10963:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
10964:                 my $chgtext;
10965:                 if (ref($usertypes) eq 'HASH') {
10966:                     if (keys(%{$usertypes}) > 0) {
10967:                         foreach my $type (@{$types}) {
10968:                             if (grep(/^\Q$type\E$/,@cansearch)) {
10969:                                 $chgtext .= $usertypes->{$type}.'; ';
10970:                             }
10971:                         }
10972:                         if (grep(/^default$/,@cansearch)) {
10973:                             $chgtext .= $othertitle;
10974:                         } else {
10975:                             $chgtext =~ s/\; $//;
10976:                         }
10977:                         $resulttext .=
10978:                             '<li>'.
10979:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
10980:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
10981:                             '</li>';
10982:                     }
10983:                 }
10984:             }
10985:             if (ref($changes{'searchby'}) eq 'ARRAY') {
10986:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
10987:                 my $chgtext;
10988:                 foreach my $type (@{$titleorder}) {
10989:                     if (grep(/^\Q$type\E$/,@searchby)) {
10990:                         if (defined($searchtitles->{$type})) {
10991:                             $chgtext .= $searchtitles->{$type}.'; ';
10992:                         }
10993:                     }
10994:                 }
10995:                 $chgtext =~ s/\; $//;
10996:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
10997:             }
10998:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
10999:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
11000:                 my $chgtext;
11001:                 foreach my $type (@{$srchtypeorder}) {
11002:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
11003:                         if (defined($srchtypes_desc->{$type})) {
11004:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
11005:                         }
11006:                     }
11007:                 }
11008:                 $chgtext =~ s/\; $//;
11009:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
11010:             }
11011:             $resulttext .= '</ul>';
11012:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
11013:             if (ref($lastactref) eq 'HASH') {
11014:                 $lastactref->{'directorysrch'} = 1;
11015:             }
11016:         } else {
11017:             $resulttext = &mt('No changes made to directory search settings');
11018:         }
11019:     } else {
11020:         $resulttext = '<span class="LC_error">'.
11021:                       &mt('An error occurred: [_1]',$putresult).'</span>';
11022:     }
11023:     return $resulttext;
11024: }
11025: 
11026: sub modify_contacts {
11027:     my ($dom,$lastactref,%domconfig) = @_;
11028:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
11029:     if (ref($domconfig{'contacts'}) eq 'HASH') {
11030:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
11031:             $currsetting{$key} = $domconfig{'contacts'}{$key};
11032:         }
11033:     }
11034:     my (%others,%to,%bcc,%includestr,%includeloc);
11035:     my @contacts = ('supportemail','adminemail');
11036:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
11037:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail');
11038:     my @toggles = ('reporterrors','reportupdates');
11039:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
11040:     foreach my $type (@mailings) {
11041:         @{$newsetting{$type}} = 
11042:             &Apache::loncommon::get_env_multiple('form.'.$type);
11043:         foreach my $item (@contacts) {
11044:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
11045:                 $contacts_hash{contacts}{$type}{$item} = 1;
11046:             } else {
11047:                 $contacts_hash{contacts}{$type}{$item} = 0;
11048:             }
11049:         }
11050:         $others{$type} = $env{'form.'.$type.'_others'};
11051:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
11052:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
11053:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
11054:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
11055:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
11056:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
11057:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
11058:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
11059:             }
11060:         }
11061:     }
11062:     foreach my $item (@contacts) {
11063:         $to{$item} = $env{'form.'.$item};
11064:         $contacts_hash{'contacts'}{$item} = $to{$item};
11065:     }
11066:     foreach my $item (@toggles) {
11067:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
11068:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
11069:         }
11070:     }
11071:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
11072:         foreach my $field (@{$fields}) {
11073:             if (ref($possoptions->{$field}) eq 'ARRAY') {
11074:                 my $value = $env{'form.helpform_'.$field};
11075:                 $value =~ s/^\s+|\s+$//g;
11076:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
11077:                     $contacts_hash{contacts}{'helpform'}{$field} = $value;
11078:                     if ($field eq 'screenshot') {
11079:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
11080:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
11081:                             $contacts_hash{contacts}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
11082:                         }
11083:                     }
11084:                 }
11085:             }
11086:         }
11087:     }
11088:     if (keys(%currsetting) > 0) {
11089:         foreach my $item (@contacts) {
11090:             if ($to{$item} ne $currsetting{$item}) {
11091:                 $changes{$item} = 1;
11092:             }
11093:         }
11094:         foreach my $type (@mailings) {
11095:             foreach my $item (@contacts) {
11096:                 if (ref($currsetting{$type}) eq 'HASH') {
11097:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
11098:                         push(@{$changes{$type}},$item);
11099:                     }
11100:                 } else {
11101:                     push(@{$changes{$type}},@{$newsetting{$type}});
11102:                 }
11103:             }
11104:             if ($others{$type} ne $currsetting{$type}{'others'}) {
11105:                 push(@{$changes{$type}},'others');
11106:             }
11107:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
11108:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
11109:                     push(@{$changes{$type}},'bcc'); 
11110:                 }
11111:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
11112:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
11113:                     push(@{$changes{$type}},'include');
11114:                 }
11115:             }
11116:         }
11117:         if (ref($fields) eq 'ARRAY') {
11118:             if (ref($currsetting{'helpform'}) eq 'HASH') {
11119:                 foreach my $field (@{$fields}) {
11120:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
11121:                         push(@{$changes{'helpform'}},$field);
11122:                     }
11123:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
11124:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
11125:                             push(@{$changes{'helpform'}},'maxsize');
11126:                         }
11127:                     }
11128:                 }
11129:             } else {
11130:                 foreach my $field (@{$fields}) {
11131:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
11132:                         push(@{$changes{'helpform'}},$field);
11133:                     }
11134:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
11135:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
11136:                             push(@{$changes{'helpform'}},'maxsize');
11137:                         }
11138:                     }
11139:                 }
11140:             }
11141:         }
11142:     } else {
11143:         my %default;
11144:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
11145:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
11146:         $default{'errormail'} = 'adminemail';
11147:         $default{'packagesmail'} = 'adminemail';
11148:         $default{'helpdeskmail'} = 'supportemail';
11149:         $default{'otherdomsmail'} = 'supportemail';
11150:         $default{'lonstatusmail'} = 'adminemail';
11151:         $default{'requestsmail'} = 'adminemail';
11152:         $default{'updatesmail'} = 'adminemail';
11153:         foreach my $item (@contacts) {
11154:            if ($to{$item} ne $default{$item}) {
11155:                $changes{$item} = 1;
11156:            }
11157:         }
11158:         foreach my $type (@mailings) {
11159:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
11160:                 push(@{$changes{$type}},@{$newsetting{$type}});
11161:             }
11162:             if ($others{$type} ne '') {
11163:                 push(@{$changes{$type}},'others');
11164:             }
11165:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
11166:                 if ($bcc{$type} ne '') {
11167:                     push(@{$changes{$type}},'bcc');
11168:                 }
11169:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
11170:                     push(@{$changes{$type}},'include');
11171:                 }
11172:             }
11173:         }
11174:         if (ref($fields) eq 'ARRAY') {
11175:             foreach my $field (@{$fields}) {
11176:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
11177:                     push(@{$changes{'helpform'}},$field);
11178:                 }
11179:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
11180:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
11181:                         push(@{$changes{'helpform'}},'maxsize');
11182:                     }
11183:                 }
11184:             }
11185:         }
11186:     }
11187:     foreach my $item (@toggles) {
11188:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
11189:             $changes{$item} = 1;
11190:         } elsif ((!$env{'form.'.$item}) &&
11191:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
11192:             $changes{$item} = 1;
11193:         }
11194:     }
11195:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
11196:                                              $dom);
11197:     if ($putresult eq 'ok') {
11198:         if (keys(%changes) > 0) {
11199:             &Apache::loncommon::devalidate_domconfig_cache($dom);
11200:             if (ref($lastactref) eq 'HASH') {
11201:                 $lastactref->{'domainconfig'} = 1;
11202:             }
11203:             my ($titles,$short_titles)  = &contact_titles();
11204:             $resulttext = &mt('Changes made:').'<ul>';
11205:             foreach my $item (@contacts) {
11206:                 if ($changes{$item}) {
11207:                     $resulttext .= '<li>'.$titles->{$item}.
11208:                                     &mt(' set to: ').
11209:                                     '<span class="LC_cusr_emph">'.
11210:                                     $to{$item}.'</span></li>';
11211:                 }
11212:             }
11213:             foreach my $type (@mailings) {
11214:                 if (ref($changes{$type}) eq 'ARRAY') {
11215:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
11216:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
11217:                     } else {
11218:                         $resulttext .= '<li>'.$titles->{$type}.': ';
11219:                     }
11220:                     my @text;
11221:                     foreach my $item (@{$newsetting{$type}}) {
11222:                         push(@text,$short_titles->{$item});
11223:                     }
11224:                     if ($others{$type} ne '') {
11225:                         push(@text,$others{$type});
11226:                     }
11227:                     if (@text) {
11228:                         $resulttext .= '<span class="LC_cusr_emph">'.
11229:                                        join(', ',@text).'</span>';
11230:                     }
11231:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
11232:                         if ($bcc{$type} ne '') {
11233:                             my $bcctext;
11234:                             if (@text) {
11235:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
11236:                             } else {
11237:                                 $bcctext = '(Bcc)';
11238:                             }
11239:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
11240:                         } elsif (!@text) {
11241:                             $resulttext .= &mt('No one');
11242:                         }   
11243:                         if ($includestr{$type} ne '') {
11244:                             if ($includeloc{$type} eq 'b') {
11245:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
11246:                             } elsif ($includeloc{$type} eq 's') {
11247:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
11248:                             }
11249:                         }
11250:                     } elsif (!@text) {
11251:                         $resulttext .= &mt('No recipients');
11252:                     }
11253:                     $resulttext .= '</li>';
11254:                 }
11255:             }
11256:             my @offon = ('off','on');
11257:             if ($changes{'reporterrors'}) {
11258:                 $resulttext .= '<li>'.
11259:                                &mt('E-mail error reports to [_1] set to "'.
11260:                                    $offon[$env{'form.reporterrors'}].'".',
11261:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
11262:                                        &mt('LON-CAPA core group - MSU'),600,500)).
11263:                                '</li>';
11264:             }
11265:             if ($changes{'reportupdates'}) {
11266:                 $resulttext .= '<li>'.
11267:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
11268:                                     $offon[$env{'form.reportupdates'}].'".',
11269:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
11270:                                         &mt('LON-CAPA core group - MSU'),600,500)).
11271:                                 '</li>';
11272:             }
11273:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
11274:                 my (@optional,@required,@unused,$maxsizechg);
11275:                 foreach my $field (@{$changes{'helpform'}}) {
11276:                     if ($field eq 'maxsize') {
11277:                         $maxsizechg = 1;
11278:                         next;
11279:                     }
11280:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
11281:                         push(@optional,$field);
11282:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
11283:                         push(@unused,$field);
11284:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
11285:                         push(@required,$field);
11286:                     }
11287:                 }
11288:                 if (@optional) {
11289:                     $resulttext .= '<li>'.
11290:                                    &mt('Help form fields changed to "Optional": [_1].',
11291:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
11292:                                    '</li>';
11293:                 }
11294:                 if (@required) {
11295:                     $resulttext .= '<li>'.
11296:                                    &mt('Help form fields changed to "Required": [_1].',
11297:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
11298:                                    '</li>';
11299:                 }
11300:                 if (@unused) {
11301:                     $resulttext .= '<li>'.
11302:                                    &mt('Help form fields changed to "Not shown": [_1].',
11303:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
11304:                                    '</li>';
11305:                 }
11306:                 if ($maxsizechg) {
11307:                     $resulttext .= '<li>'.
11308:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
11309:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
11310:                                    '</li>';
11311: 
11312:                 }
11313:             }
11314:             $resulttext .= '</ul>';
11315:         } else {
11316:             $resulttext = &mt('No changes made to contacts and form settings');
11317:         }
11318:     } else {
11319:         $resulttext = '<span class="LC_error">'.
11320:             &mt('An error occurred: [_1].',$putresult).'</span>';
11321:     }
11322:     return $resulttext;
11323: }
11324: 
11325: sub modify_usercreation {
11326:     my ($dom,%domconfig) = @_;
11327:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
11328:     my $warningmsg;
11329:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
11330:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
11331:             if ($key eq 'cancreate') {
11332:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
11333:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
11334:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
11335:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11336:                         } else {
11337:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11338:                         }
11339:                     }
11340:                 }
11341:             } elsif ($key eq 'email_rule') {
11342:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
11343:             } else {
11344:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
11345:             }
11346:         }
11347:     }
11348:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
11349:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
11350:     my @contexts = ('author','course','requestcrs');
11351:     foreach my $item(@contexts) {
11352:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
11353:     }
11354:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11355:         foreach my $item (@contexts) {
11356:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
11357:                 push(@{$changes{'cancreate'}},$item);
11358:             }
11359:         }
11360:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
11361:         foreach my $item (@contexts) {
11362:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
11363:                 if ($cancreate{$item} ne 'any') {
11364:                     push(@{$changes{'cancreate'}},$item);
11365:                 }
11366:             } else {
11367:                 if ($cancreate{$item} ne 'none') {
11368:                     push(@{$changes{'cancreate'}},$item);
11369:                 }
11370:             }
11371:         }
11372:     } else {
11373:         foreach my $item (@contexts)  {
11374:             push(@{$changes{'cancreate'}},$item);
11375:         }
11376:     }
11377: 
11378:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
11379:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
11380:             if (!grep(/^\Q$type\E$/,@username_rule)) {
11381:                 push(@{$changes{'username_rule'}},$type);
11382:             }
11383:         }
11384:         foreach my $type (@username_rule) {
11385:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
11386:                 push(@{$changes{'username_rule'}},$type);
11387:             }
11388:         }
11389:     } else {
11390:         push(@{$changes{'username_rule'}},@username_rule);
11391:     }
11392: 
11393:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
11394:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
11395:             if (!grep(/^\Q$type\E$/,@id_rule)) {
11396:                 push(@{$changes{'id_rule'}},$type);
11397:             }
11398:         }
11399:         foreach my $type (@id_rule) {
11400:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
11401:                 push(@{$changes{'id_rule'}},$type);
11402:             }
11403:         }
11404:     } else {
11405:         push(@{$changes{'id_rule'}},@id_rule);
11406:     }
11407: 
11408:     my @authen_contexts = ('author','course','domain');
11409:     my @authtypes = ('int','krb4','krb5','loc');
11410:     my %authhash;
11411:     foreach my $item (@authen_contexts) {
11412:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
11413:         foreach my $auth (@authtypes) {
11414:             if (grep(/^\Q$auth\E$/,@authallowed)) {
11415:                 $authhash{$item}{$auth} = 1;
11416:             } else {
11417:                 $authhash{$item}{$auth} = 0;
11418:             }
11419:         }
11420:     }
11421:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
11422:         foreach my $item (@authen_contexts) {
11423:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
11424:                 foreach my $auth (@authtypes) {
11425:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
11426:                         push(@{$changes{'authtypes'}},$item);
11427:                         last;
11428:                     }
11429:                 }
11430:             }
11431:         }
11432:     } else {
11433:         foreach my $item (@authen_contexts) {
11434:             push(@{$changes{'authtypes'}},$item);
11435:         }
11436:     }
11437: 
11438:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
11439:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
11440:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
11441:     $save_usercreate{'id_rule'} = \@id_rule;
11442:     $save_usercreate{'username_rule'} = \@username_rule,
11443:     $save_usercreate{'authtypes'} = \%authhash;
11444: 
11445:     my %usercreation_hash =  (
11446:         usercreation     => \%save_usercreate,
11447:     );
11448: 
11449:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
11450:                                              $dom);
11451: 
11452:     if ($putresult eq 'ok') {
11453:         if (keys(%changes) > 0) {
11454:             $resulttext = &mt('Changes made:').'<ul>';
11455:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
11456:                 my %lt = &usercreation_types();
11457:                 foreach my $type (@{$changes{'cancreate'}}) {
11458:                     my $chgtext = $lt{$type}.', ';
11459:                     if ($cancreate{$type} eq 'none') {
11460:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
11461:                     } elsif ($cancreate{$type} eq 'any') {
11462:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
11463:                     } elsif ($cancreate{$type} eq 'official') {
11464:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
11465:                     } elsif ($cancreate{$type} eq 'unofficial') {
11466:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
11467:                     }
11468:                     $resulttext .= '<li>'.$chgtext.'</li>';
11469:                 }
11470:             }
11471:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
11472:                 my ($rules,$ruleorder) = 
11473:                     &Apache::lonnet::inst_userrules($dom,'username');
11474:                 my $chgtext = '<ul>';
11475:                 foreach my $type (@username_rule) {
11476:                     if (ref($rules->{$type}) eq 'HASH') {
11477:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
11478:                     }
11479:                 }
11480:                 $chgtext .= '</ul>';
11481:                 if (@username_rule > 0) {
11482:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
11483:                 } else {
11484:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
11485:                 }
11486:             }
11487:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
11488:                 my ($idrules,$idruleorder) = 
11489:                     &Apache::lonnet::inst_userrules($dom,'id');
11490:                 my $chgtext = '<ul>';
11491:                 foreach my $type (@id_rule) {
11492:                     if (ref($idrules->{$type}) eq 'HASH') {
11493:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
11494:                     }
11495:                 }
11496:                 $chgtext .= '</ul>';
11497:                 if (@id_rule > 0) {
11498:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
11499:                 } else {
11500:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
11501:                 }
11502:             }
11503:             my %authname = &authtype_names();
11504:             my %context_title = &context_names();
11505:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
11506:                 my $chgtext = '<ul>';
11507:                 foreach my $type (@{$changes{'authtypes'}}) {
11508:                     my @allowed;
11509:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
11510:                     foreach my $auth (@authtypes) {
11511:                         if ($authhash{$type}{$auth}) {
11512:                             push(@allowed,$authname{$auth});
11513:                         }
11514:                     }
11515:                     if (@allowed > 0) {
11516:                         $chgtext .= join(', ',@allowed).'</li>';
11517:                     } else {
11518:                         $chgtext .= &mt('none').'</li>';
11519:                     }
11520:                 }
11521:                 $chgtext .= '</ul>';
11522:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
11523:                 $resulttext .= '</li>';
11524:             }
11525:             $resulttext .= '</ul>';
11526:         } else {
11527:             $resulttext = &mt('No changes made to user creation settings');
11528:         }
11529:     } else {
11530:         $resulttext = '<span class="LC_error">'.
11531:             &mt('An error occurred: [_1]',$putresult).'</span>';
11532:     }
11533:     if ($warningmsg ne '') {
11534:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
11535:     }
11536:     return $resulttext;
11537: }
11538: 
11539: sub modify_selfcreation {
11540:     my ($dom,$lastactref,%domconfig) = @_;
11541:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
11542:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
11543:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11544:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
11545:     if (ref($typesref) eq 'ARRAY') {
11546:         @types = @{$typesref};
11547:     }
11548:     if (ref($usertypesref) eq 'HASH') {
11549:         %usertypes = %{$usertypesref};
11550:     }
11551:     $usertypes{'default'} = $othertitle;
11552: #
11553: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
11554: #
11555:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
11556:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
11557:             if ($key eq 'cancreate') {
11558:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
11559:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
11560:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
11561:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
11562:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
11563:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
11564:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
11565:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
11566:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11567:                         } else {
11568:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11569:                         }
11570:                     }
11571:                 }
11572:             } elsif ($key eq 'email_rule') {
11573:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
11574:             } else {
11575:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
11576:             }
11577:         }
11578:     }
11579: #
11580: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
11581: #
11582:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
11583:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
11584:             if ($key eq 'selfcreate') {
11585:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
11586:             } else {
11587:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
11588:             }
11589:         }
11590:     }
11591: #
11592: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
11593: #
11594:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
11595:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
11596:             if ($key eq 'inststatusguest') {
11597:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
11598:             } else {
11599:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
11600:             }
11601:         }
11602:     }
11603: 
11604:     my @contexts = ('selfcreate');
11605:     @{$cancreate{'selfcreate'}} = ();
11606:     %{$cancreate{'emailusername'}} = ();
11607:     if (@types) {
11608:         @{$cancreate{'statustocreate'}} = ();
11609:     }
11610:     %{$cancreate{'selfcreateprocessing'}} = ();
11611:     %{$cancreate{'shibenv'}} = ();
11612:     %{$cancreate{'emailverified'}} = ();
11613:     %{$cancreate{'emailoptions'}} = ();
11614:     %{$cancreate{'emaildomain'}} = ();
11615:     my %selfcreatetypes = (
11616:                              sso   => 'users authenticated by institutional single sign on',
11617:                              login => 'users authenticated by institutional log-in',
11618:                              email => 'users verified by e-mail',
11619:                           );
11620: #
11621: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
11622: # is permitted.
11623: #
11624:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
11625: 
11626:     my (@statuses,%email_rule);
11627:     foreach my $item ('login','sso','email') {
11628:         if ($item eq 'email') {
11629:             if ($env{'form.cancreate_email'}) {
11630:                 if (@types) {
11631:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
11632:                     foreach my $status (@poss_statuses) {
11633:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
11634:                             push(@statuses,$status);
11635:                         }
11636:                     }
11637:                     $save_inststatus{'inststatusguest'} = \@statuses;
11638:                 } else {
11639:                     push(@statuses,'default');
11640:                 }
11641:                 if (@statuses) {
11642:                     my %curr_rule;
11643:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
11644:                         foreach my $type (@statuses) {
11645:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
11646:                         }
11647:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
11648:                         foreach my $type (@statuses) {
11649:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
11650:                         }
11651:                     }
11652:                     push(@{$cancreate{'selfcreate'}},'email');
11653:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
11654:                     my %curremaildom;
11655:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
11656:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
11657:                     }
11658:                     foreach my $type (@statuses) {
11659:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
11660:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
11661:                         }
11662:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
11663:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
11664:                         }
11665:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
11666: #
11667: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
11668: #
11669:                             my $chosen = $1;
11670:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
11671:                                 my $emaildom;
11672:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
11673:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type}; 
11674:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
11675:                                     if (ref($curremaildom{$type}) eq 'HASH') {
11676:                                         if (exists($curremaildom{$type}{$chosen})) {
11677:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
11678:                                                 push(@{$changes{'cancreate'}},'emaildomain');
11679:                                             }
11680:                                         } elsif ($emaildom ne '') {
11681:                                             push(@{$changes{'cancreate'}},'emaildomain');
11682:                                         }
11683:                                     } elsif ($emaildom ne '') {
11684:                                         push(@{$changes{'cancreate'}},'emaildomain');
11685:                                     } 
11686:                                 }
11687:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
11688:                             } elsif ($chosen eq 'custom') {
11689:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
11690:                                 $email_rule{$type} = [];
11691:                                 if (ref($emailrules) eq 'HASH') {
11692:                                     foreach my $rule (@possemail_rules) {
11693:                                         if (exists($emailrules->{$rule})) {
11694:                                             push(@{$email_rule{$type}},$rule);
11695:                                         }
11696:                                     }
11697:                                 }
11698:                                 if (@{$email_rule{$type}}) {
11699:                                     $cancreate{'emailoptions'}{$type} = 'custom';
11700:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
11701:                                         if (@{$curr_rule{$type}} > 0) {
11702:                                             foreach my $rule (@{$curr_rule{$type}}) {
11703:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
11704:                                                     push(@{$changes{'email_rule'}},$type);
11705:                                                 }
11706:                                             }
11707:                                         }
11708:                                         foreach my $type (@{$email_rule{$type}}) {
11709:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
11710:                                                 push(@{$changes{'email_rule'}},$type);
11711:                                             }
11712:                                         }
11713:                                     } else {
11714:                                         push(@{$changes{'email_rule'}},$type);
11715:                                     }
11716:                                 }
11717:                             } else {
11718:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
11719:                             }
11720:                         }
11721:                     }
11722:                     if (@types) {
11723:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
11724:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
11725:                             if (@changed) {
11726:                                 push(@{$changes{'inststatus'}},'inststatusguest');
11727:                             }
11728:                         } else {
11729:                             push(@{$changes{'inststatus'}},'inststatusguest');
11730:                         }
11731:                     }
11732:                 } else {
11733:                     delete($env{'form.cancreate_email'});
11734:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
11735:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
11736:                             push(@{$changes{'inststatus'}},'inststatusguest');
11737:                         }
11738:                     }
11739:                 }
11740:             } else {
11741:                 $save_inststatus{'inststatusguest'} = [];
11742:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
11743:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
11744:                         push(@{$changes{'inststatus'}},'inststatusguest');
11745:                     }
11746:                 }
11747:             }
11748:         } else {
11749:             if ($env{'form.cancreate_'.$item}) {
11750:                 push(@{$cancreate{'selfcreate'}},$item);
11751:             }
11752:         }
11753:     }
11754:     my (%userinfo,%savecaptcha);
11755:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
11756: #
11757: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
11758: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
11759: #
11760: 
11761:     if ($env{'form.cancreate_email'}) {
11762:         push(@contexts,'emailusername');
11763:         if (@statuses) {
11764:             foreach my $type (@statuses) {
11765:                 if (ref($infofields) eq 'ARRAY') {
11766:                     foreach my $field (@{$infofields}) {
11767:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
11768:                             $cancreate{'emailusername'}{$type}{$field} = $1;
11769:                         }
11770:                     }
11771:                 }
11772:             }
11773:         }
11774: #
11775: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
11776: # queued requests for self-creation of account verified by e-mail.
11777: #
11778: 
11779:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
11780:         @approvalnotify = sort(@approvalnotify);
11781:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
11782:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11783:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
11784:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
11785:                     push(@{$changes{'cancreate'}},'notify');
11786:                 }
11787:             } else {
11788:                 if ($cancreate{'notify'}{'approval'}) {
11789:                     push(@{$changes{'cancreate'}},'notify');
11790:                 }
11791:             }
11792:         } elsif ($cancreate{'notify'}{'approval'}) {
11793:             push(@{$changes{'cancreate'}},'notify');
11794:         }
11795: 
11796:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
11797:     }
11798: #  
11799: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
11800: # institutional log-in.
11801: #
11802:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
11803:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
11804:                ($domdefaults{'auth_def'} eq 'localauth'))) {
11805:             $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.').' '.
11806:                           &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.');
11807:         }
11808:     }
11809:     my @fields = ('lastname','firstname','middlename','generation',
11810:                   'permanentemail','id');
11811:     my @shibfields = (@fields,'inststatus');
11812:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11813: #
11814: # Where usernames may created for institutional log-in and/or institutional single sign on:
11815: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
11816: # may self-create accounts 
11817: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
11818: # which the user may supply, if institutional data is unavailable.
11819: #
11820:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
11821:         if (@types) {
11822:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
11823:             push(@contexts,'statustocreate');
11824:             foreach my $type (@types) {
11825:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
11826:                 foreach my $field (@fields) {
11827:                     if (grep(/^\Q$field\E$/,@modifiable)) {
11828:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
11829:                     } else {
11830:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
11831:                     }
11832:                 }
11833:             }
11834:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
11835:                 foreach my $type (@types) {
11836:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
11837:                         foreach my $field (@fields) {
11838:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
11839:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
11840:                                 push(@{$changes{'selfcreate'}},$type);
11841:                                 last;
11842:                             }
11843:                         }
11844:                     }
11845:                 }
11846:             } else {
11847:                 foreach my $type (@types) {
11848:                     push(@{$changes{'selfcreate'}},$type);
11849:                 }
11850:             }
11851:         }
11852:         foreach my $field (@shibfields) {
11853:             if ($env{'form.shibenv_'.$field} ne '') {
11854:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
11855:             }
11856:         }
11857:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11858:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
11859:                 foreach my $field (@shibfields) {
11860:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
11861:                         push(@{$changes{'cancreate'}},'shibenv');
11862:                     }
11863:                 }
11864:             } else {
11865:                 foreach my $field (@shibfields) {
11866:                     if ($env{'form.shibenv_'.$field}) {
11867:                         push(@{$changes{'cancreate'}},'shibenv');
11868:                         last;
11869:                     }
11870:                 }
11871:             }
11872:         }
11873:     }
11874:     foreach my $item (@contexts) {
11875:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
11876:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
11877:                 if (ref($cancreate{$item}) eq 'ARRAY') {
11878:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
11879:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11880:                             push(@{$changes{'cancreate'}},$item);
11881:                         }
11882:                     }
11883:                 }
11884:             }
11885:             if (ref($cancreate{$item}) eq 'ARRAY') {
11886:                 foreach my $type (@{$cancreate{$item}}) {
11887:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
11888:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11889:                             push(@{$changes{'cancreate'}},$item);
11890:                         }
11891:                     }
11892:                 }
11893:             }
11894:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
11895:             if (ref($cancreate{$item}) eq 'HASH') {
11896:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
11897:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
11898:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
11899:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
11900:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11901:                                     push(@{$changes{'cancreate'}},$item);
11902:                                 }
11903:                             }
11904:                         }
11905:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
11906:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
11907:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11908:                                 push(@{$changes{'cancreate'}},$item);
11909:                             }
11910:                         }
11911:                     }
11912:                 }
11913:                 foreach my $type (keys(%{$cancreate{$item}})) {
11914:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
11915:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
11916:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
11917:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
11918:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11919:                                         push(@{$changes{'cancreate'}},$item);
11920:                                     }
11921:                                 }
11922:                             } else {
11923:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11924:                                     push(@{$changes{'cancreate'}},$item);
11925:                                 }
11926:                             }
11927:                         }
11928:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
11929:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
11930:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11931:                                 push(@{$changes{'cancreate'}},$item);
11932:                             }
11933:                         }
11934:                     }
11935:                 }
11936:             }
11937:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
11938:             if (ref($cancreate{$item}) eq 'ARRAY') {
11939:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
11940:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11941:                         push(@{$changes{'cancreate'}},$item);
11942:                     }
11943:                 }
11944:             }
11945:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
11946:             if (ref($cancreate{$item}) eq 'HASH') {
11947:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11948:                     push(@{$changes{'cancreate'}},$item);
11949:                 }
11950:             }
11951:         } elsif ($item eq 'emailusername') {
11952:             if (ref($cancreate{$item}) eq 'HASH') {
11953:                 foreach my $type (keys(%{$cancreate{$item}})) {
11954:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
11955:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
11956:                             if ($cancreate{$item}{$type}{$field}) {
11957:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11958:                                     push(@{$changes{'cancreate'}},$item);
11959:                                 }
11960:                                 last;
11961:                             }
11962:                         }
11963:                     }
11964:                 }
11965:             }
11966:         }
11967:     }
11968: #
11969: # Populate %save_usercreate hash with updates to self-creation configuration.
11970: #
11971:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
11972:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
11973:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
11974:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
11975:     if (ref($cancreate{'notify'}) eq 'HASH') {
11976:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
11977:     }
11978:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
11979:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
11980:     }
11981:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
11982:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
11983:     }
11984:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
11985:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
11986:     }
11987:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
11988:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
11989:     }
11990:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
11991:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
11992:     }
11993:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
11994:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
11995:     }
11996:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
11997:     $save_usercreate{'email_rule'} = \%email_rule;
11998: 
11999:     my %userconfig_hash = (
12000:             usercreation     => \%save_usercreate,
12001:             usermodification => \%save_usermodify,
12002:             inststatus       => \%save_inststatus,
12003:     );
12004: 
12005:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
12006:                                              $dom);
12007: #
12008: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
12009: #
12010:     if ($putresult eq 'ok') {
12011:         if (keys(%changes) > 0) {
12012:             $resulttext = &mt('Changes made:').'<ul>';
12013:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
12014:                 my %lt = &selfcreation_types();
12015:                 foreach my $type (@{$changes{'cancreate'}}) {
12016:                     my $chgtext = '';
12017:                     if ($type eq 'selfcreate') {
12018:                         if (@{$cancreate{$type}} == 0) {
12019:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
12020:                         } else {
12021:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
12022:                                         '<ul>';
12023:                             foreach my $case (@{$cancreate{$type}}) {
12024:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
12025:                             }
12026:                             $chgtext .= '</ul>';
12027:                             if (ref($cancreate{$type}) eq 'ARRAY') {
12028:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
12029:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
12030:                                         if (@{$cancreate{'statustocreate'}} == 0) {
12031:                                             $chgtext .= '<span class="LC_warning">'.
12032:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
12033:                                                         '</span><br />';
12034:                                         }
12035:                                     }
12036:                                 }
12037:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
12038:                                     if (!@statuses) {
12039:                                         $chgtext .= '<span class="LC_warning">'.
12040:                                                     &mt("However, e-mail verification is currently set to 'unavailable' for all user types (including 'other'), so self-creation of accounts is not possible for non-institutional log-in.").
12041:                                                     '</span><br />';
12042: 
12043:                                     }
12044:                                 }
12045:                             }
12046:                         }
12047:                     } elsif ($type eq 'shibenv') {
12048:                         if (keys(%{$cancreate{$type}}) == 0) {
12049:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
12050:                         } else {
12051:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
12052:                                         '<ul>';
12053:                             foreach my $field (@shibfields) {
12054:                                 next if ($cancreate{$type}{$field} eq '');
12055:                                 if ($field eq 'inststatus') {
12056:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
12057:                                 } else {
12058:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
12059:                                 }
12060:                             }
12061:                             $chgtext .= '</ul>';
12062:                         }
12063:                     } elsif ($type eq 'statustocreate') {
12064:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
12065:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
12066:                             if (@{$cancreate{'selfcreate'}} > 0) {
12067:                                 if (@{$cancreate{'statustocreate'}} == 0) {
12068:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
12069:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
12070:                                         $chgtext .= '<br />'.
12071:                                                     '<span class="LC_warning">'.
12072:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
12073:                                                     '</span>';
12074:                                     }
12075:                                 } elsif (keys(%usertypes) > 0) {
12076:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
12077:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
12078:                                     } else {
12079:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
12080:                                     }
12081:                                     $chgtext .= '<ul>';
12082:                                     foreach my $case (@{$cancreate{$type}}) {
12083:                                         if ($case eq 'default') {
12084:                                             $chgtext .= '<li>'.$othertitle.'</li>';
12085:                                         } else {
12086:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
12087:                                         }
12088:                                     }
12089:                                     $chgtext .= '</ul>';
12090:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
12091:                                         $chgtext .= '<span class="LC_warning">'.
12092:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
12093:                                                     '</span>';
12094:                                     }
12095:                                 }
12096:                             } else {
12097:                                 if (@{$cancreate{$type}} == 0) {
12098:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
12099:                                 } else {
12100:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
12101:                                 }
12102:                             }
12103:                             $chgtext .= '<br />';
12104:                         }
12105:                     } elsif ($type eq 'selfcreateprocessing') {
12106:                         my %choices = &Apache::lonlocal::texthash (
12107:                                                                     automatic => 'Automatic approval',
12108:                                                                     approval  => 'Queued for approval',
12109:                                                                   );
12110:                         if (@types) {
12111:                             if (@statuses) {
12112:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:'). 
12113:                                             '<ul>';
12114:                                 foreach my $status (@statuses) {
12115:                                     if ($status eq 'default') {
12116:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
12117:                                     } else {
12118:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
12119:                                     }
12120:                                 }
12121:                                 $chgtext .= '</ul>';
12122:                             }
12123:                         } else {
12124:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
12125:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
12126:                         }
12127:                     } elsif ($type eq 'emailverified') {
12128:                         my %options = &Apache::lonlocal::texthash (
12129:                                                                     all   => 'Same as e-mail',
12130:                                                                     first => 'Omit @domain',
12131:                                                                     free  => 'Free to choose',
12132:                                                                   );
12133:                         if (@types) {
12134:                             if (@statuses) {
12135:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
12136:                                             '<ul>';
12137:                                 foreach my $status (@statuses) {
12138:                                     if ($type eq 'default') {
12139:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
12140:                                     } else {
12141:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
12142:                                     }
12143:                                 }
12144:                                 $chgtext .= '</ul>';
12145:                             }
12146:                         } else {
12147:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
12148:                                             $options{$cancreate{'emailverified'}{'default'}});
12149:                         }
12150:                     } elsif ($type eq 'emailoptions') {
12151:                         my %options = &Apache::lonlocal::texthash (
12152:                                                                     any     => 'Any e-mail',
12153:                                                                     inst    => 'Institutional only',
12154:                                                                     noninst => 'Non-institutional only',
12155:                                                                     custom  => 'Custom restrictions',
12156:                                                                   );
12157:                         if (@types) {
12158:                             if (@statuses) {
12159:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
12160:                                             '<ul>';
12161:                                 foreach my $status (@statuses) {
12162:                                     if ($type eq 'default') {
12163:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
12164:                                     } else {
12165:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
12166:                                     }
12167:                                 }
12168:                                 $chgtext .= '</ul>';
12169:                             }
12170:                         } else {
12171:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
12172:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
12173:                             } else {
12174:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
12175:                                                 $options{$cancreate{'emailoptions'}{'default'}});
12176:                             }
12177:                         }
12178:                     } elsif ($type eq 'emaildomain') {
12179:                         my $output;
12180:                         if (@statuses) {
12181:                             foreach my $type (@statuses) {
12182:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
12183:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
12184:                                         if ($type eq 'default') {
12185:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
12186:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
12187:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
12188:                                             } else {
12189:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
12190:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
12191:                                             }
12192:                                         } else {
12193:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
12194:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
12195:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
12196:                                             } else {
12197:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
12198:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>'; 
12199:                                             }
12200:                                         }
12201:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
12202:                                         if ($type eq 'default') {
12203:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
12204:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
12205:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
12206:                                             } else {
12207:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
12208:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
12209:                                             }
12210:                                         } else {
12211:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
12212:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
12213:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
12214:                                             } else {
12215:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
12216:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';   
12217:                                             }
12218:                                         }
12219:                                     }
12220:                                 }
12221:                             }
12222:                         }
12223:                         if ($output ne '') {
12224:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
12225:                                         '<ul>'.$output.'</ul>';
12226:                         }
12227:                     } elsif ($type eq 'captcha') {
12228:                         if ($savecaptcha{$type} eq 'notused') {
12229:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
12230:                         } else {
12231:                             my %captchas = &captcha_phrases();
12232:                             if ($captchas{$savecaptcha{$type}}) {
12233:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
12234:                             } else {
12235:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
12236:                             }
12237:                         }
12238:                     } elsif ($type eq 'recaptchakeys') {
12239:                         my ($privkey,$pubkey);
12240:                         if (ref($savecaptcha{$type}) eq 'HASH') {
12241:                             $pubkey = $savecaptcha{$type}{'public'};
12242:                             $privkey = $savecaptcha{$type}{'private'};
12243:                         }
12244:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
12245:                         if (!$pubkey) {
12246:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
12247:                         } else {
12248:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
12249:                         }
12250:                         if (!$privkey) {
12251:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
12252:                         } else {
12253:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
12254:                         }
12255:                         $chgtext .= '</ul>';
12256:                     } elsif ($type eq 'recaptchaversion') {
12257:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
12258:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
12259:                         }
12260:                     } elsif ($type eq 'emailusername') {
12261:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
12262:                             if (@statuses) {
12263:                                 foreach my $type (@statuses) {
12264:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
12265:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
12266:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
12267:                                                     '<ul>';
12268:                                             foreach my $field (@{$infofields}) {
12269:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
12270:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
12271:                                                 }
12272:                                             }
12273:                                             $chgtext .= '</ul>';
12274:                                         } else {
12275:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
12276:                                         }
12277:                                     } else {
12278:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
12279:                                     }
12280:                                 }
12281:                             }
12282:                         }
12283:                     } elsif ($type eq 'notify') {
12284:                         my $numapprove = 0;
12285:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
12286:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
12287:                                 if ($cancreate{'notify'}{'approval'}) {
12288:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
12289:                                     $numapprove ++;
12290:                                 }
12291:                             }
12292:                         }
12293:                         unless ($numapprove) {
12294:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
12295:                         }
12296:                     }
12297:                     if ($chgtext) {
12298:                         $resulttext .= '<li>'.$chgtext.'</li>';
12299:                     }
12300:                 }
12301:             }
12302:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
12303:                 my ($emailrules,$emailruleorder) =
12304:                     &Apache::lonnet::inst_userrules($dom,'email');
12305:                 foreach my $type (@{$changes{'email_rule'}}) {
12306:                     if (ref($email_rule{$type}) eq 'ARRAY') {
12307:                         my $chgtext = '<ul>';
12308:                         foreach my $rule (@{$email_rule{$type}}) {
12309:                             if (ref($emailrules->{$rule}) eq 'HASH') {
12310:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
12311:                             }
12312:                         }
12313:                         $chgtext .= '</ul>';
12314:                         my $typename;
12315:                         if (@types) {
12316:                             if ($type eq 'default') {
12317:                                 $typename = $othertitle;
12318:                             } else {
12319:                                 $typename = $usertypes{$type};
12320:                             } 
12321:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
12322:                         }
12323:                         if (@{$email_rule{$type}} > 0) {
12324:                             $resulttext .= '<li>'.
12325:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
12326:                                                $usertypes{$type}).
12327:                                            $chgtext.
12328:                                            '</li>';
12329:                         } else {
12330:                             $resulttext .= '<li>'.
12331:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
12332:                                            '</li>'.
12333:                                            &mt('(Affiliation: [_1])',$typename);
12334:                         }
12335:                     }
12336:                 }
12337:             }
12338:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
12339:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
12340:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
12341:                         my $chgtext = '<ul>';
12342:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
12343:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
12344:                         }
12345:                         $chgtext .= '</ul>';
12346:                         $resulttext .= '<li>'.
12347:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
12348:                                           $chgtext.
12349:                                        '</li>';
12350:                     } else {
12351:                         $resulttext .= '<li>'.
12352:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
12353:                                        '</li>';
12354:                     }
12355:                 }
12356:             }
12357:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
12358:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
12359:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
12360:                 foreach my $type (@{$changes{'selfcreate'}}) {
12361:                     my $typename = $type;
12362:                     if (keys(%usertypes) > 0) {
12363:                         if ($usertypes{$type} ne '') {
12364:                             $typename = $usertypes{$type};
12365:                         }
12366:                     }
12367:                     my @modifiable;
12368:                     $resulttext .= '<li>'.
12369:                                     &mt('Self-creation of account by users with status: [_1]',
12370:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
12371:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
12372:                     foreach my $field (@fields) {
12373:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
12374:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
12375:                         }
12376:                     }
12377:                     if (@modifiable > 0) {
12378:                         $resulttext .= join(', ',@modifiable);
12379:                     } else {
12380:                         $resulttext .= &mt('none');
12381:                     }
12382:                     $resulttext .= '</li>';
12383:                 }
12384:                 $resulttext .= '</ul></li>';
12385:             }
12386:             $resulttext .= '</ul>';
12387:             my $cachetime = 24*60*60;
12388:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
12389:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12390:             if (ref($lastactref) eq 'HASH') {
12391:                 $lastactref->{'domdefaults'} = 1;
12392:             }
12393:         } else {
12394:             $resulttext = &mt('No changes made to self-creation settings');
12395:         }
12396:     } else {
12397:         $resulttext = '<span class="LC_error">'.
12398:             &mt('An error occurred: [_1]',$putresult).'</span>';
12399:     }
12400:     if ($warningmsg ne '') {
12401:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
12402:     }
12403:     return $resulttext;
12404: }
12405: 
12406: sub process_captcha {
12407:     my ($container,$changes,$newsettings,$current) = @_;
12408:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
12409:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
12410:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
12411:         $newsettings->{'captcha'} = 'original';
12412:     }
12413:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
12414:         if ($container eq 'cancreate') {
12415:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
12416:                 push(@{$changes->{'cancreate'}},'captcha');
12417:             } elsif (!defined($changes->{'cancreate'})) {
12418:                 $changes->{'cancreate'} = ['captcha'];
12419:             }
12420:         } else {
12421:             $changes->{'captcha'} = 1;
12422:         }
12423:     }
12424:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
12425:     if ($newsettings->{'captcha'} eq 'recaptcha') {
12426:         $newpub = $env{'form.'.$container.'_recaptchapub'};
12427:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
12428:         $newpub =~ s/[^\w\-]//g;
12429:         $newpriv =~ s/[^\w\-]//g;
12430:         $newsettings->{'recaptchakeys'} = {
12431:                                              public  => $newpub,
12432:                                              private => $newpriv,
12433:                                           };
12434:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
12435:         $newversion =~ s/\D//g;
12436:         if ($newversion ne '2') {
12437:             $newversion = 1;
12438:         }
12439:         $newsettings->{'recaptchaversion'} = $newversion;
12440:     }
12441:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
12442:         $currpub = $current->{'recaptchakeys'}{'public'};
12443:         $currpriv = $current->{'recaptchakeys'}{'private'};
12444:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
12445:             $newsettings->{'recaptchakeys'} = {
12446:                                                  public  => '',
12447:                                                  private => '',
12448:                                               }
12449:         }
12450:     }
12451:     if ($current->{'captcha'} eq 'recaptcha') {
12452:         $currversion = $current->{'recaptchaversion'};
12453:         if ($currversion ne '2') {
12454:             $currversion = 1;
12455:         }
12456:     }
12457:     if ($currversion ne $newversion) {
12458:         if ($container eq 'cancreate') {
12459:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
12460:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
12461:             } elsif (!defined($changes->{'cancreate'})) {
12462:                 $changes->{'cancreate'} = ['recaptchaversion'];
12463:             }
12464:         } else {
12465:             $changes->{'recaptchaversion'} = 1;
12466:         }
12467:     }
12468:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
12469:         if ($container eq 'cancreate') {
12470:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
12471:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
12472:             } elsif (!defined($changes->{'cancreate'})) {
12473:                 $changes->{'cancreate'} = ['recaptchakeys'];
12474:             }
12475:         } else {
12476:             $changes->{'recaptchakeys'} = 1;
12477:         }
12478:     }
12479:     return;
12480: }
12481: 
12482: sub modify_usermodification {
12483:     my ($dom,%domconfig) = @_;
12484:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
12485:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
12486:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
12487:             if ($key eq 'selfcreate') {
12488:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
12489:             } else {  
12490:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
12491:             }
12492:         }
12493:     }
12494:     my @contexts = ('author','course');
12495:     my %context_title = (
12496:                            author => 'In author context',
12497:                            course => 'In course context',
12498:                         );
12499:     my @fields = ('lastname','firstname','middlename','generation',
12500:                   'permanentemail','id');
12501:     my %roles = (
12502:                   author => ['ca','aa'],
12503:                   course => ['st','ep','ta','in','cr'],
12504:                 );
12505:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
12506:     foreach my $context (@contexts) {
12507:         foreach my $role (@{$roles{$context}}) {
12508:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
12509:             foreach my $item (@fields) {
12510:                 if (grep(/^\Q$item\E$/,@modifiable)) {
12511:                     $modifyhash{$context}{$role}{$item} = 1;
12512:                 } else {
12513:                     $modifyhash{$context}{$role}{$item} = 0;
12514:                 }
12515:             }
12516:         }
12517:         if (ref($curr_usermodification{$context}) eq 'HASH') {
12518:             foreach my $role (@{$roles{$context}}) {
12519:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
12520:                     foreach my $field (@fields) {
12521:                         if ($modifyhash{$context}{$role}{$field} ne 
12522:                                 $curr_usermodification{$context}{$role}{$field}) {
12523:                             push(@{$changes{$context}},$role);
12524:                             last;
12525:                         }
12526:                     }
12527:                 }
12528:             }
12529:         } else {
12530:             foreach my $context (@contexts) {
12531:                 foreach my $role (@{$roles{$context}}) {
12532:                     push(@{$changes{$context}},$role);
12533:                 }
12534:             }
12535:         }
12536:     }
12537:     my %usermodification_hash =  (
12538:                                    usermodification => \%modifyhash,
12539:                                  );
12540:     my $putresult = &Apache::lonnet::put_dom('configuration',
12541:                                              \%usermodification_hash,$dom);
12542:     if ($putresult eq 'ok') {
12543:         if (keys(%changes) > 0) {
12544:             $resulttext = &mt('Changes made: ').'<ul>';
12545:             foreach my $context (@contexts) {
12546:                 if (ref($changes{$context}) eq 'ARRAY') {
12547:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
12548:                     if (ref($changes{$context}) eq 'ARRAY') {
12549:                         foreach my $role (@{$changes{$context}}) {
12550:                             my $rolename;
12551:                             if ($role eq 'cr') {
12552:                                 $rolename = &mt('Custom');
12553:                             } else {
12554:                                 $rolename = &Apache::lonnet::plaintext($role);
12555:                             }
12556:                             my @modifiable;
12557:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
12558:                             foreach my $field (@fields) {
12559:                                 if ($modifyhash{$context}{$role}{$field}) {
12560:                                     push(@modifiable,$fieldtitles{$field});
12561:                                 }
12562:                             }
12563:                             if (@modifiable > 0) {
12564:                                 $resulttext .= join(', ',@modifiable);
12565:                             } else {
12566:                                 $resulttext .= &mt('none'); 
12567:                             }
12568:                             $resulttext .= '</li>';
12569:                         }
12570:                         $resulttext .= '</ul></li>';
12571:                     }
12572:                 }
12573:             }
12574:             $resulttext .= '</ul>';
12575:         } else {
12576:             $resulttext = &mt('No changes made to user modification settings');
12577:         }
12578:     } else {
12579:         $resulttext = '<span class="LC_error">'.
12580:             &mt('An error occurred: [_1]',$putresult).'</span>';
12581:     }
12582:     return $resulttext;
12583: }
12584: 
12585: sub modify_defaults {
12586:     my ($dom,$lastactref,%domconfig) = @_;
12587:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
12588:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12589:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
12590:                  'portal_def','intauth_cost','intauth_check','intauth_switch');
12591:     my @authtypes = ('internal','krb4','krb5','localauth');
12592:     foreach my $item (@items) {
12593:         $newvalues{$item} = $env{'form.'.$item};
12594:         if ($item eq 'auth_def') {
12595:             if ($newvalues{$item} ne '') {
12596:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
12597:                     push(@errors,$item);
12598:                 }
12599:             }
12600:         } elsif ($item eq 'lang_def') {
12601:             if ($newvalues{$item} ne '') {
12602:                 if ($newvalues{$item} =~ /^(\w+)/) {
12603:                     my $langcode = $1;
12604:                     if ($langcode ne 'x_chef') {
12605:                         if (code2language($langcode) eq '') {
12606:                             push(@errors,$item);
12607:                         }
12608:                     }
12609:                 } else {
12610:                     push(@errors,$item);
12611:                 }
12612:             }
12613:         } elsif ($item eq 'timezone_def') {
12614:             if ($newvalues{$item} ne '') {
12615:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
12616:                     push(@errors,$item);   
12617:                 }
12618:             }
12619:         } elsif ($item eq 'datelocale_def') {
12620:             if ($newvalues{$item} ne '') {
12621:                 my @datelocale_ids = DateTime::Locale->ids();
12622:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
12623:                     push(@errors,$item);
12624:                 }
12625:             }
12626:         } elsif ($item eq 'portal_def') {
12627:             if ($newvalues{$item} ne '') {
12628:                 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])\/?$/) {
12629:                     push(@errors,$item);
12630:                 }
12631:             }
12632:         } elsif ($item eq 'intauth_cost') {
12633:             if ($newvalues{$item} ne '') {
12634:                 if ($newvalues{$item} =~ /\D/) {
12635:                     push(@errors,$item);
12636:                 }
12637:             }
12638:         } elsif ($item eq 'intauth_check') {
12639:             if ($newvalues{$item} ne '') {
12640:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
12641:                     push(@errors,$item);
12642:                 }
12643:             }
12644:         } elsif ($item eq 'intauth_switch') {
12645:             if ($newvalues{$item} ne '') {
12646:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
12647:                     push(@errors,$item);
12648:                 }
12649:             }
12650:         }
12651:         if (grep(/^\Q$item\E$/,@errors)) {
12652:             $newvalues{$item} = $domdefaults{$item};
12653:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
12654:             $changes{$item} = 1;
12655:         }
12656:         $domdefaults{$item} = $newvalues{$item};
12657:     }
12658:     my %defaults_hash = (
12659:                          defaults => \%newvalues,
12660:                         );
12661:     my $title = &defaults_titles();
12662: 
12663:     my $currinststatus;
12664:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
12665:         $currinststatus = $domconfig{'inststatus'};
12666:     } else {
12667:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12668:         $currinststatus = {
12669:                              inststatustypes => $usertypes,
12670:                              inststatusorder => $types,
12671:                              inststatusguest => [],
12672:                           };
12673:     }
12674:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
12675:     my @allpos;
12676:     my %alltypes;
12677:     my @inststatusguest;
12678:     if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
12679:         foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
12680:             unless (grep(/^\Q$type\E$/,@todelete)) {
12681:                 push(@inststatusguest,$type);
12682:             }
12683:         }
12684:     }
12685:     my ($currtitles,$currorder);
12686:     if (ref($currinststatus) eq 'HASH') {
12687:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
12688:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
12689:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
12690:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
12691:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
12692:                     }
12693:                 }
12694:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
12695:                     my $position = $env{'form.inststatus_pos_'.$type};
12696:                     $position =~ s/\D+//g;
12697:                     $allpos[$position] = $type;
12698:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
12699:                     $alltypes{$type} =~ s/`//g;
12700:                 }
12701:             }
12702:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
12703:             $currtitles =~ s/,$//;
12704:         }
12705:     }
12706:     if ($env{'form.addinststatus'}) {
12707:         my $newtype = $env{'form.addinststatus'};
12708:         $newtype =~ s/\W//g;
12709:         unless (exists($alltypes{$newtype})) {
12710:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
12711:             $alltypes{$newtype} =~ s/`//g; 
12712:             my $position = $env{'form.addinststatus_pos'};
12713:             $position =~ s/\D+//g;
12714:             if ($position ne '') {
12715:                 $allpos[$position] = $newtype;
12716:             }
12717:         }
12718:     }
12719:     my @orderedstatus;
12720:     foreach my $type (@allpos) {
12721:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
12722:             push(@orderedstatus,$type);
12723:         }
12724:     }
12725:     foreach my $type (keys(%alltypes)) {
12726:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
12727:             delete($alltypes{$type});
12728:         }
12729:     }
12730:     $defaults_hash{'inststatus'} = {
12731:                                      inststatustypes => \%alltypes,
12732:                                      inststatusorder => \@orderedstatus,
12733:                                      inststatusguest => \@inststatusguest,
12734:                                    };
12735:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
12736:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
12737:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
12738:         }
12739:     }
12740:     if ($currorder ne join(',',@orderedstatus)) {
12741:         $changes{'inststatus'}{'inststatusorder'} = 1;
12742:     }
12743:     my $newtitles;
12744:     foreach my $item (@orderedstatus) {
12745:         $newtitles .= $alltypes{$item}.',';
12746:     }
12747:     $newtitles =~ s/,$//;
12748:     if ($currtitles ne $newtitles) {
12749:         $changes{'inststatus'}{'inststatustypes'} = 1;
12750:     }
12751:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
12752:                                              $dom);
12753:     if ($putresult eq 'ok') {
12754:         if (keys(%changes) > 0) {
12755:             $resulttext = &mt('Changes made:').'<ul>';
12756:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
12757:             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";
12758:             foreach my $item (sort(keys(%changes))) {
12759:                 if ($item eq 'inststatus') {
12760:                     if (ref($changes{'inststatus'}) eq 'HASH') {
12761:                         if (@orderedstatus) {
12762:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
12763:                             foreach my $type (@orderedstatus) { 
12764:                                 $resulttext .= $alltypes{$type}.', ';
12765:                             }
12766:                             $resulttext =~ s/, $//;
12767:                             $resulttext .= '</li>';
12768:                         } else {
12769:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>'; 
12770:                         }
12771:                     }
12772:                 } else {
12773:                     my $value = $env{'form.'.$item};
12774:                     if ($value eq '') {
12775:                         $value = &mt('none');
12776:                     } elsif ($item eq 'auth_def') {
12777:                         my %authnames = &authtype_names();
12778:                         my %shortauth = (
12779:                                           internal   => 'int',
12780:                                           krb4       => 'krb4',
12781:                                           krb5       => 'krb5',
12782:                                           localauth  => 'loc',
12783:                         );
12784:                         $value = $authnames{$shortauth{$value}};
12785:                     } elsif ($item eq 'intauth_switch') {
12786:                         my %optiondesc = &Apache::lonlocal::texthash (
12787:                                             0 => 'No',
12788:                                             1 => 'Yes',
12789:                                             2 => 'Yes, and copy existing passwd file to passwd.bak file',
12790:                                          );
12791:                         if ($value =~ /^(0|1|2)$/) {
12792:                             $value = $optiondesc{$value};
12793:                         } else {
12794:                             $value = &mt('none -- defaults to No');
12795:                         }
12796:                     } elsif ($item eq 'intauth_check') {
12797:                         my %optiondesc = &Apache::lonlocal::texthash (
12798:                                              0 => 'No',
12799:                                              1 => 'Yes, allow login then update passwd file using default cost (if higher)',
12800:                                              2 => 'Yes, disallow login if stored cost is less than domain default',
12801:                                          );
12802:                         if ($value =~ /^(0|1|2)$/) {
12803:                             $value = $optiondesc{$value};
12804:                         } else {
12805:                             $value = &mt('none -- defaults to No');
12806:                         }
12807:                     }
12808:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
12809:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
12810:                 }
12811:             }
12812:             $resulttext .= '</ul>';
12813:             $mailmsgtext .= "\n";
12814:             my $cachetime = 24*60*60;
12815:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12816:             if (ref($lastactref) eq 'HASH') {
12817:                 $lastactref->{'domdefaults'} = 1;
12818:             }
12819:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
12820:                 my $notify = 1;
12821:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
12822:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
12823:                         $notify = 0;
12824:                     }
12825:                 }
12826:                 if ($notify) {
12827:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
12828:                                                "LON-CAPA Domain Settings Change - $dom",
12829:                                                $mailmsgtext);
12830:                 }
12831:             }
12832:         } else {
12833:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
12834:         }
12835:     } else {
12836:         $resulttext = '<span class="LC_error">'.
12837:             &mt('An error occurred: [_1]',$putresult).'</span>';
12838:     }
12839:     if (@errors > 0) {
12840:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
12841:         foreach my $item (@errors) {
12842:             $resulttext .= ' "'.$title->{$item}.'",';
12843:         }
12844:         $resulttext =~ s/,$//;
12845:     }
12846:     return $resulttext;
12847: }
12848: 
12849: sub modify_scantron {
12850:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
12851:     my ($resulttext,%confhash,%changes,$errors);
12852:     my $custom = 'custom.tab';
12853:     my $default = 'default.tab';
12854:     my $servadm = $r->dir_config('lonAdmEMail');
12855:     my ($configuserok,$author_ok,$switchserver) = 
12856:         &config_check($dom,$confname,$servadm);
12857:     if ($env{'form.scantronformat.filename'} ne '') {
12858:         my $error;
12859:         if ($configuserok eq 'ok') {
12860:             if ($switchserver) {
12861:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
12862:             } else {
12863:                 if ($author_ok eq 'ok') {
12864:                     my ($result,$scantronurl) =
12865:                         &publishlogo($r,'upload','scantronformat',$dom,
12866:                                      $confname,'scantron','','',$custom);
12867:                     if ($result eq 'ok') {
12868:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
12869:                         $changes{'scantronformat'} = 1;
12870:                     } else {
12871:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
12872:                     }
12873:                 } else {
12874:                     $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);
12875:                 }
12876:             }
12877:         } else {
12878:             $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);
12879:         }
12880:         if ($error) {
12881:             &Apache::lonnet::logthis($error);
12882:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12883:         }
12884:     }
12885:     if (ref($domconfig{'scantron'}) eq 'HASH') {
12886:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
12887:             if ($env{'form.scantronformat_del'}) {
12888:                 $confhash{'scantron'}{'scantronformat'} = '';
12889:                 $changes{'scantronformat'} = 1;
12890:             }
12891:         }
12892:     }
12893:     if (keys(%confhash) > 0) {
12894:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
12895:                                                  $dom);
12896:         if ($putresult eq 'ok') {
12897:             if (keys(%changes) > 0) {
12898:                 if (ref($confhash{'scantron'}) eq 'HASH') {
12899:                     $resulttext = &mt('Changes made:').'<ul>';
12900:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
12901:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
12902:                     } else {
12903:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
12904:                     }
12905:                     $resulttext .= '</ul>';
12906:                 } else {
12907:                     $resulttext = &mt('Changes made to bubblesheet format file.');
12908:                 }
12909:                 $resulttext .= '</ul>';
12910:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
12911:                 if (ref($lastactref) eq 'HASH') {
12912:                     $lastactref->{'domainconfig'} = 1;
12913:                 }
12914:             } else {
12915:                 $resulttext = &mt('No changes made to bubblesheet format file');
12916:             }
12917:         } else {
12918:             $resulttext = '<span class="LC_error">'.
12919:                 &mt('An error occurred: [_1]',$putresult).'</span>';
12920:         }
12921:     } else {
12922:         $resulttext = &mt('No changes made to bubblesheet format file'); 
12923:     }
12924:     if ($errors) {
12925:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
12926:                        $errors.'</ul>';
12927:     }
12928:     return $resulttext;
12929: }
12930: 
12931: sub modify_coursecategories {
12932:     my ($dom,$lastactref,%domconfig) = @_;
12933:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
12934:         $cathash);
12935:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
12936:     my @catitems = ('unauth','auth');
12937:     my @cattypes = ('std','domonly','codesrch','none');
12938:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
12939:         $cathash = $domconfig{'coursecategories'}{'cats'};
12940:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
12941:             $changes{'togglecats'} = 1;
12942:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
12943:         }
12944:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
12945:             $changes{'categorize'} = 1;
12946:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
12947:         }
12948:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
12949:             $changes{'togglecatscomm'} = 1;
12950:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
12951:         }
12952:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
12953:             $changes{'categorizecomm'} = 1;
12954:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
12955: 
12956:         }
12957:         if ($domconfig{'coursecategories'}{'togglecatsplace'} ne $env{'form.togglecatsplace'}) {
12958:             $changes{'togglecatsplace'} = 1;
12959:             $domconfig{'coursecategories'}{'togglecatsplace'} = $env{'form.togglecatsplace'};
12960:         }
12961:         if ($domconfig{'coursecategories'}{'categorizeplace'} ne $env{'form.categorizeplace'}) {
12962:             $changes{'categorizeplace'} = 1;
12963:             $domconfig{'coursecategories'}{'categorizeplace'} = $env{'form.categorizeplace'};
12964:         }
12965:         foreach my $item (@catitems) {
12966:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
12967:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
12968:                     $changes{$item} = 1;
12969:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
12970:                 }
12971:             }
12972:         }
12973:     } else {
12974:         $changes{'togglecats'} = 1;
12975:         $changes{'categorize'} = 1;
12976:         $changes{'togglecatscomm'} = 1;
12977:         $changes{'categorizecomm'} = 1;
12978:         $changes{'togglecatsplace'} = 1;
12979:         $changes{'categorizeplace'} = 1;
12980:         $domconfig{'coursecategories'} = {
12981:                                              togglecats => $env{'form.togglecats'},
12982:                                              categorize => $env{'form.categorize'},
12983:                                              togglecatscomm => $env{'form.togglecatscomm'},
12984:                                              categorizecomm => $env{'form.categorizecomm'},
12985:                                              togglecatsplace => $env{'form.togglecatsplace'},
12986:                                              categorizeplace => $env{'form.categorizeplace'},
12987:                                          };
12988:         foreach my $item (@catitems) {
12989:             if ($env{'form.coursecat_'.$item} ne 'std') {
12990:                 $changes{$item} = 1;
12991:             }
12992:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
12993:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
12994:             }
12995:         }
12996:     }
12997:     if (ref($cathash) eq 'HASH') {
12998:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
12999:             push (@deletecategory,'instcode::0');
13000:         }
13001:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
13002:             push(@deletecategory,'communities::0');
13003:         }
13004:         if (($domconfig{'coursecategories'}{'cats'}{'placement::0'} ne '')  && ($env{'form.placement'} == 0)) {
13005:             push(@deletecategory,'placement::0');
13006:         }
13007:     }
13008:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
13009:     if (ref($cathash) eq 'HASH') {
13010:         if (@deletecategory > 0) {
13011:             #FIXME Need to remove category from all courses using a deleted category 
13012:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
13013:             foreach my $item (@deletecategory) {
13014:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
13015:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
13016:                     $deletions{$item} = 1;
13017:                     &recurse_cat_deletes($item,$cathash,\%deletions);
13018:                 }
13019:             }
13020:         }
13021:         foreach my $item (keys(%{$cathash})) {
13022:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
13023:             if ($cathash->{$item} ne $env{'form.'.$item}) {
13024:                 $reorderings{$item} = 1;
13025:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
13026:             }
13027:             if ($env{'form.addcategory_name_'.$item} ne '') {
13028:                 my $newcat = $env{'form.addcategory_name_'.$item};
13029:                 my $newdepth = $depth+1;
13030:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
13031:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
13032:                 $adds{$newitem} = 1; 
13033:             }
13034:             if ($env{'form.subcat_'.$item} ne '') {
13035:                 my $newcat = $env{'form.subcat_'.$item};
13036:                 my $newdepth = $depth+1;
13037:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
13038:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
13039:                 $adds{$newitem} = 1;
13040:             }
13041:         }
13042:     }
13043:     if ($env{'form.instcode'} eq '1') {
13044:         if (ref($cathash) eq 'HASH') {
13045:             my $newitem = 'instcode::0';
13046:             if ($cathash->{$newitem} eq '') {  
13047:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
13048:                 $adds{$newitem} = 1;
13049:             }
13050:         } else {
13051:             my $newitem = 'instcode::0';
13052:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
13053:             $adds{$newitem} = 1;
13054:         }
13055:     }
13056:     if ($env{'form.communities'} eq '1') {
13057:         if (ref($cathash) eq 'HASH') {
13058:             my $newitem = 'communities::0';
13059:             if ($cathash->{$newitem} eq '') {
13060:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
13061:                 $adds{$newitem} = 1;
13062:             }
13063:         } else {
13064:             my $newitem = 'communities::0';
13065:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
13066:             $adds{$newitem} = 1;
13067:         }
13068:     }
13069:     if ($env{'form.placement'} eq '1') {
13070:         if (ref($cathash) eq 'HASH') {
13071:             my $newitem = 'placement::0';
13072:             if ($cathash->{$newitem} eq '') {
13073:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
13074:                 $adds{$newitem} = 1;
13075:             }
13076:         } else {
13077:             my $newitem = 'placement::0';
13078:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
13079:             $adds{$newitem} = 1;
13080:         }
13081:     }
13082:     if ($env{'form.addcategory_name'} ne '') {
13083:         if (($env{'form.addcategory_name'} ne 'instcode') &&
13084:             ($env{'form.addcategory_name'} ne 'communities') &&
13085:             ($env{'form.addcategory_name'} ne 'placement')) {
13086:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
13087:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
13088:             $adds{$newitem} = 1;
13089:         }
13090:     }
13091:     my $putresult;
13092:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
13093:         if (keys(%deletions) > 0) {
13094:             foreach my $key (keys(%deletions)) {
13095:                 if ($predelallitems{$key} ne '') {
13096:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
13097:                 }
13098:             }
13099:         }
13100:         my (@chkcats,@chktrails,%chkallitems);
13101:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
13102:         if (ref($chkcats[0]) eq 'ARRAY') {
13103:             my $depth = 0;
13104:             my $chg = 0;
13105:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
13106:                 my $name = $chkcats[0][$i];
13107:                 my $item;
13108:                 if ($name eq '') {
13109:                     $chg ++;
13110:                 } else {
13111:                     $item = &escape($name).'::0';
13112:                     if ($chg) {
13113:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
13114:                     }
13115:                     $depth ++; 
13116:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
13117:                     $depth --;
13118:                 }
13119:             }
13120:         }
13121:     }
13122:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
13123:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
13124:         if ($putresult eq 'ok') {
13125:             my %title = (
13126:                          togglecats     => 'Show/Hide a course in catalog',
13127:                          categorize     => 'Assign a category to a course',
13128:                          togglecatscomm => 'Show/Hide a community in catalog',
13129:                          categorizecomm => 'Assign a category to a community',
13130:                         );
13131:             my %level = (
13132:                          dom  => 'set in Domain ("Modify Course/Community")',
13133:                          crs  => 'set in Course ("Course Configuration")',
13134:                          comm => 'set in Community ("Community Configuration")',
13135:                          none     => 'No catalog',
13136:                          std      => 'Standard catalog',
13137:                          domonly  => 'Domain-only catalog',
13138:                          codesrch => 'Code search form',
13139:                         );
13140:             $resulttext = &mt('Changes made:').'<ul>';
13141:             if ($changes{'togglecats'}) {
13142:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
13143:             }
13144:             if ($changes{'categorize'}) {
13145:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
13146:             }
13147:             if ($changes{'togglecatscomm'}) {
13148:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
13149:             }
13150:             if ($changes{'categorizecomm'}) {
13151:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
13152:             }
13153:             if ($changes{'unauth'}) {
13154:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
13155:             }
13156:             if ($changes{'auth'}) {
13157:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
13158:             }
13159:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
13160:                 my $cathash;
13161:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
13162:                     $cathash = $domconfig{'coursecategories'}{'cats'};
13163:                 } else {
13164:                     $cathash = {};
13165:                 } 
13166:                 my (@cats,@trails,%allitems);
13167:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
13168:                 if (keys(%deletions) > 0) {
13169:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
13170:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
13171:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
13172:                     }
13173:                     $resulttext .= '</ul></li>';
13174:                 }
13175:                 if (keys(%reorderings) > 0) {
13176:                     my %sort_by_trail;
13177:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
13178:                     foreach my $key (keys(%reorderings)) {
13179:                         if ($allitems{$key} ne '') {
13180:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
13181:                         }
13182:                     }
13183:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
13184:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
13185:                     }
13186:                     $resulttext .= '</ul></li>';
13187:                 }
13188:                 if (keys(%adds) > 0) {
13189:                     my %sort_by_trail;
13190:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
13191:                     foreach my $key (keys(%adds)) {
13192:                         if ($allitems{$key} ne '') {
13193:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
13194:                         }
13195:                     }
13196:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
13197:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
13198:                     }
13199:                     $resulttext .= '</ul></li>';
13200:                 }
13201:             }
13202:             $resulttext .= '</ul>';
13203:             if ($changes{'unauth'} || $changes{'auth'}) {
13204:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
13205:                 if ($changes{'auth'}) {
13206:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
13207:                 }
13208:                 if ($changes{'unauth'}) {
13209:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
13210:                 }
13211:                 my $cachetime = 24*60*60;
13212:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13213:                 if (ref($lastactref) eq 'HASH') {
13214:                     $lastactref->{'domdefaults'} = 1;
13215:                 }
13216:             }
13217:         } else {
13218:             $resulttext = '<span class="LC_error">'.
13219:                           &mt('An error occurred: [_1]',$putresult).'</span>';
13220:         }
13221:     } else {
13222:         $resulttext = &mt('No changes made to course and community categories');
13223:     }
13224:     return $resulttext;
13225: }
13226: 
13227: sub modify_serverstatuses {
13228:     my ($dom,%domconfig) = @_;
13229:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
13230:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
13231:         %currserverstatus = %{$domconfig{'serverstatuses'}};
13232:     }
13233:     my @pages = &serverstatus_pages();
13234:     foreach my $type (@pages) {
13235:         $newserverstatus{$type}{'namedusers'} = '';
13236:         $newserverstatus{$type}{'machines'} = '';
13237:         if (defined($env{'form.'.$type.'_namedusers'})) {
13238:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
13239:             my @okusers;
13240:             foreach my $user (@users) {
13241:                 my ($uname,$udom) = split(/:/,$user);
13242:                 if (($udom =~ /^$match_domain$/) &&   
13243:                     (&Apache::lonnet::domain($udom)) &&
13244:                     ($uname =~ /^$match_username$/)) {
13245:                     if (!grep(/^\Q$user\E/,@okusers)) {
13246:                         push(@okusers,$user);
13247:                     }
13248:                 }
13249:             }
13250:             if (@okusers > 0) {
13251:                  @okusers = sort(@okusers);
13252:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
13253:             }
13254:         }
13255:         if (defined($env{'form.'.$type.'_machines'})) {
13256:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
13257:             my @okmachines;
13258:             foreach my $ip (@machines) {
13259:                 my @parts = split(/\./,$ip);
13260:                 next if (@parts < 4);
13261:                 my $badip = 0;
13262:                 for (my $i=0; $i<4; $i++) {
13263:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
13264:                         $badip = 1;
13265:                         last;
13266:                     }
13267:                 }
13268:                 if (!$badip) {
13269:                     push(@okmachines,$ip);     
13270:                 }
13271:             }
13272:             @okmachines = sort(@okmachines);
13273:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
13274:         }
13275:     }
13276:     my %serverstatushash =  (
13277:                                 serverstatuses => \%newserverstatus,
13278:                             );
13279:     foreach my $type (@pages) {
13280:         foreach my $setting ('namedusers','machines') {
13281:             my (@current,@new);
13282:             if (ref($currserverstatus{$type}) eq 'HASH') {
13283:                 if ($currserverstatus{$type}{$setting} ne '') { 
13284:                     @current = split(/,/,$currserverstatus{$type}{$setting});
13285:                 }
13286:             }
13287:             if ($newserverstatus{$type}{$setting} ne '') {
13288:                 @new = split(/,/,$newserverstatus{$type}{$setting});
13289:             }
13290:             if (@current > 0) {
13291:                 if (@new > 0) {
13292:                     foreach my $item (@current) {
13293:                         if (!grep(/^\Q$item\E$/,@new)) {
13294:                             $changes{$type}{$setting} = 1;
13295:                             last;
13296:                         }
13297:                     }
13298:                     foreach my $item (@new) {
13299:                         if (!grep(/^\Q$item\E$/,@current)) {
13300:                             $changes{$type}{$setting} = 1;
13301:                             last;
13302:                         }
13303:                     }
13304:                 } else {
13305:                     $changes{$type}{$setting} = 1;
13306:                 }
13307:             } elsif (@new > 0) {
13308:                 $changes{$type}{$setting} = 1;
13309:             }
13310:         }
13311:     }
13312:     if (keys(%changes) > 0) {
13313:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
13314:         my $putresult = &Apache::lonnet::put_dom('configuration',
13315:                                                  \%serverstatushash,$dom);
13316:         if ($putresult eq 'ok') {
13317:             $resulttext .= &mt('Changes made:').'<ul>';
13318:             foreach my $type (@pages) {
13319:                 if (ref($changes{$type}) eq 'HASH') {
13320:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
13321:                     if ($changes{$type}{'namedusers'}) {
13322:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
13323:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
13324:                         } else {
13325:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
13326:                         }
13327:                     }
13328:                     if ($changes{$type}{'machines'}) {
13329:                         if ($newserverstatus{$type}{'machines'} eq '') {
13330:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
13331:                         } else {
13332:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
13333:                         }
13334: 
13335:                     }
13336:                     $resulttext .= '</ul></li>';
13337:                 }
13338:             }
13339:             $resulttext .= '</ul>';
13340:         } else {
13341:             $resulttext = '<span class="LC_error">'.
13342:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
13343: 
13344:         }
13345:     } else {
13346:         $resulttext = &mt('No changes made to access to server status pages');
13347:     }
13348:     return $resulttext;
13349: }
13350: 
13351: sub modify_helpsettings {
13352:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
13353:     my ($resulttext,$errors,%changes,%helphash);
13354:     my %defaultchecked = ('submitbugs' => 'on');
13355:     my @offon = ('off','on');
13356:     my @toggles = ('submitbugs');
13357:     my %current = ('submitbugs' => '',
13358:                    'adhoc'      => {},
13359:                   );
13360:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
13361:         %current = %{$domconfig{'helpsettings'}};
13362:     }
13363:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13364:     foreach my $item (@toggles) {
13365:         if ($defaultchecked{$item} eq 'on') { 
13366:             if ($current{$item} eq '') {
13367:                 if ($env{'form.'.$item} eq '0') {
13368:                     $changes{$item} = 1;
13369:                 }
13370:             } elsif ($current{$item} ne $env{'form.'.$item}) {
13371:                 $changes{$item} = 1;
13372:             }
13373:         } elsif ($defaultchecked{$item} eq 'off') {
13374:             if ($current{$item} eq '') {
13375:                 if ($env{'form.'.$item} eq '1') {
13376:                     $changes{$item} = 1;
13377:                 }
13378:             } elsif ($current{$item} ne $env{'form.'.$item}) {
13379:                 $changes{$item} = 1;
13380:             }
13381:         }
13382:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
13383:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
13384:         }
13385:     }
13386:     my $maxnum = $env{'form.helproles_maxnum'};
13387:     my $confname = $dom.'-domainconfig';
13388:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
13389:     my (@allpos,%newsettings,%changedprivs,$newrole);
13390:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
13391:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
13392:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
13393:     my %lt = &Apache::lonlocal::texthash(
13394:                     s      => 'system',
13395:                     d      => 'domain',
13396:                     order  => 'Display order',
13397:                     access => 'Role usage',
13398:                     all    => 'All with domain helpdesk or helpdesk assistant role',
13399:                     dh     => 'All with domain helpdesk role',
13400:                     da     => 'All with domain helpdesk assistant role',
13401:                     none   => 'None',
13402:                     status => 'Determined based on institutional status',
13403:                     inc    => 'Include all, but exclude specific personnel',
13404:                     exc    => 'Exclude all, but include specific personnel',
13405:     );
13406:     for (my $num=0; $num<=$maxnum; $num++) {
13407:         my ($prefix,$identifier,$rolename,%curr);
13408:         if ($num == $maxnum) {
13409:             next unless ($env{'form.newcusthelp'} == $maxnum);
13410:             $identifier = 'custhelp'.$num;
13411:             $prefix = 'helproles_'.$num;
13412:             $rolename = $env{'form.custhelpname'.$num};
13413:             $rolename=~s/[^A-Za-z0-9]//gs;
13414:             next if ($rolename eq '');
13415:             next if (exists($existing{'rolesdef_'.$rolename}));
13416:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
13417:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
13418:                                                      $newprivs{'c'},$confname,$dom);
13419:             if ($result ne 'ok') {
13420:                 $errors .= '<li><span class="LC_error">'.
13421:                            &mt('An error occurred storing the new custom role: [_1]',
13422:                            $result).'</span></li>';
13423:                 next;
13424:             } else {
13425:                 $changedprivs{$rolename} = \%newprivs;
13426:                 $newrole = $rolename;
13427:             }
13428:         } else {
13429:             $prefix = 'helproles_'.$num;
13430:             $rolename = $env{'form.'.$prefix};
13431:             next if ($rolename eq '');
13432:             next unless (exists($existing{'rolesdef_'.$rolename}));
13433:             $identifier = 'custhelp'.$num;
13434:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
13435:             my %currprivs;
13436:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
13437:                 split(/\_/,$existing{'rolesdef_'.$rolename});
13438:             foreach my $level ('c','d','s') {
13439:                 if ($newprivs{$level} ne $currprivs{$level}) {
13440:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
13441:                                                              $newprivs{'c'},$confname,$dom);
13442:                     if ($result ne 'ok') {
13443:                         $errors .= '<li><span class="LC_error">'.
13444:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
13445:                                        $rolename,$result).'</span></li>';
13446:                     } else {
13447:                         $changedprivs{$rolename} = \%newprivs;
13448:                     }
13449:                     last;
13450:                 }
13451:             }
13452:             if (ref($current{'adhoc'}) eq 'HASH') {
13453:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
13454:                     %curr = %{$current{'adhoc'}{$rolename}};
13455:                 }
13456:             }
13457:         }
13458:         my $newpos = $env{'form.'.$prefix.'_pos'};
13459:         $newpos =~ s/\D+//g;
13460:         $allpos[$newpos] = $rolename;
13461:         my $newdesc = $env{'form.'.$prefix.'_desc'};
13462:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
13463:         if ($curr{'desc'}) {
13464:             if ($curr{'desc'} ne $newdesc) {
13465:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
13466:                 $newsettings{$rolename}{'desc'} = $newdesc;
13467:             }
13468:         } elsif ($newdesc ne '') {
13469:             $changes{'customrole'}{$rolename}{'desc'} = 1;
13470:             $newsettings{$rolename}{'desc'} = $newdesc;
13471:         }
13472:         my $access = $env{'form.'.$prefix.'_access'};
13473:         if (grep(/^\Q$access\E$/,@accesstypes)) {
13474:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
13475:             if ($access eq 'status') {
13476:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
13477:                 if (scalar(@statuses) == 0) {
13478:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
13479:                 } else {
13480:                     my (@shownstatus,$numtypes);
13481:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
13482:                     if (ref($types) eq 'ARRAY') {
13483:                         $numtypes = scalar(@{$types});
13484:                         foreach my $type (sort(@statuses)) {
13485:                             if ($type eq 'default') {
13486:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
13487:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
13488:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
13489:                                 push(@shownstatus,$usertypes->{$type});
13490:                             }
13491:                         }
13492:                     }
13493:                     if (grep(/^default$/,@statuses)) {
13494:                         push(@shownstatus,$othertitle);
13495:                     }
13496:                     if (scalar(@shownstatus) == 1+$numtypes) {
13497:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
13498:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
13499:                     } else {
13500:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
13501:                         if (ref($curr{'status'}) eq 'ARRAY') {
13502:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
13503:                             if (@diffs) {
13504:                                 $changes{'customrole'}{$rolename}{$access} = 1;
13505:                             }
13506:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
13507:                             $changes{'customrole'}{$rolename}{$access} = 1;
13508:                         }
13509:                     }
13510:                 }
13511:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
13512:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
13513:                 my @newspecstaff;
13514:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
13515:                 foreach my $person (sort(@personnel)) {
13516:                     if ($domhelpdesk{$person}) {
13517:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
13518:                     }
13519:                 }
13520:                 if (ref($curr{$access}) eq 'ARRAY') {
13521:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
13522:                     if (@diffs) {
13523:                         $changes{'customrole'}{$rolename}{$access} = 1;
13524:                     }
13525:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
13526:                     $changes{'customrole'}{$rolename}{$access} = 1;
13527:                 }
13528:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
13529:                     my ($uname,$udom) = split(/:/,$person);
13530:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
13531:                 }
13532:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
13533:             }
13534:         } else {
13535:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
13536:         }
13537:         unless ($curr{'access'} eq $access) {
13538:             $changes{'customrole'}{$rolename}{'access'} = 1;
13539:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
13540:         }
13541:     }
13542:     if (@allpos > 0) {
13543:         my $idx = 0;
13544:         foreach my $rolename (@allpos) {
13545:             if ($rolename ne '') {
13546:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
13547:                 if (ref($current{'adhoc'}) eq 'HASH') {
13548:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
13549:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
13550:                             $changes{'customrole'}{$rolename}{'order'} = 1;
13551:                             $newsettings{$rolename}{'order'} = $idx+1;
13552:                         }
13553:                     }
13554:                 }
13555:                 $idx ++;
13556:             }
13557:         }
13558:     }
13559:     my $putresult;
13560:     if (keys(%changes) > 0) {
13561:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
13562:         if ($putresult eq 'ok') {
13563:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
13564:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
13565:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
13566:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
13567:                 }
13568:             }
13569:             my $cachetime = 24*60*60;
13570:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13571:             if (ref($lastactref) eq 'HASH') {
13572:                 $lastactref->{'domdefaults'} = 1;
13573:             }
13574:         } else {
13575:             $errors .= '<li><span class="LC_error">'.
13576:                        &mt('An error occurred storing the settings: [_1]',
13577:                            $putresult).'</span></li>';
13578:         }
13579:     }
13580:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
13581:         $resulttext = &mt('Changes made:').'<ul>';
13582:         my (%shownprivs,@levelorder);
13583:         @levelorder = ('c','d','s');
13584:         if ((keys(%changes)) && ($putresult eq 'ok')) {
13585:             foreach my $item (sort(keys(%changes))) {
13586:                 if ($item eq 'submitbugs') {
13587:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
13588:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
13589:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
13590:                 } elsif ($item eq 'customrole') {
13591:                     if (ref($changes{'customrole'}) eq 'HASH') {
13592:                         my @keyorder = ('order','desc','access','status','exc','inc');
13593:                         my %keytext = &Apache::lonlocal::texthash(
13594:                                                                    order  => 'Order',
13595:                                                                    desc   => 'Role description',
13596:                                                                    access => 'Role usage',
13597:                                                                    status => 'Allowed institutional types',
13598:                                                                    exc    => 'Allowed personnel',
13599:                                                                    inc    => 'Disallowed personnel',
13600:                         );
13601:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
13602:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
13603:                                 if ($role eq $newrole) {
13604:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
13605:                                                               $role).'<ul>';
13606:                                 } else {
13607:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
13608:                                                               $role).'<ul>';
13609:                                 }
13610:                                 foreach my $key (@keyorder) {
13611:                                     if ($changes{'customrole'}{$role}{$key}) {
13612:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
13613:                                                                   $keytext{$key},$newsettings{$role}{$key}).
13614:                                                        '</li>';
13615:                                     }
13616:                                 }
13617:                                 if (ref($changedprivs{$role}) eq 'HASH') {
13618:                                     $shownprivs{$role} = 1;
13619:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
13620:                                     foreach my $level (@levelorder) {
13621:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
13622:                                             next if ($item eq '');
13623:                                             my ($priv) = split(/\&/,$item,2);
13624:                                             if (&Apache::lonnet::plaintext($priv)) {
13625:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
13626:                                                 unless ($level eq 'c') {
13627:                                                     $resulttext .= ' ('.$lt{$level}.')';
13628:                                                 }
13629:                                                 $resulttext .= '</li>';
13630:                                             }
13631:                                         }
13632:                                     }
13633:                                     $resulttext .= '</ul>';
13634:                                 }
13635:                                 $resulttext .= '</ul></li>';
13636:                             }
13637:                         }
13638:                     }
13639:                 }
13640:             }
13641:         }
13642:         if (keys(%changedprivs)) {
13643:             foreach my $role (sort(keys(%changedprivs))) {
13644:                 unless ($shownprivs{$role}) {
13645:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
13646:                                               $role).'<ul>'.
13647:                                    '<li>'.&mt('Privileges set to :').'<ul>';
13648:                     foreach my $level (@levelorder) {
13649:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
13650:                             next if ($item eq '');
13651:                             my ($priv) = split(/\&/,$item,2);
13652:                             if (&Apache::lonnet::plaintext($priv)) {
13653:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
13654:                                 unless ($level eq 'c') {
13655:                                     $resulttext .= ' ('.$lt{$level}.')';
13656:                                 }
13657:                                 $resulttext .= '</li>';
13658:                             }
13659:                         }
13660:                     }
13661:                     $resulttext .= '</ul></li></ul></li>';
13662:                 }
13663:             }
13664:         }
13665:         $resulttext .= '</ul>';
13666:     } else {
13667:         $resulttext = &mt('No changes made to help settings');
13668:     }
13669:     if ($errors) {
13670:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
13671:                                     $errors.'</ul>';
13672:     }
13673:     return $resulttext;
13674: }
13675: 
13676: sub modify_coursedefaults {
13677:     my ($dom,$lastactref,%domconfig) = @_;
13678:     my ($resulttext,$errors,%changes,%defaultshash);
13679:     my %defaultchecked = (
13680:                            'canuse_pdfforms' => 'off',
13681:                            'uselcmath'       => 'on',
13682:                            'usejsme'         => 'on'
13683:                          );
13684:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme');
13685:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
13686:                    'uploadquota_community','uploadquota_textbook','uploadquota_placement',
13687:                    'mysqltables_official','mysqltables_unofficial','mysqltables_community',
13688:                    'mysqltables_textbook','mysqltables_placement');
13689:     my @types = ('official','unofficial','community','textbook','placement');
13690:     my %staticdefaults = (
13691:                            anonsurvey_threshold => 10,
13692:                            uploadquota          => 500,
13693:                            postsubmit           => 60,
13694:                            mysqltables          => 172800,
13695:                          );
13696:     my %texoptions = (
13697:                         MathJax  => 'MathJax',
13698:                         mimetex  => &mt('Convert to Images'),
13699:                         tth      => &mt('TeX to HTML'),
13700:                      );
13701:     $defaultshash{'coursedefaults'} = {};
13702: 
13703:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
13704:         if ($domconfig{'coursedefaults'} eq '') {
13705:             $domconfig{'coursedefaults'} = {};
13706:         }
13707:     }
13708: 
13709:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
13710:         foreach my $item (@toggles) {
13711:             if ($defaultchecked{$item} eq 'on') {
13712:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
13713:                     ($env{'form.'.$item} eq '0')) {
13714:                     $changes{$item} = 1;
13715:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
13716:                     $changes{$item} = 1;
13717:                 }
13718:             } elsif ($defaultchecked{$item} eq 'off') {
13719:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
13720:                     ($env{'form.'.$item} eq '1')) {
13721:                     $changes{$item} = 1;
13722:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
13723:                     $changes{$item} = 1;
13724:                 }
13725:             }
13726:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
13727:         }
13728:         foreach my $item (@numbers) {
13729:             my ($currdef,$newdef);
13730:             $newdef = $env{'form.'.$item};
13731:             if ($item eq 'anonsurvey_threshold') {
13732:                 $currdef = $domconfig{'coursedefaults'}{$item};
13733:                 $newdef =~ s/\D//g;
13734:                 if ($newdef eq '' || $newdef < 1) {
13735:                     $newdef = 1;
13736:                 }
13737:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
13738:             } else {
13739:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
13740:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
13741:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
13742:                 }
13743:                 $newdef =~ s/[^\w.\-]//g;
13744:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
13745:             }
13746:             if ($currdef ne $newdef) {
13747:                 my $staticdef;
13748:                 if ($item eq 'anonsurvey_threshold') {
13749:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
13750:                         $changes{$item} = 1;
13751:                     }
13752:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
13753:                     my $setting = $1;
13754:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
13755:                         $changes{$setting} = 1;
13756:                     }
13757:                 }
13758:             }
13759:         }
13760:         my $texengine;
13761:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
13762:             $texengine = $env{'form.texengine'};
13763:             if ($defaultshash{'coursedefaults'}{'texengine'} eq '') {
13764:                 unless ($texengine eq 'MathJax') {
13765:                     $changes{'texengine'} = 1;
13766:                 }
13767:             } elsif ($defaultshash{'coursedefaults'}{'texengine'} ne $texengine) {
13768:                 $changes{'texengine'} = 1;
13769:             }
13770:         }
13771:         if ($texengine ne '') {
13772:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
13773:         }
13774:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
13775:         my @currclonecode;
13776:         if (ref($currclone) eq 'HASH') {
13777:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
13778:                 @currclonecode = @{$currclone->{'instcode'}};
13779:             }
13780:         }
13781:         my $newclone;
13782:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
13783:             $newclone = $env{'form.canclone'};
13784:         }
13785:         if ($newclone eq 'instcode') {
13786:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
13787:             my (%codedefaults,@code_order,@clonecode);
13788:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
13789:                                                     \@code_order);
13790:             foreach my $item (@code_order) {
13791:                 if (grep(/^\Q$item\E$/,@newcodes)) {
13792:                     push(@clonecode,$item);
13793:                 }
13794:             }
13795:             if (@clonecode) {
13796:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
13797:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
13798:                 if (@diffs) {
13799:                     $changes{'canclone'} = 1;
13800:                 }
13801:             } else {
13802:                 $newclone eq '';
13803:             }
13804:         } elsif ($newclone ne '') {
13805:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
13806:         }
13807:         if ($newclone ne $currclone) {
13808:             $changes{'canclone'} = 1;
13809:         }
13810:         my %credits;
13811:         foreach my $type (@types) {
13812:             unless ($type eq 'community') {
13813:                 $credits{$type} = $env{'form.'.$type.'_credits'};
13814:                 $credits{$type} =~ s/[^\d.]+//g;
13815:             }
13816:         }
13817:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
13818:             ($env{'form.coursecredits'} eq '1')) {
13819:             $changes{'coursecredits'} = 1;
13820:             foreach my $type (keys(%credits)) {
13821:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
13822:             }
13823:         } else {
13824:             if ($env{'form.coursecredits'} eq '1') {
13825:                 foreach my $type (@types) {
13826:                     unless ($type eq 'community') {
13827:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
13828:                             $changes{'coursecredits'} = 1;
13829:                         }
13830:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
13831:                     }
13832:                 }
13833:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13834:                 foreach my $type (@types) {
13835:                     unless ($type eq 'community') {
13836:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
13837:                             $changes{'coursecredits'} = 1;
13838:                             last;
13839:                         }
13840:                     }
13841:                 }
13842:             }
13843:         }
13844:         if ($env{'form.postsubmit'} eq '1') {
13845:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
13846:             my %currtimeout;
13847:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13848:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
13849:                     $changes{'postsubmit'} = 1;
13850:                 }
13851:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13852:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
13853:                 }
13854:             } else {
13855:                 $changes{'postsubmit'} = 1;
13856:             }
13857:             foreach my $type (@types) {
13858:                 my $timeout = $env{'form.'.$type.'_timeout'};
13859:                 $timeout =~ s/\D//g;
13860:                 if ($timeout == $staticdefaults{'postsubmit'}) {
13861:                     $timeout = '';
13862:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
13863:                     $timeout = '0';
13864:                 }
13865:                 unless ($timeout eq '') {
13866:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
13867:                 }
13868:                 if (exists($currtimeout{$type})) {
13869:                     if ($timeout ne $currtimeout{$type}) {
13870:                         $changes{'postsubmit'} = 1;
13871:                     }
13872:                 } elsif ($timeout ne '') {
13873:                     $changes{'postsubmit'} = 1;
13874:                 }
13875:             }
13876:         } else {
13877:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
13878:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13879:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
13880:                     $changes{'postsubmit'} = 1;
13881:                 }
13882:             } else {
13883:                 $changes{'postsubmit'} = 1;
13884:             }
13885:         }
13886:     }
13887:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13888:                                              $dom);
13889:     if ($putresult eq 'ok') {
13890:         if (keys(%changes) > 0) {
13891:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13892:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
13893:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
13894:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'})) {
13895:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme','texengine') {
13896:                     if ($changes{$item}) {
13897:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
13898:                     }
13899:                 }
13900:                 if ($changes{'coursecredits'}) {
13901:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13902:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
13903:                             $domdefaults{$type.'credits'} =
13904:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
13905:                         }
13906:                     }
13907:                 }
13908:                 if ($changes{'postsubmit'}) {
13909:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13910:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
13911:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13912:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
13913:                                 $domdefaults{$type.'postsubtimeout'} =
13914:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
13915:                             }
13916:                         }
13917:                     }
13918:                 }
13919:                 if ($changes{'uploadquota'}) {
13920:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
13921:                         foreach my $type (@types) {
13922:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
13923:                         }
13924:                     }
13925:                 }
13926:                 if ($changes{'canclone'}) {
13927:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
13928:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
13929:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
13930:                             if (@clonecodes) {
13931:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
13932:                             }
13933:                         }
13934:                     } else {
13935:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
13936:                     }
13937:                 }
13938:                 my $cachetime = 24*60*60;
13939:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13940:                 if (ref($lastactref) eq 'HASH') {
13941:                     $lastactref->{'domdefaults'} = 1;
13942:                 }
13943:             }
13944:             $resulttext = &mt('Changes made:').'<ul>';
13945:             foreach my $item (sort(keys(%changes))) {
13946:                 if ($item eq 'canuse_pdfforms') {
13947:                     if ($env{'form.'.$item} eq '1') {
13948:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
13949:                     } else {
13950:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
13951:                     }
13952:                 } elsif ($item eq 'uselcmath') {
13953:                     if ($env{'form.'.$item} eq '1') {
13954:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
13955:                     } else {
13956:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
13957:                     }
13958:                 } elsif ($item eq 'usejsme') {
13959:                     if ($env{'form.'.$item} eq '1') {
13960:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
13961:                     } else {
13962:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
13963:                     }
13964:                 } elsif ($item eq 'texengine') {
13965:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
13966:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
13967:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
13968:                     }
13969:                 } elsif ($item eq 'anonsurvey_threshold') {
13970:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
13971:                 } elsif ($item eq 'uploadquota') {
13972:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
13973:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
13974:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
13975:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
13976:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
13977:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'placement'}.'</b>').'</li>'. 
13978:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
13979:                                        '</ul>'.
13980:                                        '</li>';
13981:                     } else {
13982:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
13983:                     }
13984:                 } elsif ($item eq 'mysqltables') {
13985:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
13986:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
13987:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
13988:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
13989:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
13990:                                        '<li>'.&mt('Placement tests: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'placement'}.'</b>').'</li>'.
13991:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
13992:                                        '</ul>'.
13993:                                        '</li>';
13994:                     } else {
13995:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
13996:                     }
13997:                 } elsif ($item eq 'postsubmit') {
13998:                     if ($domdefaults{'postsubmit'} eq 'off') {
13999:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
14000:                     } else {
14001:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
14002:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
14003:                             $resulttext .= &mt('durations:').'<ul>';
14004:                             foreach my $type (@types) {
14005:                                 $resulttext .= '<li>';
14006:                                 my $timeout;
14007:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
14008:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
14009:                                 }
14010:                                 my $display;
14011:                                 if ($timeout eq '0') {
14012:                                     $display = &mt('unlimited');
14013:                                 } elsif ($timeout eq '') {
14014:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
14015:                                 } else {
14016:                                     $display = &mt('[quant,_1,second]',$timeout);
14017:                                 }
14018:                                 if ($type eq 'community') {
14019:                                     $resulttext .= &mt('Communities');
14020:                                 } elsif ($type eq 'official') {
14021:                                     $resulttext .= &mt('Official courses');
14022:                                 } elsif ($type eq 'unofficial') {
14023:                                     $resulttext .= &mt('Unofficial courses');
14024:                                 } elsif ($type eq 'textbook') {
14025:                                     $resulttext .= &mt('Textbook courses');
14026:                                 } elsif ($type eq 'placement') {
14027:                                     $resulttext .= &mt('Placement tests');
14028:                                 }
14029:                                 $resulttext .= ' -- '.$display.'</li>';
14030:                             }
14031:                             $resulttext .= '</ul>';
14032:                         }
14033:                         $resulttext .= '</li>';
14034:                     }
14035:                 } elsif ($item eq 'coursecredits') {
14036:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
14037:                         if (($domdefaults{'officialcredits'} eq '') &&
14038:                             ($domdefaults{'unofficialcredits'} eq '') &&
14039:                             ($domdefaults{'textbookcredits'} eq '')) {
14040:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
14041:                         } else {
14042:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
14043:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
14044:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
14045:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
14046:                                            '</ul>'.
14047:                                            '</li>';
14048:                         }
14049:                     } else {
14050:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
14051:                     }
14052:                 } elsif ($item eq 'canclone') {
14053:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
14054:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
14055:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
14056:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
14057:                         }
14058:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
14059:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
14060:                     } else {
14061:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
14062:                     }
14063:                 }
14064:             }
14065:             $resulttext .= '</ul>';
14066:         } else {
14067:             $resulttext = &mt('No changes made to course defaults');
14068:         }
14069:     } else {
14070:         $resulttext = '<span class="LC_error">'.
14071:             &mt('An error occurred: [_1]',$putresult).'</span>';
14072:     }
14073:     return $resulttext;
14074: }
14075: 
14076: sub modify_selfenrollment {
14077:     my ($dom,$lastactref,%domconfig) = @_;
14078:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
14079:     my @types = ('official','unofficial','community','textbook','placement');
14080:     my %titles = &tool_titles();
14081:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
14082:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
14083:     $ordered{'default'} = ['types','registered','approval','limit'];
14084: 
14085:     my (%roles,%shown,%toplevel);
14086:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
14087: 
14088:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
14089:         if ($domconfig{'selfenrollment'} eq '') {
14090:             $domconfig{'selfenrollment'} = {};
14091:         }
14092:     }
14093:     %toplevel = (
14094:                   admin      => 'Configuration Rights',
14095:                   default    => 'Default settings',
14096:                   validation => 'Validation of self-enrollment requests',
14097:                 );
14098:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
14099: 
14100:     if (ref($ordered{'admin'}) eq 'ARRAY') {
14101:         foreach my $item (@{$ordered{'admin'}}) {
14102:             foreach my $type (@types) {
14103:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
14104:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
14105:                 } else {
14106:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
14107:                 }
14108:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
14109:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
14110:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
14111:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
14112:                             push(@{$changes{'admin'}{$type}},$item);
14113:                         }
14114:                     } else {
14115:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
14116:                             push(@{$changes{'admin'}{$type}},$item);
14117:                         }
14118:                     }
14119:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
14120:                     push(@{$changes{'admin'}{$type}},$item);
14121:                 }
14122:             }
14123:         }
14124:     }
14125: 
14126:     foreach my $item (@{$ordered{'default'}}) {
14127:         foreach my $type (@types) {
14128:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
14129:             if ($item eq 'types') {
14130:                 unless (($value eq 'all') || ($value eq 'dom')) {
14131:                     $value = '';
14132:                 }
14133:             } elsif ($item eq 'registered') {
14134:                 unless ($value eq '1') {
14135:                     $value = 0;
14136:                 }
14137:             } elsif ($item eq 'approval') {
14138:                 unless ($value =~ /^[012]$/) {
14139:                     $value = 0;
14140:                 }
14141:             } else {
14142:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
14143:                     $value = 'none';
14144:                 }
14145:             }
14146:             $selfenrollhash{'default'}{$type}{$item} = $value;
14147:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
14148:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
14149:                     if ($selfenrollhash{'default'}{$type}{$item} ne
14150:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
14151:                          push(@{$changes{'default'}{$type}},$item);
14152:                     }
14153:                 } else {
14154:                     push(@{$changes{'default'}{$type}},$item);
14155:                 }
14156:             } else {
14157:                 push(@{$changes{'default'}{$type}},$item);
14158:             }
14159:             if ($item eq 'limit') {
14160:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
14161:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
14162:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
14163:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
14164:                     }
14165:                 } else {
14166:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
14167:                 }
14168:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
14169:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
14170:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
14171:                          push(@{$changes{'default'}{$type}},'cap');
14172:                     }
14173:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
14174:                     push(@{$changes{'default'}{$type}},'cap');
14175:                 }
14176:             }
14177:         }
14178:     }
14179: 
14180:     foreach my $item (@{$itemsref}) {
14181:         if ($item eq 'fields') {
14182:             my @changed;
14183:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
14184:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
14185:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
14186:             }
14187:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
14188:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
14189:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
14190:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
14191:                 } else {
14192:                     @changed = @{$selfenrollhash{'validation'}{$item}};
14193:                 }
14194:             } else {
14195:                 @changed = @{$selfenrollhash{'validation'}{$item}};
14196:             }
14197:             if (@changed) {
14198:                 if ($selfenrollhash{'validation'}{$item}) { 
14199:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
14200:                 } else {
14201:                     $changes{'validation'}{$item} = &mt('None');
14202:                 }
14203:             }
14204:         } else {
14205:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
14206:             if ($item eq 'markup') {
14207:                if ($env{'form.selfenroll_validation_'.$item}) {
14208:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
14209:                }
14210:             }
14211:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
14212:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
14213:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
14214:                 }
14215:             }
14216:         }
14217:     }
14218: 
14219:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
14220:                                              $dom);
14221:     if ($putresult eq 'ok') {
14222:         if (keys(%changes) > 0) {
14223:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14224:             $resulttext = &mt('Changes made:').'<ul>';
14225:             foreach my $key ('admin','default','validation') {
14226:                 if (ref($changes{$key}) eq 'HASH') {
14227:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
14228:                     if ($key eq 'validation') {
14229:                         foreach my $item (@{$itemsref}) {
14230:                             if (exists($changes{$key}{$item})) {
14231:                                 if ($item eq 'markup') {
14232:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
14233:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
14234:                                 } else {  
14235:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
14236:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
14237:                                 }
14238:                             }
14239:                         }
14240:                     } else {
14241:                         foreach my $type (@types) {
14242:                             if ($type eq 'community') {
14243:                                 $roles{'1'} = &mt('Community personnel');
14244:                             } else {
14245:                                 $roles{'1'} = &mt('Course personnel');
14246:                             }
14247:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
14248:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
14249:                                     if ($key eq 'admin') {
14250:                                         my @mgrdc = ();
14251:                                         if (ref($ordered{$key}) eq 'ARRAY') {
14252:                                             foreach my $item (@{$ordered{'admin'}}) {
14253:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
14254:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
14255:                                                         push(@mgrdc,$item);
14256:                                                     }
14257:                                                 }
14258:                                             }
14259:                                             if (@mgrdc) {
14260:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
14261:                                             } else {
14262:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
14263:                                             }
14264:                                         }
14265:                                     } else {
14266:                                         if (ref($ordered{$key}) eq 'ARRAY') {
14267:                                             foreach my $item (@{$ordered{$key}}) {
14268:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
14269:                                                     $domdefaults{$type.'selfenroll'.$item} =
14270:                                                         $selfenrollhash{$key}{$type}{$item};
14271:                                                 }
14272:                                             }
14273:                                         }
14274:                                     }
14275:                                 }
14276:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
14277:                                 foreach my $item (@{$ordered{$key}}) {
14278:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
14279:                                         $resulttext .= '<li>';
14280:                                         if ($key eq 'admin') {
14281:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
14282:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
14283:                                         } else {
14284:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
14285:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
14286:                                         }
14287:                                         $resulttext .= '</li>';
14288:                                     }
14289:                                 }
14290:                                 $resulttext .= '</ul></li>';
14291:                             }
14292:                         }
14293:                         $resulttext .= '</ul></li>'; 
14294:                     }
14295:                 }
14296:             }
14297:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
14298:                 my $cachetime = 24*60*60;
14299:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14300:                 if (ref($lastactref) eq 'HASH') {
14301:                     $lastactref->{'domdefaults'} = 1;
14302:                 }
14303:             }
14304:             $resulttext .= '</ul>';
14305:         } else {
14306:             $resulttext = &mt('No changes made to self-enrollment settings');
14307:         }
14308:     } else {
14309:         $resulttext = '<span class="LC_error">'.
14310:             &mt('An error occurred: [_1]',$putresult).'</span>';
14311:     }
14312:     return $resulttext;
14313: }
14314: 
14315: sub modify_usersessions {
14316:     my ($dom,$lastactref,%domconfig) = @_;
14317:     my @hostingtypes = ('version','excludedomain','includedomain');
14318:     my @offloadtypes = ('primary','default');
14319:     my %types = (
14320:                   remote => \@hostingtypes,
14321:                   hosted => \@hostingtypes,
14322:                   spares => \@offloadtypes,
14323:                 );
14324:     my @prefixes = ('remote','hosted','spares');
14325:     my @lcversions = &Apache::lonnet::all_loncaparevs();
14326:     my (%by_ip,%by_location,@intdoms,@instdoms);
14327:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
14328:     my @locations = sort(keys(%by_location));
14329:     my (%defaultshash,%changes);
14330:     foreach my $prefix (@prefixes) {
14331:         $defaultshash{'usersessions'}{$prefix} = {};
14332:     }
14333:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14334:     my $resulttext;
14335:     my %iphost = &Apache::lonnet::get_iphost();
14336:     foreach my $prefix (@prefixes) {
14337:         next if ($prefix eq 'spares');
14338:         foreach my $type (@{$types{$prefix}}) {
14339:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
14340:             if ($type eq 'version') {
14341:                 my $value = $env{'form.'.$prefix.'_'.$type};
14342:                 my $okvalue;
14343:                 if ($value ne '') {
14344:                     if (grep(/^\Q$value\E$/,@lcversions)) {
14345:                         $okvalue = $value;
14346:                     }
14347:                 }
14348:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
14349:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
14350:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
14351:                             if ($inuse == 0) {
14352:                                 $changes{$prefix}{$type} = 1;
14353:                             } else {
14354:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
14355:                                     $changes{$prefix}{$type} = 1;
14356:                                 }
14357:                                 if ($okvalue ne '') {
14358:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
14359:                                 } 
14360:                             }
14361:                         } else {
14362:                             if (($inuse == 1) && ($okvalue ne '')) {
14363:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
14364:                                 $changes{$prefix}{$type} = 1;
14365:                             }
14366:                         }
14367:                     } else {
14368:                         if (($inuse == 1) && ($okvalue ne '')) {
14369:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
14370:                             $changes{$prefix}{$type} = 1;
14371:                         }
14372:                     }
14373:                 } else {
14374:                     if (($inuse == 1) && ($okvalue ne '')) {
14375:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
14376:                         $changes{$prefix}{$type} = 1;
14377:                     }
14378:                 }
14379:             } else {
14380:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
14381:                 my @okvals;
14382:                 foreach my $val (@vals) {
14383:                     if ($val =~ /:/) {
14384:                         my @items = split(/:/,$val);
14385:                         foreach my $item (@items) {
14386:                             if (ref($by_location{$item}) eq 'ARRAY') {
14387:                                 push(@okvals,$item);
14388:                             }
14389:                         }
14390:                     } else {
14391:                         if (ref($by_location{$val}) eq 'ARRAY') {
14392:                             push(@okvals,$val);
14393:                         }
14394:                     }
14395:                 }
14396:                 @okvals = sort(@okvals);
14397:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
14398:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
14399:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
14400:                             if ($inuse == 0) {
14401:                                 $changes{$prefix}{$type} = 1; 
14402:                             } else {
14403:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
14404:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
14405:                                 if (@changed > 0) {
14406:                                     $changes{$prefix}{$type} = 1;
14407:                                 }
14408:                             }
14409:                         } else {
14410:                             if ($inuse == 1) {
14411:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
14412:                                 $changes{$prefix}{$type} = 1;
14413:                             }
14414:                         } 
14415:                     } else {
14416:                         if ($inuse == 1) {
14417:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
14418:                             $changes{$prefix}{$type} = 1;
14419:                         }
14420:                     }
14421:                 } else {
14422:                     if ($inuse == 1) {
14423:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
14424:                         $changes{$prefix}{$type} = 1;
14425:                     }
14426:                 }
14427:             }
14428:         }
14429:     }
14430: 
14431:     my @alldoms = &Apache::lonnet::all_domains();
14432:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
14433:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
14434:     my $savespares;
14435: 
14436:     foreach my $lonhost (sort(keys(%servers))) {
14437:         my $serverhomeID =
14438:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
14439:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
14440:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
14441:         my %spareschg;
14442:         foreach my $type (@{$types{'spares'}}) {
14443:             my @okspares;
14444:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
14445:             foreach my $server (@checked) {
14446:                 if (&Apache::lonnet::hostname($server) ne '') {
14447:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
14448:                         unless (grep(/^\Q$server\E$/,@okspares)) {
14449:                             push(@okspares,$server);
14450:                         }
14451:                     }
14452:                 }
14453:             }
14454:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
14455:             my $newspare;
14456:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
14457:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
14458:                     $newspare = $new;
14459:                 }
14460:             }
14461:             my @spares;
14462:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
14463:                 @spares = sort(@okspares,$newspare);
14464:             } else {
14465:                 @spares = sort(@okspares);
14466:             }
14467:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
14468:             if (ref($spareid{$lonhost}) eq 'HASH') {
14469:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
14470:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
14471:                     if (@diffs > 0) {
14472:                         $spareschg{$type} = 1;
14473:                     }
14474:                 }
14475:             }
14476:         }
14477:         if (keys(%spareschg) > 0) {
14478:             $changes{'spares'}{$lonhost} = \%spareschg;
14479:         }
14480:     }
14481:     $defaultshash{'usersessions'}{'offloadnow'} = {};
14482:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
14483:     my @okoffload;
14484:     if (@offloadnow) {
14485:         foreach my $server (@offloadnow) {
14486:             if (&Apache::lonnet::hostname($server) ne '') {
14487:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
14488:                     push(@okoffload,$server);
14489:                 }
14490:             }
14491:         }
14492:         if (@okoffload) {
14493:             foreach my $lonhost (@okoffload) {
14494:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
14495:             }
14496:         }
14497:     }
14498:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
14499:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
14500:             if (ref($changes{'spares'}) eq 'HASH') {
14501:                 if (keys(%{$changes{'spares'}}) > 0) {
14502:                     $savespares = 1;
14503:                 }
14504:             }
14505:         } else {
14506:             $savespares = 1;
14507:         }
14508:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
14509:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
14510:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
14511:                     $changes{'offloadnow'} = 1;
14512:                     last;
14513:                 }
14514:             }
14515:             unless ($changes{'offloadnow'}) {
14516:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) {
14517:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
14518:                         $changes{'offloadnow'} = 1;
14519:                         last;
14520:                     }
14521:                 }
14522:             }
14523:         } elsif (@okoffload) {
14524:             $changes{'offloadnow'} = 1;
14525:         }
14526:     } elsif (@okoffload) {
14527:         $changes{'offloadnow'} = 1;
14528:     }
14529:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
14530:     if ((keys(%changes) > 0) || ($savespares)) {
14531:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
14532:                                                  $dom);
14533:         if ($putresult eq 'ok') {
14534:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
14535:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
14536:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
14537:                 }
14538:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
14539:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
14540:                 }
14541:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
14542:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
14543:                 }
14544:             }
14545:             my $cachetime = 24*60*60;
14546:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14547:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
14548:             if (ref($lastactref) eq 'HASH') {
14549:                 $lastactref->{'domdefaults'} = 1;
14550:                 $lastactref->{'usersessions'} = 1;
14551:             }
14552:             if (keys(%changes) > 0) {
14553:                 my %lt = &usersession_titles();
14554:                 $resulttext = &mt('Changes made:').'<ul>';
14555:                 foreach my $prefix (@prefixes) {
14556:                     if (ref($changes{$prefix}) eq 'HASH') {
14557:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
14558:                         if ($prefix eq 'spares') {
14559:                             if (ref($changes{$prefix}) eq 'HASH') {
14560:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
14561:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
14562:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
14563:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
14564:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
14565:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
14566:                                         foreach my $type (@{$types{$prefix}}) {
14567:                                             if ($changes{$prefix}{$lonhost}{$type}) {
14568:                                                 my $offloadto = &mt('None');
14569:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
14570:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
14571:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
14572:                                                     }
14573:                                                 }
14574:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
14575:                                             }
14576:                                         }
14577:                                     }
14578:                                     $resulttext .= '</li>';
14579:                                 }
14580:                             }
14581:                         } else {
14582:                             foreach my $type (@{$types{$prefix}}) {
14583:                                 if (defined($changes{$prefix}{$type})) {
14584:                                     my $newvalue;
14585:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
14586:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
14587:                                             if ($type eq 'version') {
14588:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
14589:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
14590:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
14591:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
14592:                                                 }
14593:                                             }
14594:                                         }
14595:                                     }
14596:                                     if ($newvalue eq '') {
14597:                                         if ($type eq 'version') {
14598:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
14599:                                         } else {
14600:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
14601:                                         }
14602:                                     } else {
14603:                                         if ($type eq 'version') {
14604:                                             $newvalue .= ' '.&mt('(or later)'); 
14605:                                         }
14606:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
14607:                                     }
14608:                                 }
14609:                             }
14610:                         }
14611:                         $resulttext .= '</ul>';
14612:                     }
14613:                 }
14614:                 if ($changes{'offloadnow'}) {
14615:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
14616:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
14617:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
14618:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
14619:                                 $resulttext .= '<li>'.$lonhost.'</li>';
14620:                             }
14621:                             $resulttext .= '</ul>';
14622:                         } else {
14623:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
14624:                         }
14625:                     } else {
14626:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
14627:                     }
14628:                 }
14629:                 $resulttext .= '</ul>';
14630:             } else {
14631:                 $resulttext = $nochgmsg;
14632:             }
14633:         } else {
14634:             $resulttext = '<span class="LC_error">'.
14635:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14636:         }
14637:     } else {
14638:         $resulttext = $nochgmsg;
14639:     }
14640:     return $resulttext;
14641: }
14642: 
14643: sub modify_ssl {
14644:     my ($dom,$lastactref,%domconfig) = @_;
14645:     my (%by_ip,%by_location,@intdoms,@instdoms);
14646:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
14647:     my @locations = sort(keys(%by_location));
14648:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
14649:     my (%defaultshash,%changes);
14650:     my $action = 'ssl';
14651:     my @prefixes = ('connto','connfrom','replication');
14652:     foreach my $prefix (@prefixes) {
14653:         $defaultshash{$action}{$prefix} = {};
14654:     }
14655:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14656:     my $resulttext;
14657:     my %iphost = &Apache::lonnet::get_iphost();
14658:     my @reptypes = ('certreq','nocertreq');
14659:     my @connecttypes = ('dom','intdom','other');
14660:     my %types = (
14661:                   connto      => \@connecttypes,
14662:                   connfrom    => \@connecttypes,
14663:                   replication => \@reptypes,
14664:                 );
14665:     foreach my $prefix (sort(keys(%types))) {
14666:         foreach my $type (@{$types{$prefix}}) {
14667:             if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
14668:                 my $value = 'yes';
14669:                 if ($env{'form.'.$prefix.'_'.$type} =~ /^(no|req)$/) {
14670:                     $value = $env{'form.'.$prefix.'_'.$type};
14671:                 }
14672:                 if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
14673:                     if ($domconfig{$action}{$prefix}{$type} ne '') {
14674:                         if ($value ne $domconfig{$action}{$prefix}{$type}) {
14675:                             $changes{$prefix}{$type} = 1;
14676:                         }
14677:                         $defaultshash{$action}{$prefix}{$type} = $value;
14678:                     } else {
14679:                         $defaultshash{$action}{$prefix}{$type} = $value;
14680:                         $changes{$prefix}{$type} = 1;
14681:                     }
14682:                 } else {
14683:                     $defaultshash{$action}{$prefix}{$type} = $value;
14684:                     $changes{$prefix}{$type} = 1;
14685:                 }
14686:                 if (($type eq 'dom') && (keys(%servers) == 1)) {
14687:                     delete($changes{$prefix}{$type});
14688:                 } elsif (($type eq 'intdom') && (@instdoms == 1)) {
14689:                     delete($changes{$prefix}{$type});
14690:                 } elsif (($type eq 'other') && (keys(%by_location) == 0)) { 
14691:                     delete($changes{$prefix}{$type});
14692:                 }
14693:             } elsif ($prefix eq 'replication') {
14694:                 if (@locations > 0) {
14695:                     my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
14696:                     my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
14697:                     my @okvals;
14698:                     foreach my $val (@vals) {
14699:                         if ($val =~ /:/) {
14700:                             my @items = split(/:/,$val);
14701:                             foreach my $item (@items) {
14702:                                 if (ref($by_location{$item}) eq 'ARRAY') {
14703:                                     push(@okvals,$item);
14704:                                 }
14705:                             }
14706:                         } else {
14707:                             if (ref($by_location{$val}) eq 'ARRAY') {
14708:                                 push(@okvals,$val);
14709:                             }
14710:                         }
14711:                     }
14712:                     @okvals = sort(@okvals);
14713:                     if (ref($domconfig{$action}) eq 'HASH') {
14714:                         if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
14715:                             if (ref($domconfig{$action}{$prefix}{$type}) eq 'ARRAY') {
14716:                                 if ($inuse == 0) {
14717:                                     $changes{$prefix}{$type} = 1;
14718:                                 } else {
14719:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
14720:                                     my @changed = &Apache::loncommon::compare_arrays($domconfig{$action}{$prefix}{$type},$defaultshash{$action}{$prefix}{$type});
14721:                                     if (@changed > 0) {
14722:                                         $changes{$prefix}{$type} = 1;
14723:                                     }
14724:                                 }
14725:                             } else {
14726:                                 if ($inuse == 1) {
14727:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
14728:                                     $changes{$prefix}{$type} = 1;
14729:                                 }
14730:                             }
14731:                         } else {
14732:                             if ($inuse == 1) {
14733:                                 $defaultshash{$action}{$prefix}{$type} = \@okvals;
14734:                                 $changes{$prefix}{$type} = 1;
14735:                             }
14736:                         }
14737:                     } else {
14738:                         if ($inuse == 1) {
14739:                             $defaultshash{$action}{$prefix}{$type} = \@okvals;
14740:                             $changes{$prefix}{$type} = 1;
14741:                         }
14742:                     }
14743:                 }
14744:             }
14745:         }
14746:     }
14747:     my $nochgmsg = &mt('No changes made to LON-CAPA SSL settings');
14748:     if (keys(%changes) > 0) {
14749:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
14750:                                                  $dom);
14751:         if ($putresult eq 'ok') {
14752:             if (ref($defaultshash{$action}) eq 'HASH') {
14753:                 if (ref($defaultshash{$action}{'replication'}) eq 'HASH') {
14754:                     $domdefaults{'replication'} = $defaultshash{$action}{'replication'};
14755:                 }
14756:                 if (ref($defaultshash{$action}{'connto'}) eq 'HASH') {
14757:                     $domdefaults{'connto'} = $domconfig{$action}{'connto'};
14758:                 }
14759:                 if (ref($defaultshash{$action}{'connfrom'}) eq 'HASH') {
14760:                     $domdefaults{'connfrom'} = $domconfig{$action}{'connfrom'};
14761:                 }
14762:             }
14763:             my $cachetime = 24*60*60;
14764:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14765:             if (ref($lastactref) eq 'HASH') {
14766:                 $lastactref->{'domdefaults'} = 1;
14767:             }
14768:             if (keys(%changes) > 0) {
14769:                 my %titles = &ssl_titles();
14770:                 $resulttext = &mt('Changes made:').'<ul>';
14771:                 foreach my $prefix (@prefixes) {
14772:                     if (ref($changes{$prefix}) eq 'HASH') {
14773:                         $resulttext .= '<li>'.$titles{$prefix}.'<ul>';
14774:                         foreach my $type (@{$types{$prefix}}) {
14775:                             if (defined($changes{$prefix}{$type})) {
14776:                                 my $newvalue;
14777:                                 if (ref($defaultshash{$action}) eq 'HASH') {
14778:                                     if (ref($defaultshash{$action}{$prefix})) {
14779:                                         if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
14780:                                             $newvalue = $titles{$defaultshash{$action}{$prefix}{$type}};
14781:                                         } elsif (ref($defaultshash{$action}{$prefix}{$type}) eq 'ARRAY') {
14782:                                             if (@{$defaultshash{$action}{$prefix}{$type}} > 0) {
14783:                                                 $newvalue = join(', ',@{$defaultshash{$action}{$prefix}{$type}});
14784:                                             }
14785:                                         }
14786:                                     }
14787:                                     if ($newvalue eq '') {
14788:                                         $resulttext .= '<li>'.&mt('[_1] set to: none',$titles{$type}).'</li>';
14789:                                     } else {
14790:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$titles{$type},$newvalue).'</li>';
14791:                                     }
14792:                                 }
14793:                             }
14794:                         }
14795:                         $resulttext .= '</ul>';
14796:                     }
14797:                 }
14798:             } else {
14799:                 $resulttext = $nochgmsg;
14800:             }
14801:         } else {
14802:             $resulttext = '<span class="LC_error">'.
14803:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14804:         }
14805:     } else {
14806:         $resulttext = $nochgmsg;
14807:     }
14808:     return $resulttext;
14809: }
14810: 
14811: sub modify_trust {
14812:     my ($dom,$lastactref,%domconfig) = @_;
14813:     my (%by_ip,%by_location,@intdoms,@instdoms);
14814:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
14815:     my @locations = sort(keys(%by_location));
14816:     my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
14817:     my @types = ('exc','inc');
14818:     my (%defaultshash,%changes);
14819:     foreach my $prefix (@prefixes) {
14820:         $defaultshash{'trust'}{$prefix} = {};
14821:     }
14822:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14823:     my $resulttext;
14824:     foreach my $prefix (@prefixes) {
14825:         foreach my $type (@types) {
14826:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
14827:             my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
14828:             my @okvals;
14829:             foreach my $val (@vals) {
14830:                 if ($val =~ /:/) {
14831:                     my @items = split(/:/,$val);
14832:                     foreach my $item (@items) {
14833:                         if (ref($by_location{$item}) eq 'ARRAY') {
14834:                             push(@okvals,$item);
14835:                         }
14836:                     }
14837:                 } else {
14838:                     if (ref($by_location{$val}) eq 'ARRAY') {
14839:                         push(@okvals,$val);
14840:                     }
14841:                 }
14842:             }
14843:             @okvals = sort(@okvals);
14844:             if (ref($domconfig{'trust'}) eq 'HASH') {
14845:                 if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
14846:                     if (ref($domconfig{'trust'}{$prefix}{$type}) eq 'ARRAY') {
14847:                         if ($inuse == 0) {
14848:                             $changes{$prefix}{$type} = 1;
14849:                         } else {
14850:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
14851:                             my @changed = &Apache::loncommon::compare_arrays($domconfig{'trust'}{$prefix}{$type},$defaultshash{'trust'}{$prefix}{$type});
14852:                             if (@changed > 0) {
14853:                                 $changes{$prefix}{$type} = 1;
14854:                             }
14855:                         }
14856:                     } else {
14857:                         if ($inuse == 1) {
14858:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
14859:                             $changes{$prefix}{$type} = 1;
14860:                         }
14861:                     }
14862:                 } else {
14863:                     if ($inuse == 1) {
14864:                         $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
14865:                         $changes{$prefix}{$type} = 1;
14866:                     }
14867:                 }
14868:             } else {
14869:                 if ($inuse == 1) {
14870:                     $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
14871:                     $changes{$prefix}{$type} = 1;
14872:                 }
14873:             }
14874:         }
14875:     }
14876:     my $nochgmsg = &mt('No changes made to trust settings.');
14877:     if (keys(%changes) > 0) {
14878:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
14879:                                                  $dom);
14880:         if ($putresult eq 'ok') {
14881:             if (ref($defaultshash{'trust'}) eq 'HASH') {
14882:                 foreach my $prefix (@prefixes) {
14883:                     if (ref($defaultshash{'trust'}{$prefix}) eq 'HASH') {
14884:                         $domdefaults{'trust'.$prefix} = $defaultshash{'trust'}{$prefix};
14885:                     }
14886:                 }
14887:             }
14888:             my $cachetime = 24*60*60;
14889:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14890:             if (ref($lastactref) eq 'HASH') {
14891:                 $lastactref->{'domdefaults'} = 1;
14892:             }
14893:             if (keys(%changes) > 0) {
14894:                 my %lt = &trust_titles();
14895:                 $resulttext = &mt('Changes made:').'<ul>';
14896:                 foreach my $prefix (@prefixes) {
14897:                     if (ref($changes{$prefix}) eq 'HASH') {
14898:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
14899:                         foreach my $type (@types) {
14900:                             if (defined($changes{$prefix}{$type})) {
14901:                                 my $newvalue;
14902:                                 if (ref($defaultshash{'trust'}) eq 'HASH') {
14903:                                     if (ref($defaultshash{'trust'}{$prefix})) {
14904:                                         if (ref($defaultshash{'trust'}{$prefix}{$type}) eq 'ARRAY') {
14905:                                             if (@{$defaultshash{'trust'}{$prefix}{$type}} > 0) {
14906:                                                 $newvalue = join(', ',@{$defaultshash{'trust'}{$prefix}{$type}});
14907:                                             }
14908:                                         }
14909:                                     }
14910:                                 }
14911:                                 if ($newvalue eq '') {
14912:                                     $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
14913:                                 } else {
14914:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
14915:                                 }
14916:                             }
14917:                         }
14918:                         $resulttext .= '</ul>';
14919:                     }
14920:                 }
14921:                 $resulttext .= '</ul>';
14922:             } else {
14923:                 $resulttext = $nochgmsg;
14924:             }
14925:         } else {
14926:             $resulttext = '<span class="LC_error">'.
14927:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14928:         }
14929:     } else {
14930:         $resulttext = $nochgmsg;
14931:     }
14932:     return $resulttext;
14933: }
14934: 
14935: sub modify_loadbalancing {
14936:     my ($dom,%domconfig) = @_;
14937:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
14938:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
14939:     my ($othertitle,$usertypes,$types) =
14940:         &Apache::loncommon::sorted_inst_types($dom);
14941:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
14942:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
14943:     my @sparestypes = ('primary','default');
14944:     my %typetitles = &sparestype_titles();
14945:     my $resulttext;
14946:     my (%currbalancer,%currtargets,%currrules,%existing);
14947:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
14948:         %existing = %{$domconfig{'loadbalancing'}};
14949:     }
14950:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
14951:                               \%currtargets,\%currrules);
14952:     my ($saveloadbalancing,%defaultshash,%changes);
14953:     my ($alltypes,$othertypes,$titles) =
14954:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
14955:     my %ruletitles = &offloadtype_text();
14956:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
14957:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
14958:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
14959:         if ($balancer eq '') {
14960:             next;
14961:         }
14962:         if (!exists($servers{$balancer})) {
14963:             if (exists($currbalancer{$balancer})) {
14964:                 push(@{$changes{'delete'}},$balancer);
14965:             }
14966:             next;
14967:         }
14968:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
14969:             push(@{$changes{'delete'}},$balancer);
14970:             next;
14971:         }
14972:         if (!exists($currbalancer{$balancer})) {
14973:             push(@{$changes{'add'}},$balancer);
14974:         }
14975:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
14976:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
14977:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
14978:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
14979:             $saveloadbalancing = 1;
14980:         }
14981:         foreach my $sparetype (@sparestypes) {
14982:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
14983:             my @offloadto;
14984:             foreach my $target (@targets) {
14985:                 if (($servers{$target}) && ($target ne $balancer)) {
14986:                     if ($sparetype eq 'default') {
14987:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
14988:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
14989:                         }
14990:                     }
14991:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
14992:                         push(@offloadto,$target);
14993:                     }
14994:                 }
14995:             }
14996:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
14997:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
14998:                     push(@offloadto,$balancer);
14999:                 }
15000:             }
15001:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
15002:         }
15003:         if (ref($currtargets{$balancer}) eq 'HASH') {
15004:             foreach my $sparetype (@sparestypes) {
15005:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
15006:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
15007:                     if (@targetdiffs > 0) {
15008:                         $changes{'curr'}{$balancer}{'targets'} = 1;
15009:                     }
15010:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
15011:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
15012:                         $changes{'curr'}{$balancer}{'targets'} = 1;
15013:                     }
15014:                 }
15015:             }
15016:         } else {
15017:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
15018:                 foreach my $sparetype (@sparestypes) {
15019:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
15020:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
15021:                             $changes{'curr'}{$balancer}{'targets'} = 1;
15022:                         }
15023:                     }
15024:                 }
15025:             }
15026:         }
15027:         my $ishomedom;
15028:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
15029:             $ishomedom = 1;
15030:         }
15031:         if (ref($alltypes) eq 'ARRAY') {
15032:             foreach my $type (@{$alltypes}) {
15033:                 my $rule;
15034:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
15035:                          (!$ishomedom)) {
15036:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
15037:                 }
15038:                 if ($rule eq 'specific') {
15039:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
15040:                     if (exists($servers{$specifiedhost})) {
15041:                         $rule = $specifiedhost;
15042:                     }
15043:                 }
15044:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
15045:                 if (ref($currrules{$balancer}) eq 'HASH') {
15046:                     if ($rule ne $currrules{$balancer}{$type}) {
15047:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
15048:                     }
15049:                 } elsif ($rule ne '') {
15050:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
15051:                 }
15052:             }
15053:         }
15054:     }
15055:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
15056:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
15057:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
15058:             $defaultshash{'loadbalancing'} = {};
15059:         }
15060:         my $putresult = &Apache::lonnet::put_dom('configuration',
15061:                                                  \%defaultshash,$dom);
15062:         if ($putresult eq 'ok') {
15063:             if (keys(%changes) > 0) {
15064:                 my %toupdate;
15065:                 if (ref($changes{'delete'}) eq 'ARRAY') {
15066:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
15067:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
15068:                         $toupdate{$balancer} = 1;
15069:                     }
15070:                 }
15071:                 if (ref($changes{'add'}) eq 'ARRAY') {
15072:                     foreach my $balancer (sort(@{$changes{'add'}})) {
15073:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
15074:                         $toupdate{$balancer} = 1;
15075:                     }
15076:                 }
15077:                 if (ref($changes{'curr'}) eq 'HASH') {
15078:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
15079:                         $toupdate{$balancer} = 1;
15080:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
15081:                             if ($changes{'curr'}{$balancer}{'targets'}) {
15082:                                 my %offloadstr;
15083:                                 foreach my $sparetype (@sparestypes) {
15084:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
15085:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
15086:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
15087:                                         }
15088:                                     }
15089:                                 }
15090:                                 if (keys(%offloadstr) == 0) {
15091:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
15092:                                 } else {
15093:                                     my $showoffload;
15094:                                     foreach my $sparetype (@sparestypes) {
15095:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
15096:                                         if (defined($offloadstr{$sparetype})) {
15097:                                             $showoffload .= $offloadstr{$sparetype};
15098:                                         } else {
15099:                                             $showoffload .= &mt('None');
15100:                                         }
15101:                                         $showoffload .= ('&nbsp;'x3);
15102:                                     }
15103:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
15104:                                 }
15105:                             }
15106:                         }
15107:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
15108:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
15109:                                 foreach my $type (@{$alltypes}) {
15110:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
15111:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
15112:                                         my $balancetext;
15113:                                         if ($rule eq '') {
15114:                                             $balancetext =  $ruletitles{'default'};
15115:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
15116:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
15117:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
15118:                                                 foreach my $sparetype (@sparestypes) {
15119:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
15120:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
15121:                                                     }
15122:                                                 }
15123:                                                 foreach my $item (@{$alltypes}) {
15124:                                                     next if ($item =~  /^_LC_ipchange/);
15125:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
15126:                                                     if ($hasrule eq 'homeserver') {
15127:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
15128:                                                     } else {
15129:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
15130:                                                             if ($servers{$hasrule}) {
15131:                                                                 $toupdate{$hasrule} = 1;
15132:                                                             }
15133:                                                         }
15134:                                                     }
15135:                                                 }
15136:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
15137:                                                     $balancetext =  $ruletitles{$rule};
15138:                                                 } else {
15139:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
15140:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
15141:                                                     if ($receiver) {
15142:                                                         $toupdate{$receiver};
15143:                                                     }
15144:                                                 }
15145:                                             } else {
15146:                                                 $balancetext =  $ruletitles{$rule};
15147:                                             }
15148:                                         } else {
15149:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
15150:                                         }
15151:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
15152:                                     }
15153:                                 }
15154:                             }
15155:                         }
15156:                         if (keys(%toupdate)) {
15157:                             my %thismachine;
15158:                             my $updatedhere;
15159:                             my $cachetime = 60*60*24;
15160:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
15161:                             foreach my $lonhost (keys(%toupdate)) {
15162:                                 if ($thismachine{$lonhost}) {
15163:                                     unless ($updatedhere) {
15164:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
15165:                                                                       $defaultshash{'loadbalancing'},
15166:                                                                       $cachetime);
15167:                                         $updatedhere = 1;
15168:                                     }
15169:                                 } else {
15170:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
15171:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
15172:                                 }
15173:                             }
15174:                         }
15175:                     }
15176:                 }
15177:                 if ($resulttext ne '') {
15178:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
15179:                 } else {
15180:                     $resulttext = $nochgmsg;
15181:                 }
15182:             } else {
15183:                 $resulttext = $nochgmsg;
15184:             }
15185:         } else {
15186:             $resulttext = '<span class="LC_error">'.
15187:                           &mt('An error occurred: [_1]',$putresult).'</span>';
15188:         }
15189:     } else {
15190:         $resulttext = $nochgmsg;
15191:     }
15192:     return $resulttext;
15193: }
15194: 
15195: sub recurse_check {
15196:     my ($chkcats,$categories,$depth,$name) = @_;
15197:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
15198:         my $chg = 0;
15199:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
15200:             my $category = $chkcats->[$depth]{$name}[$j];
15201:             my $item;
15202:             if ($category eq '') {
15203:                 $chg ++;
15204:             } else {
15205:                 my $deeper = $depth + 1;
15206:                 $item = &escape($category).':'.&escape($name).':'.$depth;
15207:                 if ($chg) {
15208:                     $categories->{$item} -= $chg;
15209:                 }
15210:                 &recurse_check($chkcats,$categories,$deeper,$category);
15211:                 $deeper --;
15212:             }
15213:         }
15214:     }
15215:     return;
15216: }
15217: 
15218: sub recurse_cat_deletes {
15219:     my ($item,$coursecategories,$deletions) = @_;
15220:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
15221:     my $subdepth = $depth + 1;
15222:     if (ref($coursecategories) eq 'HASH') {
15223:         foreach my $subitem (keys(%{$coursecategories})) {
15224:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
15225:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
15226:                 delete($coursecategories->{$subitem});
15227:                 $deletions->{$subitem} = 1;
15228:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
15229:             }
15230:         }
15231:     }
15232:     return;
15233: }
15234: 
15235: sub active_dc_picker {
15236:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
15237:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
15238:     my @domcoord = keys(%domcoords);
15239:     if (keys(%currhash)) {
15240:         foreach my $dc (keys(%currhash)) {
15241:             unless (exists($domcoords{$dc})) {
15242:                 push(@domcoord,$dc);
15243:             }
15244:         }
15245:     }
15246:     @domcoord = sort(@domcoord);
15247:     my $numdcs = scalar(@domcoord);
15248:     my $rows = 0;
15249:     my $table;
15250:     if ($numdcs > 1) {
15251:         $table = '<table>';
15252:         for (my $i=0; $i<@domcoord; $i++) {
15253:             my $rem = $i%($numinrow);
15254:             if ($rem == 0) {
15255:                 if ($i > 0) {
15256:                     $table .= '</tr>';
15257:                 }
15258:                 $table .= '<tr>';
15259:                 $rows ++;
15260:             }
15261:             my $check = '';
15262:             if ($inputtype eq 'radio') {
15263:                 if (keys(%currhash) == 0) {
15264:                     if (!$i) {
15265:                         $check = ' checked="checked"';
15266:                     }
15267:                 } elsif (exists($currhash{$domcoord[$i]})) {
15268:                     $check = ' checked="checked"';
15269:                 }
15270:             } else {
15271:                 if (exists($currhash{$domcoord[$i]})) {
15272:                     $check = ' checked="checked"';
15273:                 }
15274:             }
15275:             if ($i == @domcoord - 1) {
15276:                 my $colsleft = $numinrow - $rem;
15277:                 if ($colsleft > 1) {
15278:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
15279:                 } else {
15280:                     $table .= '<td class="LC_left_item">';
15281:                 }
15282:             } else {
15283:                 $table .= '<td class="LC_left_item">';
15284:             }
15285:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
15286:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
15287:             $table .= '<span class="LC_nobreak"><label>'.
15288:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
15289:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
15290:             if ($user ne $dcname.':'.$dcdom) {
15291:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
15292:             }
15293:             $table .= '</label></span></td>';
15294:         }
15295:         $table .= '</tr></table>';
15296:     } elsif ($numdcs == 1) {
15297:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
15298:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
15299:         if ($inputtype eq 'radio') {
15300:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
15301:             if ($user ne $dcname.':'.$dcdom) {
15302:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
15303:             }
15304:         } else {
15305:             my $check;
15306:             if (exists($currhash{$domcoord[0]})) {
15307:                 $check = ' checked="checked"';
15308:             }
15309:             $table = '<span class="LC_nobreak"><label>'.
15310:                      '<input type="checkbox" name="'.$name.'" '.
15311:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
15312:             if ($user ne $dcname.':'.$dcdom) {
15313:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
15314:             }
15315:             $table .= '</label></span>';
15316:             $rows ++;
15317:         }
15318:     }
15319:     return ($numdcs,$table,$rows);
15320: }
15321: 
15322: sub usersession_titles {
15323:     return &Apache::lonlocal::texthash(
15324:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
15325:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
15326:                spares => 'Servers offloaded to, when busy',
15327:                version => 'LON-CAPA version requirement',
15328:                excludedomain => 'Allow all, but exclude specific domains',
15329:                includedomain => 'Deny all, but include specific domains',
15330:                primary => 'Primary (checked first)',
15331:                default => 'Default',
15332:            );
15333: }
15334: 
15335: sub id_for_thisdom {
15336:     my (%servers) = @_;
15337:     my %altids;
15338:     foreach my $server (keys(%servers)) {
15339:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
15340:         if ($serverhome ne $server) {
15341:             $altids{$serverhome} = $server;
15342:         }
15343:     }
15344:     return %altids;
15345: }
15346: 
15347: sub count_servers {
15348:     my ($currbalancer,%servers) = @_;
15349:     my (@spares,$numspares);
15350:     foreach my $lonhost (sort(keys(%servers))) {
15351:         next if ($currbalancer eq $lonhost);
15352:         push(@spares,$lonhost);
15353:     }
15354:     if ($currbalancer) {
15355:         $numspares = scalar(@spares);
15356:     } else {
15357:         $numspares = scalar(@spares) - 1;
15358:     }
15359:     return ($numspares,@spares);
15360: }
15361: 
15362: sub lonbalance_targets_js {
15363:     my ($dom,$types,$servers,$settings) = @_;
15364:     my $select = &mt('Select');
15365:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
15366:     if (ref($servers) eq 'HASH') {
15367:         $alltargets = join("','",sort(keys(%{$servers})));
15368:         my @homedoms;
15369:         foreach my $server (sort(keys(%{$servers}))) {
15370:             if (&Apache::lonnet::host_domain($server) eq $dom) {
15371:                 push(@homedoms,'1');
15372:             } else {
15373:                 push(@homedoms,'0');
15374:             }
15375:         }
15376:         $allishome = join("','",@homedoms);
15377:     }
15378:     if (ref($types) eq 'ARRAY') {
15379:         if (@{$types} > 0) {
15380:             @alltypes = @{$types};
15381:         }
15382:     }
15383:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
15384:     $allinsttypes = join("','",@alltypes);
15385:     my (%currbalancer,%currtargets,%currrules,%existing);
15386:     if (ref($settings) eq 'HASH') {
15387:         %existing = %{$settings};
15388:     }
15389:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
15390:                               \%currtargets,\%currrules);
15391:     my $balancers = join("','",sort(keys(%currbalancer)));
15392:     return <<"END";
15393: 
15394: <script type="text/javascript">
15395: // <![CDATA[
15396: 
15397: currBalancers = new Array('$balancers');
15398: 
15399: function toggleTargets(balnum) {
15400:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
15401:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
15402:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
15403:     var prevbalancer = prevhostitem.value;
15404:     var baltotal = document.getElementById('loadbalancing_total').value;
15405:     prevhostitem.value = balancer;
15406:     if (prevbalancer != '') {
15407:         var prevIdx = currBalancers.indexOf(prevbalancer);
15408:         if (prevIdx != -1) {
15409:             currBalancers.splice(prevIdx,1);
15410:         }
15411:     }
15412:     if (balancer == '') {
15413:         hideSpares(balnum);
15414:     } else {
15415:         var currIdx = currBalancers.indexOf(balancer);
15416:         if (currIdx == -1) {
15417:             currBalancers.push(balancer);
15418:         }
15419:         var homedoms = new Array('$allishome');
15420:         var ishomedom = homedoms[lonhostitem.selectedIndex];
15421:         showSpares(balancer,ishomedom,balnum);
15422:     }
15423:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
15424:     return;
15425: }
15426: 
15427: function showSpares(balancer,ishomedom,balnum) {
15428:     var alltargets = new Array('$alltargets');
15429:     var insttypes = new Array('$allinsttypes');
15430:     var offloadtypes = new Array('primary','default');
15431: 
15432:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
15433:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
15434:  
15435:     for (var i=0; i<offloadtypes.length; i++) {
15436:         var count = 0;
15437:         for (var j=0; j<alltargets.length; j++) {
15438:             if (alltargets[j] != balancer) {
15439:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
15440:                 item.value = alltargets[j];
15441:                 item.style.textAlign='left';
15442:                 item.style.textFace='normal';
15443:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
15444:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
15445:                     item.disabled = '';
15446:                 } else {
15447:                     item.disabled = 'disabled';
15448:                     item.checked = false;
15449:                 }
15450:                 count ++;
15451:             }
15452:         }
15453:     }
15454:     for (var k=0; k<insttypes.length; k++) {
15455:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
15456:             if (ishomedom == 1) {
15457:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
15458:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
15459:             } else {
15460:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
15461:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
15462:             }
15463:         } else {
15464:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
15465:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
15466:         }
15467:         if ((insttypes[k] != '_LC_external') && 
15468:             ((insttypes[k] != '_LC_internetdom') ||
15469:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
15470:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
15471:             item.options.length = 0;
15472:             item.options[0] = new Option("","",true,true);
15473:             var idx = 0;
15474:             for (var m=0; m<alltargets.length; m++) {
15475:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
15476:                     idx ++;
15477:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
15478:                 }
15479:             }
15480:         }
15481:     }
15482:     return;
15483: }
15484: 
15485: function hideSpares(balnum) {
15486:     var alltargets = new Array('$alltargets');
15487:     var insttypes = new Array('$allinsttypes');
15488:     var offloadtypes = new Array('primary','default');
15489: 
15490:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
15491:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
15492: 
15493:     var total = alltargets.length - 1;
15494:     for (var i=0; i<offloadtypes; i++) {
15495:         for (var j=0; j<total; j++) {
15496:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
15497:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
15498:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
15499:         }
15500:     }
15501:     for (var k=0; k<insttypes.length; k++) {
15502:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
15503:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
15504:         if (insttypes[k] != '_LC_external') {
15505:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
15506:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
15507:         }
15508:     }
15509:     return;
15510: }
15511: 
15512: function checkOffloads(item,balnum,type) {
15513:     var alltargets = new Array('$alltargets');
15514:     var offloadtypes = new Array('primary','default');
15515:     if (item.checked) {
15516:         var total = alltargets.length - 1;
15517:         var other;
15518:         if (type == offloadtypes[0]) {
15519:             other = offloadtypes[1];
15520:         } else {
15521:             other = offloadtypes[0];
15522:         }
15523:         for (var i=0; i<total; i++) {
15524:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
15525:             if (server == item.value) {
15526:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
15527:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
15528:                 }
15529:             }
15530:         }
15531:     }
15532:     return;
15533: }
15534: 
15535: function singleServerToggle(balnum,type) {
15536:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
15537:     if (offloadtoSelIdx == 0) {
15538:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
15539:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
15540: 
15541:     } else {
15542:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
15543:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
15544:     }
15545:     return;
15546: }
15547: 
15548: function balanceruleChange(formname,balnum,type) {
15549:     if (type == '_LC_external') {
15550:         return;
15551:     }
15552:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
15553:     for (var i=0; i<typesRules.length; i++) {
15554:         if (formname.elements[typesRules[i]].checked) {
15555:             if (formname.elements[typesRules[i]].value != 'specific') {
15556:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
15557:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
15558:             } else {
15559:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
15560:             }
15561:         }
15562:     }
15563:     return;
15564: }
15565: 
15566: function balancerDeleteChange(balnum) {
15567:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
15568:     var baltotal = document.getElementById('loadbalancing_total').value;
15569:     var addtarget;
15570:     var removetarget;
15571:     var action = 'delete';
15572:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
15573:         var lonhost = hostitem.value;
15574:         var currIdx = currBalancers.indexOf(lonhost);
15575:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
15576:             if (currIdx != -1) {
15577:                 currBalancers.splice(currIdx,1);
15578:             }
15579:             addtarget = lonhost;
15580:         } else {
15581:             if (currIdx == -1) {
15582:                 currBalancers.push(lonhost);
15583:             }
15584:             removetarget = lonhost;
15585:             action = 'undelete';
15586:         }
15587:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
15588:     }
15589:     return;
15590: }
15591: 
15592: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
15593:     if (baltotal > 1) {
15594:         var offloadtypes = new Array('primary','default');
15595:         var alltargets = new Array('$alltargets');
15596:         var insttypes = new Array('$allinsttypes');
15597:         for (var i=0; i<baltotal; i++) {
15598:             if (i != balnum) {
15599:                 for (var j=0; j<offloadtypes.length; j++) {
15600:                     var total = alltargets.length - 1;
15601:                     for (var k=0; k<total; k++) {
15602:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
15603:                         var server = serveritem.value;
15604:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
15605:                             if (server == addtarget) {
15606:                                 serveritem.disabled = '';
15607:                             }
15608:                         }
15609:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
15610:                             if (server == removetarget) {
15611:                                 serveritem.disabled = 'disabled';
15612:                                 serveritem.checked = false;
15613:                             }
15614:                         }
15615:                     }
15616:                 }
15617:                 for (var j=0; j<insttypes.length; j++) {
15618:                     if (insttypes[j] != '_LC_external') {
15619:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
15620:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
15621:                             var currSel = singleserver.selectedIndex;
15622:                             var currVal = singleserver.options[currSel].value;
15623:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
15624:                                 var numoptions = singleserver.options.length;
15625:                                 var needsnew = 1;
15626:                                 for (var k=0; k<numoptions; k++) {
15627:                                     if (singleserver.options[k] == addtarget) {
15628:                                         needsnew = 0;
15629:                                         break;
15630:                                     }
15631:                                 }
15632:                                 if (needsnew == 1) {
15633:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
15634:                                 }
15635:                             }
15636:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
15637:                                 singleserver.options.length = 0;
15638:                                 if ((currVal) && (currVal != removetarget)) {
15639:                                     singleserver.options[0] = new Option("","",false,false);
15640:                                 } else {
15641:                                     singleserver.options[0] = new Option("","",true,true);
15642:                                 }
15643:                                 var idx = 0;
15644:                                 for (var m=0; m<alltargets.length; m++) {
15645:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
15646:                                         idx ++;
15647:                                         if (currVal == alltargets[m]) {
15648:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
15649:                                         } else {
15650:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
15651:                                         }
15652:                                     }
15653:                                 }
15654:                             }
15655:                         }
15656:                     }
15657:                 }
15658:             }
15659:         }
15660:     }
15661:     return;
15662: }
15663: 
15664: // ]]>
15665: </script>
15666: 
15667: END
15668: }
15669: 
15670: sub new_spares_js {
15671:     my @sparestypes = ('primary','default');
15672:     my $types = join("','",@sparestypes);
15673:     my $select = &mt('Select');
15674:     return <<"END";
15675: 
15676: <script type="text/javascript">
15677: // <![CDATA[
15678: 
15679: function updateNewSpares(formname,lonhost) {
15680:     var types = new Array('$types');
15681:     var include = new Array();
15682:     var exclude = new Array();
15683:     for (var i=0; i<types.length; i++) {
15684:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
15685:         for (var j=0; j<spareboxes.length; j++) {
15686:             if (formname.elements[spareboxes[j]].checked) {
15687:                 exclude.push(formname.elements[spareboxes[j]].value);
15688:             } else {
15689:                 include.push(formname.elements[spareboxes[j]].value);
15690:             }
15691:         }
15692:     }
15693:     for (var i=0; i<types.length; i++) {
15694:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
15695:         var selIdx = newSpare.selectedIndex;
15696:         var currnew = newSpare.options[selIdx].value;
15697:         var okSpares = new Array();
15698:         for (var j=0; j<newSpare.options.length; j++) {
15699:             var possible = newSpare.options[j].value;
15700:             if (possible != '') {
15701:                 if (exclude.indexOf(possible) == -1) {
15702:                     okSpares.push(possible);
15703:                 } else {
15704:                     if (currnew == possible) {
15705:                         selIdx = 0;
15706:                     }
15707:                 }
15708:             }
15709:         }
15710:         for (var k=0; k<include.length; k++) {
15711:             if (okSpares.indexOf(include[k]) == -1) {
15712:                 okSpares.push(include[k]);
15713:             }
15714:         }
15715:         okSpares.sort();
15716:         newSpare.options.length = 0;
15717:         if (selIdx == 0) {
15718:             newSpare.options[0] = new Option("$select","",true,true);
15719:         } else {
15720:             newSpare.options[0] = new Option("$select","",false,false);
15721:         }
15722:         for (var m=0; m<okSpares.length; m++) {
15723:             var idx = m+1;
15724:             var selThis = 0;
15725:             if (selIdx != 0) {
15726:                 if (okSpares[m] == currnew) {
15727:                     selThis = 1;
15728:                 }
15729:             }
15730:             if (selThis == 1) {
15731:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
15732:             } else {
15733:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
15734:             }
15735:         }
15736:     }
15737:     return;
15738: }
15739: 
15740: function checkNewSpares(lonhost,type) {
15741:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
15742:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
15743:     if (chosen != '') { 
15744:         var othertype;
15745:         var othernewSpare;
15746:         if (type == 'primary') {
15747:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
15748:         }
15749:         if (type == 'default') {
15750:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
15751:         }
15752:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
15753:             othernewSpare.selectedIndex = 0;
15754:         }
15755:     }
15756:     return;
15757: }
15758: 
15759: // ]]>
15760: </script>
15761: 
15762: END
15763: 
15764: }
15765: 
15766: sub common_domprefs_js {
15767:     return <<"END";
15768: 
15769: <script type="text/javascript">
15770: // <![CDATA[
15771: 
15772: function getIndicesByName(formname,item) {
15773:     var group = new Array();
15774:     for (var i=0;i<formname.elements.length;i++) {
15775:         if (formname.elements[i].name == item) {
15776:             group.push(formname.elements[i].id);
15777:         }
15778:     }
15779:     return group;
15780: }
15781: 
15782: // ]]>
15783: </script>
15784: 
15785: END
15786: 
15787: }
15788: 
15789: sub recaptcha_js {
15790:     my %lt = &captcha_phrases();
15791:     return <<"END";
15792: 
15793: <script type="text/javascript">
15794: // <![CDATA[
15795: 
15796: function updateCaptcha(caller,context) {
15797:     var privitem;
15798:     var pubitem;
15799:     var privtext;
15800:     var pubtext;
15801:     var versionitem;
15802:     var versiontext;
15803:     if (document.getElementById(context+'_recaptchapub')) {
15804:         pubitem = document.getElementById(context+'_recaptchapub');
15805:     } else {
15806:         return;
15807:     }
15808:     if (document.getElementById(context+'_recaptchapriv')) {
15809:         privitem = document.getElementById(context+'_recaptchapriv');
15810:     } else {
15811:         return;
15812:     }
15813:     if (document.getElementById(context+'_recaptchapubtxt')) {
15814:         pubtext = document.getElementById(context+'_recaptchapubtxt');
15815:     } else {
15816:         return;
15817:     }
15818:     if (document.getElementById(context+'_recaptchaprivtxt')) {
15819:         privtext = document.getElementById(context+'_recaptchaprivtxt');
15820:     } else {
15821:         return;
15822:     }
15823:     if (document.getElementById(context+'_recaptchaversion')) {
15824:         versionitem = document.getElementById(context+'_recaptchaversion');
15825:     } else {
15826:         return;
15827:     }
15828:     if (document.getElementById(context+'_recaptchavertxt')) {
15829:         versiontext = document.getElementById(context+'_recaptchavertxt');
15830:     } else {
15831:         return;
15832:     }
15833:     if (caller.checked) {
15834:         if (caller.value == 'recaptcha') {
15835:             pubitem.type = 'text';
15836:             privitem.type = 'text';
15837:             pubitem.size = '40';
15838:             privitem.size = '40';
15839:             pubtext.innerHTML = "$lt{'pub'}";
15840:             privtext.innerHTML = "$lt{'priv'}";
15841:             versionitem.type = 'text';
15842:             versionitem.size = '3';
15843:             versiontext.innerHTML = "$lt{'ver'}";
15844:         } else {
15845:             pubitem.type = 'hidden';
15846:             privitem.type = 'hidden';
15847:             versionitem.type = 'hidden';
15848:             pubtext.innerHTML = '';
15849:             privtext.innerHTML = '';
15850:             versiontext.innerHTML = '';
15851:         }
15852:     }
15853:     return;
15854: }
15855: 
15856: // ]]>
15857: </script>
15858: 
15859: END
15860: 
15861: }
15862: 
15863: sub toggle_display_js {
15864:     return <<"END";
15865: 
15866: <script type="text/javascript">
15867: // <![CDATA[
15868: 
15869: function toggleDisplay(domForm,caller) {
15870:     if (document.getElementById(caller)) {
15871:         var divitem = document.getElementById(caller);
15872:         var optionsElement = domForm.coursecredits;
15873:         var checkval = 1;
15874:         var dispval = 'block';
15875:         var selfcreateRegExp = /^cancreate_emailverified/;
15876:         if (caller == 'emailoptions') {
15877:             optionsElement = domForm.cancreate_email; 
15878:         }
15879:         if (caller == 'studentsubmission') {
15880:             optionsElement = domForm.postsubmit;
15881:         }
15882:         if (caller == 'cloneinstcode') {
15883:             optionsElement = domForm.canclone;
15884:             checkval = 'instcode';
15885:         }
15886:         if (selfcreateRegExp.test(caller)) {
15887:             optionsElement = domForm.elements[caller];
15888:             checkval = 'other';
15889:             dispval = 'inline'
15890:         }
15891:         if (optionsElement.length) {
15892:             var currval;
15893:             for (var i=0; i<optionsElement.length; i++) {
15894:                 if (optionsElement[i].checked) {
15895:                    currval = optionsElement[i].value;
15896:                 }
15897:             }
15898:             if (currval == checkval) {
15899:                 divitem.style.display = dispval;
15900:             } else {
15901:                 divitem.style.display = 'none';
15902:             }
15903:         }
15904:     }
15905:     return;
15906: }
15907: 
15908: // ]]>
15909: </script>
15910: 
15911: END
15912: 
15913: }
15914: 
15915: sub captcha_phrases {
15916:     return &Apache::lonlocal::texthash (
15917:                  priv => 'Private key',
15918:                  pub  => 'Public key',
15919:                  original  => 'original (CAPTCHA)',
15920:                  recaptcha => 'successor (ReCAPTCHA)',
15921:                  notused   => 'unused',
15922:                  ver => 'ReCAPTCHA version (1 or 2)',
15923:     );
15924: }
15925: 
15926: sub devalidate_remote_domconfs {
15927:     my ($dom,$cachekeys) = @_;
15928:     return unless (ref($cachekeys) eq 'HASH');
15929:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
15930:     my %thismachine;
15931:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
15932:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions','directorysrch');
15933:     if (keys(%servers)) {
15934:         foreach my $server (keys(%servers)) {
15935:             next if ($thismachine{$server});
15936:             my @cached;
15937:             foreach my $name (@posscached) {
15938:                 if ($cachekeys->{$name}) {
15939:                     push(@cached,&escape($name).':'.&escape($dom));
15940:                 }
15941:             }
15942:             if (@cached) {
15943:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
15944:             }
15945:         }
15946:     }
15947:     return;
15948: }
15949: 
15950: 1;

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