File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.2: download - view: text, annotated - select for diffs
Thu Mar 1 17:54:13 2007 UTC (17 years, 4 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- missing cversion header

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.2 2007/03/01 17:54:13 albertel 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 LONCAPA();
   40: 
   41: sub handler {
   42:     my $r=shift;
   43:     if ($r->header_only) {
   44:         &Apache::loncommon::content_type($r,'text/html');
   45:         $r->send_http_header;
   46:         return OK;
   47:     }
   48: 
   49:     my $dom = $env{'request.role.domain'};
   50:     my $domdesc = $Apache::lonnet::domaindescription{$dom};
   51:     if (&Apache::lonnet::allowed('mau',$dom)) {
   52:         &Apache::loncommon::content_type($r,'text/html');
   53:         $r->send_http_header;
   54:     } else {
   55:         $env{'user.error.msg'}=
   56:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
   57:         return HTTP_NOT_ACCEPTABLE;
   58:     }
   59:     &Apache::lonhtmlcommon::clear_breadcrumbs();
   60:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
   61:                                             ['phase']);
   62:     my $phase = $env{'form.phase'};
   63:     if ($phase eq '' || $phase eq 'start') {
   64:         &Apache::lonhtmlcommon::add_breadcrumb
   65:         ({href=>"javascript:changePage(document.$phase,'start')",
   66:           text=>"Choose an action"});
   67:         &print_main_menu($r,$dom,$domdesc);
   68:     } else {
   69:         &Apache::lonhtmlcommon::add_breadcrumb
   70:         ({href=>"javascript:changePage(document.$phase,'start')",
   71:           text=>"Choose an action"});
   72:         if ($phase eq 'login') {
   73:             &Apache::lonhtmlcommon::add_breadcrumb
   74:               ({href=>"javascript:changePage(document.$phase,'$phase')",
   75:                 text=>"Configure Log-in page"});
   76:             &print_login($r,$phase,$dom)
   77:         } elsif ($phase eq 'configlogin') {
   78:             &Apache::lonhtmlcommon::add_breadcrumb
   79:               ({href=>"javascript:changePage(document.$phase,'login')",
   80:                 text=>"Configure Log-in page"});
   81:             &Apache::lonhtmlcommon::add_breadcrumb
   82:                 ({href=>"javascript:changePage(document.$phase,'$phase')",
   83:                   text=>"Result"});
   84:             &modify_login($r,$phase,$dom,$domdesc);
   85:         } elsif ($phase eq 'quotas') {
   86:             &Apache::lonhtmlcommon::add_breadcrumb
   87:               ({href=>"javascript:changePage(document.$phase,'$phase')",
   88:                 text=>"Default user quotas"});
   89:             &print_quotas($r,$phase,$dom);
   90:         } elsif ($phase eq 'configquotas') {
   91:             &Apache::lonhtmlcommon::add_breadcrumb
   92:               ({href=>"javascript:changePage(document.$phase,'quotas')",
   93:                 text=>"Default user quotas"});
   94:             &Apache::lonhtmlcommon::add_breadcrumb
   95:               ({href=>"javascript:changePage(document.$phase,'$phase')",
   96:                 text=>"Result"});
   97:             &modify_quotas($r,$phase,$dom,$domdesc);
   98:         } elsif ($phase eq 'autoenroll') {
   99:             &Apache::lonhtmlcommon::add_breadcrumb
  100:               ({href=>"javascript:changePage(document.$phase,'$phase')",
  101:                 text=>"Configure Auto-enrollment"});
  102:             &print_autoenroll($r,$phase,$dom);
  103:         } elsif ($phase eq 'configenroll') {
  104:             &Apache::lonhtmlcommon::add_breadcrumb
  105:               ({href=>"javascript:changePage(document.$phase,'autoenroll')",
  106:                 text=>"Configure Auto-enrollment"});
  107:             &Apache::lonhtmlcommon::add_breadcrumb
  108:               ({href=>"javascript:changePage(document.$phase,'$phase')",
  109:                 text=>"Result"});
  110:             &modify_autoenroll($r,$phase,$dom,$domdesc);
  111:         } elsif ($phase eq 'autoupdate') {
  112:             &Apache::lonhtmlcommon::add_breadcrumb
  113:               ({href=>"javascript:changePage(document.$phase,'$phase')",
  114:                 text=>"Configure Auto-update"});
  115:             &print_autoupdate($r,$phase,$dom);
  116:         } elsif ($phase eq 'configupdate') {
  117:             &Apache::lonhtmlcommon::add_breadcrumb
  118:               ({href=>"javascript:changePage(document.$phase,'quotas')",
  119:                 text=>"Change settings"});
  120:             &Apache::lonhtmlcommon::add_breadcrumb
  121:               ({href=>"javascript:changePage(document.$phase,'$phase')",
  122:                 text=>"Result"});
  123:             &modify_autoupdate($r,$phase,$dom,$domdesc);
  124:         }
  125:     }
  126:     &print_footer($r);
  127: }
  128: 
  129: sub print_main_menu {
  130:     my ($r,$dom,$domdesc) = @_;
  131:     my @menu =
  132:         (
  133:           { text => 'Log-in page options',
  134:             help => 'Domain_Log-in_Page',
  135:             phase => 'login',
  136:             },
  137:           { text => 'Default quotas for user portfolios',
  138:             help => 'Default_User_Quota',
  139:             phase => 'quotas',
  140:             },
  141:           { text => 'Auto-enrollment settings',
  142:             help => 'Domain_Auto_Enrollment',
  143:             phase => 'autoenroll',
  144:             },
  145:           { text => 'Auto-update settings',
  146:             help => 'Domain_Auto_Update',
  147:             phase => 'autoupdate',
  148:           },
  149:         );
  150:     my $menu_html = '';
  151:     foreach my $menu_item (@menu) {
  152:         $menu_html.='<p>';
  153:         $menu_html.='<font size="+1">';
  154:         if (exists($menu_item->{'url'})) {
  155:             $menu_html.=qq{<a href="$menu_item->{'url'}">};
  156:         } else {
  157:             $menu_html.=
  158:                 qq{<a href="/adm/domainprefs?phase=$menu_item->{'phase'}">};
  159:         }
  160:         $menu_html.= &mt($menu_item->{'text'}).'</a></font>';
  161:         if (exists($menu_item->{'help'})) {
  162:             $menu_html.=
  163:                 &Apache::loncommon::help_open_topic($menu_item->{'help'});
  164:         }
  165:         $menu_html.='</p>'.$/;
  166:     }
  167:     &print_header($r);
  168:     $r->print($menu_html);
  169:     return;
  170: }
  171: 
  172: sub print_header {
  173:     my ($r,$javascript_validations) = @_;
  174:     my $phase = "start";
  175:     if ( exists($env{'form.phase'}) ) {
  176:         $phase = $env{'form.phase'};
  177:     }
  178:     my $js = qq|
  179: <script type="text/javascript">
  180: function changePage(formname,newphase) {
  181:     formname.phase.value = newphase;
  182:     if (newphase == 'processparms') {
  183:         return;
  184:     }
  185:     formname.submit();
  186: }
  187: </script>
  188: |;
  189:     $r->print(&Apache::loncommon::start_page('View/Modify Domain Settings',
  190:                                              $js));
  191:     my $bread_text = "Domain Settings";
  192:     $r->print(&Apache::lonhtmlcommon::breadcrumbs($bread_text));
  193:     return;
  194: }
  195: 
  196: sub print_footer {
  197:     my ($r) = @_;
  198:     $r->print('<br />'.&Apache::loncommon::end_page());
  199:     return;
  200: }
  201: 
  202: sub print_login { 
  203:     my ($r,$phase,$dom) = @_;
  204:     my %domconfig = &Apache::lonnet::get_dom('configuration',
  205:                                              ['login'],$dom);
  206:     my $catalogon = ' checked="checked" ';
  207:     my $catalogoff;
  208:     my $adminmailon = ' ';
  209:     my $adminmailoff = ' checked="checked" ';
  210:     if (ref($domconfig{'login'}) eq 'HASH') {
  211:         if ($domconfig{'login'}{'coursecatalog'} eq '0') {
  212:             $catalogoff = $catalogon;
  213:             $catalogon = ' ';
  214:         }
  215:         if ($domconfig{'login'}{'adminmail'} eq '1') {
  216:             $adminmailon = $adminmailoff;
  217:             $adminmailoff = ' ';
  218:         }
  219:     }
  220:     &print_header($r);
  221:     my $datatable=&Apache::loncommon::start_data_table().
  222:                   &Apache::loncommon::start_data_table_header_row().
  223:                   '<th>'.&mt('Item').'</th><th>'.&mt('Selection').'</th>'.
  224:                   &Apache::loncommon::end_data_table_header_row().
  225:                   &Apache::loncommon::start_data_table_row().
  226:                   '<td>'.&mt('Display Course Catalog link?').'</td>'.
  227:                   '<td><nobr><input type="radio" name="coursecatalog"'.
  228:                   $catalogon.' value="1" />'.&mt('Yes').'&nbsp;'.
  229:                   '<input type="radio" name="coursecatalog"'.
  230:                   $catalogoff.'value="0" />'.&mt('No').'</nobr></td>'.
  231:                   &Apache::loncommon::end_data_table_row().
  232:                   &Apache::loncommon::start_data_table_row().
  233:                   '<td>'.&mt("Display Administrator's E-mail Address?").'</td>'.
  234:                   '<td><nobr><input type="radio" name="adminmail"'.                          $adminmailon.' value="1" />'.&mt('Yes').'&nbsp;'.
  235:                   '<input type="radio" name="adminmail"'.
  236:                   $adminmailoff.'value="0" />'.&mt('No').'</nobr></td>'.
  237:                   &Apache::loncommon::end_data_table_row().
  238:                   &Apache::loncommon::start_data_table();
  239:     &print_form($r,$datatable,$phase,'configlogin','Change'); 
  240: }
  241: 
  242: sub modify_login {
  243:     my ($r,$phase,$dom,$domdesc) = @_;
  244:     my ($resulttext,%changes);
  245:     my %domconfig = &Apache::lonnet::get_dom('configuration',
  246:                                              ['login'],$dom);
  247:     my %title = ( coursecatalog => 'Display course catalog',
  248:                   adminmail => 'Display administrator E-mail address');
  249:     my @offon = ('off','on'); 
  250:     my %loginhash =  (
  251:                        login => { coursecatalog => $env{'form.coursecatalog'},
  252:                                   adminmail => $env{'form.adminmail'},
  253:                                 }
  254:                      );
  255:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
  256:                                              $dom);
  257:     if ($putresult eq 'ok') {
  258:         if (($domconfig{'login'}{'coursecatalog'} eq '0') && 
  259:             ($env{'form.coursecatalog'} eq '1')) {
  260:             $changes{'coursecatalog'} = 1;
  261:         } elsif (($domconfig{'login'}{'coursecatalog'} eq '' || 
  262:                  $domconfig{'login'}{'coursecatalog'} eq '1') && 
  263:                  ($env{'form.coursecatalog'} eq '0')) {
  264:             $changes{'coursecatalog'} = 1;
  265:         }
  266:         if (($domconfig{'login'}{'adminmail'} eq '1') && 
  267:                 ($env{'form.adminmail'} eq '0')) {
  268:             $changes{'adminmail'} = 1;
  269:         } elsif (($domconfig{'login'}{'adminmail'} eq '' ||
  270:                  $domconfig{'login'}{'adminmail'} eq '0') &&
  271:                  ($env{'form.adminmail'} eq '1')) {
  272:             $changes{'adminmail'} = 1;
  273:         }
  274:         if (keys(%changes) > 0) {
  275:             $resulttext = &mt('Changes made:').'<ul>';
  276:             foreach my $item (sort(keys(%changes))) {
  277:                 $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
  278:             }
  279:             $resulttext .= '</ul>';
  280:         } else {
  281:             $resulttext = &mt('No changes made to log-in page settings');
  282:         }
  283:     } else {
  284:         $resulttext = &mt('An error occurred: [_1]',$putresult);
  285:     }
  286:     &print_header($r);
  287:     &print_form($r,$resulttext,$phase,'start','Back to actions menu');
  288: }
  289: 
  290: sub print_quotas {
  291:     my ($r,$phase,$dom) = @_;
  292:     my %currquota;
  293:     my %domconfig = &Apache::lonnet::get_dom('configuration',
  294:                                              ['quotas'],$dom);
  295:     if (ref($domconfig{'quotas'}) eq 'HASH') {
  296:         foreach my $key (keys(%{$domconfig{'quotas'}})) {
  297:             $currquota{$key} = $domconfig{'quotas'}{$key};
  298:         }
  299:     }
  300:     my $datatable=&Apache::loncommon::start_data_table().
  301:                   &Apache::loncommon::start_data_table_header_row().
  302:                   '<th>'.&mt('User type').'</th><th>'.&mt('Default quota').'</th>'.
  303:                   &Apache::loncommon::end_data_table_header_row();
  304:     my ($usertypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($dom);
  305:     my $othertitle = "All users";
  306:     my @types;
  307:     if (ref($order) eq 'ARRAY') {
  308:         @types = @{$order};
  309:     }
  310:     if (@types == 0) {
  311:         if (ref($usertypes) eq 'HASH') {
  312:             @types = sort(keys(%{$usertypes}));
  313:         }
  314:     }
  315:     if (@types > 0) {
  316:         foreach my $type (@types) {
  317:             if (defined($usertypes->{$type})) {
  318:                 $datatable .= &Apache::loncommon::start_data_table_row().
  319:                               '<td>'.$usertypes->{$type}.'</td><td><nobr>'.
  320:                               '<input type="text" name="quota_'.$type.
  321:                               '" value="'.$currquota{$type}.
  322:                               '" size="5" /> Mb</nobr></td>'; 
  323:             }
  324:         }
  325:         $othertitle = "Other users";
  326:     }
  327:     my $defaultquota = '20';
  328:     if (defined($currquota{'default'})) {
  329:         $defaultquota = $currquota{'default'};
  330:     }
  331:     $datatable .= &Apache::loncommon::start_data_table_row().
  332:                   '<td>'.&mt($othertitle).'</td><td><nobr>'.
  333:                   '<input type="text" name="default" value="'.
  334:                   $defaultquota.'" size="5" /> Mb</nobr></td>'.
  335:                   &Apache::loncommon::end_data_table_row().
  336:                   &Apache::loncommon::end_data_table();
  337:     &print_header($r);
  338:     &print_form($r,$datatable,$phase,'configquotas','Change');
  339: }
  340: 
  341: sub modify_quotas {
  342:     my ($r,$phase,$dom,$domdesc) = @_;
  343:     my ($resulttext,%changes);
  344:     my %domconfig = &Apache::lonnet::get_dom('configuration',
  345:                                              ['quotas'],$dom);
  346:     my ($usertypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($dom);
  347:     my %formhash;
  348:     foreach my $key (keys(%env)) {
  349:         if ($key =~ /^form\.quota_(.+)$/) {
  350:             $formhash{$1} = $env{$key};
  351:         }
  352:     }
  353:     if (ref($domconfig{'quotas'}) eq 'HASH') {
  354:         foreach my $key (keys(%{$domconfig{'quotas'}})) {
  355:             if (exists($formhash{$key})) {
  356:                 if ($formhash{$key} ne $domconfig{'quotas'}{$key}) {
  357:                     $changes{$key} = 1;
  358:                 }
  359:             } else {
  360:                 $formhash{$key} = $domconfig{'quotas'}{$key};
  361:             }
  362:         }
  363:     }
  364:     foreach my $key (keys(%formhash)) {
  365:         if ($formhash{$key} ne '') {
  366:             if (!exists($domconfig{$key})) {
  367:                 $changes{$key} = 1;
  368:             }
  369:         }
  370:     }
  371:     my %quotahash = (
  372:                       quotas => {%formhash}, 
  373:                     );
  374:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
  375:                                              $dom);
  376:     if ($putresult eq 'ok') {
  377:         if (keys(%changes) > 0) {
  378:             $resulttext = &mt('Changes made:').'<ul>';
  379:             foreach my $item (sort(keys(%changes))) {
  380:                 $resulttext .= '<li>'.&mt('[_1] set to [_2] Mb',$usertypes->{$item},$formhash{$item}).'</li>';
  381:             }
  382:             $resulttext .= '</ul>';
  383:         } else {
  384:             $resulttext = &mt('No changes made to default quotas');
  385:         }
  386:     } else {
  387:         $resulttext = &mt('An error occurred: [_1]',$putresult);
  388:     }
  389:     &print_header($r);
  390:     &print_form($r,$resulttext,$phase,'start','Back to actions menu');
  391: }
  392: 
  393: sub print_autoenroll {
  394:     my ($r,$phase,$dom) = @_;
  395:     my %currautoenroll;
  396:     my %domconfig = &Apache::lonnet::get_dom('configuration',
  397:                                              ['autoenroll'],$dom);
  398:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
  399:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
  400:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
  401:         }
  402:     }
  403:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
  404:     my ($runon,$runoff);
  405:     if (exists($currautoenroll{'run'})) {
  406:         if ($currautoenroll{'run'} eq '0') {
  407:             $runoff = ' checked="checked" ';
  408:             $runon = ' ';
  409:         } else {
  410:             $runon = ' checked="checked" ';
  411:             $runoff = ' ';
  412:         }
  413:     } else {
  414:         if ($autorun) {
  415:             $runon = ' checked="checked" ';
  416:             $runoff = ' ';
  417:         } else {
  418:             $runoff = ' checked="checked" ';
  419:             $runon = ' ';
  420:         }
  421:     }
  422:     my $defdom = $dom;
  423:     if (exists($currautoenroll{'sender_domain'})) {
  424:         $defdom = $currautoenroll{'sender_domain'};
  425:     }
  426:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
  427:     my $datatable=&Apache::loncommon::start_data_table().
  428:                   &Apache::loncommon::start_data_table_header_row().
  429:                   '<th>'.&mt('Configuration setting').'</th><th>'.&mt('Value(s)').'</th>'.
  430:                   &Apache::loncommon::end_data_table_header_row().
  431:                   &Apache::loncommon::start_data_table_row().
  432:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.                  '<td><nobr><input type="radio" name="autoenroll_run"'.
  433:                   $runon.' value="1" />'.&mt('Yes').'&nbsp;'.
  434:                   '<input type="radio" name="autoenroll_run"'.
  435:                   $runoff.'value="0" />'.&mt('No').'</nobr></td>'.
  436:                   &Apache::loncommon::end_data_table_row().
  437:                   &Apache::loncommon::start_data_table_row().
  438:                   '<td>'.&mt('Notification messages - sender').'</td><td><nobr>'.
  439:                   &mt('username').':&nbsp;<input type="text" name="sender_uname" value="'.
  440:                   $currautoenroll{'sender_uname'}.'" size="10" />&nbsp;&nbsp;'.&mt('domain').':&nbsp;'.$domform.'</nobr></td>'.
  441:                   &Apache::loncommon::end_data_table_row().
  442:                   &Apache::loncommon::end_data_table();
  443:     &print_header($r);
  444:     &print_form($r,$datatable,$phase,'configenroll','Change');
  445: }
  446: 
  447: sub modify_autoenroll { 
  448:     my ($r,$phase,$dom,$domdesc) = @_;
  449:     my ($resulttext,%changes);
  450:     my %currautoenroll;
  451:     my %domconfig = &Apache::lonnet::get_dom('configuration',
  452:                                              ['autoenroll'],$dom);
  453:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
  454:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
  455:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
  456:         }
  457:     }
  458:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
  459:     my %title = ( run => 'Auto-enrollment active',
  460:                   sender => 'Sender for notification messages');
  461:     my @offon = ('off','on');
  462:     my %autoenrollhash =  (
  463:                        autoenroll => { run => $env{'form.autoenroll_run'},
  464:                                        sender_uname => $env{'form.sender_uname'},
  465:                                        sender_domain => $env{'form.sender_domain'},
  466: 
  467:                                 }
  468:                      );
  469:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
  470:                                              $dom);
  471:     if ($putresult eq 'ok') {
  472:         if (exists($currautoenroll{'run'})) {
  473:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
  474:                  $changes{'run'} = 1;
  475:              }
  476:         } elsif ($autorun) {
  477:             if ($env{'form.autoenroll_run'} ne '1') {
  478:                 $changes{'run'} = 1;
  479:             }
  480:         }
  481:         if (exists($currautoenroll{sender_uname})) {
  482:             if ($currautoenroll{'sender_uname'} ne $env{'form.sender_uname'}) {
  483:                 $changes{'sender'} = 1;
  484:             }
  485:         } else {
  486:             $changes{'sender'} = 1;
  487:         }
  488:         if (exists($currautoenroll{sender_domain})) {
  489:             if ($currautoenroll{'sender_domain'} ne $env{'form.sender_domain'}) {
  490:                 $changes{'sender'} = 1;
  491:             }
  492:         } else {
  493:             $changes{'sender'} = 1;
  494:         }
  495:         if (keys(%changes) > 0) {
  496:             $resulttext = &mt('Changes made:').'<ul>';
  497:             if ($changes{'run'}) { 
  498:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
  499:             }
  500:             if ($changes{'sender'}) {
  501:                 $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$env{'form.sender_uname'}.':'.$env{'form.sender_domain'}).'</li>';
  502:             }
  503:             $resulttext .= '</ul>';
  504:         } else {
  505:             $resulttext = &mt('No changes made to auto-enrollment settings');
  506:         }
  507:     } else {
  508:         $resulttext = &mt('An error occurred: [_1]',$putresult);
  509:     }
  510:     &print_header($r);
  511:     &print_form($r,$resulttext,$phase,'start','Back to actions menu');
  512: }
  513: 
  514: sub print_autoupdate {
  515:     my ($r,$phase,$dom) = @_;
  516:     my (%currautoupdate,$datatable);
  517:     my %domconfig = &Apache::lonnet::get_dom('configuration',
  518:                                              ['autoupdate'],$dom);
  519:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
  520:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
  521:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
  522:         }
  523:     }
  524:     my $updateon = ' ';
  525:     my $updateoff = ' checked="checked" ';
  526:     if ($currautoupdate{'run'} eq '1') {
  527:         $updateon = $updateoff;
  528:         $updateoff = ' ';
  529:     }
  530:     my $classlistson = ' ';
  531:     my $classlistsoff = ' checked="checked" ';
  532:     if ($currautoupdate{'classlists'} eq '1') {
  533:         $classlistson = $classlistsoff;
  534:         $classlistsoff = ' ';
  535:     }
  536:     my %title = (
  537:                    run => 'Auto-update enabled?',
  538:                    classlists => 'Update information in classlists?',
  539:                 );
  540:     $datatable = &Apache::loncommon::start_data_table().
  541:                   &Apache::loncommon::start_data_table_header_row().
  542:                   '<th>'.&mt('Setting').'</th><th>'.&mt('Value').'</th>'.
  543:                   &Apache::loncommon::end_data_table_header_row().
  544:                   &Apache::loncommon::start_data_table_row().
  545:                   '<td>'.&mt($title{'run'}).'</td>'.
  546:                   '<td><nobr><input type="radio" name="autoupdate_run"'.
  547:                   $updateon.' value="1" />'.&mt('Yes').'&nbsp;'.
  548:                   '<input type="radio" name="autoupdate_run"'.
  549:                   $updateoff.'value="0" />'.&mt('No').'</nobr></td>'.
  550:                   &Apache::loncommon::end_data_table_row().
  551:                   &Apache::loncommon::start_data_table_row().
  552:                   '<td>'.&mt($title{'classlists'}).'</td>'.
  553:                   '<td><nobr><input type="radio" name="classlists"'.
  554:                   $classlistson.' value="1" />'.&mt('Yes').'&nbsp;'.
  555:                   '<input type="radio" name="classlists"'.
  556:                   $classlistsoff.'value="0" />'.&mt('No').'</nobr></td>'.
  557:                   &Apache::loncommon::end_data_table_row().
  558:                   &Apache::loncommon::end_data_table().'<br /><br />';
  559:     my ($usertypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($dom);
  560:     my @types;
  561:     if (ref($order) eq 'ARRAY') {
  562:         @types = @{$order};
  563:     }
  564:     if (@types == 0) {
  565:         if (ref($usertypes) eq 'HASH') {
  566:             @types = sort(keys(%{$usertypes}));
  567:         }
  568:     }
  569:     my $othertitle = &mt('All users');
  570:     if (keys(%{$usertypes}) > 0) {
  571:         $othertitle = &mt('Other users');
  572:     }
  573:     my @fields = ('lastname','firstname','middlename','gen','email','id');
  574:     my $numinrow = 4;
  575:     my %fieldtitles = &Apache::lonlocal::texthash ( 
  576:                         id => 'Student/Employee ID',
  577:                         email => 'E-mail address',
  578:                         lastname => 'Last Name',
  579:                         firstname => 'First Name',
  580:                         middlename => 'Middle Name',
  581:                         gen => 'Generation',
  582:                       );
  583:     $datatable .= &Apache::loncommon::start_data_table().
  584:                   &Apache::loncommon::start_data_table_header_row().
  585:                   '<th>'.&mt('User Population').'</th><th>'.&mt('Updateable user data').'</th>'.
  586:                   &Apache::loncommon::end_data_table_header_row();
  587:     if (@types > 0) {
  588:         foreach my $type (@types) {
  589:             if (defined($usertypes->{$type})) {
  590:                 $datatable .= &Apache::loncommon::start_data_table_row().
  591:                               '<td>'.$usertypes->{$type}.'</td><td><table>';
  592:                 for (my $i=0; $i<@fields; $i++) {
  593:                     my $rem = $i%($numinrow);
  594:                     if ($rem == 0) {
  595:                         if ($i > 0) {
  596:                             $datatable .= '</tr>';
  597:                         }
  598:                         $datatable .= '<tr>';
  599:                     }
  600:                     my $check = ' ';
  601:                     if (ref($currautoupdate{'fields'}) eq 'HASH') {
  602:                         if (ref($currautoupdate{'fields'}{$type}) eq 'ARRAY') {  
  603:                             if (grep(/^\Q$fields[$i]\E$/,@{$currautoupdate{'fields'}{$type}})) {
  604:                                 $check = ' checked="checked" ';
  605:                             }
  606:                         }
  607:                     }
  608:                     if ($i == @fields-1) {
  609:                         my $colsleft = $numinrow - $rem;
  610:                         if ($colsleft > 1) {
  611:                             $datatable .= '<td colspan="'.$colsleft.'">';
  612:                         } else {
  613:                             $datatable .= '<td>'; 
  614:                         }
  615:                     } else {
  616:                         $datatable .= '<td>';
  617:                     }
  618:                     $datatable .= '<nobr><input type="checkbox" name="updateable_'.
  619:                                   $type.'_'.$fields[$i].'" '.
  620:                                   '" value="1"'.$check.'/>'.
  621:                                   $fieldtitles{$fields[$i]}.'</nobr></td>';
  622:                 }
  623:                 $datatable .= '</tr></table></td>'.&Apache::loncommon::end_data_table_row();
  624:             }
  625:         }
  626:     }
  627:     $datatable .= &Apache::loncommon::start_data_table_row().
  628:                   '<td>'.&mt($othertitle).'</td><td><table>';
  629:     for (my $i=0; $i<@fields; $i++) {
  630:         my $rem = $i%($numinrow);
  631:         if ($rem == 0) {
  632:             if ($i > 0) {
  633:                 $datatable .= '</tr>';
  634:             }
  635:             $datatable .= '<tr>';
  636:         }
  637:         my $check = ' ';
  638:         if (ref($currautoupdate{'fields'}) eq 'HASH') {
  639:             if (ref($currautoupdate{'fields'}{'default'}) eq 'ARRAY') {
  640:                 if (grep(/^\Q$fields[$i]\E$/,@{$currautoupdate{'fields'}{'default'}})) {
  641:                     $check = ' checked="checked" ';
  642:                 }
  643:             }
  644:         }
  645:         if ($i == @fields-1) {
  646:             my $colsleft = $numinrow - $rem;
  647:             if ($colsleft > 1) {
  648:                 $datatable .= '<td colspan="'.$colsleft.'">';
  649:             } else {
  650:                 $datatable .= '<td>';
  651:             }
  652:         } else {
  653:             $datatable .= '<td>';
  654:         }
  655:         $datatable .=  '<nobr><input type="checkbox" name="updateable_default_'.
  656:                        $fields[$i].'" value="1"'.$check.'/>'.
  657:                        $fieldtitles{$fields[$i]}.'</nobr></td>';
  658:     }
  659:     $datatable .= '</tr></table></td>'.
  660:                   &Apache::loncommon::end_data_table_row().
  661:                   &Apache::loncommon::end_data_table();
  662:     &print_header($r);
  663:     &print_form($r,$datatable,$phase,'configupdate','Change');
  664: }
  665: 
  666: sub modify_autoupdate {
  667:     my ($r,$phase,$dom,$domdesc) = @_;
  668:     my ($resulttext,%currautoupdate,%fields,%changes);
  669:     my %domconfig = &Apache::lonnet::get_dom('configuration',
  670:                                              ['autoupdate'],$dom);
  671:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
  672:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
  673:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
  674:         }
  675:     }
  676:     my @offon = ('off','on');
  677:     my %title = &Apache::lonlocal::texthash (
  678:                    run => 'Auto-update:',
  679:                    classlists => 'Updates to user information in classlists?'
  680:                 );
  681:     my ($usertypes,$order) = &Apache::lonnet::retrieve_inst_usertypes($dom);
  682:     my %fieldtitles = &Apache::lonlocal::texthash (
  683:                         id => 'Student/Employee ID',
  684:                         email => 'E-mail address',
  685:                         lastname => 'Last Name',
  686:                         firstname => 'First Name',
  687:                         middlename => 'Middle Name',
  688:                         gen => 'Generation',
  689:                       );
  690:     my $othertitle = &mt('All users');
  691:     if (keys(%{$usertypes}) >  0) {
  692:         $othertitle = "Other users";
  693:     }
  694:     foreach my $key (keys(%env)) {
  695:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
  696:             push(@{$fields{$1}},$2);
  697:         }
  698:     }
  699:     my %updatehash = (
  700:                       autoupdate => { run => $env{'form.autoupdate_run'},
  701:                                       classlists => $env{'form.classlists'},
  702:                                       fields => {%fields},
  703:                                     }
  704:                      );
  705:     foreach my $key (keys(%currautoupdate)) {
  706:         if (($key eq 'run') || ($key eq 'classlists')) {
  707:             if (exists($updatehash{autoupdate}{$key})) {
  708:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
  709:                     $changes{$key} = 1;
  710:                 }
  711:             }
  712:         } elsif ($key eq 'fields') {
  713:             if (ref($currautoupdate{$key}) eq 'HASH') {
  714:                 foreach my $item (keys(%{$currautoupdate{$key}})) {
  715:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
  716:                         my $change = 0;
  717:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
  718:                             if (!exists($fields{$item})) {
  719:                                 $change = 1;
  720:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
  721:                                 if (!grep/^\Q$type\E$/,@{$fields{$item}}) {
  722:                                     $change = 1;
  723:                                 }
  724:                             }
  725:                         }
  726:                         if ($change) {
  727:                             push(@{$changes{$key}},$item);
  728:                         }
  729:                     }
  730:                 }
  731:             }
  732:         }
  733:     }
  734:     foreach my $key (keys(%fields)) {
  735:         if (ref($currautoupdate{'fields'}) eq 'HASH') {
  736:             if (!exists($currautoupdate{'fields'}{$key})) {
  737:                 push(@{$changes{'fields'}},$key);
  738:             }
  739:         } else {
  740:             push(@{$changes{'fields'}},$key);
  741:         }
  742:     }
  743:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
  744:                                              $dom);
  745:     if ($putresult eq 'ok') {
  746:         if (keys(%changes) > 0) {
  747:             $resulttext = &mt('Changes made:').'<ul>';
  748:             foreach my $key (sort(keys(%changes))) {
  749:                 if (ref($changes{$key}) eq 'ARRAY') {
  750:                     foreach my $item (@{$changes{$key}}) {
  751:                         my @newvalues;
  752:                         foreach my $type (@{$fields{$item}}) {
  753:                             push(@newvalues,$fieldtitles{$type});
  754:                         }
  755:                         my $newvaluestr = join(', ',@newvalues);
  756:                         if ($item eq 'default') {
  757:                             $resulttext .= '<li>'.&mt("Updates for $othertitle set to: [_1]",$newvaluestr).'</li>';
  758:                         } else {
  759:                             $resulttext .= '<li>'.&mt("Updates for [_1] set to: [_2]",$usertypes->{$item},$newvaluestr).'</li>';
  760:                         }
  761:                     }
  762:                 } else {
  763:                     my $newvalue;
  764:                     if ($key eq 'run') {
  765:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
  766:                     } else {
  767:                         $newvalue = $offon[$env{'form.'.$key}];
  768:                     } 
  769:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
  770:                 }
  771:             }
  772:             $resulttext .= '</ul>';
  773:         } else {
  774:             $resulttext = &mt('No changes made to default quotas');
  775:         }
  776:     } else {
  777:         $resulttext = &mt('An error occurred: [_1]',$putresult);
  778:     }
  779:     &print_header($r);
  780:     &print_form($r,$resulttext,$phase,'start','Back to actions menu');
  781: }
  782: 
  783: sub print_form {
  784:     my ($r,$datatable,$phase,$newphase,$button_text) = @_;
  785:     my $button_text = &mt($button_text);
  786:     $r->print(<<ENDDOCUMENT);
  787: <form action="/adm/domainprefs" method="post" name="$phase">
  788: <br />
  789: $datatable
  790: <br />
  791: <input type="hidden" name="phase" value="" />
  792: <input type="button" name="store" value="$button_text" onclick="javascript:changePage(document.$phase,'$newphase')" />
  793: </form>
  794: ENDDOCUMENT
  795:     return;
  796: }
  797: 
  798: 1;

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