File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.102.2.11: download - view: text, annotated - select for diffs
Thu Mar 11 02:51:44 2010 UTC (14 years, 2 months ago) by raeburn
Branches: version_2_9_X
Diff to branchpoint 1.102: preferred, unified
- Backport 1.129.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.102.2.11 2010/03/11 02:51:44 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA#
   23: # /home/httpd/html/adm/gpl.txt
   24: #
   25: # http://www.lon-capa.org/
   26: #
   27: #
   28: ###############################################################
   29: ##############################################################
   30: 
   31: =pod
   32: 
   33: =head1 NAME
   34: 
   35: Apache::domainprefs.pm
   36: 
   37: =head1 SYNOPSIS
   38: 
   39: Handles configuration of a LON-CAPA domain.  
   40: 
   41: This is part of the LearningOnline Network with CAPA project
   42: described at http://www.lon-capa.org.
   43: 
   44: 
   45: =head1 OVERVIEW
   46: 
   47: Each institution using LON-CAPA will typically have a single domain designated 
   48: for use by individuals affliated with the institution.  Accordingly, each domain
   49: may define a default set of logos and a color scheme which can be used to "brand"
   50: the LON-CAPA instance. In addition, an institution will typically have a language
   51: and timezone which are used for the majority of courses.
   52: 
   53: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   54: host of other domain-wide settings which determine the types of functionality
   55: available to users and courses in the domain.
   56: 
   57: There is also a mechanism to configure cataloging of courses in the domain, and
   58: controls on the operation of automated processes which govern such things as
   59: roster updates, user directory updates and processing of course requests.
   60: 
   61: The domain coordination manual which is built dynamically on install/update of 
   62: LON-CAPA from the relevant help items provides more information about domain 
   63: configuration.
   64: 
   65: Most of the domain settings are stored in the configuration.db GDBM file which is
   66: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   67: where $dom is the domain.  The configuration.db stores settings in a number of 
   68: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   69: the domain as files (e.g., image files for logos etc., or plain text files for
   70: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   71: session hosted on the primary library server in the domain, as these files are 
   72: stored in author space belonging to a special $dom-domainconfig user.   
   73: 
   74: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   75: the current settings, and provides an interface to make modifications.
   76: 
   77: =head1 SUBROUTINES
   78: 
   79: =over
   80: 
   81: =item print_quotas()
   82: 
   83: Inputs: 4 
   84: 
   85: $dom,$settings,$rowtotal,$action.
   86: 
   87: $dom is the domain, $settings is a reference to a hash of current settings for
   88: the current context, $rowtotal is a reference to the scalar used to record the 
   89: number of rows displayed on the page, and $action is the context (either quotas 
   90: or requestcourses).
   91: 
   92: The print_quotas routine was orginally created to display/store information
   93: about default quota sizes for portfolio spaces for the different types of 
   94: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   95: but is now also used to manage availability of user tools: 
   96: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   97: used by course owners to request creation of a course.
   98: 
   99: Outputs: 1
  100: 
  101: $datatable  - HTML containing form elements which allow settings to be changed. 
  102: 
  103: In the case of course requests, radio buttons are displayed for each institutional
  104: affiliate type (and also default, and _LC_adv) for each of the course types 
  105: (official, unofficial and community).  In each case the radio buttons allow the 
  106: selection of one of four values:
  107: 
  108: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  109: which have the following effects:
  110: 
  111: 0
  112: 
  113: =over
  114: 
  115: - course requests are not allowed for this course types/affiliation
  116: 
  117: =back
  118: 
  119: approval 
  120: 
  121: =over 
  122: 
  123: - course requests must be approved by a Doman Coordinator in the 
  124: course's domain
  125: 
  126: =back
  127: 
  128: validate 
  129: 
  130: =over
  131: 
  132: - an institutional validation (e.g., check requestor is instructor
  133: of record) needs to be passed before the course will be created.  The required
  134: validation is in localenroll.pm on the primary library server for the course 
  135: domain.
  136: 
  137: =back
  138: 
  139: autolimit 
  140: 
  141: =over
  142:  
  143: - course requests will be processed autoatically up to a limit of
  144: N requests for the course type for the particular requestor.
  145: If N is undefined, there is no limit to the number of course requests
  146: which a course owner may submit and have processed automatically. 
  147: 
  148: =back
  149: 
  150: =item modify_quotas() 
  151: 
  152: =back
  153: 
  154: =cut
  155: 
  156: package Apache::domainprefs;
  157: 
  158: use strict;
  159: use Apache::Constants qw(:common :http);
  160: use Apache::lonnet;
  161: use Apache::loncommon();
  162: use Apache::lonhtmlcommon();
  163: use Apache::lonlocal;
  164: use Apache::lonmsg();
  165: use Apache::lonconfigsettings;
  166: use LONCAPA qw(:DEFAULT :match);
  167: use LONCAPA::Enrollment;
  168: use LONCAPA::lonauthcgi();
  169: use File::Copy;
  170: use Locale::Language;
  171: use DateTime::TimeZone;
  172: use DateTime::Locale;
  173: 
  174: sub handler {
  175:     my $r=shift;
  176:     if ($r->header_only) {
  177:         &Apache::loncommon::content_type($r,'text/html');
  178:         $r->send_http_header;
  179:         return OK;
  180:     }
  181: 
  182:     my $context = 'domain';
  183:     my $dom = $env{'request.role.domain'};
  184:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  185:     if (&Apache::lonnet::allowed('mau',$dom)) {
  186:         &Apache::loncommon::content_type($r,'text/html');
  187:         $r->send_http_header;
  188:     } else {
  189:         $env{'user.error.msg'}=
  190:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  191:         return HTTP_NOT_ACCEPTABLE;
  192:     }
  193:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  194:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  195:                                             ['phase','actions']);
  196:     my $phase = 'pickactions';
  197:     if ( exists($env{'form.phase'}) ) {
  198:         $phase = $env{'form.phase'};
  199:     }
  200:     my %domconfig =
  201:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  202:                 'quotas','autoenroll','autoupdate','autocreate',
  203:                 'directorysrch','usercreation','usermodification',
  204:                 'contacts','defaults','scantron','coursecategories',
  205:                 'serverstatuses','requestcourses','coursedefaults'],$dom);
  206:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  207:                        'autoupdate','autocreate','directorysrch','contacts',
  208:                        'usercreation','usermodification','scantron',
  209:                        'requestcourses','coursecategories','serverstatuses',
  210:                        'coursedefaults');
  211:     my %prefs = (
  212:         'rolecolors' =>
  213:                    { text => 'Default color schemes',
  214:                      help => 'Domain_Configuration_Color_Schemes',
  215:                      header => [{col1 => 'Student Settings',
  216:                                  col2 => '',},
  217:                                 {col1 => 'Coordinator Settings',
  218:                                  col2 => '',},
  219:                                 {col1 => 'Author Settings',
  220:                                  col2 => '',},
  221:                                 {col1 => 'Administrator Settings',
  222:                                  col2 => '',}],
  223:                     },
  224:         'login' =>  
  225:                     { text => 'Log-in page options',
  226:                       help => 'Domain_Configuration_Login_Page',
  227:                       header => [{col1 => 'Item',
  228:                                   col2 => '',}],
  229:                     },
  230:         'defaults' => 
  231:                     { text => 'Default authentication/language/timezone',
  232:                       help => 'Domain_Configuration_LangTZAuth',
  233:                       header => [{col1 => 'Setting',
  234:                                   col2 => 'Value'}],
  235:                     },
  236:         'quotas' => 
  237:                     { text => 'User blogs, personal information pages and portfolios',
  238:                       help => 'Domain_Configuration_Quotas',
  239:                       header => [{col1 => 'User affiliation',
  240:                                   col2 => 'Available tools',
  241:                                   col3 => 'Portfolio quota',}],
  242:                     },
  243:         'autoenroll' =>
  244:                    { text => 'Auto-enrollment settings',
  245:                      help => 'Domain_Configuration_Auto_Enrollment',
  246:                      header => [{col1 => 'Configuration setting',
  247:                                  col2 => 'Value(s)'}],
  248:                    },
  249:         'autoupdate' => 
  250:                    { text => 'Auto-update settings',
  251:                      help => 'Domain_Configuration_Auto_Updates',
  252:                      header => [{col1 => 'Setting',
  253:                                  col2 => 'Value',},
  254:                                 {col1 => 'User population',
  255:                                  col2 => 'Updataeable user data'}],
  256:                   },
  257:         'autocreate' => 
  258:                   {  text => 'Auto-course creation settings',
  259:                      help => 'Domain_Configuration_Auto_Creation',
  260:                      header => [{col1 => 'Configuration Setting',
  261:                                  col2 => 'Value',}],
  262:                   },
  263:         'directorysrch' => 
  264:                   { text => 'Institutional directory searches',
  265:                     help => 'Domain_Configuration_InstDirectory_Search',
  266:                     header => [{col1 => 'Setting',
  267:                                 col2 => 'Value',}],
  268:                   },
  269:         'contacts' =>
  270:                   { text => 'Contact Information',
  271:                     help => 'Domain_Configuration_Contact_Info',
  272:                     header => [{col1 => 'Setting',
  273:                                 col2 => 'Value',}],
  274:                   },
  275: 
  276:         'usercreation' => 
  277:                   { text => 'User creation',
  278:                     help => 'Domain_Configuration_User_Creation',
  279:                     header => [{col1 => 'Format rule type',
  280:                                 col2 => 'Format rules in force'},
  281:                                {col1 => 'User account creation',
  282:                                 col2 => 'Usernames which may be created',},
  283:                                {col1 => 'Context',
  284:                                 col2 => 'Assignable authentication types'}],
  285:                   },
  286:         'usermodification' =>
  287:                   { text => 'User modification',
  288:                     help => 'Domain_Configuration_User_Modification',
  289:                     header => [{col1 => 'Target user has role',
  290:                                 col2 => 'User information updateable in author context'},
  291:                                {col1 => 'Target user has role',
  292:                                 col2 => 'User information updateable in course context'},
  293:                                {col1 => "Status of user",
  294:                                 col2 => 'Information settable when self-creating account (if directory data blank)'}],
  295:                   },
  296:         'scantron' =>
  297:                   { text => 'Bubblesheet format file',
  298:                     help => 'Domain_Configuration_Scantron_Format',
  299:                     header => [ {col1 => 'Item',
  300:                                  col2 => '',
  301:                               }],
  302:                   },
  303:         'requestcourses' => 
  304:                  {text => 'Request creation of courses',
  305:                   help => 'Domain_Configuration_Request_Courses',
  306:                   header => [{col1 => 'User affiliation',
  307:                               col2 => 'Availability/Processing of requests',},
  308:                              {col1 => 'Setting',
  309:                               col2 => 'Value'}],
  310:                  },
  311:         'coursecategories' =>
  312:                   { text => 'Cataloging of courses/communities',
  313:                     help => 'Domain_Configuration_Cataloging_Courses',
  314:                     header => [{col1 => 'Category settings',
  315:                                 col2 => '',},
  316:                                {col1 => 'Categories',
  317:                                 col2 => '',
  318:                                }],
  319:                   },
  320:         'serverstatuses' =>
  321:                  {text   => 'Access to server status pages',
  322:                   help   => 'Domain_Configuration_Server_Status',
  323:                   header => [{col1 => 'Status Page',
  324:                               col2 => 'Other named users',
  325:                               col3 => 'Specific IPs',
  326:                             }],
  327:                  },
  328:         'coursedefaults' =>
  329:                  {text => 'Course/Community defaults',
  330:                   help => 'Domain_Configuration_Course_Defaults',
  331:                   header => [{col1 => 'Setting',
  332:                               col2 => 'Value',}],
  333:                  },
  334:     );
  335:     my %servers = &dom_servers($dom);
  336:     if (keys(%servers) > 1) {
  337:         $prefs{'login'}  = { text   => 'Log-in page options',
  338:                              help   => 'Domain_Configuration_Login_Page',
  339:                             header => [{col1 => 'Log-in Service',
  340:                                         col2 => 'Server Setting',},
  341:                                        {col1 => 'Log-in Page Items',
  342:                                         col2 => ''}],
  343:                            };
  344:     }
  345:     my @roles = ('student','coordinator','author','admin');
  346:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  347:     &Apache::lonhtmlcommon::add_breadcrumb
  348:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  349:       text=>"Pick functionality"});
  350:     my $confname = $dom.'-domainconfig';
  351:     if ($phase eq 'process') {
  352:         &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,\@roles);
  353:     } elsif ($phase eq 'display') {
  354:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname);
  355:     } else {
  356:         if (keys(%domconfig) == 0) {
  357:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  358:             my @ids=&Apache::lonnet::current_machine_ids();
  359:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  360:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  361:                 my @loginimages = ('img','logo','domlogo','login');
  362:                 my $custom_img_count = 0;
  363:                 foreach my $img (@loginimages) {
  364:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  365:                         $custom_img_count ++;
  366:                     }
  367:                 }
  368:                 foreach my $role (@roles) {
  369:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  370:                         $custom_img_count ++;
  371:                     }
  372:                 }
  373:                 if ($custom_img_count > 0) {
  374:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  375:                     my $switch_server = &check_switchserver($dom,$confname);
  376:                     $r->print(
  377:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  378:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  379:     &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 />'.
  380:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  381:                     if ($switch_server) {
  382:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  383:                     }
  384:                     $r->print(&Apache::loncommon::end_page());
  385:                     return OK;
  386:                 }
  387:             }
  388:         }
  389:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  390:     }
  391:     return OK;
  392: }
  393: 
  394: sub process_changes {
  395:     my ($r,$dom,$confname,$action,$roles,$values) = @_;
  396:     my %domconfig;
  397:     if (ref($values) eq 'HASH') {
  398:         %domconfig = %{$values};
  399:     }
  400:     my $output;
  401:     if ($action eq 'login') {
  402:         $output = &modify_login($r,$dom,$confname,%domconfig);
  403:     } elsif ($action eq 'rolecolors') {
  404:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  405:                                      %domconfig);
  406:     } elsif ($action eq 'quotas') {
  407:         $output = &modify_quotas($dom,$action,%domconfig);
  408:     } elsif ($action eq 'autoenroll') {
  409:         $output = &modify_autoenroll($dom,%domconfig);
  410:     } elsif ($action eq 'autoupdate') {
  411:         $output = &modify_autoupdate($dom,%domconfig);
  412:     } elsif ($action eq 'autocreate') {
  413:         $output = &modify_autocreate($dom,%domconfig);
  414:     } elsif ($action eq 'directorysrch') {
  415:         $output = &modify_directorysrch($dom,%domconfig);
  416:     } elsif ($action eq 'usercreation') {
  417:         $output = &modify_usercreation($dom,%domconfig);
  418:     } elsif ($action eq 'usermodification') {
  419:         $output = &modify_usermodification($dom,%domconfig);
  420:     } elsif ($action eq 'contacts') {
  421:         $output = &modify_contacts($dom,%domconfig);
  422:     } elsif ($action eq 'defaults') {
  423:         $output = &modify_defaults($dom,$r);
  424:     } elsif ($action eq 'scantron') {
  425:         $output = &modify_scantron($r,$dom,$confname,%domconfig);
  426:     } elsif ($action eq 'coursecategories') {
  427:         $output = &modify_coursecategories($dom,%domconfig);
  428:     } elsif ($action eq 'serverstatuses') {
  429:         $output = &modify_serverstatuses($dom,%domconfig);
  430:     } elsif ($action eq 'requestcourses') {
  431:         $output = &modify_quotas($dom,$action,%domconfig);
  432:     } elsif ($action eq 'coursedefaults') {
  433:         $output = &modify_coursedefaults($dom,%domconfig);
  434:     }
  435:     return $output;
  436: }
  437: 
  438: sub print_config_box {
  439:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  440:     my $rowtotal = 0;
  441:     my $output;
  442:     if ($action eq 'coursecategories') {
  443:         $output = &coursecategories_javascript($settings);
  444:     }
  445:     $output .= 
  446:          '<table class="LC_nested_outer">
  447:           <tr>
  448:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  449:            &mt($item->{text}).'&nbsp;'.
  450:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  451:           '</tr>';
  452:     $rowtotal ++;
  453:     my $numheaders = 1;
  454:     if (ref($item->{'header'}) eq 'ARRAY') {
  455:         $numheaders = scalar(@{$item->{'header'}});
  456:     }
  457:     if ($numheaders > 1) {
  458:         my $colspan = '';
  459:         if (($action eq 'rolecolors') || ($action eq 'coursecategories')) {
  460:             $colspan = ' colspan="2"';
  461:         }
  462:         $output .= '
  463:           <tr>
  464:            <td>
  465:             <table class="LC_nested">
  466:              <tr class="LC_info_row">
  467:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  468:               <td class="LC_right_item">'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  469:              </tr>';
  470:         $rowtotal ++;
  471:         if ($action eq 'autoupdate') {
  472:             $output .= &print_autoupdate('top',$dom,$settings,\$rowtotal);
  473:         } elsif ($action eq 'usercreation') {
  474:             $output .= &print_usercreation('top',$dom,$settings,\$rowtotal);
  475:         } elsif ($action eq 'usermodification') {
  476:             $output .= &print_usermodification('top',$dom,$settings,\$rowtotal);
  477:         } elsif ($action eq 'coursecategories') {
  478:             $output .= &print_coursecategories('top',$dom,$item,$settings,\$rowtotal);
  479:         } elsif ($action eq 'login') {
  480:             $output .= &print_login('top',$dom,$confname,$phase,$settings,\$rowtotal);
  481:             $colspan = ' colspan="2"';
  482:         } elsif ($action eq 'requestcourses') {
  483:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  484:         } else {
  485:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  486:         }
  487:         $output .= '
  488:            </table>
  489:           </td>
  490:          </tr>
  491:          <tr>
  492:            <td>
  493:             <table class="LC_nested">
  494:              <tr class="LC_info_row">
  495:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>';
  496:         $output .= '
  497:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  498:              </tr>';
  499:             $rowtotal ++;
  500:         if ($action eq 'autoupdate') {
  501:             $output .= &print_autoupdate('bottom',$dom,$settings,\$rowtotal);
  502:         } elsif ($action eq 'usercreation') {
  503:             $output .= &print_usercreation('middle',$dom,$settings,\$rowtotal).'
  504:            </table>
  505:           </td>
  506:          </tr>
  507:          <tr>
  508:            <td>
  509:             <table class="LC_nested">
  510:              <tr class="LC_info_row">
  511:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  512:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>             </tr>'.
  513:             &print_usercreation('bottom',$dom,$settings,\$rowtotal);
  514:             $rowtotal ++;
  515:         } elsif ($action eq 'usermodification') {
  516:             $output .= &print_usermodification('middle',$dom,$settings,\$rowtotal).'
  517:            </table>
  518:           </td>
  519:          </tr>
  520:          <tr>
  521:            <td>
  522:             <table class="LC_nested">
  523:              <tr class="LC_info_row">
  524:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  525:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  526: 
  527:                        &print_usermodification('bottom',$dom,$settings,\$rowtotal);
  528:             $rowtotal ++;
  529:         } elsif ($action eq 'coursecategories') {
  530:             $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  531:         } elsif ($action eq 'login') {
  532:             $output .= &print_login('bottom',$dom,$confname,$phase,$settings,\$rowtotal);
  533:         } elsif ($action eq 'requestcourses') {
  534:             $output .= &print_courserequestmail($dom,$settings,\$rowtotal);
  535:         } else {
  536:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  537:            </table>
  538:           </td>
  539:          </tr>
  540:          <tr>
  541:            <td>
  542:             <table class="LC_nested">
  543:              <tr class="LC_info_row">
  544:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  545:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  546:               <td class="LC_right_item" valign="top">'.
  547:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  548:              </tr>'.
  549:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  550:            </table>
  551:           </td>
  552:          </tr>
  553:          <tr>
  554:            <td>
  555:             <table class="LC_nested">
  556:              <tr class="LC_info_row">
  557:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  558:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  559:              </tr>'.
  560:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  561:             $rowtotal += 2;
  562:         }
  563:     } else {
  564:         $output .= '
  565:           <tr>
  566:            <td>
  567:             <table class="LC_nested">
  568:              <tr class="LC_info_row">';
  569:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  570:             $output .= '  
  571:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  572:         } elsif ($action eq 'serverstatuses') {
  573:             $output .= '
  574:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  575:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  576: 
  577:         } else {
  578:             $output .= '
  579:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  580:         }
  581:         if (defined($item->{'header'}->[0]->{'col3'})) {
  582:             $output .= '<td class="LC_left_item" valign="top">'.
  583:                        &mt($item->{'header'}->[0]->{'col2'});
  584:             if ($action eq 'serverstatuses') {
  585:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  586:             } 
  587:         } else {
  588:             $output .= '<td class="LC_right_item" valign="top">'.
  589:                        &mt($item->{'header'}->[0]->{'col2'});
  590:         }
  591:         $output .= '</td>';
  592:         if ($item->{'header'}->[0]->{'col3'}) {
  593:             $output .= '<td class="LC_right_item" valign="top">'.
  594:                        &mt($item->{'header'}->[0]->{'col3'});
  595:             if ($action eq 'serverstatuses') {
  596:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  597:             }
  598:             $output .= '</td>';
  599:         }
  600:         $output .= '</tr>';
  601:         $rowtotal ++;
  602:         if ($action eq 'login') {
  603:             $output .= &print_login('bottom',$dom,$confname,$phase,$settings,
  604:                                     \$rowtotal);
  605:         } elsif ($action eq 'quotas') {
  606:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  607:         } elsif ($action eq 'autoenroll') {
  608:             $output .= &print_autoenroll($dom,$settings,\$rowtotal);
  609:         } elsif ($action eq 'autocreate') {
  610:             $output .= &print_autocreate($dom,$settings,\$rowtotal);
  611:         } elsif ($action eq 'directorysrch') {
  612:             $output .= &print_directorysrch($dom,$settings,\$rowtotal);
  613:         } elsif ($action eq 'contacts') {
  614:             $output .= &print_contacts($dom,$settings,\$rowtotal);
  615:         } elsif ($action eq 'defaults') {
  616:             $output .= &print_defaults($dom,\$rowtotal);
  617:         } elsif ($action eq 'scantron') {
  618:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  619:         } elsif ($action eq 'serverstatuses') {
  620:             $output .= &print_serverstatuses($dom,$settings,\$rowtotal);
  621:         } elsif ($action eq 'coursedefaults') {
  622:             $output .= &print_coursedefaults($dom,$settings,\$rowtotal);
  623:         }
  624:     }
  625:     $output .= '
  626:    </table>
  627:   </td>
  628:  </tr>
  629: </table><br />';
  630:     return ($output,$rowtotal);
  631: }
  632: 
  633: sub print_login {
  634:     my ($position,$dom,$confname,$phase,$settings,$rowtotal) = @_;
  635:     my ($css_class,$datatable);
  636:     my %choices = &login_choices();
  637: 
  638:     if ($position eq 'top') {
  639:         my %servers = &dom_servers($dom);
  640:         my $choice = $choices{'disallowlogin'};
  641:         $css_class = ' class="LC_odd_row"';
  642:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
  643:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
  644:                       '<th>'.$choices{'server'}.'</th>'.
  645:                       '<th>'.$choices{'serverpath'}.'</th>'.
  646:                       '<th>'.$choices{'custompath'}.'</th>'.
  647:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
  648:         my %disallowed;
  649:         if (ref($settings) eq 'HASH') {
  650:             if (ref($settings->{'loginvia'}) eq 'HASH') {
  651:                %disallowed = %{$settings->{'loginvia'}};
  652:             }
  653:         }
  654:         foreach my $lonhost (sort(keys(%servers))) {
  655:             my $direct = 'selected="selected"';
  656:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  657:                 if ($disallowed{$lonhost}{'server'} ne '') {
  658:                     $direct = '';
  659:                 }
  660:             }
  661:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
  662:                           '<td><select name="'.$lonhost.'_server">'.
  663:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
  664:                           '</option>';
  665:             foreach my $hostid (keys(%servers)) {
  666:                 next if ($servers{$hostid} eq $servers{$lonhost});
  667:                 my $selected = '';
  668:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  669:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
  670:                         $selected = 'selected="selected"';
  671:                     }
  672:                 }
  673:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
  674:                               $servers{$hostid}.'</option>';
  675:             }
  676:             $datatable .= '</select></td>'.
  677:                           '<td><select name="'.$lonhost.'_serverpath">';
  678:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
  679:                 my $pathname = $path;
  680:                 if ($path eq 'custom') {
  681:                     $pathname = &mt('Custom Path').' ->';
  682:                 }
  683:                 my $selected = '';
  684:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
  685:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
  686:                         $selected = 'selected="selected"';
  687:                     }
  688:                 } elsif ($path eq '') {
  689:                     $selected = 'selected="selected"';
  690:                 }
  691:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
  692:             }
  693:             $datatable .= '</select></td>';
  694:             my ($custom,$exempt);
  695:             if (ref($disallowed{$lonhost}) eq 'HASH') {
  696:                 $custom = $disallowed{$lonhost}{'custompath'};
  697:                 $exempt = $disallowed{$lonhost}{'exempt'};
  698:             }
  699:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
  700:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
  701:                           '</tr>';
  702:         }
  703:         $datatable .= '</table></td></tr>';
  704:         return $datatable;
  705:     }
  706: 
  707:     my %defaultchecked = ( 
  708:                            'coursecatalog' => 'on',
  709:                            'adminmail'     => 'off',
  710:                            'newuser'       => 'off',
  711:                          );
  712:     my @toggles = ('coursecatalog','adminmail','newuser');
  713:     my (%checkedon,%checkedoff);
  714:     foreach my $item (@toggles) {
  715:         if ($defaultchecked{$item} eq 'on') { 
  716:             $checkedon{$item} = ' checked="checked" ';
  717:             $checkedoff{$item} = ' ';
  718:         } elsif ($defaultchecked{$item} eq 'off') {
  719:             $checkedoff{$item} = ' checked="checked" ';
  720:             $checkedon{$item} = ' ';
  721:         }
  722:     }
  723:     my $loginheader = 'image';
  724:     my @images = ('img','logo','domlogo','login');
  725:     my @logintext = ('textcol','bgcol');
  726:     my @bgs = ('pgbg','mainbg','sidebg');
  727:     my @links = ('link','alink','vlink');
  728:     my %designhash = &Apache::loncommon::get_domainconf($dom);
  729:     my %defaultdesign = %Apache::loncommon::defaultdesign;
  730:     my (%is_custom,%designs);
  731:     my %defaults = (
  732:                    font => $defaultdesign{'login.font'},
  733:                    );
  734:     foreach my $item (@images) {
  735:         $defaults{$item} = $defaultdesign{'login.'.$item};
  736:         $defaults{'showlogo'}{$item} = 1;
  737:     }
  738:     foreach my $item (@bgs) {
  739:         $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  740:     }
  741:     foreach my $item (@logintext) {
  742:         $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  743:     }
  744:     foreach my $item (@links) {
  745:         $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  746:     }
  747:     if (ref($settings) eq 'HASH') {
  748:         foreach my $item (@toggles) {
  749:             if ($settings->{$item} eq '1') {
  750:                 $checkedon{$item} =  ' checked="checked" ';
  751:                 $checkedoff{$item} = ' ';
  752:             } elsif ($settings->{$item} eq '0') {
  753:                 $checkedoff{$item} =  ' checked="checked" ';
  754:                 $checkedon{$item} = ' ';
  755:             }
  756:         }
  757:         foreach my $item (@images) {
  758:             if (defined($settings->{$item})) {
  759:                 $designs{$item} = $settings->{$item};
  760:                 $is_custom{$item} = 1;
  761:             }
  762:             if (defined($settings->{'showlogo'}{$item})) {
  763:                 $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
  764:             }
  765:         }
  766:         foreach my $item (@logintext) {
  767:             if ($settings->{$item} ne '') {
  768:                 $designs{'logintext'}{$item} = $settings->{$item};
  769:                 $is_custom{$item} = 1;
  770:             }
  771:         }
  772:         if ($settings->{'loginheader'} ne '') {
  773:             $loginheader = $settings->{'loginheader'};
  774:         }
  775:         if ($settings->{'font'} ne '') {
  776:             $designs{'font'} = $settings->{'font'};
  777:             $is_custom{'font'} = 1;
  778:         }
  779:         foreach my $item (@bgs) {
  780:             if ($settings->{$item} ne '') {
  781:                 $designs{'bgs'}{$item} = $settings->{$item};
  782:                 $is_custom{$item} = 1;
  783:             }
  784:         }
  785:         foreach my $item (@links) {
  786:             if ($settings->{$item} ne '') {
  787:                 $designs{'links'}{$item} = $settings->{$item};
  788:                 $is_custom{$item} = 1;
  789:             }
  790:         }
  791:     } else {
  792:         if ($designhash{$dom.'.login.font'} ne '') {
  793:             $designs{'font'} = $designhash{$dom.'.login.font'};
  794:             $is_custom{'font'} = 1;
  795:         }
  796:         foreach my $item (@images) {
  797:             if ($designhash{$dom.'.login.'.$item} ne '') {
  798:                 $designs{$item} = $designhash{$dom.'.login.'.$item};
  799:                 $is_custom{$item} = 1;
  800:             }
  801:         }
  802:         foreach my $item (@bgs) {
  803:             if ($designhash{$dom.'.login.'.$item} ne '') {
  804:                 $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
  805:                 $is_custom{$item} = 1;
  806:             }
  807:         }
  808:         foreach my $item (@links) {
  809:             if ($designhash{$dom.'.login.'.$item} ne '') {
  810:                 $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
  811:                 $is_custom{$item} = 1;
  812:             }
  813:         }
  814:     }
  815:     my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
  816:                                                   logo => 'Institution Logo',
  817:                                                   domlogo => 'Domain Logo',
  818:                                                   login => 'Login box');
  819:     my $itemcount = 1;
  820:     my ($css_class,$datatable);
  821:     foreach my $item (@toggles) {
  822:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
  823:         $datatable .=  
  824:             '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
  825:             '</td><td>'.
  826:             '<span class="LC_nobreak"><label><input type="radio" name="'.
  827:             $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
  828:             '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
  829:             $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
  830:             '</tr>';
  831:         $itemcount ++;
  832:     }
  833:     $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext,$loginheader);
  834:     $datatable .= '</tr></table></td></tr>';
  835:     return $datatable;
  836: }
  837: 
  838: sub login_choices {
  839:     my %choices =
  840:         &Apache::lonlocal::texthash (
  841:             coursecatalog => 'Display Course/Community Catalog link?',
  842:             adminmail => "Display Administrator's E-mail Address?",
  843:             disallowlogin => "Login page requests redirected",
  844:             hostid        => "Server",
  845:             server        => "Redirect to:",
  846:             serverpath    => "Path",
  847:             custompath    => "Custom",
  848:             exempt        => "Exempt IP(s)",
  849:             directlogin   => "No redirect",
  850:             newuser   => "Link to create a user account",
  851:             img => "Header",
  852:             logo => "Main Logo",
  853:             domlogo => "Domain Logo",
  854:             login => "Log-in Header", 
  855:             textcol => "Text color",
  856:             bgcol   => "Box color",
  857:             bgs => "Background colors",
  858:             links => "Link colors",
  859:             font => "Font color",
  860:             pgbg => "Header",
  861:             mainbg => "Page",
  862:             sidebg => "Login box",
  863:             link => "Link",
  864:             alink => "Active link",
  865:             vlink => "Visited link",
  866:         );
  867:     return %choices;
  868: }
  869: 
  870: sub print_rolecolors {
  871:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
  872:     my %choices = &color_font_choices();
  873:     my @bgs = ('pgbg','tabbg','sidebg');
  874:     my @links = ('link','alink','vlink');
  875:     my @images = ('img');
  876:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
  877:     my %designhash = &Apache::loncommon::get_domainconf($dom);
  878:     my %defaultdesign = %Apache::loncommon::defaultdesign;
  879:     my (%is_custom,%designs);
  880:     my %defaults = (
  881:                    img => $defaultdesign{$role.'.img'},
  882:                    font => $defaultdesign{$role.'.font'},
  883:                    );
  884:     foreach my $item (@bgs) {
  885:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
  886:     }
  887:     foreach my $item (@links) {
  888:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
  889:     }
  890:     if (ref($settings) eq 'HASH') {
  891:         if (ref($settings->{$role}) eq 'HASH') {
  892:             if ($settings->{$role}->{'img'} ne '') {
  893:                 $designs{'img'} = $settings->{$role}->{'img'};
  894:                 $is_custom{'img'} = 1;
  895:             }
  896:             if ($settings->{$role}->{'font'} ne '') {
  897:                 $designs{'font'} = $settings->{$role}->{'font'};
  898:                 $is_custom{'font'} = 1;
  899:             }
  900:             foreach my $item (@bgs) {
  901:                 if ($settings->{$role}->{$item} ne '') {
  902:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
  903:                     $is_custom{$item} = 1;
  904:                 }
  905:             }
  906:             foreach my $item (@links) {
  907:                 if ($settings->{$role}->{$item} ne '') {
  908:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
  909:                     $is_custom{$item} = 1;
  910:                 }
  911:             }
  912:         }
  913:     } else {
  914:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  915:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
  916:             $is_custom{'img'} = 1;
  917:         }
  918:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
  919:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
  920:             $is_custom{'font'} = 1;
  921:         }
  922:         foreach my $item (@bgs) {
  923:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
  924:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
  925:                 $is_custom{$item} = 1;
  926:             
  927:             }
  928:         }
  929:         foreach my $item (@links) {
  930:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
  931:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
  932:                 $is_custom{$item} = 1;
  933:             }
  934:         }
  935:     }
  936:     my $itemcount = 1;
  937:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
  938:     $datatable .= '</tr></table></td></tr>';
  939:     return $datatable;
  940: }
  941: 
  942: sub display_color_options {
  943:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
  944:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext,$loginheader) = @_;
  945:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
  946:     my $datatable = '<tr'.$css_class.'>'.
  947:         '<td>'.$choices->{'font'}.'</td>';
  948:     if (!$is_custom->{'font'}) {
  949:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
  950:     } else {
  951:         $datatable .= '<td>&nbsp;</td>';
  952:     }
  953:     my $fontlink = &color_pick($phase,$role,'font',$choices->{'font'},$designs->{'font'});
  954:     $datatable .= '<td><span class="LC_nobreak">'.
  955:                   '<input type="text" size="10" name="'.$role.'_font"'.
  956:                   ' value="'.$designs->{'font'}.'" />&nbsp;'.$fontlink.
  957:                   '&nbsp;<span id="css_'.$role.'_font" style="background-color: '.
  958:                   $designs->{'font'}.';">&nbsp;&nbsp;&nbsp;</span>'.
  959:                   '</span></td></tr>';
  960:     my $switchserver = &check_switchserver($dom,$confname);
  961:     foreach my $img (@{$images}) {
  962: 	$itemcount ++;
  963:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
  964:         $datatable .= '<tr'.$css_class.'>'.
  965:                       '<td>'.$choices->{$img};
  966:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
  967:         if ($role eq 'login') {
  968:             if ($img eq 'login') {
  969:                 $login_hdr_pick =
  970:                     &login_header_options($img,$role,$defaults,$is_custom,$choices,
  971:                                           $loginheader);
  972:                 $logincolors =
  973:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
  974:                                             $designs);
  975:             } elsif ($img ne 'domlogo') {
  976:                 $datatable.= &logo_display_options($img,$defaults,$designs);
  977:             }
  978:         }
  979:         $datatable .= '</td>';
  980:         if ($designs->{$img} ne '') {
  981:             $imgfile = $designs->{$img};
  982: 	    $img_import = ($imgfile =~ m{^/adm/});
  983:         } else {
  984:             $imgfile = $defaults->{$img};
  985:         }
  986:         if ($imgfile) {
  987:             my ($showfile,$fullsize);
  988:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
  989:                 my $urldir = $1;
  990:                 my $filename = $2;
  991:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
  992:                 if (@info) {
  993:                     my $thumbfile = 'tn-'.$filename;
  994:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
  995:                     if (@thumb) {
  996:                         $showfile = $urldir.'/'.$thumbfile;
  997:                     } else {
  998:                         $showfile = $imgfile;
  999:                     }
 1000:                 } else {
 1001:                     $showfile = '';
 1002:                 }
 1003:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1004:                 $showfile = $imgfile;
 1005:                 my $imgdir = $1;
 1006:                 my $filename = $2;
 1007:                 if (-e "/home/httpd/html/$imgdir/tn-".$filename) {
 1008:                     $showfile = "/$imgdir/tn-".$filename;
 1009:                 } else {
 1010:                     my $input = "/home/httpd/html".$imgfile;
 1011:                     my $output = '/home/httpd/html/'.$imgdir.'/tn-'.$filename;
 1012:                     if (!-e $output) {
 1013:                         my ($width,$height) = &thumb_dimensions();
 1014:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1015:                         if ($fullwidth ne '' && $fullheight ne '') {
 1016:                             if ($fullwidth > $width && $fullheight > $height) { 
 1017:                                 my $size = $width.'x'.$height;
 1018:                                 system("convert -sample $size $input $output");
 1019:                                 $showfile = '/'.$imgdir.'/tn-'.$filename;
 1020:                             }
 1021:                         }
 1022:                     }
 1023:                 }
 1024:             }
 1025:             if ($showfile) {
 1026:                 if ($showfile =~ m{^/(adm|res)/}) {
 1027:                     if ($showfile =~ m{^/res/}) {
 1028:                         my $local_showfile =
 1029:                             &Apache::lonnet::filelocation('',$showfile);
 1030:                         &Apache::lonnet::repcopy($local_showfile);
 1031:                     }
 1032:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1033:                 }
 1034:                 if ($imgfile) {
 1035:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1036:                         if ($imgfile =~ m{^/res/}) {
 1037:                             my $local_imgfile =
 1038:                                 &Apache::lonnet::filelocation('',$imgfile);
 1039:                             &Apache::lonnet::repcopy($local_imgfile);
 1040:                         }
 1041:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1042:                     } else {
 1043:                         $fullsize = $imgfile;
 1044:                     }
 1045:                 }
 1046:                 $datatable .= '<td>';
 1047:                 if ($img eq 'login') {
 1048:                     $datatable .= $login_hdr_pick;    
 1049:                 }
 1050:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1051:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1052:             } else {
 1053:                 $datatable .= '<td colspan="2" class="LC_right_item"><br />'.
 1054:                               &mt('Upload:');
 1055:             }
 1056:         } else {
 1057:             $datatable .= '<td colspan="2" class="LC_right_item"><br />'.
 1058:                           &mt('Upload:');
 1059:         }
 1060:         if ($switchserver) {
 1061:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1062:         } else {
 1063:             $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1064:         }
 1065:         $datatable .= '</td></tr>';
 1066:     }
 1067:     $itemcount ++;
 1068:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1069:     $datatable .= '<tr'.$css_class.'>'.
 1070:                   '<td>'.$choices->{'bgs'}.'</td>';
 1071:     my $bgs_def;
 1072:     foreach my $item (@{$bgs}) {
 1073:         if (!$is_custom->{$item}) {
 1074:             $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>';
 1075:         }
 1076:     }
 1077:     if ($bgs_def) {
 1078:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1079:     } else {
 1080:         $datatable .= '<td>&nbsp;</td>';
 1081:     }
 1082:     $datatable .= '<td class="LC_right_item">'.
 1083:                   '<table border="0"><tr>';
 1084:     foreach my $item (@{$bgs}) {
 1085:         my $link = &color_pick($phase,$role,$item,$choices->{$item},$designs->{'bgs'}{$item});
 1086:         $datatable .= '<td align="center">'.$link;
 1087:         if ($designs->{'bgs'}{$item}) {
 1088:             $datatable .= '&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'bgs'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1089:         }
 1090:         $datatable .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.$designs->{'bgs'}{$item}.
 1091:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1092:     }
 1093:     $datatable .= '</tr></table></td></tr>';
 1094:     $itemcount ++;
 1095:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1096:     $datatable .= '<tr'.$css_class.'>'.
 1097:                   '<td>'.$choices->{'links'}.'</td>';
 1098:     my $links_def;
 1099:     foreach my $item (@{$links}) {
 1100:         if (!$is_custom->{$item}) {
 1101:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1102:         }
 1103:     }
 1104:     if ($links_def) {
 1105:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1106:     } else {
 1107:         $datatable .= '<td>&nbsp;</td>';
 1108:     }
 1109:     $datatable .= '<td class="LC_right_item">'.
 1110:                   '<table border="0"><tr>';
 1111:     foreach my $item (@{$links}) {
 1112:         $datatable .= '<td align="center">'."\n".
 1113:                       &color_pick($phase,$role,$item,$choices->{$item},
 1114:                                   $designs->{'links'}{$item});
 1115:         if ($designs->{'links'}{$item}) {
 1116:             $datatable.='&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'links'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1117:         }
 1118:         $datatable .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.$designs->{'links'}{$item}.
 1119:                       '" /></td>';
 1120:     }
 1121:     $$rowtotal += $itemcount;
 1122:     return $datatable;
 1123: }
 1124: 
 1125: sub logo_display_options {
 1126:     my ($img,$defaults,$designs) = @_;
 1127:     my $checkedon;
 1128:     if (ref($defaults) eq 'HASH') {
 1129:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1130:             if ($defaults->{'showlogo'}{$img}) {
 1131:                 $checkedon = 'checked="checked" ';     
 1132:             }
 1133:         } 
 1134:     }
 1135:     if (ref($designs) eq 'HASH') {
 1136:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1137:             if (defined($designs->{'showlogo'}{$img})) {
 1138:                 if ($designs->{'showlogo'}{$img} == 0) {
 1139:                     $checkedon = '';
 1140:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1141:                     $checkedon = 'checked="checked" ';
 1142:                 }
 1143:             }
 1144:         }
 1145:     }
 1146:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1147:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1148:            &mt('show').'</label>'."\n";
 1149: }
 1150: 
 1151: sub login_header_options  {
 1152:     my ($img,$role,$defaults,$is_custom,$choices,$loginheader) = @_;
 1153:     my $image_checked = ' checked="checked" ';
 1154:     my $text_checked = ' ';
 1155:     if ($loginheader eq 'text') {
 1156:         $image_checked = ' ';
 1157:         $text_checked = ' checked="checked" ';
 1158:     }
 1159:     my $output = '<span class="LC_nobreak"><label><input type="radio" name="'.
 1160:               'loginheader" value="image" '.$image_checked.'/>'.
 1161:               &mt('use image').'</label>&nbsp;&nbsp;&nbsp;'.
 1162:               '<label><input type="radio" name="loginheader" value="text"'.
 1163:               $text_checked.'/>'.&mt('use text').'</label><br />'."\n";
 1164:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1165:         $output .= &mt('Text default(s)').':<br />';
 1166:         if (!$is_custom->{'textcol'}) {
 1167:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1168:                        '&nbsp;&nbsp;&nbsp;';
 1169:         }
 1170:         if (!$is_custom->{'bgcol'}) {
 1171:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1172:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1173:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1174:         }
 1175:         $output .= '<br />';
 1176:     }
 1177:     $output .='<br />';
 1178:     return $output;
 1179: }
 1180: 
 1181: sub login_text_colors {
 1182:     my ($img,$role,$logintext,$phase,$choices,$designs) = @_;
 1183:     my $color_menu = '<table border="0"><tr>';
 1184:     foreach my $item (@{$logintext}) {
 1185:         my $link = &color_pick($phase,$role,$item,$choices->{$item},$designs->{'logintext'}{$item});
 1186:         $color_menu .= '<td align="center">'.$link;
 1187:         if ($designs->{'logintext'}{$item}) {
 1188:             $color_menu .= '&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'logintext'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1189:         }
 1190:         $color_menu .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.
 1191:                        $designs->{'logintext'}{$item}.'" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>'.
 1192:                        '<td>&nbsp;</td>';
 1193:     }
 1194:     $color_menu .= '</tr></table><br />';
 1195:     return $color_menu;
 1196: }
 1197: 
 1198: sub image_changes {
 1199:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1200:     my $output;
 1201:     if (!$is_custom) {
 1202:         if ($img ne 'domlogo') {
 1203:             $output .= &mt('Default image:').'<br />';
 1204:         } else {
 1205:             $output .= &mt('Default in use:').'<br />';
 1206:         }
 1207:     }
 1208:     if ($img_import) {
 1209:         $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1210:     }
 1211:     $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1212:                $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1213:     if ($is_custom) {
 1214:         $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1215:                    '<input type="checkbox" name="'.
 1216:                    $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1217:                    '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1218:     } else {
 1219:         $output .= '<td valign="bottom">'.$logincolors.&mt('Upload:').'<br />';
 1220:     }
 1221:     return $output;
 1222: }
 1223: 
 1224: sub color_pick {
 1225:     my ($phase,$role,$item,$desc,$curcol) = @_;
 1226:     my $link = '<a href="javascript:pjump('."'color_custom','".$desc.
 1227:                "','".$curcol."','".$role.'_'.$item."','parmform.pres','psub'".
 1228:                ');">'.$desc.'</a>';
 1229:     return $link;
 1230: }
 1231: 
 1232: sub print_quotas {
 1233:     my ($dom,$settings,$rowtotal,$action) = @_;
 1234:     my $context;
 1235:     if ($action eq 'quotas') {
 1236:         $context = 'tools';
 1237:     } else {
 1238:         $context = $action;
 1239:     }
 1240:     my ($datatable,$defaultquota,@usertools,@options,%validations);
 1241:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1242:     my $typecount = 0;
 1243:     my ($css_class,%titles);
 1244:     if ($context eq 'requestcourses') {
 1245:         @usertools = ('official','unofficial','community');
 1246:         @options =('norequest','approval','validate','autolimit');
 1247:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1248:         %titles = &courserequest_titles();
 1249:     } else {
 1250:         @usertools = ('aboutme','blog','portfolio');
 1251:         %titles = &tool_titles();
 1252:     }
 1253:     if (ref($types) eq 'ARRAY') {
 1254:         foreach my $type (@{$types}) {
 1255:             my $currdefquota;
 1256:             unless ($context eq 'requestcourses') {
 1257:                 if (ref($settings) eq 'HASH') {
 1258:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1259:                         $currdefquota = $settings->{defaultquota}->{$type}; 
 1260:                     } else {
 1261:                         $currdefquota = $settings->{$type};
 1262:                     }
 1263:                 }
 1264:             }
 1265:             if (defined($usertypes->{$type})) {
 1266:                 $typecount ++;
 1267:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1268:                 $datatable .= '<tr'.$css_class.'>'.
 1269:                               '<td>'.$usertypes->{$type}.'</td>'.
 1270:                               '<td class="LC_left_item">';
 1271:                 if ($context eq 'requestcourses') {
 1272:                     $datatable .= '<table><tr>';
 1273:                 }
 1274:                 my %cell;  
 1275:                 foreach my $item (@usertools) {
 1276:                     if ($context eq 'requestcourses') {
 1277:                         my ($curroption,$currlimit);
 1278:                         if (ref($settings) eq 'HASH') {
 1279:                             if (ref($settings->{$item}) eq 'HASH') {
 1280:                                 $curroption = $settings->{$item}->{$type};
 1281:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1282:                                     $currlimit = $1; 
 1283:                                 }
 1284:                             }
 1285:                         }
 1286:                         if (!$curroption) {
 1287:                             $curroption = 'norequest';
 1288:                         }
 1289:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1290:                         foreach my $option (@options) {
 1291:                             my $val = $option;
 1292:                             if ($option eq 'norequest') {
 1293:                                 $val = 0;  
 1294:                             }
 1295:                             if ($option eq 'validate') {
 1296:                                 my $canvalidate = 0;
 1297:                                 if (ref($validations{$item}) eq 'HASH') { 
 1298:                                     if ($validations{$item}{$type}) {
 1299:                                         $canvalidate = 1;
 1300:                                     }
 1301:                                 }
 1302:                                 next if (!$canvalidate);
 1303:                             }
 1304:                             my $checked = '';
 1305:                             if ($option eq $curroption) {
 1306:                                 $checked = ' checked="checked"';
 1307:                             } elsif ($option eq 'autolimit') {
 1308:                                 if ($curroption =~ /^autolimit/) {
 1309:                                     $checked = ' checked="checked"';
 1310:                                 }                       
 1311:                             } 
 1312:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1313:                                   '<input type="radio" name="crsreq_'.$item.
 1314:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1315:                                   $titles{$option}.'</label>';
 1316:                             if ($option eq 'autolimit') {
 1317:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1318:                                                 $item.'_limit_'.$type.'" size="1" '.
 1319:                                                 'value="'.$currlimit.'" />';
 1320:                             }
 1321:                             $cell{$item} .= '</span> ';
 1322:                             if ($option eq 'autolimit') {
 1323:                                 $cell{$item} .= $titles{'unlimited'};
 1324:                             }
 1325:                         }
 1326:                     } else {
 1327:                         my $checked = 'checked="checked" ';
 1328:                         if (ref($settings) eq 'HASH') {
 1329:                             if (ref($settings->{$item}) eq 'HASH') {
 1330:                                 if ($settings->{$item}->{$type} == 0) {
 1331:                                     $checked = '';
 1332:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1333:                                     $checked =  'checked="checked" ';
 1334:                                 }
 1335:                             }
 1336:                         }
 1337:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1338:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1339:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1340:                                       '</label></span>&nbsp; ';
 1341:                     }
 1342:                 }
 1343:                 if ($context eq 'requestcourses') {
 1344:                     $datatable .= '</tr><tr>';
 1345:                     foreach my $item (@usertools) {
 1346:                         $datatable .= '<td style="vertical-align: top;">'.$cell{$item}.'</td>';  
 1347:                     }
 1348:                     $datatable .= '</tr></table>';
 1349:                 }
 1350:                 $datatable .= '</td>';
 1351:                 unless ($context eq 'requestcourses') {
 1352:                     $datatable .= 
 1353:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 1354:                               '<input type="text" name="quota_'.$type.
 1355:                               '" value="'.$currdefquota.
 1356:                               '" size="5" /> Mb</span></td>';
 1357:                 }
 1358:                 $datatable .= '</tr>';
 1359:             }
 1360:         }
 1361:     }
 1362:     unless ($context eq 'requestcourses') {
 1363:         $defaultquota = '20';
 1364:         if (ref($settings) eq 'HASH') {
 1365:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 1366:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 1367:             } elsif (defined($settings->{'default'})) {
 1368:                 $defaultquota = $settings->{'default'};
 1369:             }
 1370:         }
 1371:     }
 1372:     $typecount ++;
 1373:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1374:     $datatable .= '<tr'.$css_class.'>'.
 1375:                   '<td>'.$othertitle.'</td>'.
 1376:                   '<td class="LC_left_item">';
 1377:     if ($context eq 'requestcourses') {
 1378:         $datatable .= '<table><tr>';
 1379:     }
 1380:     my %defcell;
 1381:     foreach my $item (@usertools) {
 1382:         if ($context eq 'requestcourses') {
 1383:             my ($curroption,$currlimit);
 1384:             if (ref($settings) eq 'HASH') {
 1385:                 if (ref($settings->{$item}) eq 'HASH') {
 1386:                     $curroption = $settings->{$item}->{'default'};
 1387:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1388:                         $currlimit = $1;
 1389:                     }
 1390:                 }
 1391:             }
 1392:             if (!$curroption) {
 1393:                 $curroption = 'norequest';
 1394:             }
 1395:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1396:             foreach my $option (@options) {
 1397:                 my $val = $option;
 1398:                 if ($option eq 'norequest') {
 1399:                     $val = 0;
 1400:                 }
 1401:                 if ($option eq 'validate') {
 1402:                     my $canvalidate = 0;
 1403:                     if (ref($validations{$item}) eq 'HASH') {
 1404:                         if ($validations{$item}{'default'}) {
 1405:                             $canvalidate = 1;
 1406:                         }
 1407:                     }
 1408:                     next if (!$canvalidate);
 1409:                 }
 1410:                 my $checked = '';
 1411:                 if ($option eq $curroption) {
 1412:                     $checked = ' checked="checked"';
 1413:                 } elsif ($option eq 'autolimit') {
 1414:                     if ($curroption =~ /^autolimit/) {
 1415:                         $checked = ' checked="checked"';
 1416:                     }
 1417:                 }
 1418:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 1419:                                   '<input type="radio" name="crsreq_'.$item.
 1420:                                   '_default" value="'.$val.'"'.$checked.' />'.
 1421:                                   $titles{$option}.'</label>';
 1422:                 if ($option eq 'autolimit') {
 1423:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1424:                                        $item.'_limit_default" size="1" '.
 1425:                                        'value="'.$currlimit.'" />';
 1426:                 }
 1427:                 $defcell{$item} .= '</span> ';
 1428:                 if ($option eq 'autolimit') {
 1429:                     $defcell{$item} .= $titles{'unlimited'};
 1430:                 }
 1431:             }
 1432:         } else {
 1433:             my $checked = 'checked="checked" ';
 1434:             if (ref($settings) eq 'HASH') {
 1435:                 if (ref($settings->{$item}) eq 'HASH') {
 1436:                     if ($settings->{$item}->{'default'} == 0) {
 1437:                         $checked = '';
 1438:                     } elsif ($settings->{$item}->{'default'} == 1) {
 1439:                         $checked = 'checked="checked" ';
 1440:                     }
 1441:                 }
 1442:             }
 1443:             $datatable .= '<span class="LC_nobreak"><label>'.
 1444:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1445:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 1446:                           '</label></span>&nbsp; ';
 1447:         }
 1448:     }
 1449:     if ($context eq 'requestcourses') {
 1450:         $datatable .= '</tr><tr>';
 1451:         foreach my $item (@usertools) {
 1452:             $datatable .= '<td style="vertical-align: top;">'.$defcell{$item}.'</td>';
 1453:         }
 1454:         $datatable .= '</tr></table>';
 1455:     }
 1456:     $datatable .= '</td>';
 1457:     unless ($context eq 'requestcourses') {
 1458:         $datatable .= '<td class="LC_right_item"><span class="LC_nobreak">'.
 1459:                       '<input type="text" name="defaultquota" value="'.
 1460:                       $defaultquota.'" size="5" /> Mb</span></td>';
 1461:     }
 1462:     $datatable .= '</tr>';
 1463:     $typecount ++;
 1464:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1465:     $datatable .= '<tr'.$css_class.'>'.
 1466:                   '<td>'.&mt('LON-CAPA Advanced Users').' ';
 1467:     if ($context eq 'requestcourses') {
 1468:         $datatable .= &mt('(overrides affiliation, if set)').
 1469:                       '</td>'.
 1470:                       '<td class="LC_left_item">'.
 1471:                       '<table><tr>';
 1472:     } else {
 1473:         $datatable .= &mt('(overrides affiliation, if checked)').
 1474:                       '</td>'.
 1475:                       '<td class="LC_left_item" colspan="2">'.
 1476:                       '<br />';
 1477:     }
 1478:     my %advcell;
 1479:     foreach my $item (@usertools) {
 1480:         if ($context eq 'requestcourses') {
 1481:             my ($curroption,$currlimit);
 1482:             if (ref($settings) eq 'HASH') {
 1483:                 if (ref($settings->{$item}) eq 'HASH') {
 1484:                     $curroption = $settings->{$item}->{'_LC_adv'};
 1485:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 1486:                         $currlimit = $1;
 1487:                     }
 1488:                 }
 1489:             }
 1490:             $datatable .= '<th>'.$titles{$item}.'</th>';
 1491:             my $checked = '';
 1492:             if ($curroption eq '') {
 1493:                 $checked = ' checked="checked"';
 1494:             }
 1495:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1496:                                '<input type="radio" name="crsreq_'.$item.
 1497:                                '__LC_adv" value=""'.$checked.' />'.
 1498:                                &mt('No override set').'</label></span>&nbsp; ';
 1499:             foreach my $option (@options) {
 1500:                 my $val = $option;
 1501:                 if ($option eq 'norequest') {
 1502:                     $val = 0;
 1503:                 }
 1504:                 if ($option eq 'validate') {
 1505:                     my $canvalidate = 0;
 1506:                     if (ref($validations{$item}) eq 'HASH') {
 1507:                         if ($validations{$item}{'_LC_adv'}) {
 1508:                             $canvalidate = 1;
 1509:                         }
 1510:                     }
 1511:                     next if (!$canvalidate);
 1512:                 }
 1513:                 my $checked = '';
 1514:                 if ($val eq $curroption) {
 1515:                     $checked = ' checked="checked"';
 1516:                 } elsif ($option eq 'autolimit') {
 1517:                     if ($curroption =~ /^autolimit/) {
 1518:                         $checked = ' checked="checked"';
 1519:                     }
 1520:                 }
 1521:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 1522:                                   '<input type="radio" name="crsreq_'.$item.
 1523:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 1524:                                   $titles{$option}.'</label>';
 1525:                 if ($option eq 'autolimit') {
 1526:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1527:                                        $item.'_limit__LC_adv" size="1" '.
 1528:                                        'value="'.$currlimit.'" />';
 1529:                 }
 1530:                 $advcell{$item} .= '</span> ';
 1531:                 if ($option eq 'autolimit') {
 1532:                     $advcell{$item} .= $titles{'unlimited'};
 1533:                 }
 1534:             }
 1535:         } else {
 1536:             my $checked = 'checked="checked" ';
 1537:             if (ref($settings) eq 'HASH') {
 1538:                 if (ref($settings->{$item}) eq 'HASH') {
 1539:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 1540:                         $checked = '';
 1541:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 1542:                         $checked = 'checked="checked" ';
 1543:                     }
 1544:                 }
 1545:             }
 1546:             $datatable .= '<span class="LC_nobreak"><label>'.
 1547:                           '<input type="checkbox" name="'.$context.'_'.$item.
 1548:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 1549:                           '</label></span>&nbsp; ';
 1550:         }
 1551:     }
 1552:     if ($context eq 'requestcourses') {
 1553:         $datatable .= '</tr><tr>';
 1554:         foreach my $item (@usertools) {
 1555:             $datatable .= '<td style="vertical-align: top;">'.$advcell{$item}.'</td>';
 1556:         }
 1557:         $datatable .= '</tr></table>';
 1558:     }
 1559:     $datatable .= '</td></tr>';
 1560:     $$rowtotal += $typecount;
 1561:     return $datatable;
 1562: }
 1563: 
 1564: sub print_courserequestmail {
 1565:     my ($dom,$settings,$rowtotal) = @_;
 1566:     my ($now,$datatable,%dompersonnel,@domcoord,@currapproval,$rows);
 1567:     $now = time;
 1568:     $rows = 0;
 1569:     %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc'],$now,$now);
 1570:     foreach my $server (keys(%dompersonnel)) {
 1571:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 1572:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 1573:             if (!grep(/^$uname:$udom$/,@domcoord)) {
 1574:                 push(@domcoord,$uname.':'.$udom);
 1575:             }
 1576:         }
 1577:     }
 1578:     if (ref($settings) eq 'HASH') {
 1579:         if (ref($settings->{'notify'}) eq 'HASH') {
 1580:             if ($settings->{'notify'}{'approval'} ne '') {
 1581:                @currapproval = split(',',$settings->{'notify'}{'approval'});
 1582:             }
 1583:         }
 1584:     }
 1585:     if (@currapproval) {
 1586:         foreach my $dc (@currapproval) {
 1587:             unless (grep(/^\Q$dc\E$/,@domcoord)) {
 1588:                 push(@domcoord,$dc);
 1589:             }
 1590:         }
 1591:     }
 1592:     @domcoord = sort(@domcoord);
 1593:     my $numinrow = 4;
 1594:     my $numdc = @domcoord;
 1595:     my $css_class = 'class="LC_odd_row"';
 1596:     $datatable = '<tr'.$css_class.'>'.
 1597:                  ' <td>'.&mt('Receive notification of course requests requiring approval.').
 1598:                  ' </td>'.
 1599:                  ' <td class="LC_left_item">';
 1600:     if (@domcoord > 0) {
 1601:         $datatable .= '<table>';
 1602:         for (my $i=0; $i<$numdc; $i++) {
 1603:             my $rem = $i%($numinrow);
 1604:             if ($rem == 0) {
 1605:                 if ($i > 0) {
 1606:                     $datatable .= '</tr>';
 1607:                 }
 1608:                 $datatable .= '<tr>';
 1609:                 $rows ++;
 1610:             }
 1611:             my $check = ' ';
 1612:             if (grep(/^\Q$domcoord[$i]\E$/,@currapproval)) {
 1613:                 $check = ' checked="checked" ';
 1614:             }
 1615:             my ($uname,$udom) = split(':',$domcoord[$i]);
 1616:             my $fullname = &Apache::loncommon::plainname($uname,$udom);
 1617:             if ($i == $numdc-1) {
 1618:                 my $colsleft = $numinrow-$rem;
 1619:                 if ($colsleft > 1) {
 1620:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 1621:                 } else {
 1622:                     $datatable .= '<td class="LC_left_item">';
 1623:                 }
 1624:             } else {
 1625:                 $datatable .= '<td class="LC_left_item">';
 1626:             }
 1627:             $datatable .= '<span class="LC_nobreak"><label>'.
 1628:                           '<input type="checkbox" name="reqapprovalnotify" '.
 1629:                           'value="'.$domcoord[$i].'"'.$check.'/>'.
 1630:                           $fullname.'</label></span></td>';
 1631:         }
 1632:         $datatable .= '</tr></table>';
 1633:     } else {
 1634:         $datatable .= &mt('There are no active Domain Coordinators');
 1635:         $rows ++;
 1636:     }
 1637:     $datatable .='</td></tr>';
 1638:     $$rowtotal += $rows;
 1639:     return $datatable;
 1640: }
 1641: 
 1642: sub print_autoenroll {
 1643:     my ($dom,$settings,$rowtotal) = @_;
 1644:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 1645:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff);
 1646:     if (ref($settings) eq 'HASH') {
 1647:         if (exists($settings->{'run'})) {
 1648:             if ($settings->{'run'} eq '0') {
 1649:                 $runoff = ' checked="checked" ';
 1650:                 $runon = ' ';
 1651:             } else {
 1652:                 $runon = ' checked="checked" ';
 1653:                 $runoff = ' ';
 1654:             }
 1655:         } else {
 1656:             if ($autorun) {
 1657:                 $runon = ' checked="checked" ';
 1658:                 $runoff = ' ';
 1659:             } else {
 1660:                 $runoff = ' checked="checked" ';
 1661:                 $runon = ' ';
 1662:             }
 1663:         }
 1664:         if (exists($settings->{'co-owners'})) {
 1665:             if ($settings->{'co-owners'} eq '0') {
 1666:                 $coownersoff = ' checked="checked" ';
 1667:                 $coownerson = ' ';
 1668:             } else {
 1669:                 $coownerson = ' checked="checked" ';
 1670:                 $coownersoff = ' ';
 1671:             }
 1672:         } else {
 1673:             $coownersoff = ' checked="checked" ';
 1674:             $coownerson = ' ';
 1675:         }
 1676:         if (exists($settings->{'sender_domain'})) {
 1677:             $defdom = $settings->{'sender_domain'};
 1678:         }
 1679:     } else {
 1680:         if ($autorun) {
 1681:             $runon = ' checked="checked" ';
 1682:             $runoff = ' ';
 1683:         } else {
 1684:             $runoff = ' checked="checked" ';
 1685:             $runon = ' ';
 1686:         }
 1687:     }
 1688:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 1689:     my $notif_sender;
 1690:     if (ref($settings) eq 'HASH') {
 1691:         $notif_sender = $settings->{'sender_uname'};
 1692:     }
 1693:     my $datatable='<tr class="LC_odd_row">'.
 1694:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 1695:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 1696:                   '<input type="radio" name="autoenroll_run"'.
 1697:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 1698:                   '<label><input type="radio" name="autoenroll_run"'.
 1699:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 1700:                   '</tr><tr>'.
 1701:                   '<td>'.&mt('Notification messages - sender').
 1702:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 1703:                   &mt('username').':&nbsp;'.
 1704:                   '<input type="text" name="sender_uname" value="'.
 1705:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 1706:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 1707:                   '<tr class="LC_odd_row">'.
 1708:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 1709:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 1710:                   '<input type="radio" name="autoassign_coowners"'.
 1711:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 1712:                   '<label><input type="radio" name="autoassign_coowners"'.
 1713:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 1714:                   '</tr>';
 1715:     $$rowtotal += 3;
 1716:     return $datatable;
 1717: }
 1718: 
 1719: sub print_autoupdate {
 1720:     my ($position,$dom,$settings,$rowtotal) = @_;
 1721:     my $datatable;
 1722:     if ($position eq 'top') {
 1723:         my $updateon = ' ';
 1724:         my $updateoff = ' checked="checked" ';
 1725:         my $classlistson = ' ';
 1726:         my $classlistsoff = ' checked="checked" ';
 1727:         if (ref($settings) eq 'HASH') {
 1728:             if ($settings->{'run'} eq '1') {
 1729:                 $updateon = $updateoff;
 1730:                 $updateoff = ' ';
 1731:             }
 1732:             if ($settings->{'classlists'} eq '1') {
 1733:                 $classlistson = $classlistsoff;
 1734:                 $classlistsoff = ' ';
 1735:             }
 1736:         }
 1737:         my %title = (
 1738:                    run => 'Auto-update active?',
 1739:                    classlists => 'Update information in classlists?',
 1740:                     );
 1741:         $datatable = '<tr class="LC_odd_row">'. 
 1742:                   '<td>'.&mt($title{'run'}).'</td>'.
 1743:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 1744:                   '<input type="radio" name="autoupdate_run"'.
 1745:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 1746:                   '<label><input type="radio" name="autoupdate_run"'.
 1747:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 1748:                   '</tr><tr>'.
 1749:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 1750:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 1751:                   '<label><input type="radio" name="classlists"'.
 1752:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 1753:                   '<label><input type="radio" name="classlists"'.
 1754:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 1755:                   '</tr>';
 1756:         $$rowtotal += 2;
 1757:     } else {
 1758:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1759:         my @fields = ('lastname','firstname','middlename','gen',
 1760:                       'permanentemail','id');
 1761:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 1762:         my $numrows = 0;
 1763:         if (ref($types) eq 'ARRAY') {
 1764:             if (@{$types} > 0) {
 1765:                 $datatable = 
 1766:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 1767:                                          \@fields,$types,\$numrows);
 1768:                     $$rowtotal += @{$types}; 
 1769:             }
 1770:         }
 1771:         $datatable .= 
 1772:             &usertype_update_row($settings,{'default' => $othertitle},
 1773:                                  \%fieldtitles,\@fields,['default'],
 1774:                                  \$numrows);
 1775:         $$rowtotal ++;     
 1776:     }
 1777:     return $datatable;
 1778: }
 1779: 
 1780: sub print_autocreate {
 1781:     my ($dom,$settings,$rowtotal) = @_;
 1782:     my (%createon,%createoff);
 1783:     my $curr_dc;
 1784:     my @types = ('xml','req');
 1785:     if (ref($settings) eq 'HASH') {
 1786:         foreach my $item (@types) {
 1787:             $createoff{$item} = ' checked="checked" ';
 1788:             $createon{$item} = ' ';
 1789:             if (exists($settings->{$item})) {
 1790:                 if ($settings->{$item}) {
 1791:                     $createon{$item} = ' checked="checked" ';
 1792:                     $createoff{$item} = ' ';
 1793:                 }
 1794:             }
 1795:         }
 1796:         $curr_dc = $settings->{'xmldc'};
 1797:     } else {
 1798:         foreach my $item (@types) {
 1799:             $createoff{$item} = ' checked="checked" ';
 1800:             $createon{$item} = ' ';
 1801:         }
 1802:     }
 1803:     $$rowtotal += 2;
 1804:     my $datatable='<tr class="LC_odd_row">'.
 1805:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 1806:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 1807:                   '<input type="radio" name="autocreate_xml"'.
 1808:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 1809:                   '<label><input type="radio" name="autocreate_xml"'.
 1810:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>';
 1811:     my ($numdc,$dctable) = &active_dc_picker($dom,$curr_dc);
 1812:     if ($numdc > 1) {
 1813:         $datatable .= '</td><tr><td>'.
 1814:                       &mt('XML files processed as: (choose Dom. Coord.)').
 1815:                       '</td><td class="LC_left_item">'.$dctable.'</td></tr>'.
 1816:                       '<tr class="LC_odd_row">';
 1817:         $$rowtotal ++ ;
 1818:     } else {
 1819:         $datatable .= '</td></tr><tr>';
 1820:     }
 1821:     $datatable .= '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 1822:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 1823:                   '<input type="radio" name="autocreate_req"'.
 1824:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 1825:                   '<label><input type="radio" name="autocreate_req"'.
 1826:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span></td>'.
 1827:                   '</tr>';
 1828:     return $datatable;
 1829: }
 1830: 
 1831: sub print_directorysrch {
 1832:     my ($dom,$settings,$rowtotal) = @_;
 1833:     my $srchon = ' ';
 1834:     my $srchoff = ' checked="checked" ';
 1835:     my ($exacton,$containson,$beginson);
 1836:     my $localon = ' ';
 1837:     my $localoff = ' checked="checked" ';
 1838:     if (ref($settings) eq 'HASH') {
 1839:         if ($settings->{'available'} eq '1') {
 1840:             $srchon = $srchoff;
 1841:             $srchoff = ' ';
 1842:         }
 1843:         if ($settings->{'localonly'} eq '1') {
 1844:             $localon = $localoff;
 1845:             $localoff = ' ';
 1846:         }
 1847:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 1848:             foreach my $type (@{$settings->{'searchtypes'}}) {
 1849:                 if ($type eq 'exact') {
 1850:                     $exacton = ' checked="checked" ';
 1851:                 } elsif ($type eq 'contains') {
 1852:                     $containson = ' checked="checked" ';
 1853:                 } elsif ($type eq 'begins') {
 1854:                     $beginson = ' checked="checked" ';
 1855:                 }
 1856:             }
 1857:         } else {
 1858:             if ($settings->{'searchtypes'} eq 'exact') {
 1859:                 $exacton = ' checked="checked" ';
 1860:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 1861:                 $containson = ' checked="checked" ';
 1862:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 1863:                 $exacton = ' checked="checked" ';
 1864:                 $containson = ' checked="checked" ';
 1865:             }
 1866:         }
 1867:     }
 1868:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 1869:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1870: 
 1871:     my $numinrow = 4;
 1872:     my $cansrchrow = 0;
 1873:     my $datatable='<tr class="LC_odd_row">'.
 1874:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 1875:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 1876:                   '<input type="radio" name="dirsrch_available"'.
 1877:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 1878:                   '<label><input type="radio" name="dirsrch_available"'.
 1879:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 1880:                   '</tr><tr>'.
 1881:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 1882:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 1883:                   '<input type="radio" name="dirsrch_localonly"'.
 1884:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 1885:                   '<label><input type="radio" name="dirsrch_localonly"'.
 1886:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 1887:                   '</tr>';
 1888:     $$rowtotal += 2;
 1889:     if (ref($usertypes) eq 'HASH') {
 1890:         if (keys(%{$usertypes}) > 0) {
 1891:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 1892:                                          $numinrow,$othertitle,'cansearch');
 1893:             $cansrchrow = 1;
 1894:         }
 1895:     }
 1896:     if ($cansrchrow) {
 1897:         $$rowtotal ++;
 1898:         $datatable .= '<tr>';
 1899:     } else {
 1900:         $datatable .= '<tr class="LC_odd_row">';
 1901:     }
 1902:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 1903:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 1904:     foreach my $title (@{$titleorder}) {
 1905:         if (defined($searchtitles->{$title})) {
 1906:             my $check = ' ';
 1907:             if (ref($settings) eq 'HASH') {
 1908:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 1909:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 1910:                         $check = ' checked="checked" ';
 1911:                     }
 1912:                 }
 1913:             }
 1914:             $datatable .= '<td class="LC_left_item">'.
 1915:                           '<span class="LC_nobreak"><label>'.
 1916:                           '<input type="checkbox" name="searchby" '.
 1917:                           'value="'.$title.'"'.$check.'/>'.
 1918:                           $searchtitles->{$title}.'</label></span></td>';
 1919:         }
 1920:     }
 1921:     $datatable .= '</tr></table></td></tr>';
 1922:     $$rowtotal ++;
 1923:     if ($cansrchrow) {
 1924:         $datatable .= '<tr class="LC_odd_row">';
 1925:     } else {
 1926:         $datatable .= '<tr>';
 1927:     }
 1928:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 1929:                   '<td class="LC_left_item" colspan="2">'.
 1930:                   '<span class="LC_nobreak"><label>'.
 1931:                   '<input type="checkbox" name="searchtypes" '.
 1932:                   $exacton.' value="exact" />'.&mt('Exact match').
 1933:                   '</label>&nbsp;'.
 1934:                   '<label><input type="checkbox" name="searchtypes" '.
 1935:                   $beginson.' value="begins" />'.&mt('Begins with').
 1936:                   '</label>&nbsp;'.
 1937:                   '<label><input type="checkbox" name="searchtypes" '.
 1938:                   $containson.' value="contains" />'.&mt('Contains').
 1939:                   '</label></span></td></tr>';
 1940:     $$rowtotal ++;
 1941:     return $datatable;
 1942: }
 1943: 
 1944: sub print_contacts {
 1945:     my ($dom,$settings,$rowtotal) = @_;
 1946:     my $datatable;
 1947:     my @contacts = ('adminemail','supportemail');
 1948:     my (%checked,%to,%otheremails);
 1949:     my @mailings = ('errormail','packagesmail','lonstatusmail','helpdeskmail',
 1950:                     'requestsmail');
 1951:     foreach my $type (@mailings) {
 1952:         $otheremails{$type} = '';
 1953:     }
 1954:     if (ref($settings) eq 'HASH') {
 1955:         foreach my $item (@contacts) {
 1956:             if (exists($settings->{$item})) {
 1957:                 $to{$item} = $settings->{$item};
 1958:             }
 1959:         }
 1960:         foreach my $type (@mailings) {
 1961:             if (exists($settings->{$type})) {
 1962:                 if (ref($settings->{$type}) eq 'HASH') {
 1963:                     foreach my $item (@contacts) {
 1964:                         if ($settings->{$type}{$item}) {
 1965:                             $checked{$type}{$item} = ' checked="checked" ';
 1966:                         }
 1967:                     }
 1968:                     $otheremails{$type} = $settings->{$type}{'others'};
 1969:                 }
 1970:             } elsif ($type eq 'lonstatusmail') {
 1971:                 $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 1972:             }
 1973:         }
 1974:     } else {
 1975:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 1976:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 1977:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 1978:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 1979:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 1980:         $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" '; 
 1981:         $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 1982:     }
 1983:     my ($titles,$short_titles) = &contact_titles();
 1984:     my $rownum = 0;
 1985:     my $css_class;
 1986:     foreach my $item (@contacts) {
 1987:         $rownum ++;
 1988:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 1989:         $datatable .= '<tr'.$css_class.'>'. 
 1990:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 1991:                   '</span></td><td class="LC_right_item">'.
 1992:                   '<input type="text" name="'.$item.'" value="'.
 1993:                   $to{$item}.'" /></td></tr>';
 1994:     }
 1995:     foreach my $type (@mailings) {
 1996:         $rownum ++;
 1997:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 1998:         $datatable .= '<tr'.$css_class.'>'.
 1999:                       '<td><span class="LC_nobreak">'.
 2000:                       $titles->{$type}.': </span></td>'.
 2001:                       '<td class="LC_left_item">'.
 2002:                       '<span class="LC_nobreak">';
 2003:         foreach my $item (@contacts) {
 2004:             $datatable .= '<label>'.
 2005:                           '<input type="checkbox" name="'.$type.'"'.
 2006:                           $checked{$type}{$item}.
 2007:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 2008:                           '</label>&nbsp;';
 2009:         }
 2010:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 2011:                       '<input type="text" name="'.$type.'_others" '.
 2012:                       'value="'.$otheremails{$type}.'"  />'.
 2013:                       '</td></tr>'."\n";
 2014:     }
 2015:     $$rowtotal += $rownum;
 2016:     return $datatable;
 2017: }
 2018: 
 2019: sub radiobutton_prefs {
 2020:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount) = @_;
 2021:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 2022:                    (ref($choices) eq 'HASH'));
 2023: 
 2024:     my (%checkedon,%checkedoff,$datatable,$css_class);
 2025: 
 2026:     foreach my $item (@{$toggles}) {
 2027:         if ($defaultchecked->{$item} eq 'on') {
 2028:             $checkedon{$item} = ' checked="checked" ';
 2029:             $checkedoff{$item} = ' ';
 2030:         } elsif ($defaultchecked->{$item} eq 'off') {
 2031:             $checkedoff{$item} = ' checked="checked" ';
 2032:             $checkedon{$item} = ' ';
 2033:         }
 2034:     }
 2035:     if (ref($settings) eq 'HASH') {
 2036:         foreach my $item (@{$toggles}) {
 2037:             if ($settings->{$item} eq '1') {
 2038:                 $checkedon{$item} =  ' checked="checked" ';
 2039:                 $checkedoff{$item} = ' ';
 2040:             } elsif ($settings->{$item} eq '0') {
 2041:                 $checkedoff{$item} =  ' checked="checked" ';
 2042:                 $checkedon{$item} = ' ';
 2043:             }
 2044:         }
 2045:     }
 2046:     foreach my $item (@{$toggles}) {
 2047:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2048:         $datatable .=
 2049:             '<tr'.$css_class.'><td><span class="LC_nobreak">'.$choices->{$item}.
 2050:             '</span></td>'.
 2051:             '<td class="LC_right_item"><span class="LC_nobreak">'.
 2052:             '<label><input type="radio" name="'.
 2053:             $item.'" '.$checkedon{$item}.' value="1" />'.&mt('Yes').
 2054:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 2055:             $checkedoff{$item}.' value="0" />'.&mt('No').'</label>'.
 2056:             '</span></td>'.
 2057:             '</tr>';
 2058:         $itemcount ++;
 2059:     }
 2060:     return ($datatable,$itemcount);
 2061: }
 2062: 
 2063: sub print_coursedefaults {
 2064:     my ($dom,$settings,$rowtotal) = @_;
 2065:     my ($css_class,$datatable);
 2066:     my $itemcount = 1;
 2067:     my (%checkedon,%checkedoff,%choices,%defaultchecked,@toggles);
 2068:     %choices =
 2069:         &Apache::lonlocal::texthash (
 2070:             canuse_pdfforms => 'Course/Community users can create/upload PDF forms',
 2071:     );
 2072:     %defaultchecked = ('canuse_pdfforms' => 'off');
 2073:     @toggles = ('canuse_pdfforms',);
 2074:     ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 2075:                                                  \%choices,$itemcount);
 2076:     $$rowtotal += $itemcount;
 2077:     return $datatable;
 2078: }
 2079: 
 2080: sub contact_titles {
 2081:     my %titles = &Apache::lonlocal::texthash (
 2082:                    'supportemail' => 'Support E-mail address',
 2083:                    'adminemail'   => 'Default Server Admin E-mail address',
 2084:                    'errormail'    => 'Error reports to be e-mailed to',
 2085:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 2086:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to',
 2087:                    'lonstatusmail' => 'E-mail from nightly status check (warnings/errors)',
 2088:                    'requestsmail' => 'E-mail from course requests requiring approval',
 2089:                  );
 2090:     my %short_titles = &Apache::lonlocal::texthash (
 2091:                            adminemail   => 'Admin E-mail address',
 2092:                            supportemail => 'Support E-mail',
 2093:                        );   
 2094:     return (\%titles,\%short_titles);
 2095: }
 2096: 
 2097: sub tool_titles {
 2098:     my %titles = &Apache::lonlocal::texthash (
 2099:                      aboutme    => 'Personal Information Page',
 2100:                      blog       => 'Blog',
 2101:                      portfolio  => 'Portfolio',
 2102:                      official   => 'Official courses (with institutional codes)',
 2103:                      unofficial => 'Unofficial courses',
 2104:                      community  => 'Communities',
 2105:                  );
 2106:     return %titles;
 2107: }
 2108: 
 2109: sub courserequest_titles {
 2110:     my %titles = &Apache::lonlocal::texthash (
 2111:                                    official   => 'Official',
 2112:                                    unofficial => 'Unofficial',
 2113:                                    community  => 'Communities',
 2114:                                    norequest  => 'Not allowed',
 2115:                                    approval   => 'Approval by Dom. Coord.',
 2116:                                    validate   => 'With validation',
 2117:                                    autolimit  => 'Numerical limit',
 2118:                                    unlimited  => '(blank for unlimited)',
 2119:                  );
 2120:     return %titles;
 2121: }
 2122: 
 2123: sub courserequest_conditions {
 2124:     my %conditions = &Apache::lonlocal::texthash (
 2125:        approval   => '(Processing of request subject to approval by Domain Coordinator).',
 2126:        validate   => '(Processing of request subject to instittutional validation).',
 2127:                  );
 2128:     return %conditions;
 2129: }
 2130: 
 2131: 
 2132: sub print_usercreation {
 2133:     my ($position,$dom,$settings,$rowtotal) = @_;
 2134:     my $numinrow = 4;
 2135:     my $datatable;
 2136:     if ($position eq 'top') {
 2137:         $$rowtotal ++;
 2138:         my $rowcount = 0;
 2139:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 2140:         if (ref($rules) eq 'HASH') {
 2141:             if (keys(%{$rules}) > 0) {
 2142:                 $datatable .= &user_formats_row('username',$settings,$rules,
 2143:                                                 $ruleorder,$numinrow,$rowcount);
 2144:                 $$rowtotal ++;
 2145:                 $rowcount ++;
 2146:             }
 2147:         }
 2148:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 2149:         if (ref($idrules) eq 'HASH') {
 2150:             if (keys(%{$idrules}) > 0) {
 2151:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 2152:                                                 $idruleorder,$numinrow,$rowcount);
 2153:                 $$rowtotal ++;
 2154:                 $rowcount ++;
 2155:             }
 2156:         }
 2157:         my ($emailrules,$emailruleorder) = 
 2158:             &Apache::lonnet::inst_userrules($dom,'email');
 2159:         if (ref($emailrules) eq 'HASH') {
 2160:             if (keys(%{$emailrules}) > 0) {
 2161:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 2162:                                                 $emailruleorder,$numinrow,$rowcount);
 2163:                 $$rowtotal ++;
 2164:                 $rowcount ++;
 2165:             }
 2166:         }
 2167:         if ($rowcount == 0) {
 2168:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 2169:             $$rowtotal ++;
 2170:             $rowcount ++;
 2171:         }
 2172:     } elsif ($position eq 'middle') {
 2173:         my @creators = ('author','course','requestcrs','selfcreate');
 2174:         my ($rules,$ruleorder) =
 2175:             &Apache::lonnet::inst_userrules($dom,'username');
 2176:         my %lt = &usercreation_types();
 2177:         my %checked;
 2178:         my @selfcreate; 
 2179:         if (ref($settings) eq 'HASH') {
 2180:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 2181:                 foreach my $item (@creators) {
 2182:                     $checked{$item} = $settings->{'cancreate'}{$item};
 2183:                 }
 2184:                 if (ref($settings->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
 2185:                     @selfcreate = @{$settings->{'cancreate'}{'selfcreate'}};
 2186:                 } elsif ($settings->{'cancreate'}{'selfcreate'} ne '') {
 2187:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 2188:                         @selfcreate = ('email','login','sso');
 2189:                     } elsif ($settings->{'cancreate'}{'selfcreate'} ne 'none') {
 2190:                         @selfcreate = ($settings->{'cancreate'}{'selfcreate'});
 2191:                     }
 2192:                 }
 2193:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 2194:                 foreach my $item (@creators) {
 2195:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 2196:                         $checked{$item} = 'none';
 2197:                     }
 2198:                 }
 2199:             }
 2200:         }
 2201:         my $rownum = 0;
 2202:         foreach my $item (@creators) {
 2203:             $rownum ++;
 2204:             if ($item ne 'selfcreate') {  
 2205:                 if ($checked{$item} eq '') {
 2206:                     $checked{$item} = 'any';
 2207:                 }
 2208:             }
 2209:             my $css_class;
 2210:             if ($rownum%2) {
 2211:                 $css_class = '';
 2212:             } else {
 2213:                 $css_class = ' class="LC_odd_row" ';
 2214:             }
 2215:             $datatable .= '<tr'.$css_class.'>'.
 2216:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 2217:                          '</span></td><td align="right">';
 2218:             my @options;
 2219:             if ($item eq 'selfcreate') {
 2220:                 push(@options,('email','login','sso'));
 2221:             } else {
 2222:                 @options = ('any');
 2223:                 if (ref($rules) eq 'HASH') {
 2224:                     if (keys(%{$rules}) > 0) {
 2225:                         push(@options,('official','unofficial'));
 2226:                     }
 2227:                 }
 2228:                 push(@options,'none');
 2229:             }
 2230:             foreach my $option (@options) {
 2231:                 my $type = 'radio';
 2232:                 my $check = ' ';
 2233:                 if ($item eq 'selfcreate') {
 2234:                     $type = 'checkbox';
 2235:                     if (grep(/^\Q$option\E$/,@selfcreate)) {
 2236:                         $check = ' checked="checked" ';
 2237:                     }
 2238:                 } else {
 2239:                     if ($checked{$item} eq $option) {
 2240:                         $check = ' checked="checked" ';
 2241:                     }
 2242:                 } 
 2243:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2244:                               '<input type="'.$type.'" name="can_createuser_'.
 2245:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 2246:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 2247:             }
 2248:             $datatable .= '</td></tr>';
 2249:         }
 2250:         my ($othertitle,$usertypes,$types) =
 2251:             &Apache::loncommon::sorted_inst_types($dom);
 2252:         if (ref($usertypes) eq 'HASH') {
 2253:             if (keys(%{$usertypes}) > 0) {
 2254:                 my $createsettings;
 2255:                 if (ref($settings) eq 'HASH') {
 2256:                     $createsettings = $settings->{cancreate};
 2257:                 }
 2258:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 2259:                                              $dom,$numinrow,$othertitle,
 2260:                                              'statustocreate');
 2261:                 $$rowtotal ++;
 2262:             }
 2263:         }
 2264:     } else {
 2265:         my @contexts = ('author','course','domain');
 2266:         my @authtypes = ('int','krb4','krb5','loc');
 2267:         my %checked;
 2268:         if (ref($settings) eq 'HASH') {
 2269:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 2270:                 foreach my $item (@contexts) {
 2271:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 2272:                         foreach my $auth (@authtypes) {
 2273:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 2274:                                 $checked{$item}{$auth} = ' checked="checked" ';
 2275:                             }
 2276:                         }
 2277:                     }
 2278:                 }
 2279:             }
 2280:         } else {
 2281:             foreach my $item (@contexts) {
 2282:                 foreach my $auth (@authtypes) {
 2283:                     $checked{$item}{$auth} = ' checked="checked" ';
 2284:                 }
 2285:             }
 2286:         }
 2287:         my %title = &context_names();
 2288:         my %authname = &authtype_names();
 2289:         my $rownum = 0;
 2290:         my $css_class; 
 2291:         foreach my $item (@contexts) {
 2292:             if ($rownum%2) {
 2293:                 $css_class = '';
 2294:             } else {
 2295:                 $css_class = ' class="LC_odd_row" ';
 2296:             }
 2297:             $datatable .=   '<tr'.$css_class.'>'.
 2298:                             '<td>'.$title{$item}.
 2299:                             '</td><td class="LC_left_item">'.
 2300:                             '<span class="LC_nobreak">';
 2301:             foreach my $auth (@authtypes) {
 2302:                 $datatable .= '<label>'. 
 2303:                               '<input type="checkbox" name="'.$item.'_auth" '.
 2304:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 2305:                               $authname{$auth}.'</label>&nbsp;';
 2306:             }
 2307:             $datatable .= '</span></td></tr>';
 2308:             $rownum ++;
 2309:         }
 2310:         $$rowtotal += $rownum;
 2311:     }
 2312:     return $datatable;
 2313: }
 2314: 
 2315: sub user_formats_row {
 2316:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 2317:     my $output;
 2318:     my %text = (
 2319:                    'username' => 'new usernames',
 2320:                    'id'       => 'IDs',
 2321:                    'email'    => 'self-created accounts (e-mail)',
 2322:                );
 2323:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 2324:     $output = '<tr '.$css_class.'>'.
 2325:               '<td><span class="LC_nobreak">';
 2326:     if ($type eq 'email') {
 2327:         $output .= &mt("Formats disallowed for $text{$type}: ");
 2328:     } else {
 2329:         $output .= &mt("Format rules to check for $text{$type}: ");
 2330:     }
 2331:     $output .= '</span></td>'.
 2332:                '<td class="LC_left_item" colspan="2"><table>';
 2333:     my $rem;
 2334:     if (ref($ruleorder) eq 'ARRAY') {
 2335:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 2336:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 2337:                 my $rem = $i%($numinrow);
 2338:                 if ($rem == 0) {
 2339:                     if ($i > 0) {
 2340:                         $output .= '</tr>';
 2341:                     }
 2342:                     $output .= '<tr>';
 2343:                 }
 2344:                 my $check = ' ';
 2345:                 if (ref($settings) eq 'HASH') {
 2346:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 2347:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 2348:                             $check = ' checked="checked" ';
 2349:                         }
 2350:                     }
 2351:                 }
 2352:                 $output .= '<td class="LC_left_item">'.
 2353:                            '<span class="LC_nobreak"><label>'.
 2354:                            '<input type="checkbox" name="'.$type.'_rule" '.
 2355:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 2356:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 2357:             }
 2358:         }
 2359:         $rem = @{$ruleorder}%($numinrow);
 2360:     }
 2361:     my $colsleft = $numinrow - $rem;
 2362:     if ($colsleft > 1 ) {
 2363:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2364:                    '&nbsp;</td>';
 2365:     } elsif ($colsleft == 1) {
 2366:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 2367:     }
 2368:     $output .= '</tr></table></td></tr>';
 2369:     return $output;
 2370: }
 2371: 
 2372: sub usercreation_types {
 2373:     my %lt = &Apache::lonlocal::texthash (
 2374:                     author     => 'When adding a co-author',
 2375:                     course     => 'When adding a user to a course',
 2376:                     requestcrs => 'When requesting a course',
 2377:                     selfcreate => 'User creates own account', 
 2378:                     any        => 'Any',
 2379:                     official   => 'Institutional only ',
 2380:                     unofficial => 'Non-institutional only',
 2381:                     email      => 'E-mail address',
 2382:                     login      => 'Institutional Login',
 2383:                     sso        => 'SSO', 
 2384:                     none       => 'None',
 2385:     );
 2386:     return %lt;
 2387: }
 2388: 
 2389: sub authtype_names {
 2390:     my %lt = &Apache::lonlocal::texthash(
 2391:                       int    => 'Internal',
 2392:                       krb4   => 'Kerberos 4',
 2393:                       krb5   => 'Kerberos 5',
 2394:                       loc    => 'Local',
 2395:                   );
 2396:     return %lt;
 2397: }
 2398: 
 2399: sub context_names {
 2400:     my %context_title = &Apache::lonlocal::texthash(
 2401:        author => 'Creating users when an Author',
 2402:        course => 'Creating users when in a course',
 2403:        domain => 'Creating users when a Domain Coordinator',
 2404:     );
 2405:     return %context_title;
 2406: }
 2407: 
 2408: sub print_usermodification {
 2409:     my ($position,$dom,$settings,$rowtotal) = @_;
 2410:     my $numinrow = 4;
 2411:     my ($context,$datatable,$rowcount);
 2412:     if ($position eq 'top') {
 2413:         $rowcount = 0;
 2414:         $context = 'author'; 
 2415:         foreach my $role ('ca','aa') {
 2416:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 2417:                                                    $numinrow,$rowcount);
 2418:             $$rowtotal ++;
 2419:             $rowcount ++;
 2420:         }
 2421:     } elsif ($position eq 'middle') {
 2422:         $context = 'course';
 2423:         $rowcount = 0;
 2424:         foreach my $role ('st','ep','ta','in','cr') {
 2425:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 2426:                                                    $numinrow,$rowcount);
 2427:             $$rowtotal ++;
 2428:             $rowcount ++;
 2429:         }
 2430:     } elsif ($position eq 'bottom') {
 2431:         $context = 'selfcreate';
 2432:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2433:         $usertypes->{'default'} = $othertitle;
 2434:         if (ref($types) eq 'ARRAY') {
 2435:             push(@{$types},'default');
 2436:             $usertypes->{'default'} = $othertitle;
 2437:             foreach my $status (@{$types}) {
 2438:                 $datatable .= &modifiable_userdata_row($context,$status,$settings,
 2439:                                                        $numinrow,$rowcount,$usertypes);
 2440:                 $$rowtotal ++;
 2441:                 $rowcount ++;
 2442:             }
 2443:         }
 2444:     }
 2445:     return $datatable;
 2446: }
 2447: 
 2448: sub print_defaults {
 2449:     my ($dom,$rowtotal) = @_;
 2450:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 2451:                  'datelocale_def');
 2452:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 2453:     my $titles = &defaults_titles();
 2454:     my $rownum = 0;
 2455:     my ($datatable,$css_class);
 2456:     foreach my $item (@items) {
 2457:         if ($rownum%2) {
 2458:             $css_class = '';
 2459:         } else {
 2460:             $css_class = ' class="LC_odd_row" ';
 2461:         }
 2462:         $datatable .= '<tr'.$css_class.'>'.
 2463:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 2464:                   '</span></td><td class="LC_right_item">';
 2465:         if ($item eq 'auth_def') {
 2466:             my @authtypes = ('internal','krb4','krb5','localauth');
 2467:             my %shortauth = (
 2468:                              internal => 'int',
 2469:                              krb4 => 'krb4',
 2470:                              krb5 => 'krb5',
 2471:                              localauth  => 'loc'
 2472:                            );
 2473:             my %authnames = &authtype_names();
 2474:             foreach my $auth (@authtypes) {
 2475:                 my $checked = ' ';
 2476:                 if ($domdefaults{$item} eq $auth) {
 2477:                     $checked = ' checked="checked" ';
 2478:                 }
 2479:                 $datatable .= '<label><input type="radio" name="'.$item.
 2480:                               '" value="'.$auth.'"'.$checked.'/>'.
 2481:                               $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 2482:             }
 2483:         } elsif ($item eq 'timezone_def') {
 2484:             my $includeempty = 1;
 2485:             $datatable .= &Apache::loncommon::select_timezone($item,$domdefaults{$item},undef,$includeempty);
 2486:         } elsif ($item eq 'datelocale_def') {
 2487:             my $includeempty = 1;
 2488:             $datatable .= &Apache::loncommon::select_datelocale($item,$domdefaults{$item},undef,$includeempty);
 2489:         } else {
 2490:             $datatable .= '<input type="text" name="'.$item.'" value="'.
 2491:                           $domdefaults{$item}.'" />';
 2492:         }
 2493:         $datatable .= '</td></tr>';
 2494:         $rownum ++;
 2495:     }
 2496:     $$rowtotal += $rownum;
 2497:     return $datatable;
 2498: }
 2499: 
 2500: sub defaults_titles {
 2501:     my %titles = &Apache::lonlocal::texthash (
 2502:                    'auth_def'      => 'Default authentication type',
 2503:                    'auth_arg_def'  => 'Default authentication argument',
 2504:                    'lang_def'      => 'Default language',
 2505:                    'timezone_def'  => 'Default timezone',
 2506:                    'datelocale_def' => 'Default locale for dates',
 2507:                  );
 2508:     return (\%titles);
 2509: }
 2510: 
 2511: sub print_scantronformat {
 2512:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 2513:     my $itemcount = 1;
 2514:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 2515:         %confhash);
 2516:     my $switchserver = &check_switchserver($dom,$confname);
 2517:     my %lt = &Apache::lonlocal::texthash (
 2518:                 default => 'Default bubblesheet format file error',
 2519:                 custom  => 'Custom bubblesheet format file error',
 2520:              );
 2521:     my %scantronfiles = (
 2522:         default => 'default.tab',
 2523:         custom => 'custom.tab',
 2524:     );
 2525:     foreach my $key (keys(%scantronfiles)) {
 2526:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 2527:                               .$scantronfiles{$key};
 2528:     }
 2529:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 2530:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 2531:         if (!$switchserver) {
 2532:             my $servadm = $r->dir_config('lonAdmEMail');
 2533:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 2534:             if ($configuserok eq 'ok') {
 2535:                 if ($author_ok eq 'ok') {
 2536:                     my %legacyfile = (
 2537:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 2538:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 2539:                     );
 2540:                     my %md5chk;
 2541:                     foreach my $type (keys(%legacyfile)) {
 2542:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 2543:                         chomp($md5chk{$type});
 2544:                     }
 2545:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 2546:                         foreach my $type (keys(%legacyfile)) {
 2547:                             ($scantronurls{$type},my $error) = 
 2548:                                 &legacy_scantronformat($r,$dom,$confname,
 2549:                                                  $type,$legacyfile{$type},
 2550:                                                  $scantronurls{$type},
 2551:                                                  $scantronfiles{$type});
 2552:                             if ($error ne '') {
 2553:                                 $error{$type} = $error;
 2554:                             }
 2555:                         }
 2556:                         if (keys(%error) == 0) {
 2557:                             $is_custom = 1;
 2558:                             $confhash{'scantron'}{'scantronformat'} = 
 2559:                                 $scantronurls{'custom'};
 2560:                             my $putresult = 
 2561:                                 &Apache::lonnet::put_dom('configuration',
 2562:                                                          \%confhash,$dom);
 2563:                             if ($putresult ne 'ok') {
 2564:                                 $error{'custom'} = 
 2565:                                     '<span class="LC_error">'.
 2566:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 2567:                             }
 2568:                         }
 2569:                     } else {
 2570:                         ($scantronurls{'default'},my $error) =
 2571:                             &legacy_scantronformat($r,$dom,$confname,
 2572:                                           'default',$legacyfile{'default'},
 2573:                                           $scantronurls{'default'},
 2574:                                           $scantronfiles{'default'});
 2575:                         if ($error eq '') {
 2576:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 2577:                             my $putresult =
 2578:                                 &Apache::lonnet::put_dom('configuration',
 2579:                                                          \%confhash,$dom);
 2580:                             if ($putresult ne 'ok') {
 2581:                                 $error{'default'} =
 2582:                                     '<span class="LC_error">'.
 2583:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 2584:                             }
 2585:                         } else {
 2586:                             $error{'default'} = $error;
 2587:                         }
 2588:                     }
 2589:                 }
 2590:             }
 2591:         } else {
 2592:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 2593:         }
 2594:     }
 2595:     if (ref($settings) eq 'HASH') {
 2596:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 2597:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 2598:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 2599:                 $scantronurl = '';
 2600:             } else {
 2601:                 $scantronurl = $settings->{'scantronformat'};
 2602:             }
 2603:             $is_custom = 1;
 2604:         } else {
 2605:             $scantronurl = $scantronurls{'default'};
 2606:         }
 2607:     } else {
 2608:         if ($is_custom) {
 2609:             $scantronurl = $scantronurls{'custom'};
 2610:         } else {
 2611:             $scantronurl = $scantronurls{'default'};
 2612:         }
 2613:     }
 2614:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2615:     $datatable .= '<tr'.$css_class.'>';
 2616:     if (!$is_custom) {
 2617:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 2618:                       '<span class="LC_nobreak">';
 2619:         if ($scantronurl) {
 2620:             $datatable .= '<a href="'.$scantronurl.'" target="_blank">'.
 2621:                           &mt('Default scantron format file').'</a>';
 2622:         } else {
 2623:             $datatable = &mt('File unavailable for display');
 2624:         }
 2625:         $datatable .= '</span></td>';
 2626:         if (keys(%error) == 0) { 
 2627:             $datatable .= '<td valign="bottom">';
 2628:             if (!$switchserver) {
 2629:                 $datatable .= &mt('Upload:').'<br />';
 2630:             }
 2631:         } else {
 2632:             my $errorstr;
 2633:             foreach my $key (sort(keys(%error))) {
 2634:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 2635:             }
 2636:             $datatable .= '<td>'.$errorstr;
 2637:         }
 2638:     } else {
 2639:         if (keys(%error) > 0) {
 2640:             my $errorstr;
 2641:             foreach my $key (sort(keys(%error))) {
 2642:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 2643:             } 
 2644:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 2645:         } elsif ($scantronurl) {
 2646:             $datatable .= '<td><span class="LC_nobreak">'.
 2647:                           '<a href="'.$scantronurl.'" target="_blank">'.
 2648:                           &mt('Custom scantron format file').'</a><label>'.
 2649:                           '<input type="checkbox" name="scantronformat_del"'.
 2650:                           '" value="1" />'.&mt('Delete?').'</label></span></td>'.
 2651:                           '<td><span class="LC_nobreak">&nbsp;'.
 2652:                           &mt('Replace:').'</span><br />';
 2653:         }
 2654:     }
 2655:     if (keys(%error) == 0) {
 2656:         if ($switchserver) {
 2657:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2658:         } else {
 2659:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 2660:                          '<input type="file" name="scantronformat" /></span>';
 2661:         }
 2662:     }
 2663:     $datatable .= '</td></tr>';
 2664:     $$rowtotal ++;
 2665:     return $datatable;
 2666: }
 2667: 
 2668: sub legacy_scantronformat {
 2669:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 2670:     my ($url,$error);
 2671:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 2672:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 2673:         (my $result,$url) =
 2674:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 2675:                          '','',$newfile);
 2676:         if ($result ne 'ok') {
 2677:             $error = &mt("An error occurred publishing the [_1] scantron format file in RES space. Error was: [_2].",$newfile,$result);
 2678:         }
 2679:     }
 2680:     return ($url,$error);
 2681: }
 2682: 
 2683: sub print_coursecategories {
 2684:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 2685:     my $datatable;
 2686:     if ($position eq 'top') {
 2687:         my $toggle_cats_crs = ' ';
 2688:         my $toggle_cats_dom = ' checked="checked" ';
 2689:         my $can_cat_crs = ' ';
 2690:         my $can_cat_dom = ' checked="checked" ';
 2691:         my $toggle_catscomm_comm = ' ';
 2692:         my $toggle_catscomm_dom = ' checked="checked" ';
 2693:         my $can_catcomm_comm = ' ';
 2694:         my $can_catcomm_dom = ' checked="checked" ';
 2695: 
 2696:         if (ref($settings) eq 'HASH') {
 2697:             if ($settings->{'togglecats'} eq 'crs') {
 2698:                 $toggle_cats_crs = $toggle_cats_dom;
 2699:                 $toggle_cats_dom = ' ';
 2700:             }
 2701:             if ($settings->{'categorize'} eq 'crs') {
 2702:                 $can_cat_crs = $can_cat_dom;
 2703:                 $can_cat_dom = ' ';
 2704:             }
 2705:             if ($settings->{'togglecatscomm'} eq 'comm') {
 2706:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 2707:                 $toggle_catscomm_dom = ' ';
 2708:             }
 2709:             if ($settings->{'categorizecomm'} eq 'comm') {
 2710:                 $can_catcomm_comm = $can_catcomm_dom;
 2711:                 $can_catcomm_dom = ' ';
 2712:             }
 2713:         }
 2714:         my %title = &Apache::lonlocal::texthash (
 2715:                      togglecats     => 'Show/Hide a course in catalog',
 2716:                      togglecatscomm => 'Show/Hide a community in catalog',
 2717:                      categorize     => 'Assign a category to a course',
 2718:                      categorizecomm => 'Assign a category to a community',
 2719:                     );
 2720:         my %level = &Apache::lonlocal::texthash (
 2721:                      dom => 'Set in Domain',
 2722:                      crs => 'Set in Course',   
 2723:                      comm => 'Set in Community',
 2724:                     );
 2725:         $datatable = '<tr class="LC_odd_row">'.
 2726:                   '<td>'.$title{'togglecats'}.'</td>'.
 2727:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2728:                   '<input type="radio" name="togglecats"'.
 2729:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 2730:                   '<label><input type="radio" name="togglecats"'.
 2731:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 2732:                   '</tr><tr>'.
 2733:                   '<td>'.$title{'categorize'}.'</td>'.
 2734:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2735:                   '<label><input type="radio" name="categorize"'.
 2736:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 2737:                   '<label><input type="radio" name="categorize"'.
 2738:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 2739:                   '</tr><tr class="LC_odd_row">'.
 2740:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 2741:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2742:                   '<input type="radio" name="togglecatscomm"'.
 2743:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 2744:                   '<label><input type="radio" name="togglecatscomm"'.
 2745:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 2746:                   '</tr><tr>'.
 2747:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 2748:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2749:                   '<label><input type="radio" name="categorizecomm"'.
 2750:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 2751:                   '<label><input type="radio" name="categorizecomm"'.
 2752:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 2753:                   '</tr>';
 2754:         $$rowtotal += 4;
 2755:     } else {
 2756:         my $css_class;
 2757:         my $itemcount = 1;
 2758:         my $cathash; 
 2759:         if (ref($settings) eq 'HASH') {
 2760:             $cathash = $settings->{'cats'};
 2761:         }
 2762:         if (ref($cathash) eq 'HASH') {
 2763:             my (@cats,@trails,%allitems,%idx,@jsarray);
 2764:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 2765:                                                    \%allitems,\%idx,\@jsarray);
 2766:             my $maxdepth = scalar(@cats);
 2767:             my $colattrib = '';
 2768:             if ($maxdepth > 2) {
 2769:                 $colattrib = ' colspan="2" ';
 2770:             }
 2771:             my @path;
 2772:             if (@cats > 0) {
 2773:                 if (ref($cats[0]) eq 'ARRAY') {
 2774:                     my $numtop = @{$cats[0]};
 2775:                     my $maxnum = $numtop;
 2776:                     my %default_names = (
 2777:                           instcode    => &mt('Official courses'),
 2778:                           communities => &mt('Communities'),
 2779:                     );
 2780: 
 2781:                     if ((!grep(/^instcode$/,@{$cats[0]})) ||
 2782:                         ($cathash->{'instcode::0'} eq '') ||
 2783:                         (!grep(/^communities$/,@{$cats[0]})) ||
 2784:                         ($cathash->{'communities::0'} eq '')) {
 2785:                         $maxnum ++;
 2786:                     }
 2787:                     my $lastidx;
 2788:                     for (my $i=0; $i<$numtop; $i++) {
 2789:                         my $parent = $cats[0][$i];
 2790:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2791:                         my $item = &escape($parent).'::0';
 2792:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 2793:                         $lastidx = $idx{$item};
 2794:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2795:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 2796:                         for (my $k=0; $k<=$maxnum; $k++) {
 2797:                             my $vpos = $k+1;
 2798:                             my $selstr;
 2799:                             if ($k == $i) {
 2800:                                 $selstr = ' selected="selected" ';
 2801:                             }
 2802:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2803:                         }
 2804:                         $datatable .= '</select></td><td>';
 2805:                         if ($parent eq 'instcode' || $parent eq 'communities') {
 2806:                             $datatable .=  '<span class="LC_nobreak">'
 2807:                                            .$default_names{$parent}.'</span>';
 2808:                             if ($parent eq 'instcode') {
 2809:                                 $datatable .= '<br /><span class="LC_nobreak">('
 2810:                                               .&mt('with institutional codes')
 2811:                                               .')</span></td><td'.$colattrib.'>';
 2812:                             } else {
 2813:                                 $datatable .= '<table><tr><td>';
 2814:                             }
 2815:                             $datatable .= '<span class="LC_nobreak">'
 2816:                                           .'<label><input type="radio" name="'
 2817:                                           .$parent.'" value="1" checked="checked" />'
 2818:                                           .&mt('Display').'</label>';
 2819:                             if ($parent eq 'instcode') {
 2820:                                 $datatable .= '&nbsp;';
 2821:                             } else {
 2822:                                 $datatable .= '</span></td></tr><tr><td>'
 2823:                                               .'<span class="LC_nobreak">';
 2824:                             }
 2825:                             $datatable .= '<label><input type="radio" name="'
 2826:                                           .$parent.'" value="0" />'
 2827:                                           .&mt('Do not display').'</label></span>';
 2828:                             if ($parent eq 'communities') {
 2829:                                 $datatable .= '</td></tr></table>';
 2830:                             }
 2831:                             $datatable .= '</td>';
 2832:                         } else {
 2833:                             $datatable .= $parent
 2834:                                           .'&nbsp;<label><input type="checkbox" name="deletecategory" '
 2835:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 2836:                         }
 2837:                         my $depth = 1;
 2838:                         push(@path,$parent);
 2839:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 2840:                         pop(@path);
 2841:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 2842:                         $itemcount ++;
 2843:                     }
 2844:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2845:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 2846:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 2847:                     for (my $k=0; $k<=$maxnum; $k++) {
 2848:                         my $vpos = $k+1;
 2849:                         my $selstr;
 2850:                         if ($k == $numtop) {
 2851:                             $selstr = ' selected="selected" ';
 2852:                         }
 2853:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2854:                     }
 2855:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 2856:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 2857:                                   .'</tr>'."\n";
 2858:                     $itemcount ++;
 2859:                     foreach my $default ('instcode','communities') {
 2860:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 2861:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2862:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 2863:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 2864:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 2865:                             for (my $k=0; $k<=$maxnum; $k++) {
 2866:                                 my $vpos = $k+1;
 2867:                                 my $selstr;
 2868:                                 if ($k == $maxnum) {
 2869:                                     $selstr = ' selected="selected" ';
 2870:                                 }
 2871:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2872:                             }
 2873:                             $datatable .= '</select></span></td>'.
 2874:                                           '<td><span class="LC_nobreak">'.
 2875:                                           $default_names{$default}.'</span>';
 2876:                             if ($default eq 'instcode') {
 2877:                                 $datatable .= '<br /><span class="LC_nobreak">('
 2878:                                               .&mt('with institutional codes').')</span>';
 2879:                             }
 2880:                             $datatable .= '</td>'
 2881:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 2882:                                           .&mt('Display').'</label>&nbsp;'
 2883:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 2884:                                           .&mt('Do not display').'</label></span></td></tr>';
 2885:                         }
 2886:                     }
 2887:                 }
 2888:             } else {
 2889:                 $datatable .= &initialize_categories($itemcount);
 2890:             }
 2891:         } else {
 2892:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[0]->{'col2'}.'</td>'
 2893:                           .&initialize_categories($itemcount);
 2894:         }
 2895:         $$rowtotal += $itemcount;
 2896:     }
 2897:     return $datatable;
 2898: }
 2899: 
 2900: sub print_serverstatuses {
 2901:     my ($dom,$settings,$rowtotal) = @_;
 2902:     my $datatable;
 2903:     my @pages = &serverstatus_pages();
 2904:     my (%namedaccess,%machineaccess);
 2905:     foreach my $type (@pages) {
 2906:         $namedaccess{$type} = '';
 2907:         $machineaccess{$type}= '';
 2908:     }
 2909:     if (ref($settings) eq 'HASH') {
 2910:         foreach my $type (@pages) {
 2911:             if (exists($settings->{$type})) {
 2912:                 if (ref($settings->{$type}) eq 'HASH') {
 2913:                     foreach my $key (keys(%{$settings->{$type}})) {
 2914:                         if ($key eq 'namedusers') {
 2915:                             $namedaccess{$type} = $settings->{$type}->{$key};
 2916:                         } elsif ($key eq 'machines') {
 2917:                             $machineaccess{$type} = $settings->{$type}->{$key};
 2918:                         }
 2919:                     }
 2920:                 }
 2921:             }
 2922:         }
 2923:     }
 2924:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 2925:     my $rownum = 0;
 2926:     my $css_class;
 2927:     foreach my $type (@pages) {
 2928:         $rownum ++;
 2929:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2930:         $datatable .= '<tr'.$css_class.'>'.
 2931:                       '<td><span class="LC_nobreak">'.
 2932:                       $titles->{$type}.'</span></td>'.
 2933:                       '<td class="LC_left_item">'.
 2934:                       '<input type="text" name="'.$type.'_namedusers" '.
 2935:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 2936:                       '<td class="LC_right_item">'.
 2937:                       '<span class="LC_nobreak">'.
 2938:                       '<input type="text" name="'.$type.'_machines" '.
 2939:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 2940:                       '</td></tr>'."\n";
 2941:     }
 2942:     $$rowtotal += $rownum;
 2943:     return $datatable;
 2944: }
 2945: 
 2946: sub serverstatus_pages {
 2947:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 2948:             'clusterstatus','metadata_keywords','metadata_harvest',
 2949:             'takeoffline','takeonline','showenv','toggledebug');
 2950: }
 2951: 
 2952: sub coursecategories_javascript {
 2953:     my ($settings) = @_;
 2954:     my ($output,$jstext,$cathash);
 2955:     if (ref($settings) eq 'HASH') {
 2956:         $cathash = $settings->{'cats'};
 2957:     }
 2958:     if (ref($cathash) eq 'HASH') {
 2959:         my (@cats,@jsarray,%idx);
 2960:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 2961:         if (@jsarray > 0) {
 2962:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 2963:             for (my $i=0; $i<@jsarray; $i++) {
 2964:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 2965:                     my $catstr = join('","',@{$jsarray[$i]});
 2966:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 2967:                 }
 2968:             }
 2969:         }
 2970:     } else {
 2971:         $jstext  = '    var categories = Array(1);'."\n".
 2972:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 2973:     }
 2974:     my $instcode_reserved = &mt('The name: "instcode" is a reserved category');
 2975:     my $communities_reserved = &mt('The name: "communities" is a reserved category');
 2976:     my $choose_again = '\\n'.&mt('Please use a different name for the new top level category');
 2977:     $output = <<"ENDSCRIPT";
 2978: <script type="text/javascript">
 2979: // <![CDATA[
 2980: function reorderCats(form,parent,item,idx) {
 2981:     var changedVal;
 2982: $jstext
 2983:     var newpos = 'addcategory_pos';
 2984:     var current = new Array;
 2985:     if (parent == '') {
 2986:         var has_instcode = 0;
 2987:         var maxtop = categories[idx].length;
 2988:         for (var j=0; j<maxtop; j++) {
 2989:             if (categories[idx][j] == 'instcode::0') {
 2990:                 has_instcode == 1;
 2991:             }
 2992:         }
 2993:         if (has_instcode == 0) {
 2994:             categories[idx][maxtop] = 'instcode_pos';
 2995:         }
 2996:     } else {
 2997:         newpos += '_'+parent;
 2998:     }
 2999:     var maxh = 1 + categories[idx].length;
 3000:     var current = new Array;
 3001:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3002:     if (item == newpos) {
 3003:         changedVal = newitemVal;
 3004:     } else {
 3005:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3006:         current[newitemVal] = newpos;
 3007:     }
 3008:     for (var i=0; i<categories[idx].length; i++) {
 3009:         var elementName = categories[idx][i];
 3010:         if (elementName != item) {
 3011:             if (form.elements[elementName]) {
 3012:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3013:                 current[currVal] = elementName;
 3014:             }
 3015:         }
 3016:     }
 3017:     var oldVal;
 3018:     for (var j=0; j<maxh; j++) {
 3019:         if (current[j] == undefined) {
 3020:             oldVal = j;
 3021:         }
 3022:     }
 3023:     if (oldVal < changedVal) {
 3024:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3025:            var elementName = current[k];
 3026:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3027:         }
 3028:     } else {
 3029:         for (var k=changedVal; k<oldVal; k++) {
 3030:             var elementName = current[k];
 3031:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3032:         }
 3033:     }
 3034:     return;
 3035: }
 3036: 
 3037: function categoryCheck(form) {
 3038:     if (form.elements['addcategory_name'].value == 'instcode') {
 3039:         alert('$instcode_reserved\\n$choose_again');
 3040:         return false;
 3041:     }
 3042:     if (form.elements['addcategory_name'].value == 'communities') {
 3043:         alert('$communities_reserved\\n$choose_again');
 3044:         return false;
 3045:     }
 3046:     return true;
 3047: }
 3048: 
 3049: // ]]>
 3050: </script>
 3051: 
 3052: ENDSCRIPT
 3053:     return $output;
 3054: }
 3055: 
 3056: sub initialize_categories {
 3057:     my ($itemcount) = @_;
 3058:     my ($datatable,$css_class,$chgstr);
 3059:     my %default_names = (
 3060:                       instcode    => 'Official courses (with institutional codes)',
 3061:                       communities => 'Communities',
 3062:                         );
 3063:     my $select0 = ' selected="selected"';
 3064:     my $select1 = '';
 3065:     foreach my $default ('instcode','communities') {
 3066:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3067:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 3068:         if ($default eq 'communities') {
 3069:             $select1 = $select0;
 3070:             $select0 = '';
 3071:         }
 3072:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 3073:                      .'<select name="'.$default.'_pos">'
 3074:                      .'<option value="0"'.$select0.'>1</option>'
 3075:                      .'<option value="1"'.$select1.'>2</option>'
 3076:                      .'<option value="2">3</option></select>&nbsp;'
 3077:                      .$default_names{$default}
 3078:                      .'</span></td><td><span class="LC_nobreak">'
 3079:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 3080:                      .&mt('Display').'</label>&nbsp;<label>'
 3081:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 3082:                  .'</label></span></td></tr>';
 3083:         $itemcount ++;
 3084:     }
 3085:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3086:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 3087:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 3088:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 3089:                   .'<option value="0">1</option>'
 3090:                   .'<option value="1">2</option>'
 3091:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 3092:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 3093:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 3094:     return $datatable;
 3095: }
 3096: 
 3097: sub build_category_rows {
 3098:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 3099:     my ($text,$name,$item,$chgstr);
 3100:     if (ref($cats) eq 'ARRAY') {
 3101:         my $maxdepth = scalar(@{$cats});
 3102:         if (ref($cats->[$depth]) eq 'HASH') {
 3103:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 3104:                 my $numchildren = @{$cats->[$depth]{$parent}};
 3105:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3106:                 $text .= '<td><table class="LC_datatable">';
 3107:                 my ($idxnum,$parent_name,$parent_item);
 3108:                 my $higher = $depth - 1;
 3109:                 if ($higher == 0) {
 3110:                     $parent_name = &escape($parent).'::'.$higher;
 3111:                 } else {
 3112:                     if (ref($path) eq 'ARRAY') {
 3113:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 3114:                     }
 3115:                 }
 3116:                 $parent_item = 'addcategory_pos_'.$parent_name;
 3117:                 for (my $j=0; $j<=$numchildren; $j++) {
 3118:                     if ($j < $numchildren) {
 3119:                         $name = $cats->[$depth]{$parent}[$j];
 3120:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 3121:                         $idxnum = $idx->{$item};
 3122:                     } else {
 3123:                         $name = $parent_name;
 3124:                         $item = $parent_item;
 3125:                     }
 3126:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 3127:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 3128:                     for (my $i=0; $i<=$numchildren; $i++) {
 3129:                         my $vpos = $i+1;
 3130:                         my $selstr;
 3131:                         if ($j == $i) {
 3132:                             $selstr = ' selected="selected" ';
 3133:                         }
 3134:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 3135:                     }
 3136:                     $text .= '</select>&nbsp;';
 3137:                     if ($j < $numchildren) {
 3138:                         my $deeper = $depth+1;
 3139:                         $text .= $name.'&nbsp;'
 3140:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 3141:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 3142:                         if(ref($path) eq 'ARRAY') {
 3143:                             push(@{$path},$name);
 3144:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 3145:                             pop(@{$path});
 3146:                         }
 3147:                     } else {
 3148:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 3149:                         if ($j == $numchildren) {
 3150:                             $text .= $name;
 3151:                         } else {
 3152:                             $text .= $item;
 3153:                         }
 3154:                         $text .= '" value="" />';
 3155:                     }
 3156:                     $text .= '</td></tr>';
 3157:                 }
 3158:                 $text .= '</table></td>';
 3159:             } else {
 3160:                 my $higher = $depth-1;
 3161:                 if ($higher == 0) {
 3162:                     $name = &escape($parent).'::'.$higher;
 3163:                 } else {
 3164:                     if (ref($path) eq 'ARRAY') {
 3165:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 3166:                     }
 3167:                 }
 3168:                 my $colspan;
 3169:                 if ($parent ne 'instcode') {
 3170:                     $colspan = $maxdepth - $depth - 1;
 3171:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 3172:                 }
 3173:             }
 3174:         }
 3175:     }
 3176:     return $text;
 3177: }
 3178: 
 3179: sub modifiable_userdata_row {
 3180:     my ($context,$role,$settings,$numinrow,$rowcount,$usertypes) = @_;
 3181:     my $rolename;
 3182:     if ($context eq 'selfcreate') {
 3183:         if (ref($usertypes) eq 'HASH') {
 3184:             $rolename = $usertypes->{$role};
 3185:         } else {
 3186:             $rolename = $role;
 3187:         }
 3188:     } else {
 3189:         if ($role eq 'cr') {
 3190:             $rolename = &mt('Custom role');
 3191:         } else {
 3192:             $rolename = &Apache::lonnet::plaintext($role);
 3193:         }
 3194:     }
 3195:     my @fields = ('lastname','firstname','middlename','generation',
 3196:                   'permanentemail','id');
 3197:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 3198:     my $output;
 3199:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 3200:     $output = '<tr '.$css_class.'>'.
 3201:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 3202:               '<td class="LC_left_item" colspan="2"><table>';
 3203:     my $rem;
 3204:     my %checks;
 3205:     if (ref($settings) eq 'HASH') {
 3206:         if (ref($settings->{$context}) eq 'HASH') {
 3207:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 3208:                 foreach my $field (@fields) {
 3209:                     if ($settings->{$context}->{$role}->{$field}) {
 3210:                         $checks{$field} = ' checked="checked" ';
 3211:                     }
 3212:                 }
 3213:             }
 3214:         }
 3215:     }
 3216:     for (my $i=0; $i<@fields; $i++) {
 3217:         my $rem = $i%($numinrow);
 3218:         if ($rem == 0) {
 3219:             if ($i > 0) {
 3220:                 $output .= '</tr>';
 3221:             }
 3222:             $output .= '<tr>';
 3223:         }
 3224:         my $check = ' ';
 3225:         if (exists($checks{$fields[$i]})) {
 3226:             $check = $checks{$fields[$i]}
 3227:         } else {
 3228:             if ($role eq 'st') {
 3229:                 if (ref($settings) ne 'HASH') {
 3230:                     $check = ' checked="checked" '; 
 3231:                 }
 3232:             }
 3233:         }
 3234:         $output .= '<td class="LC_left_item">'.
 3235:                    '<span class="LC_nobreak"><label>'.
 3236:                    '<input type="checkbox" name="canmodify_'.$role.'" '.
 3237:                    'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 3238:                    '</label></span></td>';
 3239:         $rem = @fields%($numinrow);
 3240:     }
 3241:     my $colsleft = $numinrow - $rem;
 3242:     if ($colsleft > 1 ) {
 3243:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3244:                    '&nbsp;</td>';
 3245:     } elsif ($colsleft == 1) {
 3246:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 3247:     }
 3248:     $output .= '</tr></table></td></tr>';
 3249:     return $output;
 3250: }
 3251: 
 3252: sub insttypes_row {
 3253:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context) = @_;
 3254:     my %lt = &Apache::lonlocal::texthash (
 3255:                       cansearch => 'Users allowed to search',
 3256:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 3257:              );
 3258:     my $showdom;
 3259:     if ($context eq 'cansearch') {
 3260:         $showdom = ' ('.$dom.')';
 3261:     }
 3262:     my $output =  '<tr class="LC_odd_row">'.
 3263:                   '<td>'.$lt{$context}.$showdom.
 3264:                   '</td><td class="LC_left_item" colspan="2"><table>';
 3265:     my $rem;
 3266:     if (ref($types) eq 'ARRAY') {
 3267:         for (my $i=0; $i<@{$types}; $i++) {
 3268:             if (defined($usertypes->{$types->[$i]})) {
 3269:                 my $rem = $i%($numinrow);
 3270:                 if ($rem == 0) {
 3271:                     if ($i > 0) {
 3272:                         $output .= '</tr>';
 3273:                     }
 3274:                     $output .= '<tr>';
 3275:                 }
 3276:                 my $check = ' ';
 3277:                 if (ref($settings) eq 'HASH') {
 3278:                     if (ref($settings->{$context}) eq 'ARRAY') {
 3279:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 3280:                             $check = ' checked="checked" ';
 3281:                         }
 3282:                     } elsif ($context eq 'statustocreate') {
 3283:                         $check = ' checked="checked" ';
 3284:                     }
 3285:                 }
 3286:                 $output .= '<td class="LC_left_item">'.
 3287:                            '<span class="LC_nobreak"><label>'.
 3288:                            '<input type="checkbox" name="'.$context.'" '.
 3289:                            'value="'.$types->[$i].'"'.$check.'/>'.
 3290:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 3291:             }
 3292:         }
 3293:        
 3294:         $rem = @{$types}%($numinrow);
 3295:     }
 3296:     my $colsleft = $numinrow - $rem;
 3297:     if ($colsleft > 1) {
 3298:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 3299:     } else {
 3300:         $output .= '<td class="LC_left_item">';
 3301:     }
 3302:     my $defcheck = ' ';
 3303:     if (ref($settings) eq 'HASH') {  
 3304:         if (ref($settings->{$context}) eq 'ARRAY') {
 3305:             if (grep(/^default$/,@{$settings->{$context}})) {
 3306:                 $defcheck = ' checked="checked" ';
 3307:             }
 3308:         } elsif ($context eq 'statustocreate') {
 3309:             $defcheck = ' checked="checked" ';
 3310:         }
 3311:     }
 3312:     $output .= '<span class="LC_nobreak"><label>'.
 3313:                '<input type="checkbox" name="'.$context.'" '.
 3314:                'value="default"'.$defcheck.'/>'.
 3315:                $othertitle.'</label></span></td>'.
 3316:                '</tr></table></td></tr>';
 3317:     return $output;
 3318: }
 3319: 
 3320: sub sorted_searchtitles {
 3321:     my %searchtitles = &Apache::lonlocal::texthash(
 3322:                          'uname' => 'username',
 3323:                          'lastname' => 'last name',
 3324:                          'lastfirst' => 'last name, first name',
 3325:                      );
 3326:     my @titleorder = ('uname','lastname','lastfirst');
 3327:     return (\%searchtitles,\@titleorder);
 3328: }
 3329: 
 3330: sub sorted_searchtypes {
 3331:     my %srchtypes_desc = (
 3332:                            exact    => 'is exact match',
 3333:                            contains => 'contains ..',
 3334:                            begins   => 'begins with ..',
 3335:                          );
 3336:     my @srchtypeorder = ('exact','begins','contains');
 3337:     return (\%srchtypes_desc,\@srchtypeorder);
 3338: }
 3339: 
 3340: sub usertype_update_row {
 3341:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 3342:     my $datatable;
 3343:     my $numinrow = 4;
 3344:     foreach my $type (@{$types}) {
 3345:         if (defined($usertypes->{$type})) {
 3346:             $$rownums ++;
 3347:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 3348:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 3349:                           '</td><td class="LC_left_item"><table>';
 3350:             for (my $i=0; $i<@{$fields}; $i++) {
 3351:                 my $rem = $i%($numinrow);
 3352:                 if ($rem == 0) {
 3353:                     if ($i > 0) {
 3354:                         $datatable .= '</tr>';
 3355:                     }
 3356:                     $datatable .= '<tr>';
 3357:                 }
 3358:                 my $check = ' ';
 3359:                 if (ref($settings) eq 'HASH') {
 3360:                     if (ref($settings->{'fields'}) eq 'HASH') {
 3361:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 3362:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 3363:                                 $check = ' checked="checked" ';
 3364:                             }
 3365:                         }
 3366:                     }
 3367:                 }
 3368: 
 3369:                 if ($i == @{$fields}-1) {
 3370:                     my $colsleft = $numinrow - $rem;
 3371:                     if ($colsleft > 1) {
 3372:                         $datatable .= '<td colspan="'.$colsleft.'">';
 3373:                     } else {
 3374:                         $datatable .= '<td>';
 3375:                     }
 3376:                 } else {
 3377:                     $datatable .= '<td>';
 3378:                 }
 3379:                 $datatable .= '<span class="LC_nobreak"><label>'.
 3380:                               '<input type="checkbox" name="updateable_'.$type.
 3381:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 3382:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 3383:             }
 3384:             $datatable .= '</tr></table></td></tr>';
 3385:         }
 3386:     }
 3387:     return $datatable;
 3388: }
 3389: 
 3390: sub modify_login {
 3391:     my ($r,$dom,$confname,%domconfig) = @_;
 3392:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges);
 3393:     my %title = ( coursecatalog => 'Display course catalog',
 3394:                   adminmail => 'Display administrator E-mail address',
 3395:                   newuser => 'Link for visitors to create a user account',
 3396:                   loginheader => 'Log-in box header');
 3397:     my @offon = ('off','on');
 3398:     my %curr_loginvia;
 3399:     if (ref($domconfig{login}) eq 'HASH') {
 3400:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 3401:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 3402:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 3403:             }
 3404:         }
 3405:     }
 3406:     my %loginhash;
 3407:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 3408:                                            \%domconfig,\%loginhash);
 3409:     my @toggles = ('coursecatalog','adminmail','newuser');
 3410:     foreach my $item (@toggles) {
 3411:         $loginhash{login}{$item} = $env{'form.'.$item};
 3412:     }
 3413:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 3414:     if (ref($colchanges{'login'}) eq 'HASH') {  
 3415:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 3416:                                          \%loginhash);
 3417:     }
 3418: 
 3419:     my %servers = &dom_servers($dom);
 3420:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 3421:     if (keys(%servers) > 1) {
 3422:         foreach my $lonhost (keys(%servers)) {
 3423:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 3424:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 3425:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 3426:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 3427:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 3428:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 3429:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 3430:                         $changes{'loginvia'}{$lonhost} = 1;
 3431:                     } else {
 3432:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 3433:                         $changes{'loginvia'}{$lonhost} = 1;
 3434:                     }
 3435:                 } else {
 3436:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 3437:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 3438:                         $changes{'loginvia'}{$lonhost} = 1;
 3439:                     }
 3440:                 }
 3441:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 3442:                     foreach my $item (@loginvia_attribs) {
 3443:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 3444:                     }
 3445:                 } else {
 3446:                     foreach my $item (@loginvia_attribs) {
 3447:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 3448:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 3449:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 3450:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 3451:                                 $new = '/';
 3452:                             }
 3453:                         }
 3454:                         if (($item eq 'custompath') &&
 3455:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 3456:                             $new = '';
 3457:                         }
 3458:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 3459:                             $changes{'loginvia'}{$lonhost} = 1;
 3460:                         }
 3461:                         if ($item eq 'exempt') {
 3462:                             $new =~ s/^\s+//;
 3463:                             $new =~ s/\s+$//;
 3464:                             my @poss_ips = split(/\s*[,:]\s*/,$new);
 3465:                             my @okips;
 3466:                             foreach my $ip (@poss_ips) {
 3467:                                 if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 3468:                                     if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 3469:                                         push(@okips,$ip);
 3470:                                     }
 3471:                                 }
 3472:                             }
 3473:                             if (@okips > 0) {
 3474:                                 $new = join(',',@okips);
 3475:                             } else {
 3476:                                 $new = '';
 3477:                             }
 3478:                         }
 3479: 
 3480:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 3481:                     }
 3482:                 }
 3483:             } else {
 3484:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 3485:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 3486:                     $changes{'loginvia'}{$lonhost} = 1;
 3487:                     foreach my $item (@loginvia_attribs) {
 3488:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 3489:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 3490:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 3491:                                 $new = '/';
 3492:                             }
 3493:                         }
 3494:                         if (($item eq 'custompath') &&
 3495:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 3496:                             $new = '';
 3497:                         }
 3498:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 3499:                     }
 3500:                 }
 3501:             }
 3502:         }
 3503:     }
 3504: 
 3505:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 3506:                                              $dom);
 3507:     if ($putresult eq 'ok') {
 3508:         my @toggles = ('coursecatalog','adminmail','newuser');
 3509:         my %defaultchecked = (
 3510:                     'coursecatalog' => 'on',
 3511:                     'adminmail'     => 'off',
 3512:                     'newuser'       => 'off',
 3513:         );
 3514:         if (ref($domconfig{'login'}) eq 'HASH') {
 3515:             foreach my $item (@toggles) {
 3516:                 if ($defaultchecked{$item} eq 'on') { 
 3517:                     if (($domconfig{'login'}{$item} eq '0') &&
 3518:                         ($env{'form.'.$item} eq '1')) {
 3519:                         $changes{$item} = 1;
 3520:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 3521:                               $domconfig{'login'}{$item} eq '1') &&
 3522:                              ($env{'form.'.$item} eq '0')) {
 3523:                         $changes{$item} = 1;
 3524:                     }
 3525:                 } elsif ($defaultchecked{$item} eq 'off') {
 3526:                     if (($domconfig{'login'}{$item} eq '1') &&
 3527:                         ($env{'form.'.$item} eq '0')) {
 3528:                         $changes{$item} = 1;
 3529:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 3530:                               $domconfig{'login'}{$item} eq '0') &&
 3531:                              ($env{'form.'.$item} eq '1')) {
 3532:                         $changes{$item} = 1;
 3533:                     }
 3534:                 }
 3535:             }
 3536:             if (($domconfig{'login'}{'loginheader'} eq 'text') && 
 3537:                 ($env{'form.loginheader'} eq 'image')) {
 3538:                 $changes{'loginheader'} = 1;
 3539:             } elsif (($domconfig{'login'}{'loginheader'} eq '' ||
 3540:                       $domconfig{'login'}{'loginheader'} eq 'image') &&
 3541:                      ($env{'form.loginheader'} eq 'text')) {
 3542:                 $changes{'loginheader'} = 1;
 3543:             }
 3544:         }
 3545:         if (keys(%changes) > 0 || $colchgtext) {
 3546:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 3547:             $resulttext = &mt('Changes made:').'<ul>';
 3548:             foreach my $item (sort(keys(%changes))) {
 3549:                 if ($item eq 'loginheader') {
 3550:                     $resulttext .= '<li>'.&mt("$title{$item} set to $env{'form.loginheader'}").'</li>';
 3551:                 } elsif ($item eq 'loginvia') {
 3552:                     if (ref($changes{$item}) eq 'HASH') {
 3553:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 3554:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 3555:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 3556:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 3557:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 3558:                                     $protocol = 'http' if ($protocol ne 'https');
 3559:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 3560: 
 3561:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 3562:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 3563:                                     } else {
 3564:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'};
 3565:                                     }
 3566:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 3567:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 3568:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 3569:                                     }
 3570:                                     $resulttext .= '</li>';
 3571:                                 } else {
 3572:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 3573:                                 }
 3574:                             } else {
 3575:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 3576:                             }
 3577:                         }
 3578:                         $resulttext .= '</ul></li>';
 3579:                     }
 3580:                 } else {
 3581:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 3582:                 }
 3583:             }
 3584:             $resulttext .= $colchgtext.'</ul>';
 3585:         } else {
 3586:             $resulttext = &mt('No changes made to log-in page settings');
 3587:         }
 3588:     } else {
 3589:         $resulttext = '<span class="LC_error">'.
 3590: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 3591:     }
 3592:     if ($errors) {
 3593:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 3594:                        $errors.'</ul>';
 3595:     }
 3596:     return $resulttext;
 3597: }
 3598: 
 3599: sub color_font_choices {
 3600:     my %choices =
 3601:         &Apache::lonlocal::texthash (
 3602:             img => "Header",
 3603:             bgs => "Background colors",
 3604:             links => "Link colors",
 3605:             images => "Images",
 3606:             font => "Font color",
 3607:             pgbg => "Page",
 3608:             tabbg => "Header",
 3609:             sidebg => "Border",
 3610:             link => "Link",
 3611:             alink => "Active link",
 3612:             vlink => "Visited link",
 3613:         );
 3614:     return %choices;
 3615: }
 3616: 
 3617: sub modify_rolecolors {
 3618:     my ($r,$dom,$confname,$roles,%domconfig) = @_;
 3619:     my ($resulttext,%rolehash);
 3620:     $rolehash{'rolecolors'} = {};
 3621:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 3622:         if ($domconfig{'rolecolors'} eq '') {
 3623:             $domconfig{'rolecolors'} = {};
 3624:         }
 3625:     }
 3626:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 3627:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 3628:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 3629:                                              $dom);
 3630:     if ($putresult eq 'ok') {
 3631:         if (keys(%changes) > 0) {
 3632:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 3633:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 3634:                                              $rolehash{'rolecolors'});
 3635:         } else {
 3636:             $resulttext = &mt('No changes made to default color schemes');
 3637:         }
 3638:     } else {
 3639:         $resulttext = '<span class="LC_error">'.
 3640: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 3641:     }
 3642:     if ($errors) {
 3643:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 3644:                        $errors.'</ul>';
 3645:     }
 3646:     return $resulttext;
 3647: }
 3648: 
 3649: sub modify_colors {
 3650:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 3651:     my (%changes,%choices);
 3652:     my @bgs;
 3653:     my @links = ('link','alink','vlink');
 3654:     my @logintext;
 3655:     my @images;
 3656:     my $servadm = $r->dir_config('lonAdmEMail');
 3657:     my $errors;
 3658:     foreach my $role (@{$roles}) {
 3659:         if ($role eq 'login') {
 3660:             %choices = &login_choices();
 3661:             @logintext = ('textcol','bgcol');
 3662:         } else {
 3663:             %choices = &color_font_choices();
 3664:         }
 3665:         if ($role eq 'login') {
 3666:             @images = ('img','logo','domlogo','login');
 3667:             @bgs = ('pgbg','mainbg','sidebg');
 3668:         } else {
 3669:             @images = ('img');
 3670:             @bgs = ('pgbg','tabbg','sidebg'); 
 3671:         }
 3672:         $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 3673:         foreach my $item (@bgs,@links,@logintext) {
 3674:             $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 3675:         }
 3676:         my ($configuserok,$author_ok,$switchserver) = 
 3677:             &config_check($dom,$confname,$servadm);
 3678:         my ($width,$height) = &thumb_dimensions();
 3679:         if (ref($domconfig->{$role}) ne 'HASH') {
 3680:             $domconfig->{$role} = {};
 3681:         }
 3682:         foreach my $img (@images) {
 3683:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 3684:                 if (defined($env{'form.login_showlogo_'.$img})) {
 3685:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 3686:                 } else { 
 3687:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 3688:                 }
 3689:             } 
 3690: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 3691: 		 && !defined($domconfig->{$role}{$img})
 3692: 		 && !$env{'form.'.$role.'_del_'.$img}
 3693: 		 && $env{'form.'.$role.'_import_'.$img}) {
 3694: 		# import the old configured image from the .tab setting
 3695: 		# if they haven't provided a new one 
 3696: 		$domconfig->{$role}{$img} = 
 3697: 		    $env{'form.'.$role.'_import_'.$img};
 3698: 	    }
 3699:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 3700:                 my $error;
 3701:                 if ($configuserok eq 'ok') {
 3702:                     if ($switchserver) {
 3703:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 3704:                     } else {
 3705:                         if ($author_ok eq 'ok') {
 3706:                             my ($result,$logourl) = 
 3707:                                 &publishlogo($r,'upload',$role.'_'.$img,
 3708:                                            $dom,$confname,$img,$width,$height);
 3709:                             if ($result eq 'ok') {
 3710:                                 $confhash->{$role}{$img} = $logourl;
 3711:                                 $changes{$role}{'images'}{$img} = 1;
 3712:                             } else {
 3713:                                 $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);
 3714:                             }
 3715:                         } else {
 3716:                             $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);
 3717:                         }
 3718:                     }
 3719:                 } else {
 3720:                     $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);
 3721:                 }
 3722:                 if ($error) {
 3723:                     &Apache::lonnet::logthis($error);
 3724:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 3725:                 }
 3726:             } elsif ($domconfig->{$role}{$img} ne '') {
 3727:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 3728:                     my $error;
 3729:                     if ($configuserok eq 'ok') {
 3730: # is confname an author?
 3731:                         if ($switchserver eq '') {
 3732:                             if ($author_ok eq 'ok') {
 3733:                                 my ($result,$logourl) = 
 3734:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 3735:                                             $dom,$confname,$img,$width,$height);
 3736:                                 if ($result eq 'ok') {
 3737:                                     $confhash->{$role}{$img} = $logourl;
 3738: 				    $changes{$role}{'images'}{$img} = 1;
 3739:                                 }
 3740:                             }
 3741:                         }
 3742:                     }
 3743:                 }
 3744:             }
 3745:         }
 3746:         if (ref($domconfig) eq 'HASH') {
 3747:             if (ref($domconfig->{$role}) eq 'HASH') {
 3748:                 foreach my $img (@images) {
 3749:                     if ($domconfig->{$role}{$img} ne '') {
 3750:                         if ($env{'form.'.$role.'_del_'.$img}) {
 3751:                             $confhash->{$role}{$img} = '';
 3752:                             $changes{$role}{'images'}{$img} = 1;
 3753:                         } else {
 3754:                             if ($confhash->{$role}{$img} eq '') {
 3755:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 3756:                             }
 3757:                         }
 3758:                     } else {
 3759:                         if ($env{'form.'.$role.'_del_'.$img}) {
 3760:                             $confhash->{$role}{$img} = '';
 3761:                             $changes{$role}{'images'}{$img} = 1;
 3762:                         } 
 3763:                     }
 3764:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 3765:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 3766:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 3767:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 3768:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 3769:                             }
 3770:                         } else {
 3771:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 3772:                                 $changes{$role}{'showlogo'}{$img} = 1;
 3773:                             }
 3774:                         }
 3775:                     }
 3776:                 }
 3777:                 if ($domconfig->{$role}{'font'} ne '') {
 3778:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 3779:                         $changes{$role}{'font'} = 1;
 3780:                     }
 3781:                 } else {
 3782:                     if ($confhash->{$role}{'font'}) {
 3783:                         $changes{$role}{'font'} = 1;
 3784:                     }
 3785:                 }
 3786:                 foreach my $item (@bgs) {
 3787:                     if ($domconfig->{$role}{$item} ne '') {
 3788:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 3789:                             $changes{$role}{'bgs'}{$item} = 1;
 3790:                         } 
 3791:                     } else {
 3792:                         if ($confhash->{$role}{$item}) {
 3793:                             $changes{$role}{'bgs'}{$item} = 1;
 3794:                         }
 3795:                     }
 3796:                 }
 3797:                 foreach my $item (@links) {
 3798:                     if ($domconfig->{$role}{$item} ne '') {
 3799:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 3800:                             $changes{$role}{'links'}{$item} = 1;
 3801:                         }
 3802:                     } else {
 3803:                         if ($confhash->{$role}{$item}) {
 3804:                             $changes{$role}{'links'}{$item} = 1;
 3805:                         }
 3806:                     }
 3807:                 }
 3808:                 foreach my $item (@logintext) {
 3809:                     if ($domconfig->{$role}{$item} ne '') {
 3810:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 3811:                             $changes{$role}{'logintext'}{$item} = 1;
 3812:                         }
 3813:                     } else {
 3814:                         if ($confhash->{$role}{$item}) {
 3815:                             $changes{$role}{'logintext'}{$item} = 1;
 3816:                         }
 3817:                     }
 3818:                 }
 3819:             } else {
 3820:                 &default_change_checker($role,\@images,\@links,\@bgs,
 3821:                                         \@logintext,$confhash,\%changes); 
 3822:             }
 3823:         } else {
 3824:             &default_change_checker($role,\@images,\@links,\@bgs,
 3825:                                     \@logintext,$confhash,\%changes); 
 3826:         }
 3827:     }
 3828:     return ($errors,%changes);
 3829: }
 3830: 
 3831: sub config_check {
 3832:     my ($dom,$confname,$servadm) = @_;
 3833:     my ($configuserok,$author_ok,$switchserver,%currroles);
 3834:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 3835:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 3836:                                                    $confname,$servadm);
 3837:     if ($configuserok eq 'ok') {
 3838:         $switchserver = &check_switchserver($dom,$confname);
 3839:         if ($switchserver eq '') {
 3840:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 3841:         }
 3842:     }
 3843:     return ($configuserok,$author_ok,$switchserver);
 3844: }
 3845: 
 3846: sub default_change_checker {
 3847:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 3848:     foreach my $item (@{$links}) {
 3849:         if ($confhash->{$role}{$item}) {
 3850:             $changes->{$role}{'links'}{$item} = 1;
 3851:         }
 3852:     }
 3853:     foreach my $item (@{$bgs}) {
 3854:         if ($confhash->{$role}{$item}) {
 3855:             $changes->{$role}{'bgs'}{$item} = 1;
 3856:         }
 3857:     }
 3858:     foreach my $item (@{$logintext}) {
 3859:         if ($confhash->{$role}{$item}) {
 3860:             $changes->{$role}{'logintext'}{$item} = 1;
 3861:         }
 3862:     }
 3863:     foreach my $img (@{$images}) {
 3864:         if ($env{'form.'.$role.'_del_'.$img}) {
 3865:             $confhash->{$role}{$img} = '';
 3866:             $changes->{$role}{'images'}{$img} = 1;
 3867:         }
 3868:         if ($role eq 'login') {
 3869:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 3870:                 $changes->{$role}{'showlogo'}{$img} = 1;
 3871:             }
 3872:         }
 3873:     }
 3874:     if ($confhash->{$role}{'font'}) {
 3875:         $changes->{$role}{'font'} = 1;
 3876:     }
 3877: }
 3878: 
 3879: sub display_colorchgs {
 3880:     my ($dom,$changes,$roles,$confhash) = @_;
 3881:     my (%choices,$resulttext);
 3882:     if (!grep(/^login$/,@{$roles})) {
 3883:         $resulttext = &mt('Changes made:').'<br />';
 3884:     }
 3885:     foreach my $role (@{$roles}) {
 3886:         if ($role eq 'login') {
 3887:             %choices = &login_choices();
 3888:         } else {
 3889:             %choices = &color_font_choices();
 3890:         }
 3891:         if (ref($changes->{$role}) eq 'HASH') {
 3892:             if ($role ne 'login') {
 3893:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 3894:             }
 3895:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 3896:                 if ($role ne 'login') {
 3897:                     $resulttext .= '<ul>';
 3898:                 }
 3899:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 3900:                     if ($role ne 'login') {
 3901:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 3902:                     }
 3903:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 3904:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 3905:                             if ($confhash->{$role}{$key}{$item}) {
 3906:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 3907:                             } else {
 3908:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 3909:                             }
 3910:                         } elsif ($confhash->{$role}{$item} eq '') {
 3911:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 3912:                         } else {
 3913:                             my $newitem = $confhash->{$role}{$item};
 3914:                             if ($key eq 'images') {
 3915:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 3916:                             }
 3917:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 3918:                         }
 3919:                     }
 3920:                     if ($role ne 'login') {
 3921:                         $resulttext .= '</ul></li>';
 3922:                     }
 3923:                 } else {
 3924:                     if ($confhash->{$role}{$key} eq '') {
 3925:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 3926:                     } else {
 3927:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 3928:                     }
 3929:                 }
 3930:                 if ($role ne 'login') {
 3931:                     $resulttext .= '</ul>';
 3932:                 }
 3933:             }
 3934:         }
 3935:     }
 3936:     return $resulttext;
 3937: }
 3938: 
 3939: sub thumb_dimensions {
 3940:     return ('200','50');
 3941: }
 3942: 
 3943: sub check_dimensions {
 3944:     my ($inputfile) = @_;
 3945:     my ($fullwidth,$fullheight);
 3946:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 3947:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 3948:             my $imageinfo = <PIPE>;
 3949:             if (!close(PIPE)) {
 3950:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 3951:             }
 3952:             chomp($imageinfo);
 3953:             my ($fullsize) = 
 3954:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 3955:             if ($fullsize) {
 3956:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 3957:             }
 3958:         }
 3959:     }
 3960:     return ($fullwidth,$fullheight);
 3961: }
 3962: 
 3963: sub check_configuser {
 3964:     my ($uhome,$dom,$confname,$servadm) = @_;
 3965:     my ($configuserok,%currroles);
 3966:     if ($uhome eq 'no_host') {
 3967:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 3968:         my $configpass = &LONCAPA::Enrollment::create_password();
 3969:         $configuserok = 
 3970:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 3971:                              $configpass,'','','','','',undef,$servadm);
 3972:     } else {
 3973:         $configuserok = 'ok';
 3974:         %currroles = 
 3975:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 3976:     }
 3977:     return ($configuserok,%currroles);
 3978: }
 3979: 
 3980: sub check_authorstatus {
 3981:     my ($dom,$confname,%currroles) = @_;
 3982:     my $author_ok;
 3983:     if (!$currroles{':'.$dom.':au'}) {
 3984:         my $start = time;
 3985:         my $end = 0;
 3986:         $author_ok = 
 3987:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 3988:                                         'au',$end,$start,'','','domconfig');
 3989:     } else {
 3990:         $author_ok = 'ok';
 3991:     }
 3992:     return $author_ok;
 3993: }
 3994: 
 3995: sub publishlogo {
 3996:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 3997:     my ($output,$fname,$logourl);
 3998:     if ($action eq 'upload') {
 3999:         $fname=$env{'form.'.$formname.'.filename'};
 4000:         chop($env{'form.'.$formname});
 4001:     } else {
 4002:         ($fname) = ($formname =~ /([^\/]+)$/);
 4003:     }
 4004:     if ($savefileas ne '') {
 4005:         $fname = $savefileas;
 4006:     }
 4007:     $fname=&Apache::lonnet::clean_filename($fname);
 4008: # See if there is anything left
 4009:     unless ($fname) { return ('error: no uploaded file'); }
 4010:     $fname="$subdir/$fname";
 4011:     my $filepath='/home/'.$confname.'/public_html';
 4012:     my ($fnamepath,$file,$fetchthumb);
 4013:     $file=$fname;
 4014:     if ($fname=~m|/|) {
 4015:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 4016:     }
 4017:     my @parts=split(/\//,$filepath.'/'.$fnamepath);
 4018:     my $count;
 4019:     for ($count=4;$count<=$#parts;$count++) {
 4020:         $filepath.="/$parts[$count]";
 4021:         if ((-e $filepath)!=1) {
 4022:             mkdir($filepath,02770);
 4023:         }
 4024:     }
 4025:     # Check for bad extension and disallow upload
 4026:     if ($file=~/\.(\w+)$/ &&
 4027:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 4028:         $output = 
 4029:             &mt('Invalid file extension ([_1]) - reserved for LONCAPA use.',$1); 
 4030:     } elsif ($file=~/\.(\w+)$/ &&
 4031:         !defined(&Apache::loncommon::fileembstyle($1))) {
 4032:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 4033:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 4034:         $output = &mt('File name not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 4035:     } elsif (-d "$filepath/$file") {
 4036:         $output = &mt('File name is a directory name - rename the file and re-upload');
 4037:     } else {
 4038:         my $source = $filepath.'/'.$file;
 4039:         my $logfile;
 4040:         if (!open($logfile,">>$source".'.log')) {
 4041:             return (&mt('No write permission to Construction Space'));
 4042:         }
 4043:         print $logfile
 4044: "\n================= Publish ".localtime()." ================\n".
 4045: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 4046: # Save the file
 4047:         if (!open(FH,'>'.$source)) {
 4048:             &Apache::lonnet::logthis('Failed to create '.$source);
 4049:             return (&mt('Failed to create file'));
 4050:         }
 4051:         if ($action eq 'upload') {
 4052:             if (!print FH ($env{'form.'.$formname})) {
 4053:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 4054:                 return (&mt('Failed to write file'));
 4055:             }
 4056:         } else {
 4057:             my $original = &Apache::lonnet::filelocation('',$formname);
 4058:             if(!copy($original,$source)) {
 4059:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 4060:                 return (&mt('Failed to write file'));
 4061:             }
 4062:         }
 4063:         close(FH);
 4064:         chmod(0660, $source); # Permissions to rw-rw---.
 4065: 
 4066:         my $docroot=$r->dir_config('lonDocRoot');
 4067:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 4068:         my $copyfile=$targetdir.'/'.$file;
 4069: 
 4070:         my @parts=split(/\//,$targetdir);
 4071:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 4072:         for (my $count=5;$count<=$#parts;$count++) {
 4073:             $path.="/$parts[$count]";
 4074:             if (!-e $path) {
 4075:                 print $logfile "\nCreating directory ".$path;
 4076:                 mkdir($path,02770);
 4077:             }
 4078:         }
 4079:         my $versionresult;
 4080:         if (-e $copyfile) {
 4081:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 4082:         } else {
 4083:             $versionresult = 'ok';
 4084:         }
 4085:         if ($versionresult eq 'ok') {
 4086:             if (copy($source,$copyfile)) {
 4087:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 4088:                 $output = 'ok';
 4089:                 &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 4090:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 4091:             } else {
 4092:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 4093:                 $output = &mt('Failed to copy file to RES space').", $!";
 4094:             }
 4095:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 4096:                 my $inputfile = $filepath.'/'.$file;
 4097:                 my $outfile = $filepath.'/'.'tn-'.$file;
 4098:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 4099:                 if ($fullwidth ne '' && $fullheight ne '') { 
 4100:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 4101:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 4102:                         system("convert -sample $thumbsize $inputfile $outfile");
 4103:                         chmod(0660, $filepath.'/tn-'.$file);
 4104:                         if (-e $outfile) {
 4105:                             my $copyfile=$targetdir.'/tn-'.$file;
 4106:                             if (copy($outfile,$copyfile)) {
 4107:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 4108:                                 &write_metadata($dom,$confname,$formname,
 4109:                                                 $targetdir,'tn-'.$file,$logfile);
 4110:                             } else {
 4111:                                 print $logfile "\nUnable to write ".$copyfile.
 4112:                                                ':'.$!."\n";
 4113:                             }
 4114:                         }
 4115:                     }
 4116:                 }
 4117:             }
 4118:         } else {
 4119:             $output = $versionresult;
 4120:         }
 4121:     }
 4122:     return ($output,$logourl);
 4123: }
 4124: 
 4125: sub logo_versioning {
 4126:     my ($targetdir,$file,$logfile) = @_;
 4127:     my $target = $targetdir.'/'.$file;
 4128:     my ($maxversion,$fn,$extn,$output);
 4129:     $maxversion = 0;
 4130:     if ($file =~ /^(.+)\.(\w+)$/) {
 4131:         $fn=$1;
 4132:         $extn=$2;
 4133:     }
 4134:     opendir(DIR,$targetdir);
 4135:     while (my $filename=readdir(DIR)) {
 4136:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 4137:             $maxversion=($1>$maxversion)?$1:$maxversion;
 4138:         }
 4139:     }
 4140:     $maxversion++;
 4141:     print $logfile "\nCreating old version ".$maxversion."\n";
 4142:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 4143:     if (copy($target,$copyfile)) {
 4144:         print $logfile "Copied old target to ".$copyfile."\n";
 4145:         $copyfile=$copyfile.'.meta';
 4146:         if (copy($target.'.meta',$copyfile)) {
 4147:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 4148:             $output = 'ok';
 4149:         } else {
 4150:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 4151:             $output = &mt('Failed to copy old meta').", $!, ";
 4152:         }
 4153:     } else {
 4154:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 4155:         $output = &mt('Failed to copy old target').", $!, ";
 4156:     }
 4157:     return $output;
 4158: }
 4159: 
 4160: sub write_metadata {
 4161:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 4162:     my (%metadatafields,%metadatakeys,$output);
 4163:     $metadatafields{'title'}=$formname;
 4164:     $metadatafields{'creationdate'}=time;
 4165:     $metadatafields{'lastrevisiondate'}=time;
 4166:     $metadatafields{'copyright'}='public';
 4167:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 4168:                                          $env{'user.domain'};
 4169:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 4170:     $metadatafields{'domain'}=$dom;
 4171:     {
 4172:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 4173:         my $mfh;
 4174:         unless (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 4175:             $output = &mt('Could not write metadata');
 4176:         }
 4177:         foreach (sort keys %metadatafields) {
 4178:             unless ($_=~/\./) {
 4179:                 my $unikey=$_;
 4180:                 $unikey=~/^([A-Za-z]+)/;
 4181:                 my $tag=$1;
 4182:                 $tag=~tr/A-Z/a-z/;
 4183:                 print $mfh "\n\<$tag";
 4184:                 foreach (split(/\,/,$metadatakeys{$unikey})) {
 4185:                     my $value=$metadatafields{$unikey.'.'.$_};
 4186:                     $value=~s/\"/\'\'/g;
 4187:                     print $mfh ' '.$_.'="'.$value.'"';
 4188:                 }
 4189:                 print $mfh '>'.
 4190:                     &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 4191:                         .'</'.$tag.'>';
 4192:             }
 4193:         }
 4194:         $output = 'ok';
 4195:         print $logfile "\nWrote metadata";
 4196:         close($mfh);
 4197:     }
 4198: }
 4199: 
 4200: sub check_switchserver {
 4201:     my ($dom,$confname) = @_;
 4202:     my ($allowed,$switchserver);
 4203:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 4204:     if ($home eq 'no_host') {
 4205:         $home = &Apache::lonnet::domain($dom,'primary');
 4206:     }
 4207:     my @ids=&Apache::lonnet::current_machine_ids();
 4208:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 4209:     if (!$allowed) {
 4210: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&role=dc./'.$dom.'/">'.&mt('Switch Server').'</a>';
 4211:     }
 4212:     return $switchserver;
 4213: }
 4214: 
 4215: sub modify_quotas {
 4216:     my ($dom,$action,%domconfig) = @_;
 4217:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 4218:         %limithash,$toolregexp,%conditions,$resulttext,%changes);
 4219:     if ($action eq 'quotas') {
 4220:         $context = 'tools'; 
 4221:     } else { 
 4222:         $context = $action;
 4223:     }
 4224:     if ($context eq 'requestcourses') {
 4225:         @usertools = ('official','unofficial','community');
 4226:         @options =('norequest','approval','validate','autolimit');
 4227:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 4228:         %titles = &courserequest_titles();
 4229:         $toolregexp = join('|',@usertools);
 4230:         %conditions = &courserequest_conditions();
 4231:     } else {
 4232:         @usertools = ('aboutme','blog','portfolio');
 4233:         %titles = &tool_titles();
 4234:     }
 4235:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 4236:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4237:     foreach my $key (keys(%env)) {
 4238:         if ($context eq 'requestcourses') {
 4239:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 4240:                 my $item = $1;
 4241:                 my $type = $2;
 4242:                 if ($type =~ /^limit_(.+)/) {
 4243:                     $limithash{$item}{$1} = $env{$key};
 4244:                 } else {
 4245:                     $confhash{$item}{$type} = $env{$key};
 4246:                 }
 4247:             }
 4248:         } else {
 4249:             if ($key =~ /^form\.quota_(.+)$/) {
 4250:                 $confhash{'defaultquota'}{$1} = $env{$key};
 4251:             }
 4252:             if ($key =~ /^form\.\Q$context\E_(.+)$/) {
 4253:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 4254:             }
 4255:         }
 4256:     }
 4257:     if ($context eq 'requestcourses') {
 4258:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.reqapprovalnotify');
 4259:         @approvalnotify = sort(@approvalnotify);
 4260:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 4261:         if (ref($domconfig{$action}) eq 'HASH') {
 4262:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 4263:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 4264:                     $changes{'notify'}{'approval'} = 1;
 4265:                 }
 4266:             } else {
 4267:                 if ($domconfig{$action}{'notify'}{'approval'}) {
 4268:                     $changes{'notify'}{'approval'} = 1;
 4269:                 }
 4270:             }
 4271:         } else {
 4272:             if ($domconfig{$action}{'notify'}{'approval'}) {
 4273:                 $changes{'notify'}{'approval'} = 1;
 4274:             }
 4275:         }
 4276:     } else {
 4277:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 4278:     }
 4279:     foreach my $item (@usertools) {
 4280:         foreach my $type (@{$types},'default','_LC_adv') {
 4281:             my $unset;
 4282:             if ($context eq 'requestcourses') {
 4283:                 $unset = '0';
 4284:                 if ($type eq '_LC_adv') {
 4285:                     $unset = '';
 4286:                 }
 4287:                 if ($confhash{$item}{$type} eq 'autolimit') {
 4288:                     $confhash{$item}{$type} .= '=';
 4289:                     unless ($limithash{$item}{$type} =~ /\D/) {
 4290:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 4291:                     }
 4292:                 }
 4293:             } else {
 4294:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 4295:                     $confhash{$item}{$type} = 1;
 4296:                 } else {
 4297:                     $confhash{$item}{$type} = 0;
 4298:                 }
 4299:             }
 4300:             if (ref($domconfig{$action}) eq 'HASH') {
 4301:                 if (ref($domconfig{$action}{$item}) eq 'HASH') {
 4302:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 4303:                         $changes{$item}{$type} = 1;
 4304:                     }
 4305:                 } else {
 4306:                     if ($context eq 'requestcourses') {
 4307:                         if ($confhash{$item}{$type} ne $unset) {
 4308:                             $changes{$item}{$type} = 1;
 4309:                         }
 4310:                     } else {
 4311:                         if (!$confhash{$item}{$type}) {
 4312:                             $changes{$item}{$type} = 1;
 4313:                         }
 4314:                     }
 4315:                 }
 4316:             } else {
 4317:                 if ($context eq 'requestcourses') {
 4318:                     if ($confhash{$item}{$type} ne $unset) {
 4319:                         $changes{$item}{$type} = 1;
 4320:                     }
 4321:                 } else {
 4322:                     if (!$confhash{$item}{$type}) {
 4323:                         $changes{$item}{$type} = 1;
 4324:                     }
 4325:                 }
 4326:             }
 4327:         }
 4328:     }
 4329:     unless ($context eq 'requestcourses') {
 4330:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 4331:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 4332:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 4333:                     if (exists($confhash{'defaultquota'}{$key})) {
 4334:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 4335:                             $changes{'defaultquota'}{$key} = 1;
 4336:                         }
 4337:                     } else {
 4338:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 4339:                     }
 4340:                 }
 4341:             } else {
 4342:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 4343:                     if (exists($confhash{'defaultquota'}{$key})) {
 4344:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 4345:                             $changes{'defaultquota'}{$key} = 1;
 4346:                         }
 4347:                     } else {
 4348:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 4349:                     }
 4350:                 }
 4351:             }
 4352:         }
 4353:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 4354:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 4355:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 4356:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 4357:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 4358:                             $changes{'defaultquota'}{$key} = 1;
 4359:                         }
 4360:                     } else {
 4361:                         if (!exists($domconfig{'quotas'}{$key})) {
 4362:                             $changes{'defaultquota'}{$key} = 1;
 4363:                         }
 4364:                     }
 4365:                 } else {
 4366:                     $changes{'defaultquota'}{$key} = 1;
 4367:                 }
 4368:             }
 4369:         }
 4370:     }
 4371: 
 4372:     foreach my $key (keys(%confhash)) {
 4373:         $domdefaults{$key} = $confhash{$key};
 4374:     }
 4375:    
 4376:     my %quotahash = (
 4377:                       $action => { %confhash }
 4378:                     );
 4379:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 4380:                                              $dom);
 4381:     if ($putresult eq 'ok') {
 4382:         if (keys(%changes) > 0) {
 4383:             my $cachetime = 24*60*60;
 4384:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 4385: 
 4386:             $resulttext = &mt('Changes made:').'<ul>';
 4387:             unless ($context eq 'requestcourses') {
 4388:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 4389:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 4390:                     foreach my $type (@{$types},'default') {
 4391:                         if (defined($changes{'defaultquota'}{$type})) {
 4392:                             my $typetitle = $usertypes->{$type};
 4393:                             if ($type eq 'default') {
 4394:                                 $typetitle = $othertitle;
 4395:                             }
 4396:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] Mb',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 4397:                         }
 4398:                     }
 4399:                     $resulttext .= '</ul></li>';
 4400:                 }
 4401:             }
 4402:             my %newenv;
 4403:             foreach my $item (@usertools) {
 4404:                 if (ref($changes{$item}) eq 'HASH') {
 4405:                     my $newacc = 
 4406:                         &Apache::lonnet::usertools_access($env{'user.name'},
 4407:                                                           $env{'user.domain'},
 4408:                                                           $item,'reload',$context);
 4409:                     if ($context eq 'requestcourses') {
 4410:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 4411:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 4412:                         }
 4413:                     } else {
 4414:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 4415:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 4416:                         }
 4417:                     }
 4418:                     $resulttext .= '<li>'.$titles{$item}.'<ul>';
 4419:                     foreach my $type (@{$types},'default','_LC_adv') {
 4420:                         if ($changes{$item}{$type}) {
 4421:                             my $typetitle = $usertypes->{$type};
 4422:                             if ($type eq 'default') {
 4423:                                 $typetitle = $othertitle;
 4424:                             } elsif ($type eq '_LC_adv') {
 4425:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 4426:                             }
 4427:                             if ($confhash{$item}{$type}) {
 4428:                                 if ($context eq 'requestcourses') {
 4429:                                     my $cond;
 4430:                                     if ($confhash{$item}{$type} =~ /^autolimit=(\d*)$/) {
 4431:                                         if ($1 eq '') {
 4432:                                             $cond = &mt('(Automatic processing of any request).');
 4433:                                         } else {
 4434:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 4435:                                         }
 4436:                                     } else { 
 4437:                                         $cond = $conditions{$confhash{$item}{$type}};
 4438:                                     }
 4439:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 4440:                                 } else {
 4441:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 4442:                                 }
 4443:                             } else {
 4444:                                 if ($type eq '_LC_adv') {
 4445:                                     if ($confhash{$item}{$type} eq '0') {
 4446:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 4447:                                     } else { 
 4448:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 4449:                                     }
 4450:                                 } else {
 4451:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 4452:                                 }
 4453:                             }
 4454:                         }
 4455:                     }
 4456:                     $resulttext .= '</ul></li>';
 4457:                 }
 4458:             }
 4459:             if ($action eq 'requestcourses') {
 4460:                 if (ref($changes{'notify'}) eq 'HASH') {
 4461:                     if ($changes{'notify'}{'approval'}) {
 4462:                         if (ref($confhash{'notify'}) eq 'HASH') {
 4463:                             if ($confhash{'notify'}{'approval'}) {
 4464:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 4465:                             } else {
 4466:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of course requests requiring approval.').'</li>';
 4467:                             }
 4468:                         }
 4469:                     }
 4470:                 }
 4471:             }
 4472:             $resulttext .= '</ul>';
 4473:             if (keys(%newenv)) {
 4474:                 &Apache::lonnet::appenv(\%newenv);
 4475:             }
 4476:         } else {
 4477:             if ($context eq 'requestcourses') {
 4478:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 4479:             } else {
 4480:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 4481:             }
 4482:         }
 4483:     } else {
 4484:         $resulttext = '<span class="LC_error">'.
 4485: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 4486:     }
 4487:     return $resulttext;
 4488: }
 4489: 
 4490: sub modify_autoenroll {
 4491:     my ($dom,%domconfig) = @_;
 4492:     my ($resulttext,%changes);
 4493:     my %currautoenroll;
 4494:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 4495:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 4496:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 4497:         }
 4498:     }
 4499:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 4500:     my %title = ( run => 'Auto-enrollment active',
 4501:                   sender => 'Sender for notification messages',
 4502:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)');
 4503:     my @offon = ('off','on');
 4504:     my $sender_uname = $env{'form.sender_uname'};
 4505:     my $sender_domain = $env{'form.sender_domain'};
 4506:     if ($sender_domain eq '') {
 4507:         $sender_uname = '';
 4508:     } elsif ($sender_uname eq '') {
 4509:         $sender_domain = '';
 4510:     }
 4511:     my $coowners = $env{'form.autoassign_coowners'};
 4512:     my %autoenrollhash =  (
 4513:                        autoenroll => { run => $env{'form.autoenroll_run'},
 4514:                                        sender_uname => $sender_uname,
 4515:                                        sender_domain => $sender_domain,
 4516:                                        'co-owners' => $coowners,
 4517:                                 }
 4518:                      );
 4519:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 4520:                                              $dom);
 4521:     if ($putresult eq 'ok') {
 4522:         if (exists($currautoenroll{'run'})) {
 4523:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 4524:                  $changes{'run'} = 1;
 4525:              }
 4526:         } elsif ($autorun) {
 4527:             if ($env{'form.autoenroll_run'} ne '1') {
 4528:                  $changes{'run'} = 1;
 4529:             }
 4530:         }
 4531:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 4532:             $changes{'sender'} = 1;
 4533:         }
 4534:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 4535:             $changes{'sender'} = 1;
 4536:         }
 4537:         if ($currautoenroll{'co-owners'} ne '') {
 4538:             if ($currautoenroll{'co-owners'} ne $coowners) {
 4539:                 $changes{'coowners'} = 1;
 4540:             }
 4541:         } elsif ($coowners) {
 4542:             $changes{'coowners'} = 1;
 4543:         }
 4544:         if (keys(%changes) > 0) {
 4545:             $resulttext = &mt('Changes made:').'<ul>';
 4546:             if ($changes{'run'}) {
 4547:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 4548:             }
 4549:             if ($changes{'sender'}) {
 4550:                 if ($sender_uname eq '' || $sender_domain eq '') {
 4551:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 4552:                 } else {
 4553:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 4554:                 }
 4555:             }
 4556:             if ($changes{'coowners'}) {
 4557:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
 4558:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 4559:             }
 4560:             $resulttext .= '</ul>';
 4561:         } else {
 4562:             $resulttext = &mt('No changes made to auto-enrollment settings');
 4563:         }
 4564:     } else {
 4565:         $resulttext = '<span class="LC_error">'.
 4566: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 4567:     }
 4568:     return $resulttext;
 4569: }
 4570: 
 4571: sub modify_autoupdate {
 4572:     my ($dom,%domconfig) = @_;
 4573:     my ($resulttext,%currautoupdate,%fields,%changes);
 4574:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 4575:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 4576:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 4577:         }
 4578:     }
 4579:     my @offon = ('off','on');
 4580:     my %title = &Apache::lonlocal::texthash (
 4581:                    run => 'Auto-update:',
 4582:                    classlists => 'Updates to user information in classlists?'
 4583:                 );
 4584:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4585:     my %fieldtitles = &Apache::lonlocal::texthash (
 4586:                         id => 'Student/Employee ID',
 4587:                         permanentemail => 'E-mail address',
 4588:                         lastname => 'Last Name',
 4589:                         firstname => 'First Name',
 4590:                         middlename => 'Middle Name',
 4591:                         gen => 'Generation',
 4592:                       );
 4593:     my $othertitle = &mt('All users');
 4594:     if (keys(%{$usertypes}) >  0) {
 4595:         $othertitle = &mt('Other users');
 4596:     }
 4597:     foreach my $key (keys(%env)) {
 4598:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 4599:             push(@{$fields{$1}},$2);
 4600:         }
 4601:     }
 4602:     my %updatehash = (
 4603:                       autoupdate => { run => $env{'form.autoupdate_run'},
 4604:                                       classlists => $env{'form.classlists'},
 4605:                                       fields => {%fields},
 4606:                                     }
 4607:                      );
 4608:     foreach my $key (keys(%currautoupdate)) {
 4609:         if (($key eq 'run') || ($key eq 'classlists')) {
 4610:             if (exists($updatehash{autoupdate}{$key})) {
 4611:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 4612:                     $changes{$key} = 1;
 4613:                 }
 4614:             }
 4615:         } elsif ($key eq 'fields') {
 4616:             if (ref($currautoupdate{$key}) eq 'HASH') {
 4617:                 foreach my $item (@{$types},'default') {
 4618:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 4619:                         my $change = 0;
 4620:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 4621:                             if (!exists($fields{$item})) {
 4622:                                 $change = 1;
 4623:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 4624:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 4625:                                     $change = 1;
 4626:                                 }
 4627:                             }
 4628:                         }
 4629:                         if ($change) {
 4630:                             push(@{$changes{$key}},$item);
 4631:                         }
 4632:                     } 
 4633:                 }
 4634:             }
 4635:         }
 4636:     }
 4637:     foreach my $item (@{$types},'default') {
 4638:         if (defined($fields{$item})) {
 4639:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 4640:                 if (!exists($currautoupdate{'fields'}{$item})) {
 4641:                     push(@{$changes{'fields'}},$item);
 4642:                 }
 4643:             } else {
 4644:                 push(@{$changes{'fields'}},$item);
 4645:             }
 4646:         }
 4647:     }
 4648:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 4649:                                              $dom);
 4650:     if ($putresult eq 'ok') {
 4651:         if (keys(%changes) > 0) {
 4652:             $resulttext = &mt('Changes made:').'<ul>';
 4653:             foreach my $key (sort(keys(%changes))) {
 4654:                 if (ref($changes{$key}) eq 'ARRAY') {
 4655:                     foreach my $item (@{$changes{$key}}) {
 4656:                         my @newvalues;
 4657:                         foreach my $type (@{$fields{$item}}) {
 4658:                             push(@newvalues,$fieldtitles{$type});
 4659:                         }
 4660:                         my $newvaluestr;
 4661:                         if (@newvalues > 0) {
 4662:                             $newvaluestr = join(', ',@newvalues);
 4663:                         } else {
 4664:                             $newvaluestr = &mt('none');
 4665:                         }
 4666:                         if ($item eq 'default') {
 4667:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 4668:                         } else {
 4669:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 4670:                         }
 4671:                     }
 4672:                 } else {
 4673:                     my $newvalue;
 4674:                     if ($key eq 'run') {
 4675:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 4676:                     } else {
 4677:                         $newvalue = $offon[$env{'form.'.$key}];
 4678:                     }
 4679:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 4680:                 }
 4681:             }
 4682:             $resulttext .= '</ul>';
 4683:         } else {
 4684:             $resulttext = &mt('No changes made to autoupdates');
 4685:         }
 4686:     } else {
 4687:         $resulttext = '<span class="LC_error">'.
 4688: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 4689:     }
 4690:     return $resulttext;
 4691: }
 4692: 
 4693: sub modify_autocreate {
 4694:     my ($dom,%domconfig) = @_;
 4695:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
 4696:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
 4697:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
 4698:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
 4699:         }
 4700:     }
 4701:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
 4702:                  req => 'Auto-creation of validated requests for official courses',
 4703:                  xmldc => 'Identity of course creator of courses from XML files',
 4704:                );
 4705:     my @types = ('xml','req');
 4706:     foreach my $item (@types) {
 4707:         $newvals{$item} = $env{'form.autocreate_'.$item};
 4708:         $newvals{$item} =~ s/\D//g;
 4709:         $newvals{$item} = 0 if ($newvals{$item} eq '');
 4710:     }
 4711:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
 4712:     my %domcoords = &get_active_dcs($dom);
 4713:     unless (exists($domcoords{$newvals{'xmldc'}})) {
 4714:         $newvals{'xmldc'} = '';
 4715:     }
 4716:     %autocreatehash =  (
 4717:                         autocreate => { xml => $newvals{'xml'},
 4718:                                         req => $newvals{'req'},
 4719:                                       }
 4720:                        );
 4721:     if ($newvals{'xmldc'} ne '') {
 4722:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
 4723:     }
 4724:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
 4725:                                              $dom);
 4726:     if ($putresult eq 'ok') {
 4727:         my @items = @types;
 4728:         if ($newvals{'xml'}) {
 4729:             push(@items,'xmldc');
 4730:         }
 4731:         foreach my $item (@items) {
 4732:             if (exists($currautocreate{$item})) {
 4733:                 if ($currautocreate{$item} ne $newvals{$item}) {
 4734:                     $changes{$item} = 1;
 4735:                 }
 4736:             } elsif ($newvals{$item}) {
 4737:                 $changes{$item} = 1;
 4738:             }
 4739:         }
 4740:         if (keys(%changes) > 0) {
 4741:             my @offon = ('off','on');
 4742:             $resulttext = &mt('Changes made:').'<ul>';
 4743:             foreach my $item (@types) {
 4744:                 if ($changes{$item}) {
 4745:                     my $newtxt = $offon[$newvals{$item}];
 4746:                     $resulttext .= '<li>'.&mt("$title{$item} set to [_1]$newtxt [_2]",'<b>','</b>').'</li>';
 4747:                 }
 4748:             }
 4749:             if ($changes{'xmldc'}) {
 4750:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
 4751:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
 4752:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]$newtxt [_2]",'<b>','</b>').'</li>';
 4753:             }
 4754:             $resulttext .= '</ul>';
 4755:         } else {
 4756:             $resulttext = &mt('No changes made to auto-creation settings');
 4757:         }
 4758:     } else {
 4759:         $resulttext = '<span class="LC_error">'.
 4760:             &mt('An error occurred: [_1]',$putresult).'</span>';
 4761:     }
 4762:     return $resulttext;
 4763: }
 4764: 
 4765: sub modify_directorysrch {
 4766:     my ($dom,%domconfig) = @_;
 4767:     my ($resulttext,%changes);
 4768:     my %currdirsrch;
 4769:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 4770:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 4771:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 4772:         }
 4773:     }
 4774:     my %title = ( available => 'Directory search available',
 4775:                   localonly => 'Other domains can search',
 4776:                   searchby => 'Search types',
 4777:                   searchtypes => 'Search latitude');
 4778:     my @offon = ('off','on');
 4779:     my @otherdoms = ('Yes','No');
 4780: 
 4781:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 4782:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 4783:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 4784: 
 4785:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4786:     if (keys(%{$usertypes}) == 0) {
 4787:         @cansearch = ('default');
 4788:     } else {
 4789:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 4790:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 4791:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 4792:                     push(@{$changes{'cansearch'}},$type);
 4793:                 }
 4794:             }
 4795:             foreach my $type (@cansearch) {
 4796:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 4797:                     push(@{$changes{'cansearch'}},$type);
 4798:                 }
 4799:             }
 4800:         } else {
 4801:             push(@{$changes{'cansearch'}},@cansearch);
 4802:         }
 4803:     }
 4804: 
 4805:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 4806:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 4807:             if (!grep(/^\Q$by\E$/,@searchby)) {
 4808:                 push(@{$changes{'searchby'}},$by);
 4809:             }
 4810:         }
 4811:         foreach my $by (@searchby) {
 4812:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 4813:                 push(@{$changes{'searchby'}},$by);
 4814:             }
 4815:         }
 4816:     } else {
 4817:         push(@{$changes{'searchby'}},@searchby);
 4818:     }
 4819: 
 4820:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 4821:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 4822:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 4823:                 push(@{$changes{'searchtypes'}},$type);
 4824:             }
 4825:         }
 4826:         foreach my $type (@searchtypes) {
 4827:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 4828:                 push(@{$changes{'searchtypes'}},$type);
 4829:             }
 4830:         }
 4831:     } else {
 4832:         if (exists($currdirsrch{'searchtypes'})) {
 4833:             foreach my $type (@searchtypes) {  
 4834:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 4835:                     push(@{$changes{'searchtypes'}},$type);
 4836:                 }
 4837:             }
 4838:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 4839:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 4840:             }   
 4841:         } else {
 4842:             push(@{$changes{'searchtypes'}},@searchtypes); 
 4843:         }
 4844:     }
 4845: 
 4846:     my %dirsrch_hash =  (
 4847:             directorysrch => { available => $env{'form.dirsrch_available'},
 4848:                                cansearch => \@cansearch,
 4849:                                localonly => $env{'form.dirsrch_localonly'},
 4850:                                searchby => \@searchby,
 4851:                                searchtypes => \@searchtypes,
 4852:                              }
 4853:             );
 4854:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 4855:                                              $dom);
 4856:     if ($putresult eq 'ok') {
 4857:         if (exists($currdirsrch{'available'})) {
 4858:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 4859:                  $changes{'available'} = 1;
 4860:              }
 4861:         } else {
 4862:             if ($env{'form.dirsrch_available'} eq '1') {
 4863:                 $changes{'available'} = 1;
 4864:             }
 4865:         }
 4866:         if (exists($currdirsrch{'localonly'})) {
 4867:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 4868:                  $changes{'localonly'} = 1;
 4869:              }
 4870:         } else {
 4871:             if ($env{'form.dirsrch_localonly'} eq '1') {
 4872:                 $changes{'localonly'} = 1;
 4873:             }
 4874:         }
 4875:         if (keys(%changes) > 0) {
 4876:             $resulttext = &mt('Changes made:').'<ul>';
 4877:             if ($changes{'available'}) {
 4878:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 4879:             }
 4880:             if ($changes{'localonly'}) {
 4881:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 4882:             }
 4883: 
 4884:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 4885:                 my $chgtext;
 4886:                 if (ref($usertypes) eq 'HASH') {
 4887:                     if (keys(%{$usertypes}) > 0) {
 4888:                         foreach my $type (@{$types}) {
 4889:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 4890:                                 $chgtext .= $usertypes->{$type}.'; ';
 4891:                             }
 4892:                         }
 4893:                         if (grep(/^default$/,@cansearch)) {
 4894:                             $chgtext .= $othertitle;
 4895:                         } else {
 4896:                             $chgtext =~ s/\; $//;
 4897:                         }
 4898:                         $resulttext .= '<li>'.&mt("Users from domain '<span class=\"LC_cusr_emph\">[_1]</span>' permitted to search the institutional directory set to: [_2]",$dom,$chgtext).'</li>';
 4899:                     }
 4900:                 }
 4901:             }
 4902:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 4903:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 4904:                 my $chgtext;
 4905:                 foreach my $type (@{$titleorder}) {
 4906:                     if (grep(/^\Q$type\E$/,@searchby)) {
 4907:                         if (defined($searchtitles->{$type})) {
 4908:                             $chgtext .= $searchtitles->{$type}.'; ';
 4909:                         }
 4910:                     }
 4911:                 }
 4912:                 $chgtext =~ s/\; $//;
 4913:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 4914:             }
 4915:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 4916:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 4917:                 my $chgtext;
 4918:                 foreach my $type (@{$srchtypeorder}) {
 4919:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 4920:                         if (defined($srchtypes_desc->{$type})) {
 4921:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 4922:                         }
 4923:                     }
 4924:                 }
 4925:                 $chgtext =~ s/\; $//;
 4926:                 $resulttext .= '<li>'.&mt("$title{'searchtypes'} set to: \"[_1]\"",$chgtext).'</li>';
 4927:             }
 4928:             $resulttext .= '</ul>';
 4929:         } else {
 4930:             $resulttext = &mt('No changes made to institution directory search settings');
 4931:         }
 4932:     } else {
 4933:         $resulttext = '<span class="LC_error">'.
 4934:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 4935:     }
 4936:     return $resulttext;
 4937: }
 4938: 
 4939: sub modify_contacts {
 4940:     my ($dom,%domconfig) = @_;
 4941:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 4942:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 4943:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 4944:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 4945:         }
 4946:     }
 4947:     my (%others,%to);
 4948:     my @contacts = ('supportemail','adminemail');
 4949:     my @mailings = ('errormail','packagesmail','helpdeskmail','lonstatusmail',
 4950:                     'requestsmail');
 4951:     foreach my $type (@mailings) {
 4952:         @{$newsetting{$type}} = 
 4953:             &Apache::loncommon::get_env_multiple('form.'.$type);
 4954:         foreach my $item (@contacts) {
 4955:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 4956:                 $contacts_hash{contacts}{$type}{$item} = 1;
 4957:             } else {
 4958:                 $contacts_hash{contacts}{$type}{$item} = 0;
 4959:             }
 4960:         }  
 4961:         $others{$type} = $env{'form.'.$type.'_others'};
 4962:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 4963:     }
 4964:     foreach my $item (@contacts) {
 4965:         $to{$item} = $env{'form.'.$item};
 4966:         $contacts_hash{'contacts'}{$item} = $to{$item};
 4967:     }
 4968:     if (keys(%currsetting) > 0) {
 4969:         foreach my $item (@contacts) {
 4970:             if ($to{$item} ne $currsetting{$item}) {
 4971:                 $changes{$item} = 1;
 4972:             }
 4973:         }
 4974:         foreach my $type (@mailings) {
 4975:             foreach my $item (@contacts) {
 4976:                 if (ref($currsetting{$type}) eq 'HASH') {
 4977:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 4978:                         push(@{$changes{$type}},$item);
 4979:                     }
 4980:                 } else {
 4981:                     push(@{$changes{$type}},@{$newsetting{$type}});
 4982:                 }
 4983:             }
 4984:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 4985:                 push(@{$changes{$type}},'others');
 4986:             }
 4987:         }
 4988:     } else {
 4989:         my %default;
 4990:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 4991:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 4992:         $default{'errormail'} = 'adminemail';
 4993:         $default{'packagesmail'} = 'adminemail';
 4994:         $default{'helpdeskmail'} = 'supportemail';
 4995:         $default{'lonstatusmail'} = 'adminemail';
 4996:         $default{'requestsmail'} = 'adminemail';
 4997:         foreach my $item (@contacts) {
 4998:            if ($to{$item} ne $default{$item}) {
 4999:               $changes{$item} = 1;
 5000:            } 
 5001:         }
 5002:         foreach my $type (@mailings) {
 5003:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 5004:                
 5005:                 push(@{$changes{$type}},@{$newsetting{$type}});
 5006:             }
 5007:             if ($others{$type} ne '') {
 5008:                 push(@{$changes{$type}},'others');
 5009:             } 
 5010:         }
 5011:     }
 5012:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 5013:                                              $dom);
 5014:     if ($putresult eq 'ok') {
 5015:         if (keys(%changes) > 0) {
 5016:             my ($titles,$short_titles)  = &contact_titles();
 5017:             $resulttext = &mt('Changes made:').'<ul>';
 5018:             foreach my $item (@contacts) {
 5019:                 if ($changes{$item}) {
 5020:                     $resulttext .= '<li>'.$titles->{$item}.
 5021:                                     &mt(' set to: ').
 5022:                                     '<span class="LC_cusr_emph">'.
 5023:                                     $to{$item}.'</span></li>';
 5024:                 }
 5025:             }
 5026:             foreach my $type (@mailings) {
 5027:                 if (ref($changes{$type}) eq 'ARRAY') {
 5028:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 5029:                     my @text;
 5030:                     foreach my $item (@{$newsetting{$type}}) {
 5031:                         push(@text,$short_titles->{$item});
 5032:                     }
 5033:                     if ($others{$type} ne '') {
 5034:                         push(@text,$others{$type});
 5035:                     }
 5036:                     $resulttext .= '<span class="LC_cusr_emph">'.
 5037:                                    join(', ',@text).'</span></li>';
 5038:                 }
 5039:             }
 5040:             $resulttext .= '</ul>';
 5041:         } else {
 5042:             $resulttext = &mt('No changes made to contact information');
 5043:         }
 5044:     } else {
 5045:         $resulttext = '<span class="LC_error">'.
 5046:             &mt('An error occurred: [_1].',$putresult).'</span>';
 5047:     }
 5048:     return $resulttext;
 5049: }
 5050: 
 5051: sub modify_usercreation {
 5052:     my ($dom,%domconfig) = @_;
 5053:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate);
 5054:     my $warningmsg;
 5055:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 5056:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 5057:             $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 5058:         }
 5059:     }
 5060:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 5061:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 5062:     my @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 5063:     my @contexts = ('author','course','requestcrs','selfcreate');
 5064:     foreach my $item(@contexts) {
 5065:         if ($item eq 'selfcreate') {
 5066:             @{$cancreate{$item}} = &Apache::loncommon::get_env_multiple('form.can_createuser_'.$item);
 5067:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5068:             if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth'))) {
 5069:                 if (ref($cancreate{$item}) eq 'ARRAY') { 
 5070:                     if (grep(/^login$/,@{$cancreate{$item}})) {
 5071:                         $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.').' '.&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.');   
 5072:                     }
 5073:                 }
 5074:             }
 5075:         } else {
 5076:             $cancreate{$item} = $env{'form.can_createuser_'.$item};
 5077:         }
 5078:     }
 5079:     my ($othertitle,$usertypes,$types) = 
 5080:         &Apache::loncommon::sorted_inst_types($dom);
 5081:     if (ref($types) eq 'ARRAY') {
 5082:         if (@{$types} > 0) {
 5083:             @{$cancreate{'statustocreate'}} = 
 5084:                 &Apache::loncommon::get_env_multiple('form.statustocreate');
 5085:         } else {
 5086:             @{$cancreate{'statustocreate'}} = ();
 5087:         }
 5088:         push(@contexts,'statustocreate');
 5089:     }
 5090:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 5091:         foreach my $item (@contexts) {
 5092:             if (($item eq 'selfcreate') || ($item eq 'statustocreate')) {
 5093:                 if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 5094:                     foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 5095:                         if (ref($cancreate{$item}) eq 'ARRAY') {
 5096:                             if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 5097:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 5098:                                     push(@{$changes{'cancreate'}},$item);
 5099:                                 }
 5100:                             }
 5101:                         }
 5102:                     }
 5103:                 } else {
 5104:                     if ($curr_usercreation{'cancreate'}{$item} eq '') {
 5105:                         if (@{$cancreate{$item}} > 0) {
 5106:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 5107:                                 push(@{$changes{'cancreate'}},$item);
 5108:                             }
 5109:                         }
 5110:                     } else {
 5111:                         if ($curr_usercreation{'cancreate'}{$item} eq 'any') {
 5112:                             if (@{$cancreate{$item}} < 3) {
 5113:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 5114:                                     push(@{$changes{'cancreate'}},$item);
 5115:                                 }
 5116:                             }
 5117:                         } elsif ($curr_usercreation{'cancreate'}{$item} eq 'none') {
 5118:                             if (@{$cancreate{$item}} > 0) {
 5119:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 5120:                                     push(@{$changes{'cancreate'}},$item);
 5121:                                 }
 5122:                             }
 5123:                         } elsif (!grep(/^$curr_usercreation{'cancreate'}{$item}$/,@{$cancreate{$item}})) {
 5124:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 5125:                                 push(@{$changes{'cancreate'}},$item);
 5126:                             }
 5127:                         }
 5128:                     }
 5129:                 }
 5130:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 5131:                     foreach my $type (@{$cancreate{$item}}) {
 5132:                         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 5133:                             if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 5134:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 5135:                                     push(@{$changes{'cancreate'}},$item);
 5136:                                 }
 5137:                             }
 5138:                         } elsif (($curr_usercreation{'cancreate'}{$item} ne 'any') &&
 5139:                                  ($curr_usercreation{'cancreate'}{$item} ne 'none')) {
 5140:                             if ($curr_usercreation{'cancreate'}{$item} ne $type) {
 5141:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 5142:                                     push(@{$changes{'cancreate'}},$item);
 5143:                                 }
 5144:                             }
 5145:                         }
 5146:                     }
 5147:                 }
 5148:             } else {
 5149:                 if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 5150:                     push(@{$changes{'cancreate'}},$item);
 5151:                 }
 5152:             }
 5153:         }
 5154:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 5155:         foreach my $item (@contexts) {
 5156:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 5157:                 if ($cancreate{$item} ne 'any') {
 5158:                     push(@{$changes{'cancreate'}},$item);
 5159:                 }
 5160:             } else {
 5161:                 if ($cancreate{$item} ne 'none') {
 5162:                     push(@{$changes{'cancreate'}},$item);
 5163:                 }
 5164:             }
 5165:         }
 5166:     } else {
 5167:         foreach my $item (@contexts)  {
 5168:             push(@{$changes{'cancreate'}},$item);
 5169:         }
 5170:     }
 5171: 
 5172:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 5173:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 5174:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 5175:                 push(@{$changes{'username_rule'}},$type);
 5176:             }
 5177:         }
 5178:         foreach my $type (@username_rule) {
 5179:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 5180:                 push(@{$changes{'username_rule'}},$type);
 5181:             }
 5182:         }
 5183:     } else {
 5184:         push(@{$changes{'username_rule'}},@username_rule);
 5185:     }
 5186: 
 5187:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 5188:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 5189:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 5190:                 push(@{$changes{'id_rule'}},$type);
 5191:             }
 5192:         }
 5193:         foreach my $type (@id_rule) {
 5194:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 5195:                 push(@{$changes{'id_rule'}},$type);
 5196:             }
 5197:         }
 5198:     } else {
 5199:         push(@{$changes{'id_rule'}},@id_rule);
 5200:     }
 5201: 
 5202:     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 5203:         foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 5204:             if (!grep(/^\Q$type\E$/,@email_rule)) {
 5205:                 push(@{$changes{'email_rule'}},$type);
 5206:             }
 5207:         }
 5208:         foreach my $type (@email_rule) {
 5209:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 5210:                 push(@{$changes{'email_rule'}},$type);
 5211:             }
 5212:         }
 5213:     } else {
 5214:         push(@{$changes{'email_rule'}},@email_rule);
 5215:     }
 5216: 
 5217:     my @authen_contexts = ('author','course','domain');
 5218:     my @authtypes = ('int','krb4','krb5','loc');
 5219:     my %authhash;
 5220:     foreach my $item (@authen_contexts) {
 5221:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 5222:         foreach my $auth (@authtypes) {
 5223:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 5224:                 $authhash{$item}{$auth} = 1;
 5225:             } else {
 5226:                 $authhash{$item}{$auth} = 0;
 5227:             }
 5228:         }
 5229:     }
 5230:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 5231:         foreach my $item (@authen_contexts) {
 5232:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 5233:                 foreach my $auth (@authtypes) {
 5234:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 5235:                         push(@{$changes{'authtypes'}},$item);
 5236:                         last;
 5237:                     }
 5238:                 }
 5239:             }
 5240:         }
 5241:     } else {
 5242:         foreach my $item (@authen_contexts) {
 5243:             push(@{$changes{'authtypes'}},$item);
 5244:         }
 5245:     }
 5246: 
 5247:     my %usercreation_hash =  (
 5248:             usercreation => {
 5249:                               cancreate     => \%cancreate,
 5250:                               username_rule => \@username_rule,
 5251:                               id_rule       => \@id_rule,
 5252:                               email_rule    => \@email_rule,
 5253:                               authtypes     => \%authhash,
 5254:                             }
 5255:             );
 5256: 
 5257:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 5258:                                              $dom);
 5259: 
 5260:     my %selfcreatetypes = (
 5261:                              sso   => 'users authenticated by institutional single sign on',
 5262:                              login => 'users authenticated by institutional log-in',
 5263:                              email => 'users who provide a valid e-mail address for use as the username',
 5264:                           );
 5265:     if ($putresult eq 'ok') {
 5266:         if (keys(%changes) > 0) {
 5267:             $resulttext = &mt('Changes made:').'<ul>';
 5268:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 5269:                 my %lt = &usercreation_types();
 5270:                 foreach my $type (@{$changes{'cancreate'}}) {
 5271:                     my $chgtext;
 5272:                     unless ($type eq 'statustocreate') {
 5273:                         $chgtext = $lt{$type}.', ';
 5274:                     }
 5275:                     if ($type eq 'selfcreate') {
 5276:                         if (@{$cancreate{$type}} == 0) {
 5277:                             $chgtext .= &mt('creation of a new user account is not permitted.');
 5278:                         } else {
 5279:                             $chgtext .= &mt('creation of a new account is permitted for:').'<ul>';
 5280:                             foreach my $case (@{$cancreate{$type}}) {
 5281:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 5282:                             }
 5283:                             $chgtext .= '</ul>';
 5284:                             if (ref($cancreate{$type}) eq 'ARRAY') {
 5285:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
 5286:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
 5287:                                         if (@{$cancreate{'statustocreate'}} == 0) {
 5288:                                             $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 5289:                                         }
 5290:                                     }
 5291:                                 }
 5292:                             }
 5293:                         }
 5294:                     } elsif ($type eq 'statustocreate') {
 5295:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
 5296:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
 5297:                             if (@{$cancreate{'selfcreate'}} > 0) {
 5298:                                 if (@{$cancreate{'statustocreate'}} == 0) {
 5299: 
 5300:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
 5301:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
 5302:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").'</span>';
 5303:                                     } 
 5304:                                 } elsif (ref($usertypes) eq 'HASH') {
 5305:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 5306:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
 5307:                                     } else {
 5308:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
 5309:                                     }
 5310:                                     $chgtext .= '<ul>';
 5311:                                     foreach my $case (@{$cancreate{$type}}) {
 5312:                                         if ($case eq 'default') {
 5313:                                             $chgtext .= '<li>'.$othertitle.'</li>';
 5314:                                         } else {
 5315:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
 5316:                                         }
 5317:                                     }
 5318:                                     $chgtext .= '</ul>';
 5319:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
 5320:                                         $chgtext .= '<br /><span class="LC_warning">'.&mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').'</span>';
 5321:                                     }
 5322:                                 }
 5323:                             } else {
 5324:                                 if (@{$cancreate{$type}} == 0) {
 5325:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
 5326:                                 } else {
 5327:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
 5328:                                 }
 5329:                             }
 5330:                         }
 5331:                     } else {
 5332:                         if ($cancreate{$type} eq 'none') {
 5333:                             $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 5334:                         } elsif ($cancreate{$type} eq 'any') {
 5335:                             $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 5336:                         } elsif ($cancreate{$type} eq 'official') {
 5337:                             $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 5338:                         } elsif ($cancreate{$type} eq 'unofficial') {
 5339:                             $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 5340:                         }
 5341:                     }
 5342:                     $resulttext .= '<li>'.$chgtext.'</li>';
 5343:                 }
 5344:             }
 5345:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 5346:                 my ($rules,$ruleorder) = 
 5347:                     &Apache::lonnet::inst_userrules($dom,'username');
 5348:                 my $chgtext = '<ul>';
 5349:                 foreach my $type (@username_rule) {
 5350:                     if (ref($rules->{$type}) eq 'HASH') {
 5351:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 5352:                     }
 5353:                 }
 5354:                 $chgtext .= '</ul>';
 5355:                 if (@username_rule > 0) {
 5356:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 5357:                 } else {
 5358:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 5359:                 }
 5360:             }
 5361:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 5362:                 my ($idrules,$idruleorder) = 
 5363:                     &Apache::lonnet::inst_userrules($dom,'id');
 5364:                 my $chgtext = '<ul>';
 5365:                 foreach my $type (@id_rule) {
 5366:                     if (ref($idrules->{$type}) eq 'HASH') {
 5367:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 5368:                     }
 5369:                 }
 5370:                 $chgtext .= '</ul>';
 5371:                 if (@id_rule > 0) {
 5372:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 5373:                 } else {
 5374:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 5375:                 }
 5376:             }
 5377:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 5378:                 my ($emailrules,$emailruleorder) =
 5379:                     &Apache::lonnet::inst_userrules($dom,'email');
 5380:                 my $chgtext = '<ul>';
 5381:                 foreach my $type (@email_rule) {
 5382:                     if (ref($emailrules->{$type}) eq 'HASH') {
 5383:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 5384:                     }
 5385:                 }
 5386:                 $chgtext .= '</ul>';
 5387:                 if (@email_rule > 0) {
 5388:                     $resulttext .= '<li>'.&mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').$chgtext.'</li>';
 5389:                 } else {
 5390:                     $resulttext .= '<li>'.&mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').'</li>';
 5391:                 }
 5392:             }
 5393: 
 5394:             my %authname = &authtype_names();
 5395:             my %context_title = &context_names();
 5396:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 5397:                 my $chgtext = '<ul>';
 5398:                 foreach my $type (@{$changes{'authtypes'}}) {
 5399:                     my @allowed;
 5400:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 5401:                     foreach my $auth (@authtypes) {
 5402:                         if ($authhash{$type}{$auth}) {
 5403:                             push(@allowed,$authname{$auth});
 5404:                         }
 5405:                     }
 5406:                     if (@allowed > 0) {
 5407:                         $chgtext .= join(', ',@allowed).'</li>';
 5408:                     } else {
 5409:                         $chgtext .= &mt('none').'</li>';
 5410:                     }
 5411:                 }
 5412:                 $chgtext .= '</ul>';
 5413:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 5414:                 $resulttext .= '</li>';
 5415:             }
 5416:             $resulttext .= '</ul>';
 5417:         } else {
 5418:             $resulttext = &mt('No changes made to user creation settings');
 5419:         }
 5420:     } else {
 5421:         $resulttext = '<span class="LC_error">'.
 5422:             &mt('An error occurred: [_1]',$putresult).'</span>';
 5423:     }
 5424:     if ($warningmsg ne '') {
 5425:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 5426:     }
 5427:     return $resulttext;
 5428: }
 5429: 
 5430: sub modify_usermodification {
 5431:     my ($dom,%domconfig) = @_;
 5432:     my ($resulttext,%curr_usermodification,%changes);
 5433:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 5434:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 5435:             $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 5436:         }
 5437:     }
 5438:     my @contexts = ('author','course','selfcreate');
 5439:     my %context_title = (
 5440:                            author => 'In author context',
 5441:                            course => 'In course context',
 5442:                            selfcreate => 'When self creating account', 
 5443:                         );
 5444:     my @fields = ('lastname','firstname','middlename','generation',
 5445:                   'permanentemail','id');
 5446:     my %roles = (
 5447:                   author => ['ca','aa'],
 5448:                   course => ['st','ep','ta','in','cr'],
 5449:                 );
 5450:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5451:     if (ref($types) eq 'ARRAY') {
 5452:         push(@{$types},'default');
 5453:         $usertypes->{'default'} = $othertitle;
 5454:     }
 5455:     $roles{'selfcreate'} = $types;  
 5456:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5457:     my %modifyhash;
 5458:     foreach my $context (@contexts) {
 5459:         foreach my $role (@{$roles{$context}}) {
 5460:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 5461:             foreach my $item (@fields) {
 5462:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 5463:                     $modifyhash{$context}{$role}{$item} = 1;
 5464:                 } else {
 5465:                     $modifyhash{$context}{$role}{$item} = 0;
 5466:                 }
 5467:             }
 5468:         }
 5469:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 5470:             foreach my $role (@{$roles{$context}}) {
 5471:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 5472:                     foreach my $field (@fields) {
 5473:                         if ($modifyhash{$context}{$role}{$field} ne 
 5474:                                 $curr_usermodification{$context}{$role}{$field}) {
 5475:                             push(@{$changes{$context}},$role);
 5476:                             last;
 5477:                         }
 5478:                     }
 5479:                 }
 5480:             }
 5481:         } else {
 5482:             foreach my $context (@contexts) {
 5483:                 foreach my $role (@{$roles{$context}}) {
 5484:                     push(@{$changes{$context}},$role);
 5485:                 }
 5486:             }
 5487:         }
 5488:     }
 5489:     my %usermodification_hash =  (
 5490:                                    usermodification => \%modifyhash,
 5491:                                  );
 5492:     my $putresult = &Apache::lonnet::put_dom('configuration',
 5493:                                              \%usermodification_hash,$dom);
 5494:     if ($putresult eq 'ok') {
 5495:         if (keys(%changes) > 0) {
 5496:             $resulttext = &mt('Changes made: ').'<ul>';
 5497:             foreach my $context (@contexts) {
 5498:                 if (ref($changes{$context}) eq 'ARRAY') {
 5499:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 5500:                     if (ref($changes{$context}) eq 'ARRAY') {
 5501:                         foreach my $role (@{$changes{$context}}) {
 5502:                             my $rolename;
 5503:                             if ($context eq 'selfcreate') {
 5504:                                 $rolename = $role;
 5505:                                 if (ref($usertypes) eq 'HASH') {
 5506:                                     if ($usertypes->{$role} ne '') {
 5507:                                         $rolename = $usertypes->{$role};
 5508:                                     }
 5509:                                 }
 5510:                             } else {
 5511:                                 if ($role eq 'cr') {
 5512:                                     $rolename = &mt('Custom');
 5513:                                 } else {
 5514:                                     $rolename = &Apache::lonnet::plaintext($role);
 5515:                                 }
 5516:                             }
 5517:                             my @modifiable;
 5518:                             if ($context eq 'selfcreate') {
 5519:                                 $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Self-creation of account by users with status: [_1]',$rolename).'</span> - '.&mt('modifiable fields (if institutional data blank): ');
 5520:                             } else {
 5521:                                 $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 5522:                             }
 5523:                             foreach my $field (@fields) {
 5524:                                 if ($modifyhash{$context}{$role}{$field}) {
 5525:                                     push(@modifiable,$fieldtitles{$field});
 5526:                                 }
 5527:                             }
 5528:                             if (@modifiable > 0) {
 5529:                                 $resulttext .= join(', ',@modifiable);
 5530:                             } else {
 5531:                                 $resulttext .= &mt('none'); 
 5532:                             }
 5533:                             $resulttext .= '</li>';
 5534:                         }
 5535:                         $resulttext .= '</ul></li>';
 5536:                     }
 5537:                 }
 5538:             }
 5539:             $resulttext .= '</ul>';
 5540:         } else {
 5541:             $resulttext = &mt('No changes made to user modification settings');
 5542:         }
 5543:     } else {
 5544:         $resulttext = '<span class="LC_error">'.
 5545:             &mt('An error occurred: [_1]',$putresult).'</span>';
 5546:     }
 5547:     return $resulttext;
 5548: }
 5549: 
 5550: sub modify_defaults {
 5551:     my ($dom,$r) = @_;
 5552:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 5553:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5554:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def');
 5555:     my @authtypes = ('internal','krb4','krb5','localauth');
 5556:     foreach my $item (@items) {
 5557:         $newvalues{$item} = $env{'form.'.$item};
 5558:         if ($item eq 'auth_def') {
 5559:             if ($newvalues{$item} ne '') {
 5560:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 5561:                     push(@errors,$item);
 5562:                 }
 5563:             }
 5564:         } elsif ($item eq 'lang_def') {
 5565:             if ($newvalues{$item} ne '') {
 5566:                 if ($newvalues{$item} =~ /^(\w+)/) {
 5567:                     my $langcode = $1;
 5568:                     if ($langcode ne 'x_chef') {
 5569:                         if (code2language($langcode) eq '') {
 5570:                             push(@errors,$item);
 5571:                         }
 5572:                     }
 5573:                 } else {
 5574:                     push(@errors,$item);
 5575:                 }
 5576:             }
 5577:         } elsif ($item eq 'timezone_def') {
 5578:             if ($newvalues{$item} ne '') {
 5579:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 5580:                     push(@errors,$item);   
 5581:                 }
 5582:             }
 5583:         } elsif ($item eq 'datelocale_def') {
 5584:             if ($newvalues{$item} ne '') {
 5585:                 my @datelocale_ids = DateTime::Locale->ids();
 5586:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 5587:                     push(@errors,$item);
 5588:                 }
 5589:             }
 5590:         }
 5591:         if (grep(/^\Q$item\E$/,@errors)) {
 5592:             $newvalues{$item} = $domdefaults{$item};
 5593:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 5594:             $changes{$item} = 1;
 5595:         }
 5596:         $domdefaults{$item} = $newvalues{$item};
 5597:     }
 5598:     my %defaults_hash = (
 5599:                          defaults => \%newvalues,
 5600:                         );
 5601:     my $title = &defaults_titles();
 5602:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 5603:                                              $dom);
 5604:     if ($putresult eq 'ok') {
 5605:         if (keys(%changes) > 0) {
 5606:             $resulttext = &mt('Changes made:').'<ul>';
 5607:             my $version = $r->dir_config('lonVersion');
 5608:             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";
 5609:             foreach my $item (sort(keys(%changes))) {
 5610:                 my $value = $env{'form.'.$item};
 5611:                 if ($value eq '') {
 5612:                     $value = &mt('none');
 5613:                 } elsif ($item eq 'auth_def') {
 5614:                     my %authnames = &authtype_names();
 5615:                     my %shortauth = (
 5616:                              internal => 'int',
 5617:                              krb4 => 'krb4',
 5618:                              krb5 => 'krb5',
 5619:                              localauth  => 'loc',
 5620:                     );
 5621:                     $value = $authnames{$shortauth{$value}};
 5622:                 }
 5623:                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 5624:                 $mailmsgtext .= "$title->{$item} set to $value\n";  
 5625:             }
 5626:             $resulttext .= '</ul>';
 5627:             $mailmsgtext .= "\n";
 5628:             my $cachetime = 24*60*60;
 5629:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 5630:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 5631:                 my $sysmail = $r->dir_config('lonSysEMail');
 5632:                 &Apache::lonmsg::sendemail($sysmail,"LON-CAPA Domain Settings Change - $dom",$mailmsgtext);
 5633:             }
 5634:         } else {
 5635:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 5636:         }
 5637:     } else {
 5638:         $resulttext = '<span class="LC_error">'.
 5639:             &mt('An error occurred: [_1]',$putresult).'</span>';
 5640:     }
 5641:     if (@errors > 0) {
 5642:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 5643:         foreach my $item (@errors) {
 5644:             $resulttext .= ' "'.$title->{$item}.'",';
 5645:         }
 5646:         $resulttext =~ s/,$//;
 5647:     }
 5648:     return $resulttext;
 5649: }
 5650: 
 5651: sub modify_scantron {
 5652:     my ($r,$dom,$confname,%domconfig) = @_;
 5653:     my ($resulttext,%confhash,%changes,$errors);
 5654:     my $custom = 'custom.tab';
 5655:     my $default = 'default.tab';
 5656:     my $servadm = $r->dir_config('lonAdmEMail');
 5657:     my ($configuserok,$author_ok,$switchserver) = 
 5658:         &config_check($dom,$confname,$servadm);
 5659:     if ($env{'form.scantronformat.filename'} ne '') {
 5660:         my $error;
 5661:         if ($configuserok eq 'ok') {
 5662:             if ($switchserver) {
 5663:                 $error = &mt("Upload of scantron format file is not permitted to this server: [_1]",$switchserver);
 5664:             } else {
 5665:                 if ($author_ok eq 'ok') {
 5666:                     my ($result,$scantronurl) =
 5667:                         &publishlogo($r,'upload','scantronformat',$dom,
 5668:                                      $confname,'scantron','','',$custom);
 5669:                     if ($result eq 'ok') {
 5670:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 5671:                         $changes{'scantronformat'} = 1;
 5672:                     } else {
 5673:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 5674:                     }
 5675:                 } else {
 5676:                     $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);
 5677:                 }
 5678:             }
 5679:         } else {
 5680:             $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);
 5681:         }
 5682:         if ($error) {
 5683:             &Apache::lonnet::logthis($error);
 5684:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 5685:         }
 5686:     }
 5687:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 5688:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 5689:             if ($env{'form.scantronformat_del'}) {
 5690:                 $confhash{'scantron'}{'scantronformat'} = '';
 5691:                 $changes{'scantronformat'} = 1;
 5692:             }
 5693:         }
 5694:     }
 5695:     if (keys(%confhash) > 0) {
 5696:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 5697:                                                  $dom);
 5698:         if ($putresult eq 'ok') {
 5699:             if (keys(%changes) > 0) {
 5700:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 5701:                     $resulttext = &mt('Changes made:').'<ul>';
 5702:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 5703:                         $resulttext .= '<li>'.&mt('[_1] scantron format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 5704:                     } else {
 5705:                         $resulttext .= '<li>'.&mt('Custom scantron format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 5706:                     }
 5707:                     $resulttext .= '</ul>';
 5708:                 } else {
 5709:                     $resulttext = &mt('Changes made to scantron format file.');
 5710:                 }
 5711:                 $resulttext .= '</ul>';
 5712:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 5713:             } else {
 5714:                 $resulttext = &mt('No changes made to scantron format file');
 5715:             }
 5716:         } else {
 5717:             $resulttext = '<span class="LC_error">'.
 5718:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 5719:         }
 5720:     } else {
 5721:         $resulttext = &mt('No changes made to scantron format file'); 
 5722:     }
 5723:     if ($errors) {
 5724:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 5725:                        $errors.'</ul>';
 5726:     }
 5727:     return $resulttext;
 5728: }
 5729: 
 5730: sub modify_coursecategories {
 5731:     my ($dom,%domconfig) = @_;
 5732:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 5733:         $cathash);
 5734:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 5735:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 5736:         $cathash = $domconfig{'coursecategories'}{'cats'};
 5737:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 5738:             $changes{'togglecats'} = 1;
 5739:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 5740:         }
 5741:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 5742:             $changes{'categorize'} = 1;
 5743:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 5744:         }
 5745:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
 5746:             $changes{'togglecatscomm'} = 1;
 5747:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
 5748:         }
 5749:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
 5750:             $changes{'categorizecomm'} = 1;
 5751:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
 5752:         }
 5753:     } else {
 5754:         $changes{'togglecats'} = 1;
 5755:         $changes{'categorize'} = 1;
 5756:         $changes{'togglecatscomm'} = 1;
 5757:         $changes{'categorizecomm'} = 1;
 5758:         $domconfig{'coursecategories'} = {
 5759:                                              togglecats => $env{'form.togglecats'},
 5760:                                              categorize => $env{'form.categorize'},
 5761:                                              togglecatscomm => $env{'form.togglecatscomm'},
 5762:                                              categorizecomm => $env{'form.categorizecomm'},
 5763:                                          };
 5764:     }
 5765:     if (ref($cathash) eq 'HASH') {
 5766:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 5767:             push (@deletecategory,'instcode::0');
 5768:         }
 5769:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
 5770:             push(@deletecategory,'communities::0');
 5771:         }
 5772:     }
 5773:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 5774:     if (ref($cathash) eq 'HASH') {
 5775:         if (@deletecategory > 0) {
 5776:             #FIXME Need to remove category from all courses using a deleted category 
 5777:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 5778:             foreach my $item (@deletecategory) {
 5779:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 5780:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 5781:                     $deletions{$item} = 1;
 5782:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 5783:                 }
 5784:             }
 5785:         }
 5786:         foreach my $item (keys(%{$cathash})) {
 5787:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 5788:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 5789:                 $reorderings{$item} = 1;
 5790:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 5791:             }
 5792:             if ($env{'form.addcategory_name_'.$item} ne '') {
 5793:                 my $newcat = $env{'form.addcategory_name_'.$item};
 5794:                 my $newdepth = $depth+1;
 5795:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 5796:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 5797:                 $adds{$newitem} = 1; 
 5798:             }
 5799:             if ($env{'form.subcat_'.$item} ne '') {
 5800:                 my $newcat = $env{'form.subcat_'.$item};
 5801:                 my $newdepth = $depth+1;
 5802:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 5803:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 5804:                 $adds{$newitem} = 1;
 5805:             }
 5806:         }
 5807:     }
 5808:     if ($env{'form.instcode'} eq '1') {
 5809:         if (ref($cathash) eq 'HASH') {
 5810:             my $newitem = 'instcode::0';
 5811:             if ($cathash->{$newitem} eq '') {  
 5812:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 5813:                 $adds{$newitem} = 1;
 5814:             }
 5815:         } else {
 5816:             my $newitem = 'instcode::0';
 5817:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 5818:             $adds{$newitem} = 1;
 5819:         }
 5820:     }
 5821:     if ($env{'form.communities'} eq '1') {
 5822:         if (ref($cathash) eq 'HASH') {
 5823:             my $newitem = 'communities::0';
 5824:             if ($cathash->{$newitem} eq '') {
 5825:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 5826:                 $adds{$newitem} = 1;
 5827:             }
 5828:         } else {
 5829:             my $newitem = 'communities::0';
 5830:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
 5831:             $adds{$newitem} = 1;
 5832:         }
 5833:     }
 5834:     if ($env{'form.addcategory_name'} ne '') {
 5835:         if (($env{'form.addcategory_name'} ne 'instcode') &&
 5836:             ($env{'form.addcategory_name'} ne 'communities')) {
 5837:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 5838:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 5839:             $adds{$newitem} = 1;
 5840:         }
 5841:     }
 5842:     my $putresult;
 5843:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 5844:         if (keys(%deletions) > 0) {
 5845:             foreach my $key (keys(%deletions)) {
 5846:                 if ($predelallitems{$key} ne '') {
 5847:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 5848:                 }
 5849:             }
 5850:         }
 5851:         my (@chkcats,@chktrails,%chkallitems);
 5852:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 5853:         if (ref($chkcats[0]) eq 'ARRAY') {
 5854:             my $depth = 0;
 5855:             my $chg = 0;
 5856:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 5857:                 my $name = $chkcats[0][$i];
 5858:                 my $item;
 5859:                 if ($name eq '') {
 5860:                     $chg ++;
 5861:                 } else {
 5862:                     $item = &escape($name).'::0';
 5863:                     if ($chg) {
 5864:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 5865:                     }
 5866:                     $depth ++; 
 5867:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 5868:                     $depth --;
 5869:                 }
 5870:             }
 5871:         }
 5872:     }
 5873:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 5874:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 5875:         if ($putresult eq 'ok') {
 5876:             my %title = (
 5877:                          togglecats     => 'Show/Hide a course in catalog',
 5878:                          categorize     => 'Assign a category to a course',
 5879:                          togglecatscomm => 'Show/Hide a community in catalog',
 5880:                          categorizecomm => 'Assign a category to a community',
 5881:                         );
 5882:             my %level = (
 5883:                          dom  => 'set in Domain ("Modify Course/Community")',
 5884:                          crs  => 'set in Course ("Course Configuration")',
 5885:                          comm => 'set in Community ("Community Configuration")',
 5886:                         );
 5887:             $resulttext = &mt('Changes made:').'<ul>';
 5888:             if ($changes{'togglecats'}) {
 5889:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 5890:             }
 5891:             if ($changes{'categorize'}) {
 5892:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 5893:             }
 5894:             if ($changes{'togglecatscomm'}) {
 5895:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
 5896:             }
 5897:             if ($changes{'categorizecomm'}) {
 5898:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
 5899:             }
 5900:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 5901:                 my $cathash;
 5902:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 5903:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 5904:                 } else {
 5905:                     $cathash = {};
 5906:                 } 
 5907:                 my (@cats,@trails,%allitems);
 5908:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 5909:                 if (keys(%deletions) > 0) {
 5910:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 5911:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 5912:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 5913:                     }
 5914:                     $resulttext .= '</ul></li>';
 5915:                 }
 5916:                 if (keys(%reorderings) > 0) {
 5917:                     my %sort_by_trail;
 5918:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 5919:                     foreach my $key (keys(%reorderings)) {
 5920:                         if ($allitems{$key} ne '') {
 5921:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 5922:                         }
 5923:                     }
 5924:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 5925:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 5926:                     }
 5927:                     $resulttext .= '</ul></li>';
 5928:                 }
 5929:                 if (keys(%adds) > 0) {
 5930:                     my %sort_by_trail;
 5931:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 5932:                     foreach my $key (keys(%adds)) {
 5933:                         if ($allitems{$key} ne '') {
 5934:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 5935:                         }
 5936:                     }
 5937:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 5938:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 5939:                     }
 5940:                     $resulttext .= '</ul></li>';
 5941:                 }
 5942:             }
 5943:             $resulttext .= '</ul>';
 5944:         } else {
 5945:             $resulttext = '<span class="LC_error">'.
 5946:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 5947:         }
 5948:     } else {
 5949:         $resulttext = &mt('No changes made to course and community categories');
 5950:     }
 5951:     return $resulttext;
 5952: }
 5953: 
 5954: sub modify_serverstatuses {
 5955:     my ($dom,%domconfig) = @_;
 5956:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 5957:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 5958:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 5959:     }
 5960:     my @pages = &serverstatus_pages();
 5961:     foreach my $type (@pages) {
 5962:         $newserverstatus{$type}{'namedusers'} = '';
 5963:         $newserverstatus{$type}{'machines'} = '';
 5964:         if (defined($env{'form.'.$type.'_namedusers'})) {
 5965:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 5966:             my @okusers;
 5967:             foreach my $user (@users) {
 5968:                 my ($uname,$udom) = split(/:/,$user);
 5969:                 if (($udom =~ /^$match_domain$/) &&   
 5970:                     (&Apache::lonnet::domain($udom)) &&
 5971:                     ($uname =~ /^$match_username$/)) {
 5972:                     if (!grep(/^\Q$user\E/,@okusers)) {
 5973:                         push(@okusers,$user);
 5974:                     }
 5975:                 }
 5976:             }
 5977:             if (@okusers > 0) {
 5978:                  @okusers = sort(@okusers);
 5979:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 5980:             }
 5981:         }
 5982:         if (defined($env{'form.'.$type.'_machines'})) {
 5983:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 5984:             my @okmachines;
 5985:             foreach my $ip (@machines) {
 5986:                 my @parts = split(/\./,$ip);
 5987:                 next if (@parts < 4);
 5988:                 my $badip = 0;
 5989:                 for (my $i=0; $i<4; $i++) {
 5990:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 5991:                         $badip = 1;
 5992:                         last;
 5993:                     }
 5994:                 }
 5995:                 if (!$badip) {
 5996:                     push(@okmachines,$ip);     
 5997:                 }
 5998:             }
 5999:             @okmachines = sort(@okmachines);
 6000:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 6001:         }
 6002:     }
 6003:     my %serverstatushash =  (
 6004:                                 serverstatuses => \%newserverstatus,
 6005:                             );
 6006:     my %changes;
 6007:     foreach my $type (@pages) {
 6008:         foreach my $setting ('namedusers','machines') {
 6009:             my (@current,@new);
 6010:             if (ref($currserverstatus{$type}) eq 'HASH') {
 6011:                 if ($currserverstatus{$type}{$setting} ne '') { 
 6012:                     @current = split(/,/,$currserverstatus{$type}{$setting});
 6013:                 }
 6014:             }
 6015:             if ($newserverstatus{$type}{$setting} ne '') {
 6016:                 @new = split(/,/,$newserverstatus{$type}{$setting});
 6017:             }
 6018:             if (@current > 0) {
 6019:                 if (@new > 0) {
 6020:                     foreach my $item (@current) {
 6021:                         if (!grep(/^\Q$item\E$/,@new)) {
 6022:                             $changes{$type}{$setting} = 1;
 6023:                             last;
 6024:                         }
 6025:                     }
 6026:                     foreach my $item (@new) {
 6027:                         if (!grep(/^\Q$item\E$/,@current)) {
 6028:                             $changes{$type}{$setting} = 1;
 6029:                             last;
 6030:                         }
 6031:                     }
 6032:                 } else {
 6033:                     $changes{$type}{$setting} = 1;
 6034:                 }
 6035:             } elsif (@new > 0) {
 6036:                 $changes{$type}{$setting} = 1;
 6037:             }
 6038:         }
 6039:     }
 6040:     if (keys(%changes) > 0) {
 6041:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 6042:         my $putresult = &Apache::lonnet::put_dom('configuration',
 6043:                                                  \%serverstatushash,$dom);
 6044:         if ($putresult eq 'ok') {
 6045:             $resulttext .= &mt('Changes made:').'<ul>';
 6046:             foreach my $type (@pages) {
 6047:                 if (ref($changes{$type}) eq 'HASH') {
 6048:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 6049:                     if ($changes{$type}{'namedusers'}) {
 6050:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 6051:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 6052:                         } else {
 6053:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 6054:                         }
 6055:                     }
 6056:                     if ($changes{$type}{'machines'}) {
 6057:                         if ($newserverstatus{$type}{'machines'} eq '') {
 6058:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 6059:                         } else {
 6060:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 6061:                         }
 6062: 
 6063:                     }
 6064:                     $resulttext .= '</ul></li>';
 6065:                 }
 6066:             }
 6067:             $resulttext .= '</ul>';
 6068:         } else {
 6069:             $resulttext = '<span class="LC_error">'.
 6070:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 6071: 
 6072:         }
 6073:     } else {
 6074:         $resulttext = &mt('No changes made to access to server status pages');
 6075:     }
 6076:     return $resulttext;
 6077: }
 6078: 
 6079: sub modify_coursedefaults {
 6080:     my ($dom,%domconfig) = @_;
 6081:     my ($resulttext,$errors,%changes,%defaultshash);
 6082:     my %defaultchecked = ('canuse_pdfforms' => 'off');
 6083:     my @offon = ('off','on');
 6084:     my @toggles = ('canuse_pdfforms');
 6085: 
 6086:     $defaultshash{'coursedefaults'} = {};
 6087: 
 6088:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
 6089:         if ($domconfig{'coursedefaults'} eq '') {
 6090:             $domconfig{'coursedefaults'} = {};
 6091:         }
 6092:     }
 6093: 
 6094:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
 6095:         foreach my $item (@toggles) {
 6096:             if ($defaultchecked{$item} eq 'on') {
 6097:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 6098:                     ($env{'form.'.$item} eq '0')) {
 6099:                     $changes{$item} = 1;
 6100:                 } elsif ($domconfig{'coursdefaults'}{$item} ne $env{'form.'.$item}) {
 6101:                     $changes{$item} = 1;
 6102:                 }
 6103:             } elsif ($defaultchecked{$item} eq 'off') {
 6104:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
 6105:                     ($env{'form.'.$item} eq '1')) {
 6106:                     $changes{$item} = 1;
 6107:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
 6108:                     $changes{$item} = 1;
 6109:                 }
 6110:             }
 6111:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
 6112:         }
 6113:     }
 6114:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
 6115:                                              $dom);
 6116:     if ($putresult eq 'ok') {
 6117:         if (keys(%changes) > 0) {
 6118:             if ($changes{'canuse_pdfforms'}) {
 6119:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 6120:                 $domdefaults{'canuse_pdfforms'}=$defaultshash{'coursedefaults'}{'canuse_pdfforms'};
 6121:                 my $cachetime = 24*60*60;
 6122:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 6123:             }
 6124:             $resulttext = &mt('Changes made:').'<ul>';
 6125:             foreach my $item (sort(keys(%changes))) {
 6126:                 if ($item eq 'canuse_pdfforms') {
 6127:                     if ($env{'form.'.$item} eq '1') {
 6128:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
 6129:                     } else {
 6130:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
 6131:                     }
 6132:                 }
 6133:             }
 6134:             $resulttext .= '</ul>';
 6135:         } else {
 6136:             $resulttext = &mt('No changes made to course defaults');
 6137:         }
 6138:     } else {
 6139:         $resulttext = '<span class="LC_error">'.
 6140:             &mt('An error occurred: [_1]',$putresult).'</span>';
 6141:     }
 6142:     return $resulttext;
 6143: }
 6144: 
 6145: sub recurse_check {
 6146:     my ($chkcats,$categories,$depth,$name) = @_;
 6147:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
 6148:         my $chg = 0;
 6149:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
 6150:             my $category = $chkcats->[$depth]{$name}[$j];
 6151:             my $item;
 6152:             if ($category eq '') {
 6153:                 $chg ++;
 6154:             } else {
 6155:                 my $deeper = $depth + 1;
 6156:                 $item = &escape($category).':'.&escape($name).':'.$depth;
 6157:                 if ($chg) {
 6158:                     $categories->{$item} -= $chg;
 6159:                 }
 6160:                 &recurse_check($chkcats,$categories,$deeper,$category);
 6161:                 $deeper --;
 6162:             }
 6163:         }
 6164:     }
 6165:     return;
 6166: }
 6167: 
 6168: sub recurse_cat_deletes {
 6169:     my ($item,$coursecategories,$deletions) = @_;
 6170:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 6171:     my $subdepth = $depth + 1;
 6172:     if (ref($coursecategories) eq 'HASH') {
 6173:         foreach my $subitem (keys(%{$coursecategories})) {
 6174:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
 6175:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
 6176:                 delete($coursecategories->{$subitem});
 6177:                 $deletions->{$subitem} = 1;
 6178:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
 6179:             }  
 6180:         }
 6181:     }
 6182:     return;
 6183: }
 6184: 
 6185: sub dom_servers {
 6186:     my ($dom) = @_;
 6187:     my (%uniqservers,%servers);
 6188:     my $primaryserver = &Apache::lonnet::hostname(&Apache::lonnet::domain($dom,'primary'));
 6189:     my @machinedoms = &Apache::lonnet::machine_domains($primaryserver);
 6190:     foreach my $mdom (@machinedoms) {
 6191:         my %currservers = %servers;
 6192:         my %server = &Apache::lonnet::get_servers($mdom);
 6193:         %servers = (%currservers,%server);
 6194:     }
 6195:     my %by_hostname;
 6196:     foreach my $id (keys(%servers)) {
 6197:         push(@{$by_hostname{$servers{$id}}},$id);
 6198:     }
 6199:     foreach my $hostname (sort(keys(%by_hostname))) {
 6200:         if (@{$by_hostname{$hostname}} > 1) {
 6201:             my $match = 0;
 6202:             foreach my $id (@{$by_hostname{$hostname}}) {
 6203:                 if (&Apache::lonnet::host_domain($id) eq $dom) {
 6204:                     $uniqservers{$id} = $hostname;
 6205:                     $match = 1;
 6206:                 }
 6207:             }
 6208:             unless ($match) {
 6209:                 $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
 6210:             }
 6211:         } else {
 6212:             $uniqservers{$by_hostname{$hostname}[0]} = $hostname;
 6213:         }
 6214:     }
 6215:     return %uniqservers;
 6216: }
 6217: 
 6218: sub get_active_dcs {
 6219:     my ($dom) = @_;
 6220:     my %dompersonnel = &Apache::lonnet::get_domain_roles($dom,['dc']);
 6221:     my %domcoords;
 6222:     my $numdcs = 0;
 6223:     my $now = time;
 6224:     foreach my $server (keys(%dompersonnel)) {
 6225:         foreach my $user (sort(keys(%{$dompersonnel{$server}}))) {
 6226:             my ($trole,$uname,$udom,$runame,$rudom,$rsec) = split(/:/,$user);
 6227:             my ($end,$start) = split(':',$dompersonnel{$server}{$user});
 6228:             if (($end eq '') || ($end == 0) || ($end > $now)) {
 6229:                 if ($start <= $now) {
 6230:                     $domcoords{$uname.':'.$udom} = $dompersonnel{$server}{$user};
 6231:                 }
 6232:             }
 6233:         }
 6234:     }
 6235:     return %domcoords;
 6236: }
 6237: 
 6238: sub active_dc_picker {
 6239:     my ($dom,$curr_dc) = @_;
 6240:     my %domcoords = &get_active_dcs($dom);
 6241:     my @dcs = sort(keys(%domcoords));
 6242:     my $numdcs = scalar(@dcs);
 6243:     my $datatable;
 6244:     my $numinrow = 2;
 6245:     if ($numdcs > 1) {
 6246:         $datatable = '<table>';
 6247:         for (my $i=0; $i<@dcs; $i++) {
 6248:             my $rem = $i%($numinrow);
 6249:             if ($rem == 0) {
 6250:                 if ($i > 0) {
 6251:                     $datatable .= '</tr>';
 6252:                 }
 6253:                 $datatable .= '<tr>';
 6254:             }
 6255:             my $check = ' ';
 6256:             if ($curr_dc eq '') {
 6257:                 if (!$i) {
 6258:                     $check = ' checked="checked" ';
 6259:                 }
 6260:             } elsif ($dcs[$i] eq $curr_dc) {
 6261:                 $check = ' checked="checked" ';
 6262:             }
 6263:             if ($i == @dcs - 1) {
 6264:                 my $colsleft = $numinrow - $rem;
 6265:                 if ($colsleft > 1) {
 6266:                     $datatable .= '<td colspan="'.$colsleft.'">';
 6267:                 } else {
 6268:                     $datatable .= '<td>';
 6269:                 }
 6270:             } else {
 6271:                 $datatable .= '<td>';
 6272:             }
 6273:             my ($dcname,$dcdom) = split(':',$dcs[$i]);
 6274:             $datatable .= '<span class="LC_nobreak"><label>'.
 6275:                           '<input type="radio" name="autocreate_xmldc"'.
 6276:                           ' value="'.$dcs[$i].'"'.$check.'/>'.
 6277:                           &Apache::loncommon::plainname($dcname,$dcdom).
 6278:                           '</label></span></td>';
 6279:         }
 6280:         $datatable .= '</tr></table>';
 6281:     } elsif (@dcs) {
 6282:         $datatable .= '<input type="hidden" name="autocreate_dc" value="'.
 6283:                       $dcs[0].'" />';
 6284:     }
 6285:     return ($numdcs,$datatable);
 6286: }
 6287: 
 6288: 1;

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