File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.70: download - view: text, annotated - select for diffs
Mon Dec 1 15:03:40 2008 UTC (15 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- New subroutine in domainprefs: &logo_display_options() - used to provide "show" checkbox for login page images - 'img' and 'logo' ('Header' image and 'Main Logo' respectively).
- Domains can choose to include/exclude these two images in the log-in page.
  - Default is to include the images (and to display defaults, if not custom image was uploaded).

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

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