File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.51: download - view: text, annotated - select for diffs
Thu May 15 01:56:44 2008 UTC (16 years ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Middle background color setting is mainbg for login page, tabbg for student, coord, author and admin roles.

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

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