File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.69: download - view: text, annotated - select for diffs
Fri Nov 28 21:02:35 2008 UTC (15 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Add new domain configuration to control access to server status pages.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.69 2008/11/28 21:02:35 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: package Apache::domainprefs;
   32: 
   33: use strict;
   34: use Apache::Constants qw(:common :http);
   35: use Apache::lonnet;
   36: use Apache::loncommon();
   37: use Apache::lonhtmlcommon();
   38: use Apache::lonlocal;
   39: use Apache::lonmsg();
   40: use LONCAPA qw(:DEFAULT :match);
   41: use LONCAPA::Enrollment;
   42: use LONCAPA::loncgi();
   43: use File::Copy;
   44: use Locale::Language;
   45: use DateTime::TimeZone;
   46: use DateTime::Locale;
   47: 
   48: sub handler {
   49:     my $r=shift;
   50:     if ($r->header_only) {
   51:         &Apache::loncommon::content_type($r,'text/html');
   52:         $r->send_http_header;
   53:         return OK;
   54:     }
   55: 
   56:     my $dom = $env{'request.role.domain'};
   57:     my $domdesc = &Apache::lonnet::domain($dom,'description');
   58:     if (&Apache::lonnet::allowed('mau',$dom)) {
   59:         &Apache::loncommon::content_type($r,'text/html');
   60:         $r->send_http_header;
   61:     } else {
   62:         $env{'user.error.msg'}=
   63:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
   64:         return HTTP_NOT_ACCEPTABLE;
   65:     }
   66:     &Apache::lonhtmlcommon::clear_breadcrumbs();
   67:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
   68:                                             ['phase','actions']);
   69:     my $phase = 'pickactions';
   70:     if ( exists($env{'form.phase'}) ) {
   71:         $phase = $env{'form.phase'};
   72:     }
   73:     my %domconfig =
   74:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
   75:                 'quotas','autoenroll','autoupdate','directorysrch',
   76:                 'usercreation','usermodification','contacts','defaults',
   77:                 'scantron','coursecategories','serverstatuses'],$dom);
   78:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
   79:                        'autoupdate','directorysrch','contacts',
   80:                        'usercreation','usermodification','scantron',
   81:                        'coursecategories','serverstatuses');
   82:     my %prefs = (
   83:         'rolecolors' =>
   84:                    { text => 'Default color schemes',
   85:                      help => 'Domain_Configuration_Color_Schemes',
   86:                      header => [{col1 => 'Student Settings',
   87:                                  col2 => '',},
   88:                                 {col1 => 'Coordinator Settings',
   89:                                  col2 => '',},
   90:                                 {col1 => 'Author Settings',
   91:                                  col2 => '',},
   92:                                 {col1 => 'Administrator Settings',
   93:                                  col2 => '',}],
   94:                     },
   95:         'login' =>  
   96:                     { text => 'Log-in page options',
   97:                       help => 'Domain_Configuration_Login_Page',
   98:                       header => [{col1 => 'Item',
   99:                                   col2 => '',}],
  100:                     },
  101:         'defaults' => 
  102:                     { text => 'Default authentication/language/timezone',
  103:                       help => 'Domain_Configuration_LangTZAuth',
  104:                       header => [{col1 => 'Setting',
  105:                                   col2 => 'Value'}],
  106:                     },
  107:         'quotas' => 
  108:                     { text => 'Default quotas for user portfolios',
  109:                       help => 'Domain_Configuration_Quotas',
  110:                       header => [{col1 => 'User type',
  111:                                   col2 => 'Default quota'}],
  112:                     },
  113:         'autoenroll' =>
  114:                    { text => 'Auto-enrollment settings',
  115:                      help => 'Domain_Configuration_Auto_Enrollment',
  116:                      header => [{col1 => 'Configuration setting',
  117:                                  col2 => 'Value(s)'}],
  118:                    },
  119:         'autoupdate' => 
  120:                    { text => 'Auto-update settings',
  121:                      help => 'Domain_Configuration_Auto_Updates',
  122:                      header => [{col1 => 'Setting',
  123:                                  col2 => 'Value',},
  124:                                 {col1 => 'User population',
  125:                                  col2 => 'Updataeable user data'}],
  126:                   },
  127:         'directorysrch' => 
  128:                   { text => 'Institutional directory searches',
  129:                     help => 'Domain_Configuration_InstDirectory_Search',
  130:                     header => [{col1 => 'Setting',
  131:                                 col2 => 'Value',}],
  132:                   },
  133:         'contacts' =>
  134:                   { text => 'Contact Information',
  135:                     help => 'Domain_Configuration_Contact_Info',
  136:                     header => [{col1 => 'Setting',
  137:                                 col2 => 'Value',}],
  138:                   },
  139: 
  140:         'usercreation' => 
  141:                   { text => 'User creation',
  142:                     help => 'Domain_Configuration_User_Creation',
  143:                     header => [{col1 => 'Format rule type',
  144:                                 col2 => 'Format rules in force'},
  145:                                {col1 => 'User account creation',
  146:                                 col2 => 'Usernames which may be created',},
  147:                                {col1 => 'Context',
  148:                                 col2 => 'Assignable authentication types'}],
  149:                   },
  150:         'usermodification' =>
  151:                   { text => 'User modification',
  152:                     help => 'Domain_Configuration_User_Modification',
  153:                     header => [{col1 => 'Target user has role',
  154:                                 col2 => 'User information updateable in author context'},
  155:                                {col1 => 'Target user has role',
  156:                                 col2 => 'User information updateable in course context'},
  157:                                {col1 => "Status of user",
  158:                                 col2 => 'Information settable when self-creating account (if directory data blank)'}],
  159:                   },
  160:         'scantron' =>
  161:                   { text => 'Scantron format file',
  162:                     help => 'Domain_Configuration_Scantron_Format',
  163:                     header => [ {col1 => 'Item',
  164:                                  col2 => '',
  165:                               }],
  166:                   },
  167:         'coursecategories' =>
  168:                   { text => 'Cataloging of courses',
  169:                     help => 'Domain_Configuration_Cataloging_Courses',
  170:                     header => [{col1 => 'Category settings',
  171:                                 col2 => '',},
  172:                                {col1 => 'Categories',
  173:                                 col2 => '',
  174:                                }],
  175:                   },
  176:         'serverstatuses' =>
  177:                  {text   => 'Access to Server Status Pages',
  178:                   help   => 'Domain_Configuration_Server_Status',
  179:                   header => [{col1 => 'Status Page',
  180:                               col2 => 'Other named users',
  181:                               col3 => 'Specific IPs',
  182:                             }],
  183:                  },
  184:     );
  185:     my @roles = ('student','coordinator','author','admin');
  186:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  187:     &Apache::lonhtmlcommon::add_breadcrumb
  188:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  189:       text=>"Pick functionality"});
  190:     my $confname = $dom.'-domainconfig';
  191:     if ($phase eq 'process') {
  192:         &Apache::lonhtmlcommon::add_breadcrumb
  193:           ({href=>"javascript:changePage(document.$phase,'display')",
  194:             text=>"Domain Configuration"},
  195:            {href=>"javascript:changePage(document.$phase,'$phase')",
  196:             text=>"Updated"});
  197:         &print_header($r,$phase);
  198:         foreach my $item (@prefs_order) {
  199:             if (grep(/^\Q$item\E$/,@actions)) {
  200:                 $r->print('<h3>'.&mt($prefs{$item}{'text'}).'</h3>'.
  201:                           &process_changes($r,$dom,$confname,$item,
  202:                           \@roles,%domconfig));
  203:             }
  204:         }
  205:         $r->print('<p>');
  206:         &print_footer($r,$phase,'display','Back to configuration display',
  207:                       \@actions);
  208:         $r->print('</p>');
  209:     } elsif ($phase eq 'display') {
  210:         &Apache::lonhtmlcommon::add_breadcrumb
  211:             ({href=>"javascript:changePage(document.$phase,'display')",
  212:               text=>"Domain Configuration"});
  213:         &print_header($r,$phase);
  214:         if (@actions > 0) {
  215:             my $rowsum = 0;
  216:             my (%output,%rowtotal,@items);
  217:             my $halfway = @actions/2;
  218:             foreach my $item (@prefs_order) {
  219:                 if (grep(/^\Q$item\E$/,@actions)) {
  220:                     push(@items,$item);
  221:                     ($output{$item},$rowtotal{$item}) = 
  222:                         &print_config_box($r,$dom,$confname,$phase,
  223:                                           $item,$prefs{$item},
  224:                                           $domconfig{$item});
  225:                     $rowsum += $rowtotal{$item};
  226:                 }
  227:             }
  228:             my $colend;
  229:             my $halfway = $rowsum/2;
  230:             my $aggregate = 0;
  231:             my $sumleft = 0;
  232:             my $sumright = 0;
  233:             my $crossover;
  234:             for (my $i=0; $i<@items; $i++) {
  235:                 $aggregate += $rowtotal{$items[$i]};
  236:                 if ($aggregate > $halfway) {
  237:                     $crossover = $i;
  238:                     last;
  239:                 }
  240:             }
  241:             for (my $i=0; $i<$crossover; $i++) {
  242:                 $sumleft += $rowtotal{$items[$i]}; 
  243:             }
  244:             for (my $i=$crossover+1; $i<@items; $i++) {
  245:                 $sumright += $rowtotal{$items[$i]};
  246:             }
  247:             if ((@items > 1) && ($env{'form.numcols'} == 2)) {
  248:                 my $sumdiff = $sumright - $sumleft;
  249:                 if ($sumdiff > 0) {
  250:                     $colend = $crossover + 1;
  251:                 } else {
  252:                     $colend = $crossover;
  253:                 }
  254:             } else {
  255:                 $colend = @items;
  256:             }
  257:             $r->print('<p><table class="LC_double_column"><tr><td class="LC_left_col">');
  258:             for (my $i=0; $i<$colend; $i++) {
  259:                 $r->print($output{$items[$i]});
  260:             }
  261:             $r->print('</td><td></td><td class="LC_right_col">');
  262:             if ($colend < @items) {
  263:                 for (my $i=$colend; $i<@items; $i++) { 
  264:                     $r->print($output{$items[$i]});
  265:                 }
  266:             }
  267:             $r->print('</td></tr></table></p>');
  268:             $r->print(&print_footer($r,$phase,'process','Save',\@actions));
  269:         } else {
  270:             $r->print('<input type="hidden" name="phase" value="" />'.
  271:                       '<input type="hidden" name="numcols" value="'.
  272:                       $env{'form.numcols'}.'" />'."\n".
  273:                       '<span clas="LC_error">'.&mt('No settings chosen').
  274:                       '</span>');
  275:         }
  276:         $r->print('</form>');
  277:         $r->print(&Apache::loncommon::end_page());
  278:     } else {
  279:         if ($phase eq '') {
  280:             $phase = 'pickactions';
  281:         }
  282:         my %helphash;
  283:         &print_header($r,$phase);
  284:         if (keys(%domconfig) == 0) {
  285:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  286:             my @ids=&Apache::lonnet::current_machine_ids();
  287:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  288:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  289:                 my @loginimages = ('img','logo','domlogo','login');
  290:                 my $custom_img_count = 0;
  291:                 foreach my $img (@loginimages) {
  292:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  293:                         $custom_img_count ++;
  294:                     }
  295:                 }
  296:                 foreach my $role (@roles) {
  297:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  298:                         $custom_img_count ++;
  299:                     }
  300:                 }
  301:                 if ($custom_img_count > 0) {
  302:                     my $switch_server = &check_switchserver($dom,$confname);
  303:                     $r->print(
  304:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  305:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  306:     &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 />'.
  307:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  308:                     if ($switch_server) {
  309:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  310:                     }
  311:                     return OK;
  312:                 }
  313:             }
  314:         }
  315:         $r->print('<h3>'.&mt('Functionality to display/modify').'</h3>');
  316:         $r->print('<script type="text/javascript">'."\n".
  317:               &Apache::loncommon::check_uncheck_jscript()."\n".
  318:               '</script>'."\n".'<p><input type="button" value="'.&mt('check all').'" '.
  319:               'onclick="javascript:checkAll(document.pickactions.actions)"'.
  320:               ' />&nbsp;&nbsp;'.
  321:               '<input type="button" value="'.&mt('uncheck all').'" '.
  322:               'onclick="javascript:uncheckAll(document.pickactions.actions)"'.
  323:               ' /></p><div class="LC_left_float">');
  324:         my ($numitems,$midpoint,$seconddiv,$count); 
  325:         $numitems = @prefs_order;
  326:         $midpoint = int($numitems/2);
  327:         if ($numitems%2) {
  328:             $midpoint ++;
  329:         }
  330:         $count = 0;
  331:         foreach my $item (@prefs_order) {
  332:             $r->print('<h4>'.
  333:                       &Apache::loncommon::help_open_topic($prefs{$item}->{'help'}).
  334:                       '<label><input type="checkbox" name="actions" value="'.$item.
  335:                       '" />&nbsp;'.&mt($prefs{$item}->{'text'}).'</label></h4>');
  336:             $count ++;
  337:             if ((!$seconddiv) && ($count >= $midpoint)) {
  338:                 $r->print('</div>'."\n".'<div class="LC_left_float">'."\n");
  339:                 $seconddiv = 1;
  340:             }
  341:         }
  342:         $r->print('</div><div class="LC_clear_float_footer"></div><h3>'.
  343:                   &mt('Display options').'</h3>'."\n".
  344:                   '<p><span class="LC_nobreak">'.&mt('Display using: ')."\n".
  345:                   '<label><input type="radio" name="numcols" value="1" />'.
  346:                   &mt('one column').'</label>&nbsp;&nbsp;<label>'.
  347:                   '<input type="radio" name="numcols" value="2" />'.
  348:                   &mt('two columns').'</label></span></p>');
  349:         $r->print(&print_footer($r,$phase,'display','Go'));
  350:         $r->print('</form>');
  351:         $r->print(&Apache::loncommon::end_page());
  352:     }
  353:     return OK;
  354: }
  355: 
  356: sub process_changes {
  357:     my ($r,$dom,$confname,$action,$roles,%domconfig) = @_;
  358:     my $output;
  359:     if ($action eq 'login') {
  360:         $output = &modify_login($r,$dom,$confname,%domconfig);
  361:     } elsif ($action eq 'rolecolors') {
  362:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  363:                                      %domconfig);
  364:     } elsif ($action eq 'quotas') {
  365:         $output = &modify_quotas($dom,%domconfig);
  366:     } elsif ($action eq 'autoenroll') {
  367:         $output = &modify_autoenroll($dom,%domconfig);
  368:     } elsif ($action eq 'autoupdate') {
  369:         $output = &modify_autoupdate($dom,%domconfig);
  370:     } elsif ($action eq 'directorysrch') {
  371:         $output = &modify_directorysrch($dom,%domconfig);
  372:     } elsif ($action eq 'usercreation') {
  373:         $output = &modify_usercreation($dom,%domconfig);
  374:     } elsif ($action eq 'usermodification') {
  375:         $output = &modify_usermodification($dom,%domconfig);
  376:     } elsif ($action eq 'contacts') {
  377:         $output = &modify_contacts($dom,%domconfig);
  378:     } elsif ($action eq 'defaults') {
  379:         $output = &modify_defaults($dom,$r);
  380:     } elsif ($action eq 'scantron') {
  381:         $output = &modify_scantron($r,$dom,$confname,%domconfig);
  382:     } elsif ($action eq 'coursecategories') {
  383:         $output = &modify_coursecategories($dom,%domconfig);
  384:     } elsif ($action eq 'serverstatuses') {
  385:         $output = &modify_serverstatuses($dom,%domconfig);
  386:     }
  387:     return $output;
  388: }
  389: 
  390: sub print_config_box {
  391:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  392:     my $rowtotal = 0;
  393:     my $output;
  394:     if ($action eq 'coursecategories') {
  395:         $output = &coursecategories_javascript($settings);
  396:     }  
  397:     $output .= 
  398:          '<table class="LC_nested_outer">
  399:           <tr>
  400:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  401:            &mt($item->{text}).'&nbsp;'.
  402:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  403:           '</tr>';
  404:     $rowtotal ++;
  405:     if (($action eq 'autoupdate') || ($action eq 'rolecolors') || 
  406:         ($action eq 'usercreation') || ($action eq 'usermodification') ||
  407:         ($action eq 'coursecategories')) {
  408:         my $colspan = '';
  409:         if (($action eq 'rolecolors') || ($action eq 'coursecategories')) {
  410:             $colspan = ' colspan="2"';
  411:         }
  412:         $output .= '
  413:           <tr>
  414:            <td>
  415:             <table class="LC_nested">
  416:              <tr class="LC_info_row">
  417:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  418:               <td class="LC_right_item">'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  419:              </tr>';
  420:         $rowtotal ++;
  421:         if ($action eq 'autoupdate') {
  422:             $output .= &print_autoupdate('top',$dom,$settings,\$rowtotal);
  423:         } elsif ($action eq 'usercreation') {
  424:             $output .= &print_usercreation('top',$dom,$settings,\$rowtotal);
  425:         } elsif ($action eq 'usermodification') {
  426:             $output .= &print_usermodification('top',$dom,$settings,\$rowtotal);
  427:         } elsif ($action eq 'coursecategories') {
  428:             $output .= &print_coursecategories('top',$dom,$item,$settings,\$rowtotal);
  429:         } else {
  430:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  431:         }
  432:         $output .= '
  433:            </table>
  434:           </td>
  435:          </tr>
  436:          <tr>
  437:            <td>
  438:             <table class="LC_nested">
  439:              <tr class="LC_info_row">
  440:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>';
  441:         $output .= '
  442:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  443:              </tr>';
  444:             $rowtotal ++;
  445:         if ($action eq 'autoupdate') {
  446:             $output .= &print_autoupdate('bottom',$dom,$settings,\$rowtotal);
  447:         } elsif ($action eq 'usercreation') {
  448:             $output .= &print_usercreation('middle',$dom,$settings,\$rowtotal).'
  449:            </table>
  450:           </td>
  451:          </tr>
  452:          <tr>
  453:            <td>
  454:             <table class="LC_nested">
  455:              <tr class="LC_info_row">
  456:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  457:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>             </tr>'.
  458:             &print_usercreation('bottom',$dom,$settings,\$rowtotal);
  459:             $rowtotal ++;
  460:         } elsif ($action eq 'usermodification') {
  461:             $output .= &print_usermodification('middle',$dom,$settings,\$rowtotal).'
  462:            </table>
  463:           </td>
  464:          </tr>
  465:          <tr>
  466:            <td>
  467:             <table class="LC_nested">
  468:              <tr class="LC_info_row">
  469:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  470:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>      </tr>'.
  471: 
  472:                        &print_usermodification('bottom',$dom,$settings,\$rowtotal);
  473:             $rowtotal ++;
  474:         } elsif ($action eq 'coursecategories') {
  475:             $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  476:         } else {
  477:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  478:            </table>
  479:           </td>
  480:          </tr>
  481:          <tr>
  482:            <td>
  483:             <table class="LC_nested">
  484:              <tr class="LC_info_row">
  485:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  486:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  487:               <td class="LC_right_item" valign="top">'.
  488:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  489:              </tr>'.
  490:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
  491:            </table>
  492:           </td>
  493:          </tr>
  494:          <tr>
  495:            <td>
  496:             <table class="LC_nested">
  497:              <tr class="LC_info_row">
  498:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  499:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  500:              </tr>'.
  501:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
  502:             $rowtotal += 2;
  503:         }
  504:     } else {
  505:         $output .= '
  506:           <tr>
  507:            <td>
  508:             <table class="LC_nested">
  509:              <tr class="LC_info_row">';
  510:         if (($action eq 'login') || ($action eq 'directorysrch')) {
  511:             $output .= '  
  512:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  513:         } elsif ($action eq 'serverstatuses') {
  514:             $output .= '
  515:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
  516:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
  517: 
  518:         } else {
  519:             $output .= '
  520:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
  521:         }
  522:         if ($action eq 'serverstatuses') {
  523:              $output .= '<td class="LC_left_item" valign="top">'.
  524:                         &mt($item->{'header'}->[0]->{'col2'}).
  525:                         '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
  526:         } else {
  527:             $output .= '<td class="LC_right_item" valign="top">'.
  528:                        &mt($item->{'header'}->[0]->{'col2'});
  529:         }
  530:         $output .= '</td>';
  531:         if ($item->{'header'}->[0]->{'col3'}) {
  532:             $output .= '<td class="LC_right_item" valign="top">'.
  533:                        &mt($item->{'header'}->[0]->{'col3'});
  534:             if ($action eq 'serverstatuses') {
  535:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
  536:             }
  537:             $output .= '</td>';
  538:         }
  539:         $output .= '</tr>';
  540:         $rowtotal ++;
  541:         if ($action eq 'login') {
  542:             $output .= &print_login($dom,$confname,$phase,$settings,\$rowtotal);
  543:         } elsif ($action eq 'quotas') {
  544:             $output .= &print_quotas($dom,$settings,\$rowtotal);
  545:         } elsif ($action eq 'autoenroll') {
  546:             $output .= &print_autoenroll($dom,$settings,\$rowtotal);
  547:         } elsif ($action eq 'directorysrch') {
  548:             $output .= &print_directorysrch($dom,$settings,\$rowtotal);
  549:         } elsif ($action eq 'contacts') {
  550:             $output .= &print_contacts($dom,$settings,\$rowtotal);
  551:         } elsif ($action eq 'defaults') {
  552:             $output .= &print_defaults($dom,\$rowtotal);
  553:         } elsif ($action eq 'scantron') {
  554:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
  555:         } elsif ($action eq 'serverstatuses') {
  556:             $output .= &print_serverstatuses($dom,$settings,\$rowtotal);
  557:         }
  558:     }
  559:     $output .= '
  560:    </table>
  561:   </td>
  562:  </tr>
  563: </table><br />';
  564:     return ($output,$rowtotal);
  565: }
  566: 
  567: sub print_header {
  568:     my ($r,$phase) = @_;
  569:     my $alert = &mt('You must select at least one functionality type to display.'); 
  570:     my $js = '
  571: <script type="text/javascript">
  572: function changePage(formname,newphase) {
  573:     formname.phase.value = newphase;
  574:     numchecked = 0;
  575:     if (formname == document.pickactions) {
  576:         if (formname.actions.length > 0) {
  577:             for (var i = 0; i<formname.actions.length; i++) {
  578:                 if (formname.actions[i].checked) {
  579:                     numchecked ++;
  580:                 }
  581:             }
  582:         } else {
  583:             if (formname.actions.checked) {
  584:                 numchecked ++;
  585:             }
  586:         }
  587:         if (numchecked > 0) {
  588:             formname.submit();
  589:         } else {
  590:             alert("'.$alert.'");
  591:             return;
  592:         }
  593:     }
  594:     formname.submit();
  595: }'."\n";
  596:     if ($phase eq 'pickactions') {
  597:         $js .= 
  598:             &Apache::lonhtmlcommon::set_form_elements({actions => 'checkbox',numcols => 'radio',})."\n".
  599:             &javascript_set_colnums();
  600:     } elsif ($phase eq 'display') {
  601:         $js .= &color_pick_js()."\n";
  602:     }
  603:     $js .= &Apache::loncommon::viewport_size_js().'
  604: </script>
  605: ';
  606:     my $additem;
  607:     if ($phase eq 'pickactions') {
  608:         my %loaditems = (
  609:                     'onload' => "javascript:getViewportDims(document.$phase.width,document.$phase.height);setDisplayColumns();setFormElements(document.pickactions);",
  610:                         );
  611:         $additem = {'add_entries' => \%loaditems,};
  612:     } else {
  613:         my %loaditems = (
  614:                     'onload' => "javascript:getViewportDims(document.$phase.width,document.$phase.height);",
  615:                         );
  616:         $additem = {'add_entries' => \%loaditems,};
  617:     }
  618:     $r->print(&Apache::loncommon::start_page('View/Modify Domain Settings',
  619:                                            $js,$additem));
  620:     $r->print(&Apache::lonhtmlcommon::breadcrumbs('Domain Settings'));
  621:     $r->print('
  622: <form name="parmform" action="">
  623: <input type="hidden" name="pres_marker" />
  624: <input type="hidden" name="pres_type" />
  625: <input type="hidden" name="pres_value" />
  626: </form>
  627: ');
  628:     $r->print('<form method="post" name="'.$phase.'" action="/adm/domainprefs"'.
  629:               ' enctype="multipart/form-data">');
  630:     return;
  631: }
  632: 
  633: sub print_footer {
  634:     my ($r,$phase,$newphase,$button_text,$actions) = @_;
  635:     $button_text = &mt($button_text);
  636:     $r->print('<input type="hidden" name="phase" value="" />'.
  637:               '<input type="hidden" name="width" value="'.
  638:               $env{'form.width'}.'" />'.
  639:               '<input type="hidden" name="height" value="'.
  640:               $env{'form.height'}.'" />');
  641:     if (($phase eq 'display') || ($phase eq 'process')) {
  642:         if (ref($actions) eq 'ARRAY') {
  643:             foreach my $item (@{$actions}) {
  644:                 $r->print('<input type="hidden" name="actions" value="'.$item.'" />')."\n";
  645:             }
  646:         }
  647:         $r->print('<input type="hidden" name="numcols" value="'.$env{'form.numcols'}.'" />');
  648:     }
  649:     my $dest='"javascript:changePage(document.'.$phase.','."'$newphase'".')"';
  650:     if ($phase eq 'process') {
  651:         $r->print('<p><a href='.$dest.'>'.$button_text.'</a></p>');
  652:     } else {
  653:         my $onclick;
  654:         if ($phase eq 'display') {
  655:             $onclick = '"javascript:changePage(document.'.$phase.','."'$newphase'".')"';
  656:         } else {
  657:             $onclick = '"javascript:changePage(document.'.$phase.','."'$newphase'".')"';
  658:         } 
  659:         $r->print('<p><input type="button" name="store" value="'.
  660:                   $button_text.'" onclick='.$onclick.' /></p>');
  661:     }
  662:     if ($phase eq 'process') {
  663:         $r->print('</form>'.&Apache::loncommon::end_page());
  664:     }
  665:     return;
  666: }
  667: 
  668: sub print_login {
  669:     my ($dom,$confname,$phase,$settings,$rowtotal) = @_;
  670:     my %choices = &login_choices();
  671:     my %defaultchecked = ( 
  672:                            'coursecatalog' => 'on',
  673:                            'adminmail'     => 'off',
  674:                            'newuser'       => 'off',
  675:                          );
  676:     my @toggles = ('coursecatalog','adminmail','newuser');
  677:     my (%checkedon,%checkedoff);
  678:     foreach my $item (@toggles) {
  679:         if ($defaultchecked{$item} eq 'on') { 
  680:             $checkedon{$item} = ' checked="checked" ';
  681:             $checkedoff{$item} = ' ';
  682:         } elsif ($defaultchecked{$item} eq 'off') {
  683:             $checkedoff{$item} = ' checked="checked" ';
  684:             $checkedon{$item} = ' ';
  685:         }
  686:     }
  687:     my $loginheader = 'image';
  688:     my @images = ('img','logo','domlogo','login');
  689:     my @logintext = ('textcol','bgcol');
  690:     my @bgs = ('pgbg','mainbg','sidebg');
  691:     my @links = ('link','alink','vlink');
  692:     my %designhash = &Apache::loncommon::get_domainconf($dom);
  693:     my %defaultdesign = %Apache::loncommon::defaultdesign;
  694:     my (%is_custom,%designs);
  695:     my %defaults = (
  696:                    font => $defaultdesign{'login.font'},
  697:                    );
  698:     foreach my $item (@images) {
  699:         $defaults{$item} = $defaultdesign{'login.'.$item};
  700:     }
  701:     foreach my $item (@bgs) {
  702:         $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
  703:     }
  704:     foreach my $item (@logintext) {
  705:         $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
  706:     }
  707:     foreach my $item (@links) {
  708:         $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
  709:     }
  710:     if (ref($settings) eq 'HASH') {
  711:         foreach my $item (@toggles) {
  712:             if ($settings->{$item} eq '1') {
  713:                 $checkedon{$item} =  ' checked="checked" ';
  714:                 $checkedoff{$item} = ' ';
  715:             } elsif ($settings->{$item} eq '0') {
  716:                 $checkedoff{$item} =  ' checked="checked" ';
  717:                 $checkedon{$item} = ' ';
  718:             }
  719:         }
  720:         foreach my $item (@images) {
  721:             if ($settings->{$item} ne '') {
  722:                 $designs{$item} = $settings->{$item};
  723:                 $is_custom{$item} = 1;
  724:             }
  725:         }
  726:         foreach my $item (@logintext) {
  727:             if ($settings->{$item} ne '') {
  728:                 $designs{'logintext'}{$item} = $settings->{$item};
  729:                 $is_custom{$item} = 1;
  730:             }
  731:         }
  732:         if ($settings->{'loginheader'} ne '') {
  733:             $loginheader = $settings->{'loginheader'};
  734:         }
  735:         if ($settings->{'font'} ne '') {
  736:             $designs{'font'} = $settings->{'font'};
  737:             $is_custom{'font'} = 1;
  738:         }
  739:         foreach my $item (@bgs) {
  740:             if ($settings->{$item} ne '') {
  741:                 $designs{'bgs'}{$item} = $settings->{$item};
  742:                 $is_custom{$item} = 1;
  743:             }
  744:         }
  745:         foreach my $item (@links) {
  746:             if ($settings->{$item} ne '') {
  747:                 $designs{'links'}{$item} = $settings->{$item};
  748:                 $is_custom{$item} = 1;
  749:             }
  750:         }
  751:     } else {
  752:         if ($designhash{$dom.'.login.font'} ne '') {
  753:             $designs{'font'} = $designhash{$dom.'.login.font'};
  754:             $is_custom{'font'} = 1;
  755:         }
  756:         foreach my $item (@images) {
  757:             if ($designhash{$dom.'.login.'.$item} ne '') {
  758:                 $designs{$item} = $designhash{$dom.'.login.'.$item};
  759:                 $is_custom{$item} = 1;
  760:             }
  761:         }
  762:         foreach my $item (@bgs) {
  763:             if ($designhash{$dom.'.login.'.$item} ne '') {
  764:                 $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
  765:                 $is_custom{$item} = 1;
  766:             }
  767:         }
  768:         foreach my $item (@links) {
  769:             if ($designhash{$dom.'.login.'.$item} ne '') {
  770:                 $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
  771:                 $is_custom{$item} = 1;
  772:             }
  773:         }
  774:     }
  775:     my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
  776:                                                   logo => 'Institution Logo',
  777:                                                   domlogo => 'Domain Logo',
  778:                                                   login => 'Login box');
  779:     my $itemcount = 1;
  780:     my ($css_class,$datatable);
  781:     foreach my $item (@toggles) {
  782:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
  783:         $datatable .=  
  784:             '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
  785:             '</td><td>'.
  786:             '<span class="LC_nobreak"><label><input type="radio" name="'.
  787:             $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
  788:             '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
  789:             $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
  790:             '</tr>';
  791:         $itemcount ++;
  792:     }
  793:     $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext,$loginheader);
  794:     $datatable .= '</tr></table></td></tr>';
  795:     return $datatable;
  796: }
  797: 
  798: sub login_choices {
  799:     my %choices =
  800:         &Apache::lonlocal::texthash (
  801:             coursecatalog => 'Display Course Catalog link?',
  802:             adminmail => "Display Administrator's E-mail Address?",
  803:             newuser   => "Link to create a user account",
  804:             img => "Header",
  805:             logo => "Main Logo",
  806:             domlogo => "Domain Logo",
  807:             login => "Log-in Header", 
  808:             textcol => "Text color",
  809:             bgcol   => "Box color",
  810:             bgs => "Background colors",
  811:             links => "Link colors",
  812:             font => "Font color",
  813:             pgbg => "Page",
  814:             mainbg => "Main panel",
  815:             sidebg => "Side panel",
  816:             link => "Link",
  817:             alink => "Active link",
  818:             vlink => "Visited link",
  819:         );
  820:     return %choices;
  821: }
  822: 
  823: sub print_rolecolors {
  824:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
  825:     my %choices = &color_font_choices();
  826:     my @bgs = ('pgbg','tabbg','sidebg');
  827:     my @links = ('link','alink','vlink');
  828:     my @images = ('img');
  829:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
  830:     my %designhash = &Apache::loncommon::get_domainconf($dom);
  831:     my %defaultdesign = %Apache::loncommon::defaultdesign;
  832:     my (%is_custom,%designs);
  833:     my %defaults = (
  834:                    img => $defaultdesign{$role.'.img'},
  835:                    font => $defaultdesign{$role.'.font'},
  836:                    );
  837:     foreach my $item (@bgs) {
  838:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
  839:     }
  840:     foreach my $item (@links) {
  841:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
  842:     }
  843:     if (ref($settings) eq 'HASH') {
  844:         if (ref($settings->{$role}) eq 'HASH') {
  845:             if ($settings->{$role}->{'img'} ne '') {
  846:                 $designs{'img'} = $settings->{$role}->{'img'};
  847:                 $is_custom{'img'} = 1;
  848:             }
  849:             if ($settings->{$role}->{'font'} ne '') {
  850:                 $designs{'font'} = $settings->{$role}->{'font'};
  851:                 $is_custom{'font'} = 1;
  852:             }
  853:             foreach my $item (@bgs) {
  854:                 if ($settings->{$role}->{$item} ne '') {
  855:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
  856:                     $is_custom{$item} = 1;
  857:                 }
  858:             }
  859:             foreach my $item (@links) {
  860:                 if ($settings->{$role}->{$item} ne '') {
  861:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
  862:                     $is_custom{$item} = 1;
  863:                 }
  864:             }
  865:         }
  866:     } else {
  867:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  868:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
  869:             $is_custom{'img'} = 1;
  870:         }
  871:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
  872:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
  873:             $is_custom{'font'} = 1;
  874:         }
  875:         foreach my $item (@bgs) {
  876:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
  877:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
  878:                 $is_custom{$item} = 1;
  879:             
  880:             }
  881:         }
  882:         foreach my $item (@links) {
  883:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
  884:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
  885:                 $is_custom{$item} = 1;
  886:             }
  887:         }
  888:     }
  889:     my $itemcount = 1;
  890:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
  891:     $datatable .= '</tr></table></td></tr>';
  892:     return $datatable;
  893: }
  894: 
  895: sub display_color_options {
  896:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
  897:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext,$loginheader) = @_;
  898:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
  899:     my $datatable = '<tr'.$css_class.'>'.
  900:         '<td>'.$choices->{'font'}.'</td>';
  901:     if (!$is_custom->{'font'}) {
  902:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
  903:     } else {
  904:         $datatable .= '<td>&nbsp;</td>';
  905:     }
  906:     my $fontlink = &color_pick($phase,$role,'font',$choices->{'font'},$designs->{'font'});
  907:     $datatable .= '<td><span class="LC_nobreak">'.
  908:                   '<input type="text" size="10" name="'.$role.'_font"'.
  909:                   ' value="'.$designs->{'font'}.'" />&nbsp;'.$fontlink.
  910:                   '&nbsp;<span id="css_'.$role.'_font" style="background-color: '.
  911:                   $designs->{'font'}.';">&nbsp;&nbsp;&nbsp;</span>'.
  912:                   '</span></td></tr>';
  913:     my $switchserver = &check_switchserver($dom,$confname);
  914:     foreach my $img (@{$images}) {
  915: 	$itemcount ++;
  916:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
  917:         $datatable .= '<tr'.$css_class.'>'.
  918:                       '<td>'.$choices->{$img}.'</td>';
  919:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
  920:         if ($designs->{$img} ne '') {
  921:             $imgfile = $designs->{$img};
  922: 	    $img_import = ($imgfile =~ m{^/adm/});
  923:         } else {
  924:             $imgfile = $defaults->{$img};
  925:         }
  926:         if ($img eq 'login') {
  927:             $login_hdr_pick = &login_header_options($img,$role,$defaults,$is_custom,$choices,
  928:                                                     $loginheader);
  929:             $logincolors =
  930:                 &login_text_colors($img,$role,$logintext,$phase,$choices,$designs);
  931:         }
  932:         if ($imgfile) {
  933:             my ($showfile,$fullsize);
  934:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
  935:                 my $urldir = $1;
  936:                 my $filename = $2;
  937:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
  938:                 if (@info) {
  939:                     my $thumbfile = 'tn-'.$filename;
  940:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
  941:                     if (@thumb) {
  942:                         $showfile = $urldir.'/'.$thumbfile;
  943:                     } else {
  944:                         $showfile = $imgfile;
  945:                     }
  946:                 } else {
  947:                     $showfile = '';
  948:                 }
  949:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
  950:                 $showfile = $imgfile;
  951:                 my $imgdir = $1;
  952:                 my $filename = $2;
  953:                 if (-e "/home/httpd/html/$imgdir/tn-".$filename) {
  954:                     $showfile = "/$imgdir/tn-".$filename;
  955:                 } else {
  956:                     my $input = "/home/httpd/html".$imgfile;
  957:                     my $output = '/home/httpd/html/'.$imgdir.'/tn-'.$filename;
  958:                     if (!-e $output) {
  959:                         my ($width,$height) = &thumb_dimensions();
  960:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
  961:                         if ($fullwidth ne '' && $fullheight ne '') {
  962:                             if ($fullwidth > $width && $fullheight > $height) { 
  963:                                 my $size = $width.'x'.$height;
  964:                                 system("convert -sample $size $input $output");
  965:                                 $showfile = '/'.$imgdir.'/tn-'.$filename;
  966:                             }
  967:                         }
  968:                     }
  969:                 }
  970:             }
  971:             if ($showfile) {
  972:                 if ($showfile =~ m{^/(adm|res)/}) {
  973:                     if ($showfile =~ m{^/res/}) {
  974:                         my $local_showfile =
  975:                             &Apache::lonnet::filelocation('',$showfile);
  976:                         &Apache::lonnet::repcopy($local_showfile);
  977:                     }
  978:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
  979:                 }
  980:                 if ($imgfile) {
  981:                     if ($imgfile  =~ m{^/(adm|res)/}) {
  982:                         if ($imgfile =~ m{^/res/}) {
  983:                             my $local_imgfile =
  984:                                 &Apache::lonnet::filelocation('',$imgfile);
  985:                             &Apache::lonnet::repcopy($local_imgfile);
  986:                         }
  987:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
  988:                     } else {
  989:                         $fullsize = $imgfile;
  990:                     }
  991:                 }
  992:                 $datatable .= '<td>';
  993:                 if ($img eq 'login') {
  994:                     $datatable .= $login_hdr_pick;    
  995:                 }
  996:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
  997:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
  998:             } else {
  999:                 $datatable .= '<td colspan="2" class="LC_right_item"><br />'.
 1000:                               &mt('Upload:');
 1001:             }
 1002:         } else {
 1003:             $datatable .= '<td colspan="2" class="LC_right_item"><br />'.
 1004:                           &mt('Upload:');
 1005:         }
 1006:         if ($switchserver) {
 1007:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1008:         } else {
 1009:             $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1010:         }
 1011:         $datatable .= '</td></tr>';
 1012:     }
 1013:     $itemcount ++;
 1014:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1015:     $datatable .= '<tr'.$css_class.'>'.
 1016:                   '<td>'.$choices->{'bgs'}.'</td>';
 1017:     my $bgs_def;
 1018:     foreach my $item (@{$bgs}) {
 1019:         if (!$is_custom->{$item}) {
 1020:             $bgs_def .= '<td>'.$choices->{$item}.'&nbsp;<span id="css_default_'.$role.'_'.$item.'" style="background-color: '.$defaults->{'bgs'}{$item}.';">&nbsp;&nbsp;&nbsp;</span><br />'.$defaults->{'bgs'}{$item}.'</td>';
 1021:         }
 1022:     }
 1023:     if ($bgs_def) {
 1024:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1025:     } else {
 1026:         $datatable .= '<td>&nbsp;</td>';
 1027:     }
 1028:     $datatable .= '<td class="LC_right_item">'.
 1029:                   '<table border="0"><tr>';
 1030:     foreach my $item (@{$bgs}) {
 1031:         my $link = &color_pick($phase,$role,$item,$choices->{$item},$designs->{'bgs'}{$item});
 1032:         $datatable .= '<td align="center">'.$link;
 1033:         if ($designs->{'bgs'}{$item}) {
 1034:             $datatable .= '&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'bgs'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1035:         }
 1036:         $datatable .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.$designs->{'bgs'}{$item}.
 1037:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1038:     }
 1039:     $datatable .= '</tr></table></td></tr>';
 1040:     $itemcount ++;
 1041:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1042:     $datatable .= '<tr'.$css_class.'>'.
 1043:                   '<td>'.$choices->{'links'}.'</td>';
 1044:     my $links_def;
 1045:     foreach my $item (@{$links}) {
 1046:         if (!$is_custom->{$item}) {
 1047:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1048:         }
 1049:     }
 1050:     if ($links_def) {
 1051:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1052:     } else {
 1053:         $datatable .= '<td>&nbsp;</td>';
 1054:     }
 1055:     $datatable .= '<td class="LC_right_item">'.
 1056:                   '<table border="0"><tr>';
 1057:     foreach my $item (@{$links}) {
 1058:         $datatable .= '<td align="center">'."\n".
 1059:                       &color_pick($phase,$role,$item,$choices->{$item},
 1060:                                   $designs->{'links'}{$item});
 1061:         if ($designs->{'links'}{$item}) {
 1062:             $datatable.='&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'links'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1063:         }
 1064:         $datatable .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.$designs->{'links'}{$item}.
 1065:                       '" /></td>';
 1066:     }
 1067:     $$rowtotal += $itemcount;
 1068:     return $datatable;
 1069: }
 1070: 
 1071: sub login_header_options  {
 1072:     my ($img,$role,$defaults,$is_custom,$choices,$loginheader) = @_;
 1073:     my $image_checked = ' checked="checked" ';
 1074:     my $text_checked = ' ';
 1075:     if ($loginheader eq 'text') {
 1076:         $image_checked = ' ';
 1077:         $text_checked = ' checked="checked" ';
 1078:     }
 1079:     my $output = '<span class="LC_nobreak"><label><input type="radio" name="'.
 1080:               'loginheader" value="image" '.$image_checked.'/>'.
 1081:               &mt('use image').'</label>&nbsp;&nbsp;&nbsp;'.
 1082:               '<label><input type="radio" name="loginheader" value="text"'.
 1083:               $text_checked.'/>'.&mt('use text').'</label><br />'."\n";
 1084:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1085:         $output .= &mt('Text default(s)').':<br />';
 1086:         if (!$is_custom->{'textcol'}) {
 1087:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1088:                        '&nbsp;&nbsp;&nbsp;';
 1089:         }
 1090:         if (!$is_custom->{'bgcol'}) {
 1091:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1092:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1093:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1094:         }
 1095:         $output .= '<br />';
 1096:     }
 1097:     $output .='<br />';
 1098:     return $output;
 1099: }
 1100: 
 1101: sub login_text_colors {
 1102:     my ($img,$role,$logintext,$phase,$choices,$designs) = @_;
 1103:     my $color_menu = '<table border="0"><tr>';
 1104:     foreach my $item (@{$logintext}) {
 1105:         my $link = &color_pick($phase,$role,$item,$choices->{$item},$designs->{'logintext'}{$item});
 1106:         $color_menu .= '<td align="center">'.$link;
 1107:         if ($designs->{'logintext'}{$item}) {
 1108:             $color_menu .= '&nbsp;<span id="css_'.$role.'_'.$item.'" style="background-color: '.$designs->{'logintext'}{$item}.';">&nbsp;&nbsp;&nbsp;</span>';
 1109:         }
 1110:         $color_menu .= '<br /><input type="text" size="8" name="'.$role.'_'.$item.'" value="'.
 1111:                        $designs->{'logintext'}{$item}.'" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>'.
 1112:                        '<td>&nbsp;</td>';
 1113:     }
 1114:     $color_menu .= '</tr></table><br />';
 1115:     return $color_menu;
 1116: }
 1117: 
 1118: sub image_changes {
 1119:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1120:     my $output;
 1121:     if (!$is_custom) {
 1122:         if ($img eq 'login') {
 1123:             $output .= &mt('Default image:').'<br />';
 1124:         } else {
 1125:             $output .= &mt('Default in use:').'<br />';
 1126:         }
 1127:     }
 1128:     if ($img_import) {
 1129:         $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1130:     }
 1131:     $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1132:                $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1133:     if ($is_custom) {
 1134:         $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1135:                    '<input type="checkbox" name="'.
 1136:                    $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1137:                    '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1138:     } else {
 1139:         $output .= '<td valign="bottom">'.$logincolors.&mt('Upload:').'<br />';
 1140:     }
 1141:     return $output;
 1142: }
 1143: 
 1144: sub color_pick {
 1145:     my ($phase,$role,$item,$desc,$curcol) = @_;
 1146:     my $link = '<a href="javascript:pjump('."'color_custom','".$desc.
 1147:                "','".$curcol."','".$role.'_'.$item."','parmform.pres','psub'".
 1148:                ');">'.$desc.'</a>';
 1149:     return $link;
 1150: }
 1151: 
 1152: sub color_pick_js {
 1153:     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
 1154:     my $output = <<"ENDCOL";
 1155:     function pclose() {
 1156:         parmwin=window.open("/adm/rat/empty.html","LONCAPAparms","height=350,width=350,scrollbars=no,menubar=no");
 1157:         parmwin.close();
 1158:     }
 1159: 
 1160:     $pjump_def
 1161: 
 1162:     function psub() {
 1163:         pclose();
 1164:         if (document.parmform.pres_marker.value!='') {
 1165:             if (document.parmform.pres_type.value!='') {
 1166:                 eval('document.display.'+
 1167:                      document.parmform.pres_marker.value+
 1168:                      '.value=document.parmform.pres_value.value;');
 1169:             }
 1170:         } else {
 1171:             document.parmform.pres_value.value='';
 1172:             document.parmform.pres_marker.value='';
 1173:         }
 1174:     }
 1175: 
 1176:     function get_id (span_id) {
 1177: 	if (document.getElementById) {
 1178:             return document.getElementById(span_id);
 1179:         }
 1180: 	if (document.all) {
 1181:             return document.all[span_id];
 1182:         } 
 1183: 	return false;
 1184:     }
 1185: 
 1186:     function colchg_span (span_id_str,new_color_item) {
 1187: 	var span_ref = get_id(span_id_str);
 1188: 	if (span_ref.style) { span_ref = span_ref.style; }
 1189: 	span_ref.background = new_color_item.value;
 1190: 	span_ref.backgroundColor = new_color_item.value;
 1191: 	span_ref.bgColor = new_color_item.value;
 1192:     }
 1193: 
 1194: ENDCOL
 1195:     return $output;
 1196: }
 1197: 
 1198: sub print_quotas {
 1199:     my ($dom,$settings,$rowtotal) = @_;
 1200:     my $datatable;
 1201:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1202:     my $typecount = 0;
 1203:     my $css_class;
 1204:     if (ref($types) eq 'ARRAY') {
 1205:         foreach my $type (@{$types}) {
 1206:             if (defined($usertypes->{$type})) {
 1207:                 $typecount ++;
 1208:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1209:                 $datatable .= '<tr'.$css_class.'>'. 
 1210:                               '<td>'.$usertypes->{$type}.'</td>'.
 1211:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 1212:                               '<input type="text" name="quota_'.$type.
 1213:                               '" value="'.$settings->{$type}.
 1214:                               '" size="5" /> Mb</span></td></tr>';
 1215:             }
 1216:         }
 1217:     }
 1218:     my $defaultquota = '20';
 1219:     if (ref($settings) eq 'HASH') {
 1220:         if (defined($settings->{'default'})) {
 1221:             $defaultquota = $settings->{'default'};
 1222:         }
 1223:     }
 1224:     $typecount ++;
 1225:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 1226:     $datatable .= '<tr'.$css_class.'>'.
 1227:                   '<td>'.$othertitle.'</td>'.
 1228:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 1229:                   '<input type="text" name="defaultquota" value="'.
 1230:                   $defaultquota.'" size="5" /> Mb</span></td></tr>';
 1231:     $$rowtotal += $typecount;
 1232:     return $datatable;
 1233: }
 1234: 
 1235: sub print_autoenroll {
 1236:     my ($dom,$settings,$rowtotal) = @_;
 1237:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 1238:     my ($defdom,$runon,$runoff);
 1239:     if (ref($settings) eq 'HASH') {
 1240:         if (exists($settings->{'run'})) {
 1241:             if ($settings->{'run'} eq '0') {
 1242:                 $runoff = ' checked="checked" ';
 1243:                 $runon = ' ';
 1244:             } else {
 1245:                 $runon = ' checked="checked" ';
 1246:                 $runoff = ' ';
 1247:             }
 1248:         } else {
 1249:             if ($autorun) {
 1250:                 $runon = ' checked="checked" ';
 1251:                 $runoff = ' ';
 1252:             } else {
 1253:                 $runoff = ' checked="checked" ';
 1254:                 $runon = ' ';
 1255:             }
 1256:         }
 1257:         if (exists($settings->{'sender_domain'})) {
 1258:             $defdom = $settings->{'sender_domain'};
 1259:         }
 1260:     } else {
 1261:         if ($autorun) {
 1262:             $runon = ' checked="checked" ';
 1263:             $runoff = ' ';
 1264:         } else {
 1265:             $runoff = ' checked="checked" ';
 1266:             $runon = ' ';
 1267:         }
 1268:     }
 1269:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 1270:     my $notif_sender;
 1271:     if (ref($settings) eq 'HASH') {
 1272:         $notif_sender = $settings->{'sender_uname'};
 1273:     }
 1274:     my $datatable='<tr class="LC_odd_row">'.
 1275:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 1276:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 1277:                   '<input type="radio" name="autoenroll_run"'.
 1278:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 1279:                   '<label><input type="radio" name="autoenroll_run"'.
 1280:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 1281:                   '</tr><tr>'.
 1282:                   '<td>'.&mt('Notification messages - sender').
 1283:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 1284:                   &mt('username').':&nbsp;'.
 1285:                   '<input type="text" name="sender_uname" value="'.
 1286:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 1287:                   ':&nbsp;'.$domform.'</span></td></tr>';
 1288:     $$rowtotal += 2;
 1289:     return $datatable;
 1290: }
 1291: 
 1292: sub print_autoupdate {
 1293:     my ($position,$dom,$settings,$rowtotal) = @_;
 1294:     my $datatable;
 1295:     if ($position eq 'top') {
 1296:         my $updateon = ' ';
 1297:         my $updateoff = ' checked="checked" ';
 1298:         my $classlistson = ' ';
 1299:         my $classlistsoff = ' checked="checked" ';
 1300:         if (ref($settings) eq 'HASH') {
 1301:             if ($settings->{'run'} eq '1') {
 1302:                 $updateon = $updateoff;
 1303:                 $updateoff = ' ';
 1304:             }
 1305:             if ($settings->{'classlists'} eq '1') {
 1306:                 $classlistson = $classlistsoff;
 1307:                 $classlistsoff = ' ';
 1308:             }
 1309:         }
 1310:         my %title = (
 1311:                    run => 'Auto-update active?',
 1312:                    classlists => 'Update information in classlists?',
 1313:                     );
 1314:         $datatable = '<tr class="LC_odd_row">'. 
 1315:                   '<td>'.&mt($title{'run'}).'</td>'.
 1316:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 1317:                   '<input type="radio" name="autoupdate_run"'.
 1318:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 1319:                   '<label><input type="radio" name="autoupdate_run"'.
 1320:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 1321:                   '</tr><tr>'.
 1322:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 1323:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 1324:                   '<label><input type="radio" name="classlists"'.
 1325:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 1326:                   '<label><input type="radio" name="classlists"'.
 1327:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 1328:                   '</tr>';
 1329:         $$rowtotal += 2;
 1330:     } else {
 1331:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1332:         my @fields = ('lastname','firstname','middlename','gen',
 1333:                       'permanentemail','id');
 1334:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 1335:         my $numrows = 0;
 1336:         if (ref($types) eq 'ARRAY') {
 1337:             if (@{$types} > 0) {
 1338:                 $datatable = 
 1339:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 1340:                                          \@fields,$types,\$numrows);
 1341:                     $$rowtotal += @{$types}; 
 1342:             }
 1343:         }
 1344:         $datatable .= 
 1345:             &usertype_update_row($settings,{'default' => $othertitle},
 1346:                                  \%fieldtitles,\@fields,['default'],
 1347:                                  \$numrows);
 1348:         $$rowtotal ++;     
 1349:     }
 1350:     return $datatable;
 1351: }
 1352: 
 1353: sub print_directorysrch {
 1354:     my ($dom,$settings,$rowtotal) = @_;
 1355:     my $srchon = ' ';
 1356:     my $srchoff = ' checked="checked" ';
 1357:     my ($exacton,$containson,$beginson);
 1358:     my $localon = ' ';
 1359:     my $localoff = ' checked="checked" ';
 1360:     if (ref($settings) eq 'HASH') {
 1361:         if ($settings->{'available'} eq '1') {
 1362:             $srchon = $srchoff;
 1363:             $srchoff = ' ';
 1364:         }
 1365:         if ($settings->{'localonly'} eq '1') {
 1366:             $localon = $localoff;
 1367:             $localoff = ' ';
 1368:         }
 1369:         if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 1370:             foreach my $type (@{$settings->{'searchtypes'}}) {
 1371:                 if ($type eq 'exact') {
 1372:                     $exacton = ' checked="checked" ';
 1373:                 } elsif ($type eq 'contains') {
 1374:                     $containson = ' checked="checked" ';
 1375:                 } elsif ($type eq 'begins') {
 1376:                     $beginson = ' checked="checked" ';
 1377:                 }
 1378:             }
 1379:         } else {
 1380:             if ($settings->{'searchtypes'} eq 'exact') {
 1381:                 $exacton = ' checked="checked" ';
 1382:             } elsif ($settings->{'searchtypes'} eq 'contains') {
 1383:                 $containson = ' checked="checked" ';
 1384:             } elsif ($settings->{'searchtypes'} eq 'specify') {
 1385:                 $exacton = ' checked="checked" ';
 1386:                 $containson = ' checked="checked" ';
 1387:             }
 1388:         }
 1389:     }
 1390:     my ($searchtitles,$titleorder) = &sorted_searchtitles();
 1391:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1392: 
 1393:     my $numinrow = 4;
 1394:     my $cansrchrow = 0;
 1395:     my $datatable='<tr class="LC_odd_row">'.
 1396:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Directory search available?').'</span></td>'.
 1397:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 1398:                   '<input type="radio" name="dirsrch_available"'.
 1399:                   $srchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 1400:                   '<label><input type="radio" name="dirsrch_available"'.
 1401:                   $srchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 1402:                   '</tr><tr>'.
 1403:                   '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search?').'</span></td>'.
 1404:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 1405:                   '<input type="radio" name="dirsrch_localonly"'.
 1406:                   $localoff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 1407:                   '<label><input type="radio" name="dirsrch_localonly"'.
 1408:                   $localon.' value="1" />'.&mt('No').'</label></span></td>'.
 1409:                   '</tr>';
 1410:     $$rowtotal += 2;
 1411:     if (ref($usertypes) eq 'HASH') {
 1412:         if (keys(%{$usertypes}) > 0) {
 1413:             $datatable .= &users_cansearch_row($settings,$types,$usertypes,$dom,
 1414:                                                $numinrow,$othertitle);
 1415:             $cansrchrow = 1;
 1416:         }
 1417:     }
 1418:     if ($cansrchrow) {
 1419:         $$rowtotal ++;
 1420:         $datatable .= '<tr>';
 1421:     } else {
 1422:         $datatable .= '<tr class="LC_odd_row">';
 1423:     }
 1424:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 1425:                   '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 1426:     foreach my $title (@{$titleorder}) {
 1427:         if (defined($searchtitles->{$title})) {
 1428:             my $check = ' ';
 1429:             if (ref($settings) eq 'HASH') { 
 1430:                 if (ref($settings->{'searchby'}) eq 'ARRAY') {
 1431:                     if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 1432:                         $check = ' checked="checked" ';
 1433:                     }
 1434:                 }
 1435:             }
 1436:             $datatable .= '<td class="LC_left_item">'.
 1437:                           '<span class="LC_nobreak"><label>'.
 1438:                           '<input type="checkbox" name="searchby" '.
 1439:                           'value="'.$title.'"'.$check.'/>'.
 1440:                           $searchtitles->{$title}.'</label></span></td>';
 1441:         }
 1442:     }
 1443:     $datatable .= '</tr></table></td></tr>';
 1444:     $$rowtotal ++;
 1445:     if ($cansrchrow) {
 1446:         $datatable .= '<tr class="LC_odd_row">';
 1447:     } else {
 1448:         $datatable .= '<tr>';
 1449:     }
 1450:     $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 1451:                   '<td class="LC_left_item" colspan="2">'.
 1452:                   '<span class="LC_nobreak"><label>'.
 1453:                   '<input type="checkbox" name="searchtypes" '.
 1454:                   $exacton.' value="exact" />'.&mt('Exact match').
 1455:                   '</label>&nbsp;'.
 1456:                   '<label><input type="checkbox" name="searchtypes" '.
 1457:                   $beginson.' value="begins" />'.&mt('Begins with').
 1458:                   '</label>&nbsp;'.
 1459:                   '<label><input type="checkbox" name="searchtypes" '.
 1460:                   $containson.' value="contains" />'.&mt('Contains').
 1461:                   '</label></span></td></tr>';
 1462:     $$rowtotal ++;
 1463:     return $datatable;
 1464: }
 1465: 
 1466: sub print_contacts {
 1467:     my ($dom,$settings,$rowtotal) = @_;
 1468:     my $datatable;
 1469:     my @contacts = ('adminemail','supportemail');
 1470:     my (%checked,%to,%otheremails);
 1471:     my @mailings = ('errormail','packagesmail','helpdeskmail');
 1472:     foreach my $type (@mailings) {
 1473:         $otheremails{$type} = '';
 1474:     }
 1475:     if (ref($settings) eq 'HASH') {
 1476:         foreach my $item (@contacts) {
 1477:             if (exists($settings->{$item})) {
 1478:                 $to{$item} = $settings->{$item};
 1479:             }
 1480:         }
 1481:         foreach my $type (@mailings) {
 1482:             if (exists($settings->{$type})) {
 1483:                 if (ref($settings->{$type}) eq 'HASH') {
 1484:                     foreach my $item (@contacts) {
 1485:                         if ($settings->{$type}{$item}) {
 1486:                             $checked{$type}{$item} = ' checked="checked" ';
 1487:                         }
 1488:                     }
 1489:                     $otheremails{$type} = $settings->{$type}{'others'};
 1490:                 }
 1491:             }
 1492:         }
 1493:     } else {
 1494:         $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 1495:         $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 1496:         $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 1497:         $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 1498:         $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" '; 
 1499:     }
 1500:     my ($titles,$short_titles) = &contact_titles();
 1501:     my $rownum = 0;
 1502:     my $css_class;
 1503:     foreach my $item (@contacts) {
 1504:         $rownum ++;
 1505:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 1506:         $datatable .= '<tr'.$css_class.'>'. 
 1507:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 1508:                   '</span></td><td class="LC_right_item">'.
 1509:                   '<input type="text" name="'.$item.'" value="'.
 1510:                   $to{$item}.'" /></td></tr>';
 1511:     }
 1512:     foreach my $type (@mailings) {
 1513:         $rownum ++;
 1514:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 1515:         $datatable .= '<tr'.$css_class.'>'.
 1516:                       '<td><span class="LC_nobreak">'.
 1517:                       $titles->{$type}.': </span></td>'.
 1518:                       '<td class="LC_left_item">'.
 1519:                       '<span class="LC_nobreak">';
 1520:         foreach my $item (@contacts) {
 1521:             $datatable .= '<label>'.
 1522:                           '<input type="checkbox" name="'.$type.'"'.
 1523:                           $checked{$type}{$item}.
 1524:                           ' value="'.$item.'" />'.$short_titles->{$item}.
 1525:                           '</label>&nbsp;';
 1526:         }
 1527:         $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 1528:                       '<input type="text" name="'.$type.'_others" '.
 1529:                       'value="'.$otheremails{$type}.'"  />'.
 1530:                       '</td></tr>'."\n";
 1531:     }
 1532:     $$rowtotal += $rownum;
 1533:     return $datatable;
 1534: }
 1535: 
 1536: sub contact_titles {
 1537:     my %titles = &Apache::lonlocal::texthash (
 1538:                    'supportemail' => 'Support E-mail address',
 1539:                    'adminemail'   => 'Default Server Admin E-mail address',
 1540:                    'errormail'    => 'Error reports to be e-mailed to',
 1541:                    'packagesmail' => 'Package update alerts to be e-mailed to',
 1542:                    'helpdeskmail' => 'Helpdesk requests to be e-mailed to'
 1543:                  );
 1544:     my %short_titles = &Apache::lonlocal::texthash (
 1545:                            adminemail   => 'Admin E-mail address',
 1546:                            supportemail => 'Support E-mail',
 1547:                        );   
 1548:     return (\%titles,\%short_titles);
 1549: }
 1550: 
 1551: sub print_usercreation {
 1552:     my ($position,$dom,$settings,$rowtotal) = @_;
 1553:     my $numinrow = 4;
 1554:     my $datatable;
 1555:     if ($position eq 'top') {
 1556:         $$rowtotal ++;
 1557:         my $rowcount = 0;
 1558:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 1559:         if (ref($rules) eq 'HASH') {
 1560:             if (keys(%{$rules}) > 0) {
 1561:                 $datatable .= &user_formats_row('username',$settings,$rules,
 1562:                                                 $ruleorder,$numinrow,$rowcount);
 1563:                 $$rowtotal ++;
 1564:                 $rowcount ++;
 1565:             }
 1566:         }
 1567:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 1568:         if (ref($idrules) eq 'HASH') {
 1569:             if (keys(%{$idrules}) > 0) {
 1570:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 1571:                                                 $idruleorder,$numinrow,$rowcount);
 1572:                 $$rowtotal ++;
 1573:                 $rowcount ++;
 1574:             }
 1575:         }
 1576:         my ($emailrules,$emailruleorder) = 
 1577:             &Apache::lonnet::inst_userrules($dom,'email');
 1578:         if (ref($emailrules) eq 'HASH') {
 1579:             if (keys(%{$emailrules}) > 0) {
 1580:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 1581:                                                 $emailruleorder,$numinrow,$rowcount);
 1582:                 $$rowtotal ++;
 1583:                 $rowcount ++;
 1584:             }
 1585:         }
 1586:         if ($rowcount == 0) {
 1587:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 1588:             $$rowtotal ++;
 1589:             $rowcount ++;
 1590:         }
 1591:     } elsif ($position eq 'middle') {
 1592:         my @creators = ('author','course','selfcreate');
 1593:         my ($rules,$ruleorder) =
 1594:             &Apache::lonnet::inst_userrules($dom,'username');
 1595:         my %lt = &usercreation_types();
 1596:         my %checked;
 1597:         my @selfcreate; 
 1598:         if (ref($settings) eq 'HASH') {
 1599:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 1600:                 foreach my $item (@creators) {
 1601:                     $checked{$item} = $settings->{'cancreate'}{$item};
 1602:                 }
 1603:                 if (ref($settings->{'cancreate'}{'selfcreate'}) eq 'ARRAY') {
 1604:                     @selfcreate = @{$settings->{'cancreate'}{'selfcreate'}};
 1605:                 } elsif ($settings->{'cancreate'}{'selfcreate'} ne '') {
 1606:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 1607:                         @selfcreate = ('email','login','sso');
 1608:                     } elsif ($settings->{'cancreate'}{'selfcreate'} ne 'none') {
 1609:                         @selfcreate = ($settings->{'cancreate'}{'selfcreate'});
 1610:                     }
 1611:                 }
 1612:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 1613:                 foreach my $item (@creators) {
 1614:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 1615:                         $checked{$item} = 'none';
 1616:                     }
 1617:                 }
 1618:             }
 1619:         }
 1620:         my $rownum = 0;
 1621:         foreach my $item (@creators) {
 1622:             $rownum ++;
 1623:             if ($item ne 'selfcreate') {  
 1624:                 if ($checked{$item} eq '') {
 1625:                     $checked{$item} = 'any';
 1626:                 }
 1627:             }
 1628:             my $css_class;
 1629:             if ($rownum%2) {
 1630:                 $css_class = '';
 1631:             } else {
 1632:                 $css_class = ' class="LC_odd_row" ';
 1633:             }
 1634:             $datatable .= '<tr'.$css_class.'>'.
 1635:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 1636:                          '</span></td><td align="right">';
 1637:             my @options;
 1638:             if ($item eq 'selfcreate') {
 1639:                 push(@options,('email','login','sso'));
 1640:             } else {
 1641:                 @options = ('any');
 1642:                 if (ref($rules) eq 'HASH') {
 1643:                     if (keys(%{$rules}) > 0) {
 1644:                         push(@options,('official','unofficial'));
 1645:                     }
 1646:                 }
 1647:                 push(@options,'none');
 1648:             }
 1649:             foreach my $option (@options) {
 1650:                 my $type = 'radio';
 1651:                 my $check = ' ';
 1652:                 if ($item eq 'selfcreate') {
 1653:                     $type = 'checkbox';
 1654:                     if (grep(/^\Q$option\E$/,@selfcreate)) {
 1655:                         $check = ' checked="checked" ';
 1656:                     }
 1657:                 } else {
 1658:                     if ($checked{$item} eq $option) {
 1659:                         $check = ' checked="checked" ';
 1660:                     }
 1661:                 } 
 1662:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1663:                               '<input type="'.$type.'" name="can_createuser_'.
 1664:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 1665:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 1666:             }
 1667:             $datatable .= '</td></tr>';
 1668:         }
 1669:     } else {
 1670:         my @contexts = ('author','course','domain');
 1671:         my @authtypes = ('int','krb4','krb5','loc');
 1672:         my %checked;
 1673:         if (ref($settings) eq 'HASH') {
 1674:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 1675:                 foreach my $item (@contexts) {
 1676:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 1677:                         foreach my $auth (@authtypes) {
 1678:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 1679:                                 $checked{$item}{$auth} = ' checked="checked" ';
 1680:                             }
 1681:                         }
 1682:                     }
 1683:                 }
 1684:             }
 1685:         } else {
 1686:             foreach my $item (@contexts) {
 1687:                 foreach my $auth (@authtypes) {
 1688:                     $checked{$item}{$auth} = ' checked="checked" ';
 1689:                 }
 1690:             }
 1691:         }
 1692:         my %title = &context_names();
 1693:         my %authname = &authtype_names();
 1694:         my $rownum = 0;
 1695:         my $css_class; 
 1696:         foreach my $item (@contexts) {
 1697:             if ($rownum%2) {
 1698:                 $css_class = '';
 1699:             } else {
 1700:                 $css_class = ' class="LC_odd_row" ';
 1701:             }
 1702:             $datatable .=   '<tr'.$css_class.'>'.
 1703:                             '<td>'.$title{$item}.
 1704:                             '</td><td class="LC_left_item">'.
 1705:                             '<span class="LC_nobreak">';
 1706:             foreach my $auth (@authtypes) {
 1707:                 $datatable .= '<label>'. 
 1708:                               '<input type="checkbox" name="'.$item.'_auth" '.
 1709:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 1710:                               $authname{$auth}.'</label>&nbsp;';
 1711:             }
 1712:             $datatable .= '</span></td></tr>';
 1713:             $rownum ++;
 1714:         }
 1715:         $$rowtotal += $rownum;
 1716:     }
 1717:     return $datatable;
 1718: }
 1719: 
 1720: sub user_formats_row {
 1721:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 1722:     my $output;
 1723:     my %text = (
 1724:                    'username' => 'new usernames',
 1725:                    'id'       => 'IDs',
 1726:                    'email'    => 'self-created accounts (e-mail)',
 1727:                );
 1728:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 1729:     $output = '<tr '.$css_class.'>'.
 1730:               '<td><span class="LC_nobreak">';
 1731:     if ($type eq 'email') {
 1732:         $output .= &mt("Formats disallowed for $text{$type}: ");
 1733:     } else {
 1734:         $output .= &mt("Format rules to check for $text{$type}: ");
 1735:     }
 1736:     $output .= '</span></td>'.
 1737:                '<td class="LC_left_item" colspan="2"><table>';
 1738:     my $rem;
 1739:     if (ref($ruleorder) eq 'ARRAY') {
 1740:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 1741:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 1742:                 my $rem = $i%($numinrow);
 1743:                 if ($rem == 0) {
 1744:                     if ($i > 0) {
 1745:                         $output .= '</tr>';
 1746:                     }
 1747:                     $output .= '<tr>';
 1748:                 }
 1749:                 my $check = ' ';
 1750:                 if (ref($settings) eq 'HASH') {
 1751:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 1752:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 1753:                             $check = ' checked="checked" ';
 1754:                         }
 1755:                     }
 1756:                 }
 1757:                 $output .= '<td class="LC_left_item">'.
 1758:                            '<span class="LC_nobreak"><label>'.
 1759:                            '<input type="checkbox" name="'.$type.'_rule" '.
 1760:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 1761:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 1762:             }
 1763:         }
 1764:         $rem = @{$ruleorder}%($numinrow);
 1765:     }
 1766:     my $colsleft = $numinrow - $rem;
 1767:     if ($colsleft > 1 ) {
 1768:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 1769:                    '&nbsp;</td>';
 1770:     } elsif ($colsleft == 1) {
 1771:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 1772:     }
 1773:     $output .= '</tr></table></td></tr>';
 1774:     return $output;
 1775: }
 1776: 
 1777: sub usercreation_types {
 1778:     my %lt = &Apache::lonlocal::texthash (
 1779:                     author     => 'When adding a co-author',
 1780:                     course     => 'When adding a user to a course',
 1781:                     selfcreate => 'User creates own account', 
 1782:                     any        => 'Any',
 1783:                     official   => 'Institutional only ',
 1784:                     unofficial => 'Non-institutional only',
 1785:                     email      => 'Email address',
 1786:                     login      => 'Institutional Login',
 1787:                     sso        => 'SSO', 
 1788:                     none       => 'None',
 1789:     );
 1790:     return %lt;
 1791: }
 1792: 
 1793: sub authtype_names {
 1794:     my %lt = &Apache::lonlocal::texthash(
 1795:                       int    => 'Internal',
 1796:                       krb4   => 'Kerberos 4',
 1797:                       krb5   => 'Kerberos 5',
 1798:                       loc    => 'Local',
 1799:                   );
 1800:     return %lt;
 1801: }
 1802: 
 1803: sub context_names {
 1804:     my %context_title = &Apache::lonlocal::texthash(
 1805:        author => 'Creating users when an Author',
 1806:        course => 'Creating users when in a course',
 1807:        domain => 'Creating users when a Domain Coordinator',
 1808:     );
 1809:     return %context_title;
 1810: }
 1811: 
 1812: sub print_usermodification {
 1813:     my ($position,$dom,$settings,$rowtotal) = @_;
 1814:     my $numinrow = 4;
 1815:     my ($context,$datatable,$rowcount);
 1816:     if ($position eq 'top') {
 1817:         $rowcount = 0;
 1818:         $context = 'author'; 
 1819:         foreach my $role ('ca','aa') {
 1820:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 1821:                                                    $numinrow,$rowcount);
 1822:             $$rowtotal ++;
 1823:             $rowcount ++;
 1824:         }
 1825:     } elsif ($position eq 'middle') {
 1826:         $context = 'course';
 1827:         $rowcount = 0;
 1828:         foreach my $role ('st','ep','ta','in','cr') {
 1829:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 1830:                                                    $numinrow,$rowcount);
 1831:             $$rowtotal ++;
 1832:             $rowcount ++;
 1833:         }
 1834:     } elsif ($position eq 'bottom') {
 1835:         $context = 'selfcreate';
 1836:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1837:         $usertypes->{'default'} = $othertitle;
 1838:         if (ref($types) eq 'ARRAY') {
 1839:             push(@{$types},'default');
 1840:             $usertypes->{'default'} = $othertitle;
 1841:             foreach my $status (@{$types}) {
 1842:                 $datatable .= &modifiable_userdata_row($context,$status,$settings,
 1843:                                                        $numinrow,$rowcount,$usertypes);
 1844:                 $$rowtotal ++;
 1845:                 $rowcount ++;
 1846:             }
 1847:         }
 1848:     }
 1849:     return $datatable;
 1850: }
 1851: 
 1852: sub print_defaults {
 1853:     my ($dom,$rowtotal) = @_;
 1854:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 1855:                  'datelocale_def');
 1856:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 1857:     my $titles = &defaults_titles();
 1858:     my $rownum = 0;
 1859:     my ($datatable,$css_class);
 1860:     foreach my $item (@items) {
 1861:         if ($rownum%2) {
 1862:             $css_class = '';
 1863:         } else {
 1864:             $css_class = ' class="LC_odd_row" ';
 1865:         }
 1866:         $datatable .= '<tr'.$css_class.'>'.
 1867:                   '<td><span class="LC_nobreak">'.$titles->{$item}.
 1868:                   '</span></td><td class="LC_right_item">';
 1869:         if ($item eq 'auth_def') {
 1870:             my @authtypes = ('internal','krb4','krb5','localauth');
 1871:             my %shortauth = (
 1872:                              internal => 'int',
 1873:                              krb4 => 'krb4',
 1874:                              krb5 => 'krb5',
 1875:                              localauth  => 'loc'
 1876:                            );
 1877:             my %authnames = &authtype_names();
 1878:             foreach my $auth (@authtypes) {
 1879:                 my $checked = ' ';
 1880:                 if ($domdefaults{$item} eq $auth) {
 1881:                     $checked = ' checked="checked" ';
 1882:                 }
 1883:                 $datatable .= '<label><input type="radio" name="'.$item.
 1884:                               '" value="'.$auth.'"'.$checked.'/>'.
 1885:                               $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 1886:             }
 1887:         } elsif ($item eq 'timezone_def') {
 1888:             my $includeempty = 1;
 1889:             $datatable .= &Apache::loncommon::select_timezone($item,$domdefaults{$item},undef,$includeempty);
 1890:         } elsif ($item eq 'datelocale_def') {
 1891:             my $includeempty = 1;
 1892:             $datatable .= &Apache::loncommon::select_datelocale($item,$domdefaults{$item},undef,$includeempty);
 1893:         } else {
 1894:             $datatable .= '<input type="text" name="'.$item.'" value="'.
 1895:                           $domdefaults{$item}.'" />';
 1896:         }
 1897:         $datatable .= '</td></tr>';
 1898:         $rownum ++;
 1899:     }
 1900:     $$rowtotal += $rownum;
 1901:     return $datatable;
 1902: }
 1903: 
 1904: sub defaults_titles {
 1905:     my %titles = &Apache::lonlocal::texthash (
 1906:                    'auth_def'      => 'Default authentication type',
 1907:                    'auth_arg_def'  => 'Default authentication argument',
 1908:                    'lang_def'      => 'Default language',
 1909:                    'timezone_def'  => 'Default timezone',
 1910:                    'datelocale_def' => 'Default locale for dates',
 1911:                  );
 1912:     return (\%titles);
 1913: }
 1914: 
 1915: sub print_scantronformat {
 1916:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 1917:     my $itemcount = 1;
 1918:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 1919:         %confhash);
 1920:     my $switchserver = &check_switchserver($dom,$confname);
 1921:     my %lt = &Apache::lonlocal::texthash (
 1922:                 default => 'Default scantron format file error',
 1923:                 custom  => 'Custom scantron format file error',
 1924:              );
 1925:     my %scantronfiles = (
 1926:         default => 'default.tab',
 1927:         custom => 'custom.tab',
 1928:     );
 1929:     foreach my $key (keys(%scantronfiles)) {
 1930:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 1931:                               .$scantronfiles{$key};
 1932:     }
 1933:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 1934:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 1935:         if (!$switchserver) {
 1936:             my $servadm = $r->dir_config('lonAdmEMail');
 1937:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 1938:             if ($configuserok eq 'ok') {
 1939:                 if ($author_ok eq 'ok') {
 1940:                     my %legacyfile = (
 1941:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 1942:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 1943:                     );
 1944:                     my %md5chk;
 1945:                     foreach my $type (keys(%legacyfile)) {
 1946:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 1947:                         chomp($md5chk{$type});
 1948:                     }
 1949:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 1950:                         foreach my $type (keys(%legacyfile)) {
 1951:                             ($scantronurls{$type},my $error) = 
 1952:                                 &legacy_scantronformat($r,$dom,$confname,
 1953:                                                  $type,$legacyfile{$type},
 1954:                                                  $scantronurls{$type},
 1955:                                                  $scantronfiles{$type});
 1956:                             if ($error ne '') {
 1957:                                 $error{$type} = $error;
 1958:                             }
 1959:                         }
 1960:                         if (keys(%error) == 0) {
 1961:                             $is_custom = 1;
 1962:                             $confhash{'scantron'}{'scantronformat'} = 
 1963:                                 $scantronurls{'custom'};
 1964:                             my $putresult = 
 1965:                                 &Apache::lonnet::put_dom('configuration',
 1966:                                                          \%confhash,$dom);
 1967:                             if ($putresult ne 'ok') {
 1968:                                 $error{'custom'} = 
 1969:                                     '<span class="LC_error">'.
 1970:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 1971:                             }
 1972:                         }
 1973:                     } else {
 1974:                         ($scantronurls{'default'},my $error) =
 1975:                             &legacy_scantronformat($r,$dom,$confname,
 1976:                                           'default',$legacyfile{'default'},
 1977:                                           $scantronurls{'default'},
 1978:                                           $scantronfiles{'default'});
 1979:                         if ($error eq '') {
 1980:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 1981:                             my $putresult =
 1982:                                 &Apache::lonnet::put_dom('configuration',
 1983:                                                          \%confhash,$dom);
 1984:                             if ($putresult ne 'ok') {
 1985:                                 $error{'default'} =
 1986:                                     '<span class="LC_error">'.
 1987:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 1988:                             }
 1989:                         } else {
 1990:                             $error{'default'} = $error;
 1991:                         }
 1992:                     }
 1993:                 }
 1994:             }
 1995:         } else {
 1996:             $error{'default'} = &mt("Unable to copy default scantron formatfile to domain's RES space: [_1]",$switchserver);
 1997:         }
 1998:     }
 1999:     if (ref($settings) eq 'HASH') {
 2000:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 2001:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 2002:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 2003:                 $scantronurl = '';
 2004:             } else {
 2005:                 $scantronurl = $settings->{'scantronformat'};
 2006:             }
 2007:             $is_custom = 1;
 2008:         } else {
 2009:             $scantronurl = $scantronurls{'default'};
 2010:         }
 2011:     } else {
 2012:         if ($is_custom) {
 2013:             $scantronurl = $scantronurls{'custom'};
 2014:         } else {
 2015:             $scantronurl = $scantronurls{'default'};
 2016:         }
 2017:     }
 2018:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2019:     $datatable .= '<tr'.$css_class.'>';
 2020:     if (!$is_custom) {
 2021:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 2022:                       '<span class="LC_nobreak">';
 2023:         if ($scantronurl) {
 2024:             $datatable .= '<a href="'.$scantronurl.'" target="_blank">'.
 2025:                           &mt('Default scantron format file').'</a>';
 2026:         } else {
 2027:             $datatable = &mt('File unavailable for display');
 2028:         }
 2029:         $datatable .= '</span></td>';
 2030:         if (keys(%error) == 0) { 
 2031:             $datatable .= '<td valign="bottom">';
 2032:             if (!$switchserver) {
 2033:                 $datatable .= &mt('Upload:').'<br />';
 2034:             }
 2035:         } else {
 2036:             my $errorstr;
 2037:             foreach my $key (sort(keys(%error))) {
 2038:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 2039:             }
 2040:             $datatable .= '<td>'.$errorstr;
 2041:         }
 2042:     } else {
 2043:         if (keys(%error) > 0) {
 2044:             my $errorstr;
 2045:             foreach my $key (sort(keys(%error))) {
 2046:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 2047:             } 
 2048:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 2049:         } elsif ($scantronurl) {
 2050:             $datatable .= '<td><span class="LC_nobreak">'.
 2051:                           '<a href="'.$scantronurl.'" target="_blank">'.
 2052:                           &mt('Custom scantron format file').'</a><label>'.
 2053:                           '<input type="checkbox" name="scantronformat_del"'.
 2054:                           '" value="1" />'.&mt('Delete?').'</label></span></td>'.
 2055:                           '<td><span class="LC_nobreak">&nbsp;'.
 2056:                           &mt('Replace:').'</span><br />';
 2057:         }
 2058:     }
 2059:     if (keys(%error) == 0) {
 2060:         if ($switchserver) {
 2061:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2062:         } else {
 2063:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 2064:                          '<input type="file" name="scantronformat" /></span>';
 2065:         }
 2066:     }
 2067:     $datatable .= '</td></tr>';
 2068:     $$rowtotal ++;
 2069:     return $datatable;
 2070: }
 2071: 
 2072: sub legacy_scantronformat {
 2073:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 2074:     my ($url,$error);
 2075:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 2076:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 2077:         (my $result,$url) =
 2078:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 2079:                          '','',$newfile);
 2080:         if ($result ne 'ok') {
 2081:             $error = &mt("An error occurred publishing the [_1] scantron format file in RES space. Error was: [_2].",$newfile,$result);
 2082:         }
 2083:     }
 2084:     return ($url,$error);
 2085: }
 2086: 
 2087: sub print_coursecategories {
 2088:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 2089:     my $datatable;
 2090:     if ($position eq 'top') {
 2091:         my $toggle_cats_crs = ' ';
 2092:         my $toggle_cats_dom = ' checked="checked" ';
 2093:         my $can_cat_crs = ' ';
 2094:         my $can_cat_dom = ' checked="checked" ';
 2095:         if (ref($settings) eq 'HASH') {
 2096:             if ($settings->{'togglecats'} eq 'crs') {
 2097:                 $toggle_cats_crs = $toggle_cats_dom;
 2098:                 $toggle_cats_dom = ' ';
 2099:             }
 2100:             if ($settings->{'categorize'} eq 'crs') {
 2101:                 $can_cat_crs = $can_cat_dom;
 2102:                 $can_cat_dom = ' ';
 2103:             }
 2104:         }
 2105:         my %title = &Apache::lonlocal::texthash (
 2106:                      togglecats => 'Show/Hide a course in the catalog',
 2107:                      categorize    => 'Assign a category to a course',
 2108:                     );
 2109:         my %level = &Apache::lonlocal::texthash (
 2110:                      dom => 'Set in "Modify Course" (Domain)',
 2111:                      crs => 'Set in "Modify Parameters" (Course)',   
 2112:                     );
 2113:         $datatable = '<tr class="LC_odd_row">'.
 2114:                   '<td>'.$title{'togglecats'}.'</td>'.
 2115:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2116:                   '<input type="radio" name="togglecats"'.
 2117:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 2118:                   '<label><input type="radio" name="togglecats"'.
 2119:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 2120:                   '</tr><tr>'.
 2121:                   '<td>'.$title{'categorize'}.'</td>'.
 2122:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2123:                   '<label><input type="radio" name="categorize"'.
 2124:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 2125:                   '<label><input type="radio" name="categorize"'.
 2126:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 2127:                   '</tr>';
 2128:         $$rowtotal += 2;
 2129:     } else {
 2130:         my $css_class;
 2131:         my $itemcount = 1;
 2132:         my $cathash; 
 2133:         if (ref($settings) eq 'HASH') {
 2134:             $cathash = $settings->{'cats'};
 2135:         }
 2136:         if (ref($cathash) eq 'HASH') {
 2137:             my (@cats,@trails,%allitems,%idx,@jsarray);
 2138:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 2139:                                                    \%allitems,\%idx,\@jsarray);
 2140:             my $maxdepth = scalar(@cats);
 2141:             my $colattrib = '';
 2142:             if ($maxdepth > 2) {
 2143:                 $colattrib = ' colspan="2" ';
 2144:             }
 2145:             my @path;
 2146:             if (@cats > 0) {
 2147:                 if (ref($cats[0]) eq 'ARRAY') {
 2148:                     my $numtop = @{$cats[0]};
 2149:                     my $maxnum = $numtop;
 2150:                     if ((!grep(/^instcode$/,@{$cats[0]})) || ($cathash->{'instcode::0'} eq '')) {
 2151:                         $maxnum ++;
 2152:                     }
 2153:                     my $lastidx;
 2154:                     for (my $i=0; $i<$numtop; $i++) {
 2155:                         my $parent = $cats[0][$i];
 2156:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2157:                         my $item = &escape($parent).'::0';
 2158:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 2159:                         $lastidx = $idx{$item};
 2160:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2161:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 2162:                         for (my $k=0; $k<=$maxnum; $k++) {
 2163:                             my $vpos = $k+1;
 2164:                             my $selstr;
 2165:                             if ($k == $i) {
 2166:                                 $selstr = ' selected="selected" ';
 2167:                             }
 2168:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2169:                         }
 2170:                         $datatable .= '</select></td><td>';
 2171:                         if ($parent eq 'instcode') {
 2172:                             $datatable .=  '<span class="LC_nobreak">'.&mt('Official courses')
 2173:                                            .'</span><br /><span class="LC_nobreak">('
 2174:                                            .&mt('with institutional codes').')</span></td>'
 2175:                                            .'<td'.$colattrib.'><span class="LC_nobreak"><label><input type="radio" name="instcode" value="1" checked="checked" />'
 2176:                                             .&mt('Display').'</label>&nbsp;'
 2177:                                             .'<label><input type="radio" name="instcode" value="0" />'
 2178:                                             .&mt('Do not display').'</label></span></td>';
 2179:                         } else {
 2180:                             $datatable .= $parent
 2181:                                           .'&nbsp;<label><input type="checkbox" name="deletecategory" '
 2182:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 2183:                         }
 2184:                         my $depth = 1;
 2185:                         push(@path,$parent);
 2186:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 2187:                         pop(@path);
 2188:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 2189:                         $itemcount ++;
 2190:                     }
 2191:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2192:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 2193:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 2194:                     for (my $k=0; $k<=$maxnum; $k++) {
 2195:                         my $vpos = $k+1;
 2196:                         my $selstr;
 2197:                         if ($k == $numtop) {
 2198:                             $selstr = ' selected="selected" ';
 2199:                         }
 2200:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2201:                     }
 2202:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 2203:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 2204:                                   .'</tr>'."\n";
 2205:                     $itemcount ++;
 2206:                     if ((!grep(/^instcode$/,@{$cats[0]})) || ($cathash->{'instcode::0'} eq '')) {
 2207:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2208:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','instcode_pos','$lastidx'".');"';
 2209:                         $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 2210:                                       '<span class="LC_nobreak"><select name="instcode_pos"'.$chgstr.'>';
 2211:                         for (my $k=0; $k<=$maxnum; $k++) {
 2212:                             my $vpos = $k+1;
 2213:                             my $selstr;
 2214:                             if ($k == $maxnum) {
 2215:                                 $selstr = ' selected="selected" ';
 2216:                             }
 2217:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2218:                         }
 2219:                         $datatable .= '</select></span></td><td><span class="LC_nobreak">'
 2220:                                       .&mt('Official courses').'</span>'.'<br /><span class="LC_nobreak">('
 2221:                                       .&mt('with institutional codes').')</span></td>'
 2222:                                       .'<td><span class="LC_nobreak"><label><input type="radio" name="instcode" value="1" />'
 2223:                                       .&mt('Display').'</label>&nbsp;'
 2224:                                       .'<label><input type="radio" name="instcode" value="0" checked="checked"/>'
 2225:                                       .&mt('Do not display').'</label></span></td></tr>';
 2226:                     }
 2227:                 }
 2228:             } else {
 2229:                 $datatable .= &initialize_categories($itemcount);
 2230:             }
 2231:         } else {
 2232:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[0]->{'col2'}.'</td>'
 2233:                           .&initialize_categories($itemcount);
 2234:         }
 2235:         $$rowtotal += $itemcount;
 2236:     }
 2237:     return $datatable;
 2238: }
 2239: 
 2240: sub print_serverstatuses {
 2241:     my ($dom,$settings,$rowtotal) = @_;
 2242:     my $datatable;
 2243:     my @pages = &serverstatus_pages();
 2244:     my (%namedaccess,%machineaccess);
 2245:     foreach my $type (@pages) {
 2246:         $namedaccess{$type} = '';
 2247:         $machineaccess{$type}= '';
 2248:     }
 2249:     if (ref($settings) eq 'HASH') {
 2250:         foreach my $type (@pages) {
 2251:             if (exists($settings->{$type})) {
 2252:                 if (ref($settings->{$type}) eq 'HASH') {
 2253:                     foreach my $key (keys(%{$settings->{$type}})) {
 2254:                         if ($key eq 'namedusers') {
 2255:                             $namedaccess{$type} = $settings->{$type}->{$key};
 2256:                         } elsif ($key eq 'machines') {
 2257:                             $machineaccess{$type} = $settings->{$type}->{$key};
 2258:                         }
 2259:                     }
 2260:                 }
 2261:             }
 2262:         }
 2263:     }
 2264:     my $titles= &LONCAPA::loncgi::serverstatus_titles();
 2265:     my $rownum = 0;
 2266:     my $css_class;
 2267:     foreach my $type (@pages) {
 2268:         $rownum ++;
 2269:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 2270:         $datatable .= '<tr'.$css_class.'>'.
 2271:                       '<td><span class="LC_nobreak">'.
 2272:                       $titles->{$type}.'</span></td>'.
 2273:                       '<td class="LC_left_item">'.
 2274:                       '<input type="text" name="'.$type.'_namedusers" '.
 2275:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 2276:                       '<td class="LC_right_item">'.
 2277:                       '<span class="LC_nobreak">'.
 2278:                       '<input type="text" name="'.$type.'_machines" '.
 2279:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 2280:                       '</td></tr>'."\n";
 2281:     }
 2282:     $$rowtotal += $rownum;
 2283:     return $datatable;
 2284: }
 2285: 
 2286: sub serverstatus_pages {
 2287:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 2288:             'clusterstatus','metadata_keywords','metadata_harvest',
 2289:             'takeoffline','takeonline','showenv');
 2290: }
 2291: 
 2292: sub coursecategories_javascript {
 2293:     my ($settings) = @_;
 2294:     my ($output,$jstext,$cathash);
 2295:     if (ref($settings) eq 'HASH') {
 2296:         $cathash = $settings->{'cats'};
 2297:     }
 2298:     if (ref($cathash) eq 'HASH') {
 2299:         my (@cats,@jsarray,%idx);
 2300:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 2301:         if (@jsarray > 0) {
 2302:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 2303:             for (my $i=0; $i<@jsarray; $i++) {
 2304:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 2305:                     my $catstr = join('","',@{$jsarray[$i]});
 2306:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 2307:                 }
 2308:             }
 2309:         }
 2310:     } else {
 2311:         $jstext  = '    var categories = Array(1);'."\n".
 2312:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 2313:     }
 2314:     $output = <<"ENDSCRIPT";
 2315: <script type="text/javascript">
 2316: function reorderCats(form,parent,item,idx) {
 2317:     var changedVal;
 2318: $jstext
 2319:     var newpos = 'addcategory_pos';
 2320:     var current = new Array;
 2321:     if (parent == '') {
 2322:         var has_instcode = 0;
 2323:         var maxtop = categories[idx].length;
 2324:         for (var j=0; j<maxtop; j++) {
 2325:             if (categories[idx][j] == 'instcode::0') {
 2326:                 has_instcode == 1;
 2327:             }
 2328:         }
 2329:         if (has_instcode == 0) {
 2330:             categories[idx][maxtop] = 'instcode_pos';
 2331:         }
 2332:     } else {
 2333:         newpos += '_'+parent;
 2334:     }
 2335:     var maxh = 1 + categories[idx].length;
 2336:     var current = new Array;
 2337:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2338:     if (item == newpos) {
 2339:         changedVal = newitemVal;
 2340:     } else {
 2341:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2342:         current[newitemVal] = newpos;
 2343:     }
 2344:     for (var i=0; i<categories[idx].length; i++) {
 2345:         var elementName = categories[idx][i];
 2346:         if (elementName != item) {
 2347:             if (form.elements[elementName]) {
 2348:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2349:                 current[currVal] = elementName;
 2350:             }
 2351:         }
 2352:     }
 2353:     var oldVal;
 2354:     for (var j=0; j<maxh; j++) {
 2355:         if (current[j] == undefined) {
 2356:             oldVal = j;
 2357:         }
 2358:     }
 2359:     if (oldVal < changedVal) {
 2360:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2361:            var elementName = current[k];
 2362:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2363:         }
 2364:     } else {
 2365:         for (var k=changedVal; k<oldVal; k++) {
 2366:             var elementName = current[k];
 2367:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2368:         }
 2369:     }
 2370:     return;
 2371: }
 2372: </script>
 2373: 
 2374: ENDSCRIPT
 2375:     return $output;
 2376: }
 2377: 
 2378: sub initialize_categories {
 2379:     my ($itemcount) = @_;
 2380:     my $datatable;
 2381:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2382:     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','instcode_pos','0'".');"';
 2383: 
 2384:     $datatable = '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2385:                  .'<select name="instcode_pos"><option value="0" selected="selected">1</option>'
 2386:                  .'<option value="1">2</option></select>&nbsp;'
 2387:                  .&mt('Official courses (with institutional codes)')
 2388:                  .'</span></td><td><span class="LC_nobreak">'
 2389:                  .'<label><input type="radio" name="instcode" value="1" checked="checked" />'
 2390:                  .&mt('Display').'</label>&nbsp;<label>'
 2391:                  .'<input type="radio" name="instcode" value="0" />'.&mt('Do not display')
 2392:                  .'</label></span></td></tr>';
 2393:     $itemcount ++;
 2394:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2395:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 2396:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2397:                   .'<select name="addcategory_pos"'.$chgstr.'><option value="0">1</option>'
 2398:                   .'<option value="1" selected="selected">2</option></select>&nbsp;'
 2399:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 2400:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 2401:     return $datatable;
 2402: }
 2403: 
 2404: sub build_category_rows {
 2405:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 2406:     my ($text,$name,$item,$chgstr);
 2407:     if (ref($cats) eq 'ARRAY') {
 2408:         my $maxdepth = scalar(@{$cats});
 2409:         if (ref($cats->[$depth]) eq 'HASH') {
 2410:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 2411:                 my $numchildren = @{$cats->[$depth]{$parent}};
 2412:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2413:                 $text .= '<td><table class="LC_datatable">';
 2414:                 my ($idxnum,$parent_name,$parent_item);
 2415:                 my $higher = $depth - 1;
 2416:                 if ($higher == 0) {
 2417:                     $parent_name = &escape($parent).'::'.$higher;
 2418:                 } else {
 2419:                     if (ref($path) eq 'ARRAY') {
 2420:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 2421:                     }
 2422:                 }
 2423:                 $parent_item = 'addcategory_pos_'.$parent_name;
 2424:                 for (my $j=0; $j<=$numchildren; $j++) {
 2425:                     if ($j < $numchildren) {
 2426:                         $name = $cats->[$depth]{$parent}[$j];
 2427:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 2428:                         $idxnum = $idx->{$item};
 2429:                     } else {
 2430:                         $name = $parent_name;
 2431:                         $item = $parent_item;
 2432:                     }
 2433:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 2434:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 2435:                     for (my $i=0; $i<=$numchildren; $i++) {
 2436:                         my $vpos = $i+1;
 2437:                         my $selstr;
 2438:                         if ($j == $i) {
 2439:                             $selstr = ' selected="selected" ';
 2440:                         }
 2441:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 2442:                     }
 2443:                     $text .= '</select>&nbsp;';
 2444:                     if ($j < $numchildren) {
 2445:                         my $deeper = $depth+1;
 2446:                         $text .= $name.'&nbsp;'
 2447:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 2448:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 2449:                         if(ref($path) eq 'ARRAY') {
 2450:                             push(@{$path},$name);
 2451:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 2452:                             pop(@{$path});
 2453:                         }
 2454:                     } else {
 2455:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 2456:                         if ($j == $numchildren) {
 2457:                             $text .= $name;
 2458:                         } else {
 2459:                             $text .= $item;
 2460:                         }
 2461:                         $text .= '" value="" />';
 2462:                     }
 2463:                     $text .= '</td></tr>';
 2464:                 }
 2465:                 $text .= '</table></td>';
 2466:             } else {
 2467:                 my $higher = $depth-1;
 2468:                 if ($higher == 0) {
 2469:                     $name = &escape($parent).'::'.$higher;
 2470:                 } else {
 2471:                     if (ref($path) eq 'ARRAY') {
 2472:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 2473:                     }
 2474:                 }
 2475:                 my $colspan;
 2476:                 if ($parent ne 'instcode') {
 2477:                     $colspan = $maxdepth - $depth - 1;
 2478:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 2479:                 }
 2480:             }
 2481:         }
 2482:     }
 2483:     return $text;
 2484: }
 2485: 
 2486: sub modifiable_userdata_row {
 2487:     my ($context,$role,$settings,$numinrow,$rowcount,$usertypes) = @_;
 2488:     my $rolename;
 2489:     if ($context eq 'selfcreate') {
 2490:         if (ref($usertypes) eq 'HASH') {
 2491:             $rolename = $usertypes->{$role};
 2492:         } else {
 2493:             $rolename = $role;
 2494:         }
 2495:     } else {
 2496:         if ($role eq 'cr') {
 2497:             $rolename = &mt('Custom role');
 2498:         } else {
 2499:             $rolename = &Apache::lonnet::plaintext($role);
 2500:         }
 2501:     }
 2502:     my @fields = ('lastname','firstname','middlename','generation',
 2503:                   'permanentemail','id');
 2504:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2505:     my $output;
 2506:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 2507:     $output = '<tr '.$css_class.'>'.
 2508:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 2509:               '<td class="LC_left_item" colspan="2"><table>';
 2510:     my $rem;
 2511:     my %checks;
 2512:     if (ref($settings) eq 'HASH') {
 2513:         if (ref($settings->{$context}) eq 'HASH') {
 2514:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 2515:                 foreach my $field (@fields) {
 2516:                     if ($settings->{$context}->{$role}->{$field}) {
 2517:                         $checks{$field} = ' checked="checked" ';
 2518:                     }
 2519:                 }
 2520:             }
 2521:         }
 2522:     }
 2523:     for (my $i=0; $i<@fields; $i++) {
 2524:         my $rem = $i%($numinrow);
 2525:         if ($rem == 0) {
 2526:             if ($i > 0) {
 2527:                 $output .= '</tr>';
 2528:             }
 2529:             $output .= '<tr>';
 2530:         }
 2531:         my $check = ' ';
 2532:         if (exists($checks{$fields[$i]})) {
 2533:             $check = $checks{$fields[$i]}
 2534:         } else {
 2535:             if ($role eq 'st') {
 2536:                 if (ref($settings) ne 'HASH') {
 2537:                     $check = ' checked="checked" '; 
 2538:                 }
 2539:             }
 2540:         }
 2541:         $output .= '<td class="LC_left_item">'.
 2542:                    '<span class="LC_nobreak"><label>'.
 2543:                    '<input type="checkbox" name="canmodify_'.$role.'" '.
 2544:                    'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 2545:                    '</label></span></td>';
 2546:         $rem = @fields%($numinrow);
 2547:     }
 2548:     my $colsleft = $numinrow - $rem;
 2549:     if ($colsleft > 1 ) {
 2550:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 2551:                    '&nbsp;</td>';
 2552:     } elsif ($colsleft == 1) {
 2553:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 2554:     }
 2555:     $output .= '</tr></table></td></tr>';
 2556:     return $output;
 2557: }
 2558: 
 2559: sub users_cansearch_row {
 2560:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle) = @_;
 2561:     my $output =  '<tr class="LC_odd_row">'.
 2562:                   '<td>'.&mt('Users allowed to search').' ('.$dom.')'.
 2563:                   '</td><td class="LC_left_item" colspan="2"><table>';
 2564:     my $rem;
 2565:     if (ref($types) eq 'ARRAY') {
 2566:         for (my $i=0; $i<@{$types}; $i++) {
 2567:             if (defined($usertypes->{$types->[$i]})) {
 2568:                 my $rem = $i%($numinrow);
 2569:                 if ($rem == 0) {
 2570:                     if ($i > 0) {
 2571:                         $output .= '</tr>';
 2572:                     }
 2573:                     $output .= '<tr>';
 2574:                 }
 2575:                 my $check = ' ';
 2576:                 if (ref($settings->{'cansearch'}) eq 'ARRAY') {
 2577:                     if (grep(/^\Q$types->[$i]\E$/,@{$settings->{'cansearch'}})) {
 2578:                         $check = ' checked="checked" ';
 2579:                     }
 2580:                 }
 2581:                 $output .= '<td class="LC_left_item">'.
 2582:                            '<span class="LC_nobreak"><label>'.
 2583:                            '<input type="checkbox" name="cansearch" '.
 2584:                            'value="'.$types->[$i].'"'.$check.'/>'.
 2585:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 2586:             }
 2587:         }
 2588:        
 2589:         $rem = @{$types}%($numinrow);
 2590:     }
 2591:     my $colsleft = $numinrow - $rem;
 2592:     if ($colsleft > 1) {
 2593:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 2594:     } else {
 2595:         $output .= '<td class="LC_left_item">';
 2596:     }
 2597:     my $defcheck = ' ';
 2598:     if (ref($settings->{'cansearch'}) eq 'ARRAY') {
 2599:         if (grep(/^default$/,@{$settings->{'cansearch'}})) {
 2600:             $defcheck = ' checked="checked" ';
 2601:         }
 2602:     }
 2603:     $output .= '<span class="LC_nobreak"><label>'.
 2604:                '<input type="checkbox" name="cansearch" '.
 2605:                'value="default"'.$defcheck.'/>'.
 2606:                $othertitle.'</label></span></td>'.
 2607:                '</tr></table></td></tr>';
 2608:     return $output;
 2609: }
 2610: 
 2611: sub sorted_searchtitles {
 2612:     my %searchtitles = &Apache::lonlocal::texthash(
 2613:                          'uname' => 'username',
 2614:                          'lastname' => 'last name',
 2615:                          'lastfirst' => 'last name, first name',
 2616:                      );
 2617:     my @titleorder = ('uname','lastname','lastfirst');
 2618:     return (\%searchtitles,\@titleorder);
 2619: }
 2620: 
 2621: sub sorted_searchtypes {
 2622:     my %srchtypes_desc = (
 2623:                            exact    => 'is exact match',
 2624:                            contains => 'contains ..',
 2625:                            begins   => 'begins with ..',
 2626:                          );
 2627:     my @srchtypeorder = ('exact','begins','contains');
 2628:     return (\%srchtypes_desc,\@srchtypeorder);
 2629: }
 2630: 
 2631: sub usertype_update_row {
 2632:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 2633:     my $datatable;
 2634:     my $numinrow = 4;
 2635:     foreach my $type (@{$types}) {
 2636:         if (defined($usertypes->{$type})) {
 2637:             $$rownums ++;
 2638:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 2639:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 2640:                           '</td><td class="LC_left_item"><table>';
 2641:             for (my $i=0; $i<@{$fields}; $i++) {
 2642:                 my $rem = $i%($numinrow);
 2643:                 if ($rem == 0) {
 2644:                     if ($i > 0) {
 2645:                         $datatable .= '</tr>';
 2646:                     }
 2647:                     $datatable .= '<tr>';
 2648:                 }
 2649:                 my $check = ' ';
 2650:                 if (ref($settings) eq 'HASH') {
 2651:                     if (ref($settings->{'fields'}) eq 'HASH') {
 2652:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 2653:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 2654:                                 $check = ' checked="checked" ';
 2655:                             }
 2656:                         }
 2657:                     }
 2658:                 }
 2659: 
 2660:                 if ($i == @{$fields}-1) {
 2661:                     my $colsleft = $numinrow - $rem;
 2662:                     if ($colsleft > 1) {
 2663:                         $datatable .= '<td colspan="'.$colsleft.'">';
 2664:                     } else {
 2665:                         $datatable .= '<td>';
 2666:                     }
 2667:                 } else {
 2668:                     $datatable .= '<td>';
 2669:                 }
 2670:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2671:                               '<input type="checkbox" name="updateable_'.$type.
 2672:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 2673:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 2674:             }
 2675:             $datatable .= '</tr></table></td></tr>';
 2676:         }
 2677:     }
 2678:     return $datatable;
 2679: }
 2680: 
 2681: sub modify_login {
 2682:     my ($r,$dom,$confname,%domconfig) = @_;
 2683:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges);
 2684:     my %title = ( coursecatalog => 'Display course catalog',
 2685:                   adminmail => 'Display administrator E-mail address',
 2686:                   newuser => 'Link for visitors to create a user account',
 2687:                   loginheader => 'Log-in box header');
 2688:     my @offon = ('off','on');
 2689:     my %loginhash;
 2690:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 2691:                                            \%domconfig,\%loginhash);
 2692:     my @toggles = ('coursecatalog','adminmail','newuser');
 2693:     foreach my $item (@toggles) {
 2694:         $loginhash{login}{$item} = $env{'form.'.$item};
 2695:     }
 2696:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 2697:     if (ref($colchanges{'login'}) eq 'HASH') {  
 2698:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 2699:                                          \%loginhash);
 2700:     }
 2701:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 2702:                                              $dom);
 2703:     if ($putresult eq 'ok') {
 2704:         my @toggles = ('coursecatalog','adminmail','newuser');
 2705:         my %defaultchecked = (
 2706:                     'coursecatalog' => 'on',
 2707:                     'adminmail'     => 'off',
 2708:                     'newuser'       => 'off',
 2709:         );
 2710:         if (ref($domconfig{'login'}) eq 'HASH') {
 2711:             foreach my $item (@toggles) {
 2712:                 if ($defaultchecked{$item} eq 'on') { 
 2713:                     if (($domconfig{'login'}{$item} eq '0') &&
 2714:                         ($env{'form.'.$item} eq '1')) {
 2715:                         $changes{$item} = 1;
 2716:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 2717:                               $domconfig{'login'}{$item} eq '1') &&
 2718:                              ($env{'form.'.$item} eq '0')) {
 2719:                         $changes{$item} = 1;
 2720:                     }
 2721:                 } elsif ($defaultchecked{$item} eq 'off') {
 2722:                     if (($domconfig{'login'}{$item} eq '1') &&
 2723:                         ($env{'form.'.$item} eq '0')) {
 2724:                         $changes{$item} = 1;
 2725:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 2726:                               $domconfig{'login'}{$item} eq '0') &&
 2727:                              ($env{'form.'.$item} eq '1')) {
 2728:                         $changes{$item} = 1;
 2729:                     }
 2730:                 }
 2731:             }
 2732:             if (($domconfig{'login'}{'loginheader'} eq 'text') && 
 2733:                 ($env{'form.loginheader'} eq 'image')) {
 2734:                 $changes{'loginheader'} = 1;
 2735:             } elsif (($domconfig{'login'}{'loginheader'} eq '' ||
 2736:                       $domconfig{'login'}{'loginheader'} eq 'image') &&
 2737:                      ($env{'form.loginheader'} eq 'text')) {
 2738:                 $changes{'loginheader'} = 1;
 2739:             }
 2740:         }
 2741:         if (keys(%changes) > 0 || $colchgtext) {
 2742:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 2743:             $resulttext = &mt('Changes made:').'<ul>';
 2744:             foreach my $item (sort(keys(%changes))) {
 2745:                 if ($item eq 'loginheader') {
 2746:                     $resulttext .= '<li>'.&mt("$title{$item} set to $env{'form.loginheader'}").'</li>';
 2747:                 } else {
 2748:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 2749:                 }
 2750:             }
 2751:             $resulttext .= $colchgtext.'</ul>';
 2752:         } else {
 2753:             $resulttext = &mt('No changes made to log-in page settings');
 2754:         }
 2755:     } else {
 2756:         $resulttext = '<span class="LC_error">'.
 2757: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 2758:     }
 2759:     if ($errors) {
 2760:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 2761:                        $errors.'</ul>';
 2762:     }
 2763:     return $resulttext;
 2764: }
 2765: 
 2766: sub color_font_choices {
 2767:     my %choices =
 2768:         &Apache::lonlocal::texthash (
 2769:             img => "Header",
 2770:             bgs => "Background colors",
 2771:             links => "Link colors",
 2772:             images => "Images",
 2773:             font => "Font color",
 2774:             pgbg => "Page",
 2775:             tabbg => "Header",
 2776:             sidebg => "Border",
 2777:             link => "Link",
 2778:             alink => "Active link",
 2779:             vlink => "Visited link",
 2780:         );
 2781:     return %choices;
 2782: }
 2783: 
 2784: sub modify_rolecolors {
 2785:     my ($r,$dom,$confname,$roles,%domconfig) = @_;
 2786:     my ($resulttext,%rolehash);
 2787:     $rolehash{'rolecolors'} = {};
 2788:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 2789:         if ($domconfig{'rolecolors'} eq '') {
 2790:             $domconfig{'rolecolors'} = {};
 2791:         }
 2792:     }
 2793:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 2794:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 2795:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 2796:                                              $dom);
 2797:     if ($putresult eq 'ok') {
 2798:         if (keys(%changes) > 0) {
 2799:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 2800:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 2801:                                              $rolehash{'rolecolors'});
 2802:         } else {
 2803:             $resulttext = &mt('No changes made to default color schemes');
 2804:         }
 2805:     } else {
 2806:         $resulttext = '<span class="LC_error">'.
 2807: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 2808:     }
 2809:     if ($errors) {
 2810:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 2811:                        $errors.'</ul>';
 2812:     }
 2813:     return $resulttext;
 2814: }
 2815: 
 2816: sub modify_colors {
 2817:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 2818:     my (%changes,%choices);
 2819:     my @bgs;
 2820:     my @links = ('link','alink','vlink');
 2821:     my @logintext;
 2822:     my @images;
 2823:     my $servadm = $r->dir_config('lonAdmEMail');
 2824:     my $errors;
 2825:     foreach my $role (@{$roles}) {
 2826:         if ($role eq 'login') {
 2827:             %choices = &login_choices();
 2828:             @logintext = ('textcol','bgcol');
 2829:         } else {
 2830:             %choices = &color_font_choices();
 2831:         }
 2832:         if ($role eq 'login') {
 2833:             @images = ('img','logo','domlogo','login');
 2834:             @bgs = ('pgbg','mainbg','sidebg');
 2835:         } else {
 2836:             @images = ('img');
 2837:             @bgs = ('pgbg','tabbg','sidebg'); 
 2838:         }
 2839:         $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 2840:         foreach my $item (@bgs,@links,@logintext) {
 2841:             $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 2842:         }
 2843:         my ($configuserok,$author_ok,$switchserver) = 
 2844:             &config_check($dom,$confname,$servadm);
 2845:         my ($width,$height) = &thumb_dimensions();
 2846:         if (ref($domconfig->{$role}) ne 'HASH') {
 2847:             $domconfig->{$role} = {};
 2848:         }
 2849:         foreach my $img (@images) {
 2850: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 2851: 		 && !defined($domconfig->{$role}{$img})
 2852: 		 && !$env{'form.'.$role.'_del_'.$img}
 2853: 		 && $env{'form.'.$role.'_import_'.$img}) {
 2854: 		# import the old configured image from the .tab setting
 2855: 		# if they haven't provided a new one 
 2856: 		$domconfig->{$role}{$img} = 
 2857: 		    $env{'form.'.$role.'_import_'.$img};
 2858: 	    }
 2859:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 2860:                 my $error;
 2861:                 if ($configuserok eq 'ok') {
 2862:                     if ($switchserver) {
 2863:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 2864:                     } else {
 2865:                         if ($author_ok eq 'ok') {
 2866:                             my ($result,$logourl) = 
 2867:                                 &publishlogo($r,'upload',$role.'_'.$img,
 2868:                                            $dom,$confname,$img,$width,$height);
 2869:                             if ($result eq 'ok') {
 2870:                                 $confhash->{$role}{$img} = $logourl;
 2871:                                 $changes{$role}{'images'}{$img} = 1;
 2872:                             } else {
 2873:                                 $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);
 2874:                             }
 2875:                         } else {
 2876:                             $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);
 2877:                         }
 2878:                     }
 2879:                 } else {
 2880:                     $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);
 2881:                 }
 2882:                 if ($error) {
 2883:                     &Apache::lonnet::logthis($error);
 2884:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 2885:                 }
 2886:             } elsif ($domconfig->{$role}{$img} ne '') {
 2887:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 2888:                     my $error;
 2889:                     if ($configuserok eq 'ok') {
 2890: # is confname an author?
 2891:                         if ($switchserver eq '') {
 2892:                             if ($author_ok eq 'ok') {
 2893:                                 my ($result,$logourl) = 
 2894:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 2895:                                             $dom,$confname,$img,$width,$height);
 2896:                                 if ($result eq 'ok') {
 2897:                                     $confhash->{$role}{$img} = $logourl;
 2898: 				    $changes{$role}{'images'}{$img} = 1;
 2899:                                 }
 2900:                             }
 2901:                         }
 2902:                     }
 2903:                 }
 2904:             }
 2905:         }
 2906:         if (ref($domconfig) eq 'HASH') {
 2907:             if (ref($domconfig->{$role}) eq 'HASH') {
 2908:                 foreach my $img (@images) {
 2909:                     if ($domconfig->{$role}{$img} ne '') {
 2910:                         if ($env{'form.'.$role.'_del_'.$img}) {
 2911:                             $confhash->{$role}{$img} = '';
 2912:                             $changes{$role}{'images'}{$img} = 1;
 2913:                         } else {
 2914:                             if ($confhash->{$role}{$img} eq '') {
 2915:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 2916:                             }
 2917:                         }
 2918:                     } else {
 2919:                         if ($env{'form.'.$role.'_del_'.$img}) {
 2920:                             $confhash->{$role}{$img} = '';
 2921:                             $changes{$role}{'images'}{$img} = 1;
 2922:                         } 
 2923:                     }
 2924:                 }  
 2925:                 if ($domconfig->{$role}{'font'} ne '') {
 2926:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 2927:                         $changes{$role}{'font'} = 1;
 2928:                     }
 2929:                 } else {
 2930:                     if ($confhash->{$role}{'font'}) {
 2931:                         $changes{$role}{'font'} = 1;
 2932:                     }
 2933:                 }
 2934:                 foreach my $item (@bgs) {
 2935:                     if ($domconfig->{$role}{$item} ne '') {
 2936:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 2937:                             $changes{$role}{'bgs'}{$item} = 1;
 2938:                         } 
 2939:                     } else {
 2940:                         if ($confhash->{$role}{$item}) {
 2941:                             $changes{$role}{'bgs'}{$item} = 1;
 2942:                         }
 2943:                     }
 2944:                 }
 2945:                 foreach my $item (@links) {
 2946:                     if ($domconfig->{$role}{$item} ne '') {
 2947:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 2948:                             $changes{$role}{'links'}{$item} = 1;
 2949:                         }
 2950:                     } else {
 2951:                         if ($confhash->{$role}{$item}) {
 2952:                             $changes{$role}{'links'}{$item} = 1;
 2953:                         }
 2954:                     }
 2955:                 }
 2956:                 foreach my $item (@logintext) {
 2957:                     if ($domconfig->{$role}{$item} ne '') {
 2958:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 2959:                             $changes{$role}{'logintext'}{$item} = 1;
 2960:                         }
 2961:                     } else {
 2962:                         if ($confhash->{$role}{$item}) {
 2963:                             $changes{$role}{'logintext'}{$item} = 1;
 2964:                         }
 2965:                     }
 2966:                 }
 2967:             } else {
 2968:                 &default_change_checker($role,\@images,\@links,\@bgs,
 2969:                                         \@logintext,$confhash,\%changes); 
 2970:             }
 2971:         } else {
 2972:             &default_change_checker($role,\@images,\@links,\@bgs,
 2973:                                     \@logintext,$confhash,\%changes); 
 2974:         }
 2975:     }
 2976:     return ($errors,%changes);
 2977: }
 2978: 
 2979: sub config_check {
 2980:     my ($dom,$confname,$servadm) = @_;
 2981:     my ($configuserok,$author_ok,$switchserver,%currroles);
 2982:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 2983:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 2984:                                                    $confname,$servadm);
 2985:     if ($configuserok eq 'ok') {
 2986:         $switchserver = &check_switchserver($dom,$confname);
 2987:         if ($switchserver eq '') {
 2988:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 2989:         }
 2990:     }
 2991:     return ($configuserok,$author_ok,$switchserver);
 2992: }
 2993: 
 2994: sub default_change_checker {
 2995:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 2996:     foreach my $item (@{$links}) {
 2997:         if ($confhash->{$role}{$item}) {
 2998:             $changes->{$role}{'links'}{$item} = 1;
 2999:         }
 3000:     }
 3001:     foreach my $item (@{$bgs}) {
 3002:         if ($confhash->{$role}{$item}) {
 3003:             $changes->{$role}{'bgs'}{$item} = 1;
 3004:         }
 3005:     }
 3006:     foreach my $item (@{$logintext}) {
 3007:         if ($confhash->{$role}{$item}) {
 3008:             $changes->{$role}{'logintext'}{$item} = 1;
 3009:         }
 3010:     }
 3011:     foreach my $img (@{$images}) {
 3012:         if ($env{'form.'.$role.'_del_'.$img}) {
 3013:             $confhash->{$role}{$img} = '';
 3014:             $changes->{$role}{'images'}{$img} = 1;
 3015:         }
 3016:     }
 3017:     if ($confhash->{$role}{'font'}) {
 3018:         $changes->{$role}{'font'} = 1;
 3019:     }
 3020: }
 3021: 
 3022: sub display_colorchgs {
 3023:     my ($dom,$changes,$roles,$confhash) = @_;
 3024:     my (%choices,$resulttext);
 3025:     if (!grep(/^login$/,@{$roles})) {
 3026:         $resulttext = &mt('Changes made:').'<br />';
 3027:     }
 3028:     foreach my $role (@{$roles}) {
 3029:         if ($role eq 'login') {
 3030:             %choices = &login_choices();
 3031:         } else {
 3032:             %choices = &color_font_choices();
 3033:         }
 3034:         if (ref($changes->{$role}) eq 'HASH') {
 3035:             if ($role ne 'login') {
 3036:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 3037:             }
 3038:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 3039:                 if ($role ne 'login') {
 3040:                     $resulttext .= '<ul>';
 3041:                 }
 3042:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 3043:                     if ($role ne 'login') {
 3044:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 3045:                     }
 3046:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 3047:                         if ($confhash->{$role}{$item} eq '') {
 3048:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 3049:                         } else {
 3050:                             my $newitem = $confhash->{$role}{$item};
 3051:                             if ($key eq 'images') {
 3052:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 3053:                             }
 3054:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 3055:                         }
 3056:                     }
 3057:                     if ($role ne 'login') {
 3058:                         $resulttext .= '</ul></li>';
 3059:                     }
 3060:                 } else {
 3061:                     if ($confhash->{$role}{$key} eq '') {
 3062:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 3063:                     } else {
 3064:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 3065:                     }
 3066:                 }
 3067:                 if ($role ne 'login') {
 3068:                     $resulttext .= '</ul>';
 3069:                 }
 3070:             }
 3071:         }
 3072:     }
 3073:     return $resulttext;
 3074: }
 3075: 
 3076: sub thumb_dimensions {
 3077:     return ('200','50');
 3078: }
 3079: 
 3080: sub check_dimensions {
 3081:     my ($inputfile) = @_;
 3082:     my ($fullwidth,$fullheight);
 3083:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 3084:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 3085:             my $imageinfo = <PIPE>;
 3086:             if (!close(PIPE)) {
 3087:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 3088:             }
 3089:             chomp($imageinfo);
 3090:             my ($fullsize) = 
 3091:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 3092:             if ($fullsize) {
 3093:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 3094:             }
 3095:         }
 3096:     }
 3097:     return ($fullwidth,$fullheight);
 3098: }
 3099: 
 3100: sub check_configuser {
 3101:     my ($uhome,$dom,$confname,$servadm) = @_;
 3102:     my ($configuserok,%currroles);
 3103:     if ($uhome eq 'no_host') {
 3104:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 3105:         my $configpass = &LONCAPA::Enrollment::create_password();
 3106:         $configuserok = 
 3107:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 3108:                              $configpass,'','','','','',undef,$servadm);
 3109:     } else {
 3110:         $configuserok = 'ok';
 3111:         %currroles = 
 3112:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 3113:     }
 3114:     return ($configuserok,%currroles);
 3115: }
 3116: 
 3117: sub check_authorstatus {
 3118:     my ($dom,$confname,%currroles) = @_;
 3119:     my $author_ok;
 3120:     if (!$currroles{':'.$dom.':au'}) {
 3121:         my $start = time;
 3122:         my $end = 0;
 3123:         $author_ok = 
 3124:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 3125:                                         'au',$end,$start,'','','domconfig');
 3126:     } else {
 3127:         $author_ok = 'ok';
 3128:     }
 3129:     return $author_ok;
 3130: }
 3131: 
 3132: sub publishlogo {
 3133:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 3134:     my ($output,$fname,$logourl);
 3135:     if ($action eq 'upload') {
 3136:         $fname=$env{'form.'.$formname.'.filename'};
 3137:         chop($env{'form.'.$formname});
 3138:     } else {
 3139:         ($fname) = ($formname =~ /([^\/]+)$/);
 3140:     }
 3141:     if ($savefileas ne '') {
 3142:         $fname = $savefileas;
 3143:     }
 3144:     $fname=&Apache::lonnet::clean_filename($fname);
 3145: # See if there is anything left
 3146:     unless ($fname) { return ('error: no uploaded file'); }
 3147:     $fname="$subdir/$fname";
 3148:     my $filepath='/home/'.$confname.'/public_html';
 3149:     my ($fnamepath,$file,$fetchthumb);
 3150:     $file=$fname;
 3151:     if ($fname=~m|/|) {
 3152:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 3153:     }
 3154:     my @parts=split(/\//,$filepath.'/'.$fnamepath);
 3155:     my $count;
 3156:     for ($count=4;$count<=$#parts;$count++) {
 3157:         $filepath.="/$parts[$count]";
 3158:         if ((-e $filepath)!=1) {
 3159:             mkdir($filepath,02770);
 3160:         }
 3161:     }
 3162:     # Check for bad extension and disallow upload
 3163:     if ($file=~/\.(\w+)$/ &&
 3164:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 3165:         $output = 
 3166:             &mt('Invalid file extension ([_1]) - reserved for LONCAPA use.',$1); 
 3167:     } elsif ($file=~/\.(\w+)$/ &&
 3168:         !defined(&Apache::loncommon::fileembstyle($1))) {
 3169:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 3170:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 3171:         $output = &mt('File name not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 3172:     } elsif (-d "$filepath/$file") {
 3173:         $output = &mt('File name is a directory name - rename the file and re-upload');
 3174:     } else {
 3175:         my $source = $filepath.'/'.$file;
 3176:         my $logfile;
 3177:         if (!open($logfile,">>$source".'.log')) {
 3178:             return (&mt('No write permission to Construction Space'));
 3179:         }
 3180:         print $logfile
 3181: "\n================= Publish ".localtime()." ================\n".
 3182: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 3183: # Save the file
 3184:         if (!open(FH,'>'.$source)) {
 3185:             &Apache::lonnet::logthis('Failed to create '.$source);
 3186:             return (&mt('Failed to create file'));
 3187:         }
 3188:         if ($action eq 'upload') {
 3189:             if (!print FH ($env{'form.'.$formname})) {
 3190:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 3191:                 return (&mt('Failed to write file'));
 3192:             }
 3193:         } else {
 3194:             my $original = &Apache::lonnet::filelocation('',$formname);
 3195:             if(!copy($original,$source)) {
 3196:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 3197:                 return (&mt('Failed to write file'));
 3198:             }
 3199:         }
 3200:         close(FH);
 3201:         chmod(0660, $source); # Permissions to rw-rw---.
 3202: 
 3203:         my $docroot=$r->dir_config('lonDocRoot');
 3204:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 3205:         my $copyfile=$targetdir.'/'.$file;
 3206: 
 3207:         my @parts=split(/\//,$targetdir);
 3208:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 3209:         for (my $count=5;$count<=$#parts;$count++) {
 3210:             $path.="/$parts[$count]";
 3211:             if (!-e $path) {
 3212:                 print $logfile "\nCreating directory ".$path;
 3213:                 mkdir($path,02770);
 3214:             }
 3215:         }
 3216:         my $versionresult;
 3217:         if (-e $copyfile) {
 3218:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 3219:         } else {
 3220:             $versionresult = 'ok';
 3221:         }
 3222:         if ($versionresult eq 'ok') {
 3223:             if (copy($source,$copyfile)) {
 3224:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 3225:                 $output = 'ok';
 3226:                 &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 3227:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 3228:             } else {
 3229:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 3230:                 $output = &mt('Failed to copy file to RES space').", $!";
 3231:             }
 3232:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 3233:                 my $inputfile = $filepath.'/'.$file;
 3234:                 my $outfile = $filepath.'/'.'tn-'.$file;
 3235:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 3236:                 if ($fullwidth ne '' && $fullheight ne '') { 
 3237:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 3238:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 3239:                         system("convert -sample $thumbsize $inputfile $outfile");
 3240:                         chmod(0660, $filepath.'/tn-'.$file);
 3241:                         if (-e $outfile) {
 3242:                             my $copyfile=$targetdir.'/tn-'.$file;
 3243:                             if (copy($outfile,$copyfile)) {
 3244:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 3245:                                 &write_metadata($dom,$confname,$formname,
 3246:                                                 $targetdir,'tn-'.$file,$logfile);
 3247:                             } else {
 3248:                                 print $logfile "\nUnable to write ".$copyfile.
 3249:                                                ':'.$!."\n";
 3250:                             }
 3251:                         }
 3252:                     }
 3253:                 }
 3254:             }
 3255:         } else {
 3256:             $output = $versionresult;
 3257:         }
 3258:     }
 3259:     return ($output,$logourl);
 3260: }
 3261: 
 3262: sub logo_versioning {
 3263:     my ($targetdir,$file,$logfile) = @_;
 3264:     my $target = $targetdir.'/'.$file;
 3265:     my ($maxversion,$fn,$extn,$output);
 3266:     $maxversion = 0;
 3267:     if ($file =~ /^(.+)\.(\w+)$/) {
 3268:         $fn=$1;
 3269:         $extn=$2;
 3270:     }
 3271:     opendir(DIR,$targetdir);
 3272:     while (my $filename=readdir(DIR)) {
 3273:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 3274:             $maxversion=($1>$maxversion)?$1:$maxversion;
 3275:         }
 3276:     }
 3277:     $maxversion++;
 3278:     print $logfile "\nCreating old version ".$maxversion."\n";
 3279:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 3280:     if (copy($target,$copyfile)) {
 3281:         print $logfile "Copied old target to ".$copyfile."\n";
 3282:         $copyfile=$copyfile.'.meta';
 3283:         if (copy($target.'.meta',$copyfile)) {
 3284:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 3285:             $output = 'ok';
 3286:         } else {
 3287:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 3288:             $output = &mt('Failed to copy old meta').", $!, ";
 3289:         }
 3290:     } else {
 3291:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 3292:         $output = &mt('Failed to copy old target').", $!, ";
 3293:     }
 3294:     return $output;
 3295: }
 3296: 
 3297: sub write_metadata {
 3298:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 3299:     my (%metadatafields,%metadatakeys,$output);
 3300:     $metadatafields{'title'}=$formname;
 3301:     $metadatafields{'creationdate'}=time;
 3302:     $metadatafields{'lastrevisiondate'}=time;
 3303:     $metadatafields{'copyright'}='public';
 3304:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 3305:                                          $env{'user.domain'};
 3306:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 3307:     $metadatafields{'domain'}=$dom;
 3308:     {
 3309:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 3310:         my $mfh;
 3311:         unless (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 3312:             $output = &mt('Could not write metadata');
 3313:         }
 3314:         foreach (sort keys %metadatafields) {
 3315:             unless ($_=~/\./) {
 3316:                 my $unikey=$_;
 3317:                 $unikey=~/^([A-Za-z]+)/;
 3318:                 my $tag=$1;
 3319:                 $tag=~tr/A-Z/a-z/;
 3320:                 print $mfh "\n\<$tag";
 3321:                 foreach (split(/\,/,$metadatakeys{$unikey})) {
 3322:                     my $value=$metadatafields{$unikey.'.'.$_};
 3323:                     $value=~s/\"/\'\'/g;
 3324:                     print $mfh ' '.$_.'="'.$value.'"';
 3325:                 }
 3326:                 print $mfh '>'.
 3327:                     &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 3328:                         .'</'.$tag.'>';
 3329:             }
 3330:         }
 3331:         $output = 'ok';
 3332:         print $logfile "\nWrote metadata";
 3333:         close($mfh);
 3334:     }
 3335: }
 3336: 
 3337: sub check_switchserver {
 3338:     my ($dom,$confname) = @_;
 3339:     my ($allowed,$switchserver);
 3340:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 3341:     if ($home eq 'no_host') {
 3342:         $home = &Apache::lonnet::domain($dom,'primary');
 3343:     }
 3344:     my @ids=&Apache::lonnet::current_machine_ids();
 3345:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 3346:     if (!$allowed) {
 3347: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&role=dc./'.$dom.'/">'.&mt('Switch Server').'</a>';
 3348:     }
 3349:     return $switchserver;
 3350: }
 3351: 
 3352: sub javascript_set_colnums {
 3353:     return <<END;
 3354: function setDisplayColumns() {
 3355:     if (document.pickactions.width.value > 1100) {
 3356:         document.pickactions.numcols[1].checked = true;
 3357:     } else {
 3358:         document.pickactions.numcols[0].checked = true;
 3359:     }
 3360: }
 3361: END
 3362: }
 3363: 
 3364: sub modify_quotas {
 3365:     my ($dom,%domconfig) = @_;
 3366:     my ($resulttext,%changes);
 3367:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3368:     my %formhash;
 3369:     foreach my $key (keys(%env)) {
 3370:         if ($key =~ /^form\.quota_(.+)$/) {
 3371:             $formhash{$1} = $env{$key};
 3372:         }
 3373:     }
 3374:     $formhash{'default'} = $env{'form.defaultquota'};
 3375:     if (ref($domconfig{'quotas'}) eq 'HASH') {
 3376:         foreach my $key (keys(%{$domconfig{'quotas'}})) {
 3377:             if (exists($formhash{$key})) {
 3378:                 if ($formhash{$key} ne $domconfig{'quotas'}{$key}) {
 3379:                     $changes{$key} = 1;
 3380:                 }
 3381:             } else {
 3382:                 $formhash{$key} = $domconfig{'quotas'}{$key};
 3383:             }
 3384:         }
 3385:     }
 3386:     foreach my $key (keys(%formhash)) {
 3387:         if ($formhash{$key} ne '') {
 3388:             if (ref($domconfig{'quotas'}) eq 'HASH') {
 3389:                 if (!exists($domconfig{'quotas'}{$key})) {
 3390:                     $changes{$key} = 1;
 3391:                 }
 3392:             } else {
 3393:                 $changes{$key} = 1;
 3394:             }
 3395:         }
 3396:     }
 3397:     my %quotahash = (
 3398:                       quotas => {%formhash},
 3399:                     );
 3400:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 3401:                                              $dom);
 3402:     if ($putresult eq 'ok') {
 3403:         if (keys(%changes) > 0) {
 3404:             $resulttext = &mt('Changes made:').'<ul>';
 3405:             foreach my $type (@{$types},'default') {
 3406:                 if (defined($changes{$type})) { 
 3407:                     my $typetitle = $usertypes->{$type};
 3408:                     if ($type eq 'default') {
 3409:                         $typetitle = $othertitle;
 3410:                     }
 3411:                     $resulttext .= '<li>'.&mt('[_1] set to [_2] Mb',$typetitle,$formhash{$type}).'</li>';
 3412:                 }
 3413:             }
 3414:             $resulttext .= '</ul>';
 3415:         } else {
 3416:             $resulttext = &mt('No changes made to default quotas');
 3417:         }
 3418:     } else {
 3419:         $resulttext = '<span class="LC_error">'.
 3420: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 3421:     }
 3422:     return $resulttext;
 3423: }
 3424: 
 3425: sub modify_autoenroll {
 3426:     my ($dom,%domconfig) = @_;
 3427:     my ($resulttext,%changes);
 3428:     my %currautoenroll;
 3429:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
 3430:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
 3431:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
 3432:         }
 3433:     }
 3434:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 3435:     my %title = ( run => 'Auto-enrollment active',
 3436:                   sender => 'Sender for notification messages');
 3437:     my @offon = ('off','on');
 3438:     my $sender_uname = $env{'form.sender_uname'};
 3439:     my $sender_domain = $env{'form.sender_domain'};
 3440:     if ($sender_domain eq '') {
 3441:         $sender_uname = '';
 3442:     } elsif ($sender_uname eq '') {
 3443:         $sender_domain = '';
 3444:     }
 3445:     my %autoenrollhash =  (
 3446:                        autoenroll => { run => $env{'form.autoenroll_run'},
 3447:                                        sender_uname => $sender_uname,
 3448:                                        sender_domain => $sender_domain,
 3449: 
 3450:                                 }
 3451:                      );
 3452:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
 3453:                                              $dom);
 3454:     if ($putresult eq 'ok') {
 3455:         if (exists($currautoenroll{'run'})) {
 3456:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
 3457:                  $changes{'run'} = 1;
 3458:              }
 3459:         } elsif ($autorun) {
 3460:             if ($env{'form.autoenroll_run'} ne '1') {
 3461:                  $changes{'run'} = 1;
 3462:             }
 3463:         }
 3464:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
 3465:             $changes{'sender'} = 1;
 3466:         }
 3467:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
 3468:             $changes{'sender'} = 1;
 3469:         }
 3470:         if (keys(%changes) > 0) {
 3471:             $resulttext = &mt('Changes made:').'<ul>';
 3472:             if ($changes{'run'}) {
 3473:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
 3474:             }
 3475:             if ($changes{'sender'}) {
 3476:                 if ($sender_uname eq '' || $sender_domain eq '') {
 3477:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
 3478:                 } else {
 3479:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
 3480:                 }
 3481:             }
 3482:             $resulttext .= '</ul>';
 3483:         } else {
 3484:             $resulttext = &mt('No changes made to auto-enrollment settings');
 3485:         }
 3486:     } else {
 3487:         $resulttext = '<span class="LC_error">'.
 3488: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 3489:     }
 3490:     return $resulttext;
 3491: }
 3492: 
 3493: sub modify_autoupdate {
 3494:     my ($dom,%domconfig) = @_;
 3495:     my ($resulttext,%currautoupdate,%fields,%changes);
 3496:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
 3497:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
 3498:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
 3499:         }
 3500:     }
 3501:     my @offon = ('off','on');
 3502:     my %title = &Apache::lonlocal::texthash (
 3503:                    run => 'Auto-update:',
 3504:                    classlists => 'Updates to user information in classlists?'
 3505:                 );
 3506:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3507:     my %fieldtitles = &Apache::lonlocal::texthash (
 3508:                         id => 'Student/Employee ID',
 3509:                         permanentemail => 'E-mail address',
 3510:                         lastname => 'Last Name',
 3511:                         firstname => 'First Name',
 3512:                         middlename => 'Middle Name',
 3513:                         gen => 'Generation',
 3514:                       );
 3515:     my $othertitle = &mt('All users');
 3516:     if (keys(%{$usertypes}) >  0) {
 3517:         $othertitle = &mt('Other users');
 3518:     }
 3519:     foreach my $key (keys(%env)) {
 3520:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
 3521:             push(@{$fields{$1}},$2);
 3522:         }
 3523:     }
 3524:     my %updatehash = (
 3525:                       autoupdate => { run => $env{'form.autoupdate_run'},
 3526:                                       classlists => $env{'form.classlists'},
 3527:                                       fields => {%fields},
 3528:                                     }
 3529:                      );
 3530:     foreach my $key (keys(%currautoupdate)) {
 3531:         if (($key eq 'run') || ($key eq 'classlists')) {
 3532:             if (exists($updatehash{autoupdate}{$key})) {
 3533:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
 3534:                     $changes{$key} = 1;
 3535:                 }
 3536:             }
 3537:         } elsif ($key eq 'fields') {
 3538:             if (ref($currautoupdate{$key}) eq 'HASH') {
 3539:                 foreach my $item (@{$types},'default') {
 3540:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
 3541:                         my $change = 0;
 3542:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
 3543:                             if (!exists($fields{$item})) {
 3544:                                 $change = 1;
 3545:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
 3546:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
 3547:                                     $change = 1;
 3548:                                 }
 3549:                             }
 3550:                         }
 3551:                         if ($change) {
 3552:                             push(@{$changes{$key}},$item);
 3553:                         }
 3554:                     } 
 3555:                 }
 3556:             }
 3557:         }
 3558:     }
 3559:     foreach my $item (@{$types},'default') {
 3560:         if (defined($fields{$item})) {
 3561:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
 3562:                 if (!exists($currautoupdate{'fields'}{$item})) {
 3563:                     push(@{$changes{'fields'}},$item);
 3564:                 }
 3565:             } else {
 3566:                 push(@{$changes{'fields'}},$item);
 3567:             }
 3568:         }
 3569:     }
 3570:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
 3571:                                              $dom);
 3572:     if ($putresult eq 'ok') {
 3573:         if (keys(%changes) > 0) {
 3574:             $resulttext = &mt('Changes made:').'<ul>';
 3575:             foreach my $key (sort(keys(%changes))) {
 3576:                 if (ref($changes{$key}) eq 'ARRAY') {
 3577:                     foreach my $item (@{$changes{$key}}) {
 3578:                         my @newvalues;
 3579:                         foreach my $type (@{$fields{$item}}) {
 3580:                             push(@newvalues,$fieldtitles{$type});
 3581:                         }
 3582:                         my $newvaluestr;
 3583:                         if (@newvalues > 0) {
 3584:                             $newvaluestr = join(', ',@newvalues);
 3585:                         } else {
 3586:                             $newvaluestr = &mt('none');
 3587:                         }
 3588:                         if ($item eq 'default') {
 3589:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
 3590:                         } else {
 3591:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
 3592:                         }
 3593:                     }
 3594:                 } else {
 3595:                     my $newvalue;
 3596:                     if ($key eq 'run') {
 3597:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
 3598:                     } else {
 3599:                         $newvalue = $offon[$env{'form.'.$key}];
 3600:                     }
 3601:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
 3602:                 }
 3603:             }
 3604:             $resulttext .= '</ul>';
 3605:         } else {
 3606:             $resulttext = &mt('No changes made to autoupdates');
 3607:         }
 3608:     } else {
 3609:         $resulttext = '<span class="LC_error">'.
 3610: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 3611:     }
 3612:     return $resulttext;
 3613: }
 3614: 
 3615: sub modify_directorysrch {
 3616:     my ($dom,%domconfig) = @_;
 3617:     my ($resulttext,%changes);
 3618:     my %currdirsrch;
 3619:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
 3620:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
 3621:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
 3622:         }
 3623:     }
 3624:     my %title = ( available => 'Directory search available',
 3625:                   localonly => 'Other domains can search',
 3626:                   searchby => 'Search types',
 3627:                   searchtypes => 'Search latitude');
 3628:     my @offon = ('off','on');
 3629:     my @otherdoms = ('Yes','No');
 3630: 
 3631:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
 3632:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
 3633:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
 3634: 
 3635:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3636:     if (keys(%{$usertypes}) == 0) {
 3637:         @cansearch = ('default');
 3638:     } else {
 3639:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
 3640:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
 3641:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
 3642:                     push(@{$changes{'cansearch'}},$type);
 3643:                 }
 3644:             }
 3645:             foreach my $type (@cansearch) {
 3646:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
 3647:                     push(@{$changes{'cansearch'}},$type);
 3648:                 }
 3649:             }
 3650:         } else {
 3651:             push(@{$changes{'cansearch'}},@cansearch);
 3652:         }
 3653:     }
 3654: 
 3655:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
 3656:         foreach my $by (@{$currdirsrch{'searchby'}}) {
 3657:             if (!grep(/^\Q$by\E$/,@searchby)) {
 3658:                 push(@{$changes{'searchby'}},$by);
 3659:             }
 3660:         }
 3661:         foreach my $by (@searchby) {
 3662:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
 3663:                 push(@{$changes{'searchby'}},$by);
 3664:             }
 3665:         }
 3666:     } else {
 3667:         push(@{$changes{'searchby'}},@searchby);
 3668:     }
 3669: 
 3670:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
 3671:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
 3672:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
 3673:                 push(@{$changes{'searchtypes'}},$type);
 3674:             }
 3675:         }
 3676:         foreach my $type (@searchtypes) {
 3677:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
 3678:                 push(@{$changes{'searchtypes'}},$type);
 3679:             }
 3680:         }
 3681:     } else {
 3682:         if (exists($currdirsrch{'searchtypes'})) {
 3683:             foreach my $type (@searchtypes) {  
 3684:                 if ($type ne $currdirsrch{'searchtypes'}) { 
 3685:                     push(@{$changes{'searchtypes'}},$type);
 3686:                 }
 3687:             }
 3688:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
 3689:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
 3690:             }   
 3691:         } else {
 3692:             push(@{$changes{'searchtypes'}},@searchtypes); 
 3693:         }
 3694:     }
 3695: 
 3696:     my %dirsrch_hash =  (
 3697:             directorysrch => { available => $env{'form.dirsrch_available'},
 3698:                                cansearch => \@cansearch,
 3699:                                localonly => $env{'form.dirsrch_localonly'},
 3700:                                searchby => \@searchby,
 3701:                                searchtypes => \@searchtypes,
 3702:                              }
 3703:             );
 3704:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
 3705:                                              $dom);
 3706:     if ($putresult eq 'ok') {
 3707:         if (exists($currdirsrch{'available'})) {
 3708:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
 3709:                  $changes{'available'} = 1;
 3710:              }
 3711:         } else {
 3712:             if ($env{'form.dirsrch_available'} eq '1') {
 3713:                 $changes{'available'} = 1;
 3714:             }
 3715:         }
 3716:         if (exists($currdirsrch{'localonly'})) {
 3717:              if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_localonly'}) {
 3718:                  $changes{'localonly'} = 1;
 3719:              }
 3720:         } else {
 3721:             if ($env{'form.dirsrch_localonly'} eq '1') {
 3722:                 $changes{'localonly'} = 1;
 3723:             }
 3724:         }
 3725:         if (keys(%changes) > 0) {
 3726:             $resulttext = &mt('Changes made:').'<ul>';
 3727:             if ($changes{'available'}) {
 3728:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
 3729:             }
 3730:             if ($changes{'localonly'}) {
 3731:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_localonly'}]").'</li>';
 3732:             }
 3733: 
 3734:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
 3735:                 my $chgtext;
 3736:                 if (ref($usertypes) eq 'HASH') {
 3737:                     if (keys(%{$usertypes}) > 0) {
 3738:                         foreach my $type (@{$types}) {
 3739:                             if (grep(/^\Q$type\E$/,@cansearch)) {
 3740:                                 $chgtext .= $usertypes->{$type}.'; ';
 3741:                             }
 3742:                         }
 3743:                         if (grep(/^default$/,@cansearch)) {
 3744:                             $chgtext .= $othertitle;
 3745:                         } else {
 3746:                             $chgtext =~ s/\; $//;
 3747:                         }
 3748:                         $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>';
 3749:                     }
 3750:                 }
 3751:             }
 3752:             if (ref($changes{'searchby'}) eq 'ARRAY') {
 3753:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
 3754:                 my $chgtext;
 3755:                 foreach my $type (@{$titleorder}) {
 3756:                     if (grep(/^\Q$type\E$/,@searchby)) {
 3757:                         if (defined($searchtitles->{$type})) {
 3758:                             $chgtext .= $searchtitles->{$type}.'; ';
 3759:                         }
 3760:                     }
 3761:                 }
 3762:                 $chgtext =~ s/\; $//;
 3763:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
 3764:             }
 3765:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
 3766:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
 3767:                 my $chgtext;
 3768:                 foreach my $type (@{$srchtypeorder}) {
 3769:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
 3770:                         if (defined($srchtypes_desc->{$type})) {
 3771:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
 3772:                         }
 3773:                     }
 3774:                 }
 3775:                 $chgtext =~ s/\; $//;
 3776:                 $resulttext .= '<li>'.&mt("$title{'searchtypes'} set to: \"[_1]\"",$chgtext).'</li>';
 3777:             }
 3778:             $resulttext .= '</ul>';
 3779:         } else {
 3780:             $resulttext = &mt('No changes made to institution directory search settings');
 3781:         }
 3782:     } else {
 3783:         $resulttext = '<span class="LC_error">'.
 3784:                       &mt('An error occurred: [_1]',$putresult).'</span>';
 3785:     }
 3786:     return $resulttext;
 3787: }
 3788: 
 3789: sub modify_contacts {
 3790:     my ($dom,%domconfig) = @_;
 3791:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
 3792:     if (ref($domconfig{'contacts'}) eq 'HASH') {
 3793:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
 3794:             $currsetting{$key} = $domconfig{'contacts'}{$key};
 3795:         }
 3796:     }
 3797:     my (%others,%to);
 3798:     my @contacts = ('supportemail','adminemail');
 3799:     my @mailings = ('errormail','packagesmail','helpdeskmail');
 3800:     foreach my $type (@mailings) {
 3801:         @{$newsetting{$type}} = 
 3802:             &Apache::loncommon::get_env_multiple('form.'.$type);
 3803:         foreach my $item (@contacts) {
 3804:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
 3805:                 $contacts_hash{contacts}{$type}{$item} = 1;
 3806:             } else {
 3807:                 $contacts_hash{contacts}{$type}{$item} = 0;
 3808:             }
 3809:         }  
 3810:         $others{$type} = $env{'form.'.$type.'_others'};
 3811:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
 3812:     }
 3813:     foreach my $item (@contacts) {
 3814:         $to{$item} = $env{'form.'.$item};
 3815:         $contacts_hash{'contacts'}{$item} = $to{$item};
 3816:     }
 3817:     if (keys(%currsetting) > 0) {
 3818:         foreach my $item (@contacts) {
 3819:             if ($to{$item} ne $currsetting{$item}) {
 3820:                 $changes{$item} = 1;
 3821:             }
 3822:         }
 3823:         foreach my $type (@mailings) {
 3824:             foreach my $item (@contacts) {
 3825:                 if (ref($currsetting{$type}) eq 'HASH') {
 3826:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
 3827:                         push(@{$changes{$type}},$item);
 3828:                     }
 3829:                 } else {
 3830:                     push(@{$changes{$type}},@{$newsetting{$type}});
 3831:                 }
 3832:             }
 3833:             if ($others{$type} ne $currsetting{$type}{'others'}) {
 3834:                 push(@{$changes{$type}},'others');
 3835:             }
 3836:         }
 3837:     } else {
 3838:         my %default;
 3839:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 3840:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 3841:         $default{'errormail'} = 'adminemail';
 3842:         $default{'packagesmail'} = 'adminemail';
 3843:         $default{'helpdeskmail'} = 'supportemail';
 3844:         foreach my $item (@contacts) {
 3845:            if ($to{$item} ne $default{$item}) {
 3846:               $changes{$item} = 1;
 3847:            } 
 3848:         }
 3849:         foreach my $type (@mailings) {
 3850:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
 3851:                
 3852:                 push(@{$changes{$type}},@{$newsetting{$type}});
 3853:             }
 3854:             if ($others{$type} ne '') {
 3855:                 push(@{$changes{$type}},'others');
 3856:             } 
 3857:         }
 3858:     }
 3859:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
 3860:                                              $dom);
 3861:     if ($putresult eq 'ok') {
 3862:         if (keys(%changes) > 0) {
 3863:             my ($titles,$short_titles)  = &contact_titles();
 3864:             $resulttext = &mt('Changes made:').'<ul>';
 3865:             foreach my $item (@contacts) {
 3866:                 if ($changes{$item}) {
 3867:                     $resulttext .= '<li>'.$titles->{$item}.
 3868:                                     &mt(' set to: ').
 3869:                                     '<span class="LC_cusr_emph">'.
 3870:                                     $to{$item}.'</span></li>';
 3871:                 }
 3872:             }
 3873:             foreach my $type (@mailings) {
 3874:                 if (ref($changes{$type}) eq 'ARRAY') {
 3875:                     $resulttext .= '<li>'.$titles->{$type}.': ';
 3876:                     my @text;
 3877:                     foreach my $item (@{$newsetting{$type}}) {
 3878:                         push(@text,$short_titles->{$item});
 3879:                     }
 3880:                     if ($others{$type} ne '') {
 3881:                         push(@text,$others{$type});
 3882:                     }
 3883:                     $resulttext .= '<span class="LC_cusr_emph">'.
 3884:                                    join(', ',@text).'</span></li>';
 3885:                 }
 3886:             }
 3887:             $resulttext .= '</ul>';
 3888:         } else {
 3889:             $resulttext = &mt('No changes made to contact information');
 3890:         }
 3891:     } else {
 3892:         $resulttext = '<span class="LC_error">'.
 3893:             &mt('An error occurred: [_1].',$putresult).'</span>';
 3894:     }
 3895:     return $resulttext;
 3896: }
 3897: 
 3898: sub modify_usercreation {
 3899:     my ($dom,%domconfig) = @_;
 3900:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate);
 3901:     my $warningmsg;
 3902:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
 3903:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
 3904:             $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
 3905:         }
 3906:     }
 3907:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
 3908:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
 3909:     my @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
 3910:     my @contexts = ('author','course','selfcreate');
 3911:     foreach my $item(@contexts) {
 3912:         if ($item eq 'selfcreate') {
 3913:             @{$cancreate{$item}} = &Apache::loncommon::get_env_multiple('form.can_createuser_'.$item);
 3914:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 3915:             if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || ($domdefaults{'auth_def'} eq 'localauth'))) {
 3916:                 if (ref($cancreate{$item}) eq 'ARRAY') { 
 3917:                     if (grep(/^login$/,@{$cancreate{$item}})) {
 3918:                         $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.');   
 3919:                     }
 3920:                 }
 3921:             }
 3922:         } else {
 3923:             $cancreate{$item} = $env{'form.can_createuser_'.$item};
 3924:         }
 3925:     }
 3926:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
 3927:         foreach my $item (@contexts) {
 3928:             if ($item eq 'selfcreate') {
 3929:                 if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') { 
 3930:                     foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
 3931:                         if (!grep(/^$curr$/,@{$cancreate{$item}})) {
 3932:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 3933:                                 push(@{$changes{'cancreate'}},$item);
 3934:                             }
 3935:                         }
 3936:                     }
 3937:                 } else {
 3938:                     if ($curr_usercreation{'cancreate'}{$item} eq '') {
 3939:                         if (@{$cancreate{$item}} > 0) {
 3940:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 3941:                                 push(@{$changes{'cancreate'}},$item);
 3942:                             }
 3943:                         }
 3944:                     } else {
 3945:                         if ($curr_usercreation{'cancreate'}{$item} eq 'any') {
 3946:                             if (@{$cancreate{$item}} < 3) {
 3947:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 3948:                                     push(@{$changes{'cancreate'}},$item);
 3949:                                 }
 3950:                             }
 3951:                         } elsif ($curr_usercreation{'cancreate'}{$item} eq 'none') {
 3952:                             if (@{$cancreate{$item}} > 0) {
 3953:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 3954:                                     push(@{$changes{'cancreate'}},$item);
 3955:                                 }
 3956:                             }
 3957:                         } elsif (!grep(/^$curr_usercreation{'cancreate'}{$item}$/,@{$cancreate{$item}})) {
 3958:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 3959:                                 push(@{$changes{'cancreate'}},$item);
 3960:                             }
 3961:                         }
 3962:                     }
 3963:                 }
 3964:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 3965:                     foreach my $type (@{$cancreate{$item}}) {
 3966:                         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
 3967:                             if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
 3968:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 3969:                                     push(@{$changes{'cancreate'}},$item);
 3970:                                 }
 3971:                             }
 3972:                         } elsif (($curr_usercreation{'cancreate'}{$item} ne 'any') &&
 3973:                                  ($curr_usercreation{'cancreate'}{$item} ne 'none')) {
 3974:                             if ($curr_usercreation{'cancreate'}{$item} ne $type) {
 3975:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
 3976:                                     push(@{$changes{'cancreate'}},$item);
 3977:                                 }
 3978:                             }
 3979:                         }
 3980:                     }
 3981:                 }
 3982:             } else {
 3983:                 if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
 3984:                     push(@{$changes{'cancreate'}},$item);
 3985:                 }
 3986:             }
 3987:         }
 3988:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
 3989:         foreach my $item (@contexts) {
 3990:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
 3991:                 if ($cancreate{$item} ne 'any') {
 3992:                     push(@{$changes{'cancreate'}},$item);
 3993:                 }
 3994:             } else {
 3995:                 if ($cancreate{$item} ne 'none') {
 3996:                     push(@{$changes{'cancreate'}},$item);
 3997:                 }
 3998:             }
 3999:         }
 4000:     } else {
 4001:         foreach my $item (@contexts)  {
 4002:             push(@{$changes{'cancreate'}},$item);
 4003:         }
 4004:     }
 4005: 
 4006:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
 4007:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
 4008:             if (!grep(/^\Q$type\E$/,@username_rule)) {
 4009:                 push(@{$changes{'username_rule'}},$type);
 4010:             }
 4011:         }
 4012:         foreach my $type (@username_rule) {
 4013:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
 4014:                 push(@{$changes{'username_rule'}},$type);
 4015:             }
 4016:         }
 4017:     } else {
 4018:         push(@{$changes{'username_rule'}},@username_rule);
 4019:     }
 4020: 
 4021:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
 4022:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
 4023:             if (!grep(/^\Q$type\E$/,@id_rule)) {
 4024:                 push(@{$changes{'id_rule'}},$type);
 4025:             }
 4026:         }
 4027:         foreach my $type (@id_rule) {
 4028:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
 4029:                 push(@{$changes{'id_rule'}},$type);
 4030:             }
 4031:         }
 4032:     } else {
 4033:         push(@{$changes{'id_rule'}},@id_rule);
 4034:     }
 4035: 
 4036:     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
 4037:         foreach my $type (@{$curr_usercreation{'email_rule'}}) {
 4038:             if (!grep(/^\Q$type\E$/,@email_rule)) {
 4039:                 push(@{$changes{'email_rule'}},$type);
 4040:             }
 4041:         }
 4042:         foreach my $type (@email_rule) {
 4043:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
 4044:                 push(@{$changes{'email_rule'}},$type);
 4045:             }
 4046:         }
 4047:     } else {
 4048:         push(@{$changes{'email_rule'}},@email_rule);
 4049:     }
 4050: 
 4051:     my @authen_contexts = ('author','course','domain');
 4052:     my @authtypes = ('int','krb4','krb5','loc');
 4053:     my %authhash;
 4054:     foreach my $item (@authen_contexts) {
 4055:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
 4056:         foreach my $auth (@authtypes) {
 4057:             if (grep(/^\Q$auth\E$/,@authallowed)) {
 4058:                 $authhash{$item}{$auth} = 1;
 4059:             } else {
 4060:                 $authhash{$item}{$auth} = 0;
 4061:             }
 4062:         }
 4063:     }
 4064:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
 4065:         foreach my $item (@authen_contexts) {
 4066:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
 4067:                 foreach my $auth (@authtypes) {
 4068:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
 4069:                         push(@{$changes{'authtypes'}},$item);
 4070:                         last;
 4071:                     }
 4072:                 }
 4073:             }
 4074:         }
 4075:     } else {
 4076:         foreach my $item (@authen_contexts) {
 4077:             push(@{$changes{'authtypes'}},$item);
 4078:         }
 4079:     }
 4080: 
 4081:     my %usercreation_hash =  (
 4082:             usercreation => {
 4083:                               cancreate     => \%cancreate,
 4084:                               username_rule => \@username_rule,
 4085:                               id_rule       => \@id_rule,
 4086:                               email_rule    => \@email_rule,
 4087:                               authtypes     => \%authhash,
 4088:                             }
 4089:             );
 4090: 
 4091:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
 4092:                                              $dom);
 4093: 
 4094:     my %selfcreatetypes = (
 4095:                              sso   => 'users authenticated by institutional single sign on',
 4096:                              login => 'users authenticated by institutional log-in',
 4097:                              email => 'users who provide a valid e-mail address for use as the username',
 4098:                           );
 4099:     if ($putresult eq 'ok') {
 4100:         if (keys(%changes) > 0) {
 4101:             $resulttext = &mt('Changes made:').'<ul>';
 4102:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
 4103:                 my %lt = &usercreation_types();
 4104:                 foreach my $type (@{$changes{'cancreate'}}) {
 4105:                     my $chgtext =  $lt{$type}.', ';
 4106:                     if ($type eq 'selfcreate') {
 4107:                         if (@{$cancreate{$type}} == 0) {
 4108:                             $chgtext .= &mt('creation of a new user account is not permitted.');
 4109:                         } else {
 4110:                             $chgtext .= &mt('creation of a new account is permitted for:<ul>');
 4111:                             foreach my $case (@{$cancreate{$type}}) {
 4112:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
 4113:                             }
 4114:                             $chgtext .= '</ul>';
 4115:                         }
 4116:                     } else {
 4117:                         if ($cancreate{$type} eq 'none') {
 4118:                             $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
 4119:                         } elsif ($cancreate{$type} eq 'any') {
 4120:                             $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
 4121:                         } elsif ($cancreate{$type} eq 'official') {
 4122:                             $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
 4123:                         } elsif ($cancreate{$type} eq 'unofficial') {
 4124:                             $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
 4125:                         }
 4126:                     }
 4127:                     $resulttext .= '<li>'.$chgtext.'</li>';
 4128:                 }
 4129:             }
 4130:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
 4131:                 my ($rules,$ruleorder) = 
 4132:                     &Apache::lonnet::inst_userrules($dom,'username');
 4133:                 my $chgtext = '<ul>';
 4134:                 foreach my $type (@username_rule) {
 4135:                     if (ref($rules->{$type}) eq 'HASH') {
 4136:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
 4137:                     }
 4138:                 }
 4139:                 $chgtext .= '</ul>';
 4140:                 if (@username_rule > 0) {
 4141:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
 4142:                 } else {
 4143:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
 4144:                 }
 4145:             }
 4146:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
 4147:                 my ($idrules,$idruleorder) = 
 4148:                     &Apache::lonnet::inst_userrules($dom,'id');
 4149:                 my $chgtext = '<ul>';
 4150:                 foreach my $type (@id_rule) {
 4151:                     if (ref($idrules->{$type}) eq 'HASH') {
 4152:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
 4153:                     }
 4154:                 }
 4155:                 $chgtext .= '</ul>';
 4156:                 if (@id_rule > 0) {
 4157:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
 4158:                 } else {
 4159:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
 4160:                 }
 4161:             }
 4162:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
 4163:                 my ($emailrules,$emailruleorder) =
 4164:                     &Apache::lonnet::inst_userrules($dom,'email');
 4165:                 my $chgtext = '<ul>';
 4166:                 foreach my $type (@email_rule) {
 4167:                     if (ref($emailrules->{$type}) eq 'HASH') {
 4168:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
 4169:                     }
 4170:                 }
 4171:                 $chgtext .= '</ul>';
 4172:                 if (@email_rule > 0) {
 4173:                     $resulttext .= '<li>'.&mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').$chgtext.'</li>';
 4174:                 } else {
 4175:                     $resulttext .= '<li>'.&mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').'</li>';
 4176:                 }
 4177:             }
 4178: 
 4179:             my %authname = &authtype_names();
 4180:             my %context_title = &context_names();
 4181:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
 4182:                 my $chgtext = '<ul>';
 4183:                 foreach my $type (@{$changes{'authtypes'}}) {
 4184:                     my @allowed;
 4185:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
 4186:                     foreach my $auth (@authtypes) {
 4187:                         if ($authhash{$type}{$auth}) {
 4188:                             push(@allowed,$authname{$auth});
 4189:                         }
 4190:                     }
 4191:                     if (@allowed > 0) {
 4192:                         $chgtext .= join(', ',@allowed).'</li>';
 4193:                     } else {
 4194:                         $chgtext .= &mt('none').'</li>';
 4195:                     }
 4196:                 }
 4197:                 $chgtext .= '</ul>';
 4198:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
 4199:                 $resulttext .= '</li>';
 4200:             }
 4201:             $resulttext .= '</ul>';
 4202:         } else {
 4203:             $resulttext = &mt('No changes made to user creation settings');
 4204:         }
 4205:     } else {
 4206:         $resulttext = '<span class="LC_error">'.
 4207:             &mt('An error occurred: [_1]',$putresult).'</span>';
 4208:     }
 4209:     if ($warningmsg ne '') {
 4210:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
 4211:     }
 4212:     return $resulttext;
 4213: }
 4214: 
 4215: sub modify_usermodification {
 4216:     my ($dom,%domconfig) = @_;
 4217:     my ($resulttext,%curr_usermodification,%changes);
 4218:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
 4219:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
 4220:             $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
 4221:         }
 4222:     }
 4223:     my @contexts = ('author','course','selfcreate');
 4224:     my %context_title = (
 4225:                            author => 'In author context',
 4226:                            course => 'In course context',
 4227:                            selfcreate => 'When self creating account', 
 4228:                         );
 4229:     my @fields = ('lastname','firstname','middlename','generation',
 4230:                   'permanentemail','id');
 4231:     my %roles = (
 4232:                   author => ['ca','aa'],
 4233:                   course => ['st','ep','ta','in','cr'],
 4234:                 );
 4235:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4236:     if (ref($types) eq 'ARRAY') {
 4237:         push(@{$types},'default');
 4238:         $usertypes->{'default'} = $othertitle;
 4239:     }
 4240:     $roles{'selfcreate'} = $types;  
 4241:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4242:     my %modifyhash;
 4243:     foreach my $context (@contexts) {
 4244:         foreach my $role (@{$roles{$context}}) {
 4245:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
 4246:             foreach my $item (@fields) {
 4247:                 if (grep(/^\Q$item\E$/,@modifiable)) {
 4248:                     $modifyhash{$context}{$role}{$item} = 1;
 4249:                 } else {
 4250:                     $modifyhash{$context}{$role}{$item} = 0;
 4251:                 }
 4252:             }
 4253:         }
 4254:         if (ref($curr_usermodification{$context}) eq 'HASH') {
 4255:             foreach my $role (@{$roles{$context}}) {
 4256:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
 4257:                     foreach my $field (@fields) {
 4258:                         if ($modifyhash{$context}{$role}{$field} ne 
 4259:                                 $curr_usermodification{$context}{$role}{$field}) {
 4260:                             push(@{$changes{$context}},$role);
 4261:                             last;
 4262:                         }
 4263:                     }
 4264:                 }
 4265:             }
 4266:         } else {
 4267:             foreach my $context (@contexts) {
 4268:                 foreach my $role (@{$roles{$context}}) {
 4269:                     push(@{$changes{$context}},$role);
 4270:                 }
 4271:             }
 4272:         }
 4273:     }
 4274:     my %usermodification_hash =  (
 4275:                                    usermodification => \%modifyhash,
 4276:                                  );
 4277:     my $putresult = &Apache::lonnet::put_dom('configuration',
 4278:                                              \%usermodification_hash,$dom);
 4279:     if ($putresult eq 'ok') {
 4280:         if (keys(%changes) > 0) {
 4281:             $resulttext = &mt('Changes made: ').'<ul>';
 4282:             foreach my $context (@contexts) {
 4283:                 if (ref($changes{$context}) eq 'ARRAY') {
 4284:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
 4285:                     if (ref($changes{$context}) eq 'ARRAY') {
 4286:                         foreach my $role (@{$changes{$context}}) {
 4287:                             my $rolename;
 4288:                             if ($context eq 'selfcreate') {
 4289:                                 $rolename = $role;
 4290:                                 if (ref($usertypes) eq 'HASH') {
 4291:                                     if ($usertypes->{$role} ne '') {
 4292:                                         $rolename = $usertypes->{$role};
 4293:                                     }
 4294:                                 }
 4295:                             } else {
 4296:                                 if ($role eq 'cr') {
 4297:                                     $rolename = &mt('Custom');
 4298:                                 } else {
 4299:                                     $rolename = &Apache::lonnet::plaintext($role);
 4300:                                 }
 4301:                             }
 4302:                             my @modifiable;
 4303:                             if ($context eq 'selfcreate') {
 4304:                                 $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): ');
 4305:                             } else {
 4306:                                 $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
 4307:                             }
 4308:                             foreach my $field (@fields) {
 4309:                                 if ($modifyhash{$context}{$role}{$field}) {
 4310:                                     push(@modifiable,$fieldtitles{$field});
 4311:                                 }
 4312:                             }
 4313:                             if (@modifiable > 0) {
 4314:                                 $resulttext .= join(', ',@modifiable);
 4315:                             } else {
 4316:                                 $resulttext .= &mt('none'); 
 4317:                             }
 4318:                             $resulttext .= '</li>';
 4319:                         }
 4320:                         $resulttext .= '</ul></li>';
 4321:                     }
 4322:                 }
 4323:             }
 4324:             $resulttext .= '</ul>';
 4325:         } else {
 4326:             $resulttext = &mt('No changes made to user modification settings');
 4327:         }
 4328:     } else {
 4329:         $resulttext = '<span class="LC_error">'.
 4330:             &mt('An error occurred: [_1]',$putresult).'</span>';
 4331:     }
 4332:     return $resulttext;
 4333: }
 4334: 
 4335: sub modify_defaults {
 4336:     my ($dom,$r) = @_;
 4337:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
 4338:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 4339:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def');
 4340:     my @authtypes = ('internal','krb4','krb5','localauth');
 4341:     foreach my $item (@items) {
 4342:         $newvalues{$item} = $env{'form.'.$item};
 4343:         if ($item eq 'auth_def') {
 4344:             if ($newvalues{$item} ne '') {
 4345:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
 4346:                     push(@errors,$item);
 4347:                 }
 4348:             }
 4349:         } elsif ($item eq 'lang_def') {
 4350:             if ($newvalues{$item} ne '') {
 4351:                 if ($newvalues{$item} =~ /^(\w+)/) {
 4352:                     my $langcode = $1;
 4353:                     if (code2language($langcode) eq '') {
 4354:                         push(@errors,$item);
 4355:                     }
 4356:                 } else {
 4357:                     push(@errors,$item);
 4358:                 }
 4359:             }
 4360:         } elsif ($item eq 'timezone_def') {
 4361:             if ($newvalues{$item} ne '') {
 4362:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
 4363:                     push(@errors,$item);   
 4364:                 }
 4365:             }
 4366:         } elsif ($item eq 'datelocale_def') {
 4367:             if ($newvalues{$item} ne '') {
 4368:                 my @datelocale_ids = DateTime::Locale->ids();
 4369:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
 4370:                     push(@errors,$item);
 4371:                 }
 4372:             }
 4373:         }
 4374:         if (grep(/^\Q$item\E$/,@errors)) {
 4375:             $newvalues{$item} = $domdefaults{$item};
 4376:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
 4377:             $changes{$item} = 1;
 4378:         }
 4379:     }
 4380:     my %defaults_hash = (
 4381:                          defaults => { auth_def => $newvalues{'auth_def'},
 4382:                                        auth_arg_def => $newvalues{'auth_arg_def'},
 4383:                                        lang_def => $newvalues{'lang_def'},
 4384:                                        timezone_def => $newvalues{'timezone_def'},
 4385:                                        datelocale_def => $newvalues{'datelocale_def'},
 4386:                                      }
 4387:                        );
 4388:     my $title = &defaults_titles();
 4389:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
 4390:                                              $dom);
 4391:     if ($putresult eq 'ok') {
 4392:         if (keys(%changes) > 0) {
 4393:             $resulttext = &mt('Changes made:').'<ul>';
 4394:             my $version = $r->dir_config('lonVersion');
 4395:             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";
 4396:             foreach my $item (sort(keys(%changes))) {
 4397:                 my $value = $env{'form.'.$item};
 4398:                 if ($value eq '') {
 4399:                     $value = &mt('none');
 4400:                 } elsif ($item eq 'auth_def') {
 4401:                     my %authnames = &authtype_names();
 4402:                     my %shortauth = (
 4403:                              internal => 'int',
 4404:                              krb4 => 'krb4',
 4405:                              krb5 => 'krb5',
 4406:                              localauth  => 'loc',
 4407:                     );
 4408:                     $value = $authnames{$shortauth{$value}};
 4409:                 }
 4410:                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
 4411:                 $mailmsgtext .= "$title->{$item} set to $value\n";  
 4412:             }
 4413:             $resulttext .= '</ul>';
 4414:             $mailmsgtext .= "\n";
 4415:             my $cachetime = 24*60*60;
 4416:             &Apache::lonnet::do_cache_new('domdefaults',$dom,
 4417:                                           $defaults_hash{'defaults'},$cachetime);
 4418:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
 4419:                 my $sysmail = $r->dir_config('lonSysEMail');
 4420:                 &Apache::lonmsg::sendemail($sysmail,"LON-CAPA Domain Settings Change - $dom",$mailmsgtext);
 4421:             }
 4422:         } else {
 4423:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
 4424:         }
 4425:     } else {
 4426:         $resulttext = '<span class="LC_error">'.
 4427:             &mt('An error occurred: [_1]',$putresult).'</span>';
 4428:     }
 4429:     if (@errors > 0) {
 4430:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
 4431:         foreach my $item (@errors) {
 4432:             $resulttext .= ' "'.$title->{$item}.'",';
 4433:         }
 4434:         $resulttext =~ s/,$//;
 4435:     }
 4436:     return $resulttext;
 4437: }
 4438: 
 4439: sub modify_scantron {
 4440:     my ($r,$dom,$confname,%domconfig) = @_;
 4441:     my ($resulttext,%confhash,%changes,$errors);
 4442:     my $custom = 'custom.tab';
 4443:     my $default = 'default.tab';
 4444:     my $servadm = $r->dir_config('lonAdmEMail');
 4445:     my ($configuserok,$author_ok,$switchserver) = 
 4446:         &config_check($dom,$confname,$servadm);
 4447:     if ($env{'form.scantronformat.filename'} ne '') {
 4448:         my $error;
 4449:         if ($configuserok eq 'ok') {
 4450:             if ($switchserver) {
 4451:                 $error = &mt("Upload of scantron format file is not permitted to this server: [_1]",$switchserver);
 4452:             } else {
 4453:                 if ($author_ok eq 'ok') {
 4454:                     my ($result,$scantronurl) =
 4455:                         &publishlogo($r,'upload','scantronformat',$dom,
 4456:                                      $confname,'scantron','','',$custom);
 4457:                     if ($result eq 'ok') {
 4458:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
 4459:                         $changes{'scantronformat'} = 1;
 4460:                     } else {
 4461:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
 4462:                     }
 4463:                 } else {
 4464:                     $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);
 4465:                 }
 4466:             }
 4467:         } else {
 4468:             $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);
 4469:         }
 4470:         if ($error) {
 4471:             &Apache::lonnet::logthis($error);
 4472:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 4473:         }
 4474:     }
 4475:     if (ref($domconfig{'scantron'}) eq 'HASH') {
 4476:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
 4477:             if ($env{'form.scantronformat_del'}) {
 4478:                 $confhash{'scantron'}{'scantronformat'} = '';
 4479:                 $changes{'scantronformat'} = 1;
 4480:             }
 4481:         }
 4482:     }
 4483:     if (keys(%confhash) > 0) {
 4484:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
 4485:                                                  $dom);
 4486:         if ($putresult eq 'ok') {
 4487:             if (keys(%changes) > 0) {
 4488:                 if (ref($confhash{'scantron'}) eq 'HASH') {
 4489:                     $resulttext = &mt('Changes made:').'<ul>';
 4490:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
 4491:                         $resulttext .= '<li>'.&mt('[_1] scantron format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
 4492:                     } else {
 4493:                         $resulttext .= '<li>'.&mt('Custom scantron format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
 4494:                     }
 4495:                     $resulttext .= '</ul>';
 4496:                 } else {
 4497:                     $resulttext = &mt('Changes made to scantron format file.');
 4498:                 }
 4499:                 $resulttext .= '</ul>';
 4500:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
 4501:             } else {
 4502:                 $resulttext = &mt('No changes made to scantron format file');
 4503:             }
 4504:         } else {
 4505:             $resulttext = '<span class="LC_error">'.
 4506:                 &mt('An error occurred: [_1]',$putresult).'</span>';
 4507:         }
 4508:     } else {
 4509:         $resulttext = &mt('No changes made to scantron format file'); 
 4510:     }
 4511:     if ($errors) {
 4512:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 4513:                        $errors.'</ul>';
 4514:     }
 4515:     return $resulttext;
 4516: }
 4517: 
 4518: sub modify_coursecategories {
 4519:     my ($dom,%domconfig) = @_;
 4520:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
 4521:         $cathash);
 4522:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
 4523:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 4524:         $cathash = $domconfig{'coursecategories'}{'cats'};
 4525:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
 4526:             $changes{'togglecats'} = 1;
 4527:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 4528:         }
 4529:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
 4530:             $changes{'categorize'} = 1;
 4531:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 4532:         }
 4533:     } else {
 4534:         $changes{'togglecats'} = 1;
 4535:         $changes{'categorize'} = 1;
 4536:         $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
 4537:         $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
 4538:     }
 4539:     if (ref($cathash) eq 'HASH') {
 4540:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
 4541:             push (@deletecategory,'instcode::0');
 4542:         }
 4543:     }
 4544:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
 4545:     if (ref($cathash) eq 'HASH') {
 4546:         if (@deletecategory > 0) {
 4547:             #FIXME Need to remove category from all courses using a deleted category 
 4548:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
 4549:             foreach my $item (@deletecategory) {
 4550:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
 4551:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
 4552:                     $deletions{$item} = 1;
 4553:                     &recurse_cat_deletes($item,$cathash,\%deletions);
 4554:                 }
 4555:             }
 4556:         }
 4557:         foreach my $item (keys(%{$cathash})) {
 4558:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 4559:             if ($cathash->{$item} ne $env{'form.'.$item}) {
 4560:                 $reorderings{$item} = 1;
 4561:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
 4562:             }
 4563:             if ($env{'form.addcategory_name_'.$item} ne '') {
 4564:                 my $newcat = $env{'form.addcategory_name_'.$item};
 4565:                 my $newdepth = $depth+1;
 4566:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 4567:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
 4568:                 $adds{$newitem} = 1; 
 4569:             }
 4570:             if ($env{'form.subcat_'.$item} ne '') {
 4571:                 my $newcat = $env{'form.subcat_'.$item};
 4572:                 my $newdepth = $depth+1;
 4573:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
 4574:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
 4575:                 $adds{$newitem} = 1;
 4576:             }
 4577:         }
 4578:     }
 4579:     if ($env{'form.instcode'} eq '1') {
 4580:         if (ref($cathash) eq 'HASH') {
 4581:             my $newitem = 'instcode::0';
 4582:             if ($cathash->{$newitem} eq '') {  
 4583:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 4584:                 $adds{$newitem} = 1;
 4585:             }
 4586:         } else {
 4587:             my $newitem = 'instcode::0';
 4588:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
 4589:             $adds{$newitem} = 1;
 4590:         }
 4591:     }
 4592:     if ($env{'form.addcategory_name'} ne '') {
 4593:         my $newitem = &escape($env{'form.addcategory_name'}).'::0';
 4594:         $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
 4595:         $adds{$newitem} = 1;
 4596:     }
 4597:     my $putresult;
 4598:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 4599:         if (keys(%deletions) > 0) {
 4600:             foreach my $key (keys(%deletions)) {
 4601:                 if ($predelallitems{$key} ne '') {
 4602:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
 4603:                 }
 4604:             }
 4605:         }
 4606:         my (@chkcats,@chktrails,%chkallitems);
 4607:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
 4608:         if (ref($chkcats[0]) eq 'ARRAY') {
 4609:             my $depth = 0;
 4610:             my $chg = 0;
 4611:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
 4612:                 my $name = $chkcats[0][$i];
 4613:                 my $item;
 4614:                 if ($name eq '') {
 4615:                     $chg ++;
 4616:                 } else {
 4617:                     $item = &escape($name).'::0';
 4618:                     if ($chg) {
 4619:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
 4620:                     }
 4621:                     $depth ++; 
 4622:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
 4623:                     $depth --;
 4624:                 }
 4625:             }
 4626:         }
 4627:     }
 4628:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 4629:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
 4630:         if ($putresult eq 'ok') {
 4631:             my %title = (
 4632:                          togglecats  => 'Show/Hide a course in the catalog',
 4633:                          categorize     => 'Category assigned to course',
 4634:                         );
 4635:             my %level = (
 4636:                          dom => 'set from "Modify Course" (Domain)',
 4637:                          crs => 'set from "Parameters" (Course)',
 4638:                         );
 4639:             $resulttext = &mt('Changes made:').'<ul>';
 4640:             if ($changes{'togglecats'}) {
 4641:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
 4642:             }
 4643:             if ($changes{'categorize'}) {
 4644:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
 4645:             }
 4646:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
 4647:                 my $cathash;
 4648:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
 4649:                     $cathash = $domconfig{'coursecategories'}{'cats'};
 4650:                 } else {
 4651:                     $cathash = {};
 4652:                 } 
 4653:                 my (@cats,@trails,%allitems);
 4654:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
 4655:                 if (keys(%deletions) > 0) {
 4656:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
 4657:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
 4658:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
 4659:                     }
 4660:                     $resulttext .= '</ul></li>';
 4661:                 }
 4662:                 if (keys(%reorderings) > 0) {
 4663:                     my %sort_by_trail;
 4664:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
 4665:                     foreach my $key (keys(%reorderings)) {
 4666:                         if ($allitems{$key} ne '') {
 4667:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 4668:                         }
 4669:                     }
 4670:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 4671:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 4672:                     }
 4673:                     $resulttext .= '</ul></li>';
 4674:                 }
 4675:                 if (keys(%adds) > 0) {
 4676:                     my %sort_by_trail;
 4677:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
 4678:                     foreach my $key (keys(%adds)) {
 4679:                         if ($allitems{$key} ne '') {
 4680:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
 4681:                         }
 4682:                     }
 4683:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
 4684:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
 4685:                     }
 4686:                     $resulttext .= '</ul></li>';
 4687:                 }
 4688:             }
 4689:             $resulttext .= '</ul>';
 4690:         } else {
 4691:             $resulttext = '<span class="LC_error">'.
 4692:                           &mt('An error occurred: [_1]',$putresult).'</span>';
 4693:         }
 4694:     } else {
 4695:         $resulttext = &mt('No changes made to course categories');
 4696:     }
 4697:     return $resulttext;
 4698: }
 4699: 
 4700: sub modify_serverstatuses {
 4701:     my ($dom,%domconfig) = @_;
 4702:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
 4703:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
 4704:         %currserverstatus = %{$domconfig{'serverstatuses'}};
 4705:     }
 4706:     my @pages = &serverstatus_pages();
 4707:     foreach my $type (@pages) {
 4708:         $newserverstatus{$type}{'namedusers'} = '';
 4709:         $newserverstatus{$type}{'machines'} = '';
 4710:         if (defined($env{'form.'.$type.'_namedusers'})) {
 4711:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
 4712:             my @okusers;
 4713:             foreach my $user (@users) {
 4714:                 my ($uname,$udom) = split(/:/,$user);
 4715:                 if (($udom =~ /^$match_domain$/) &&   
 4716:                     (&Apache::lonnet::domain($udom)) &&
 4717:                     ($uname =~ /^$match_username$/)) {
 4718:                     if (!grep(/^\Q$user\E/,@okusers)) {
 4719:                         push(@okusers,$user);
 4720:                     }
 4721:                 }
 4722:             }
 4723:             if (@okusers > 0) {
 4724:                  @okusers = sort(@okusers);
 4725:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
 4726:             }
 4727:         }
 4728:         if (defined($env{'form.'.$type.'_machines'})) {
 4729:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
 4730:             my @okmachines;
 4731:             foreach my $ip (@machines) {
 4732:                 my @parts = split(/\./,$ip);
 4733:                 next if (@parts < 4);
 4734:                 my $badip = 0;
 4735:                 for (my $i=0; $i<4; $i++) {
 4736:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
 4737:                         $badip = 1;
 4738:                         last;
 4739:                     }
 4740:                 }
 4741:                 if (!$badip) {
 4742:                     push(@okmachines,$ip);     
 4743:                 }
 4744:             }
 4745:             @okmachines = sort(@okmachines);
 4746:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
 4747:         }
 4748:     }
 4749:     my %serverstatushash =  (
 4750:                                 serverstatuses => \%newserverstatus,
 4751:                             );
 4752:     my $putresult = &Apache::lonnet::put_dom('configuration',\%serverstatushash,
 4753:                                              $dom);
 4754:     my %changes;
 4755:     foreach my $type (@pages) {
 4756:         if (ref($currserverstatus{$type}) eq 'HASH') {
 4757:             my @currnamed = split(/,/,$currserverstatus{$type}{'namedusers'});
 4758:             my @newusers = split(/,/,$newserverstatus{$type}{'namedusers'});
 4759:             foreach my $item (@currnamed) {
 4760:                 if (!grep(/^\Q$item\E$/,@newusers)) {
 4761:                     $changes{$type}{'namedusers'} = 1;
 4762:                     last;
 4763:                 }
 4764:             }
 4765:             foreach my $item (@newusers) {
 4766:                 if (!grep(/^\Q$item\E$/,@currnamed)) {
 4767:                     $changes{$type}{'namedusers'} = 1;
 4768:                     last;
 4769:                 }
 4770:             }
 4771:             my @currmachines = split(/,/,$currserverstatus{$type}{'machines'});
 4772:             my @newmachines = split(/,/,$newserverstatus{$type}{'machines'});
 4773:             foreach my $item (@currmachines) {
 4774:                 if (!grep(/^\Q$item\E$/,@newmachines)) {
 4775:                     $changes{$type}{'machines'} = 1;
 4776:                     last;
 4777:                 }
 4778:             }
 4779:             foreach my $item (@newmachines) {
 4780:                 if (!grep(/^\Q$item\E$/,@currmachines)) {
 4781:                     $changes{$type}{'machines'} = 1;
 4782:                     last;
 4783:                 }
 4784:             }
 4785: 
 4786:         }
 4787:     }
 4788:     if (keys(%changes) > 0) {
 4789:         my $titles= &LONCAPA::loncgi::serverstatus_titles();
 4790:         my $putresult = &Apache::lonnet::put_dom('configuration',
 4791:                                                  \%serverstatushash,$dom);
 4792:         if ($putresult eq 'ok') {
 4793:             $resulttext .= &mt('Changes made:').'<ul>';
 4794:             foreach my $type (@pages) {
 4795:                 if (defined($changes{$type})) {
 4796:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
 4797:                     if (defined($changes{$type}{'namedusers'})) {
 4798:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
 4799:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
 4800:                         } else {
 4801:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
 4802:                         }
 4803:                     } elsif (defined($changes{$type}{'machines'})) {
 4804:                         if ($newserverstatus{$type}{'machines'} eq '') {
 4805:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
 4806:                         } else {
 4807:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
 4808:                         }
 4809: 
 4810:                     }
 4811:                     $resulttext .= '</ul></li>';
 4812:                 }
 4813:             }
 4814:             $resulttext .= '</ul>';
 4815:         } else {
 4816:             $resulttext = '<span class="LC_error">'.
 4817:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
 4818: 
 4819:         }
 4820:     } else {
 4821:         $resulttext = &mt('No changes made to access to server status pages');
 4822:     }
 4823:     return $resulttext;
 4824: }
 4825: 
 4826: sub recurse_check {
 4827:     my ($chkcats,$categories,$depth,$name) = @_;
 4828:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
 4829:         my $chg = 0;
 4830:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
 4831:             my $category = $chkcats->[$depth]{$name}[$j];
 4832:             my $item;
 4833:             if ($category eq '') {
 4834:                 $chg ++;
 4835:             } else {
 4836:                 my $deeper = $depth + 1;
 4837:                 $item = &escape($category).':'.&escape($name).':'.$depth;
 4838:                 if ($chg) {
 4839:                     $categories->{$item} -= $chg;
 4840:                 }
 4841:                 &recurse_check($chkcats,$categories,$deeper,$category);
 4842:                 $deeper --;
 4843:             }
 4844:         }
 4845:     }
 4846:     return;
 4847: }
 4848: 
 4849: sub recurse_cat_deletes {
 4850:     my ($item,$coursecategories,$deletions) = @_;
 4851:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
 4852:     my $subdepth = $depth + 1;
 4853:     if (ref($coursecategories) eq 'HASH') {
 4854:         foreach my $subitem (keys(%{$coursecategories})) {
 4855:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
 4856:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
 4857:                 delete($coursecategories->{$subitem});
 4858:                 $deletions->{$subitem} = 1;
 4859:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
 4860:             }  
 4861:         }
 4862:     }
 4863:     return;
 4864: }
 4865: 
 4866: 1;

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