File:  [LON-CAPA] / loncom / interface / lonconfigsettings.pm
Revision 1.1: download - view: text, annotated - select for diffs
Tue Mar 31 16:59:01 2009 UTC (15 years, 2 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Re-use of configuration interface for domain settings.
- Move &print_header(), &print_footer(), &color_pick_js(), and &javascript_set_colnums from domainprefs.pm to lonconfigsettings.pm.
- Move code from domainprefs.pm to new routines in lonconfigsettings.pm
  - &make_changes()
  - &display_settings()
  - &display_choices()

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: lonconfigsettings.pm,v 1.1 2009/03/31 16:59:01 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::lonconfigsettings;
   32: 
   33: use strict;
   34: use Apache::lonnet;
   35: use Apache::loncommon();
   36: use Apache::lonhtmlcommon();
   37: use Apache::lonlocal;
   38: 
   39: sub print_header {
   40:     my ($r,$phase,$context) = @_;
   41:     my ($pagetitle,$brcrumtitle,$action);
   42:     if ($context eq 'domain') {
   43:         ($pagetitle, $brcrumtitle) = ('View/Modify Domain Settings','Domain Settings');
   44:         $action = '/adm/domainprefs';
   45:     } else {
   46:         ($pagetitle, $brcrumtitle) = ('Set Course Environment','Course Environment');
   47:         $action = '/adm/courseprefs';
   48:     }
   49:     my $alert = &mt('You must select at least one functionality type to display.');
   50:     my $js = '
   51: <script type="text/javascript">
   52: function changePage(formname,newphase) {
   53:     formname.phase.value = newphase;
   54:     numchecked = 0;
   55:     if (formname == document.pickactions) {
   56:         if (formname.actions.length > 0) {
   57:             for (var i = 0; i<formname.actions.length; i++) {
   58:                 if (formname.actions[i].checked) {
   59:                     numchecked ++;
   60:                 }
   61:             }
   62:         } else {
   63:             if (formname.actions.checked) {
   64:                 numchecked ++;
   65:             }
   66:         }
   67:         if (numchecked > 0) {
   68:             formname.submit();
   69:         } else {
   70:             alert("'.$alert.'");
   71:             return;
   72:         }
   73:     }
   74:     formname.submit();
   75: }'."\n";
   76:     if ($phase eq 'pickactions') {
   77:         $js .=
   78:             &Apache::lonhtmlcommon::set_form_elements({actions => 'checkbox',numcols => 'radio',})."\n".
   79:             &javascript_set_colnums();
   80:     } elsif ($phase eq 'display') {
   81:         $js .= &color_pick_js()."\n";
   82:     }
   83:     $js .= &Apache::loncommon::viewport_size_js().'
   84: </script>
   85: ';
   86:     my $additem;
   87:     if ($phase eq 'pickactions') {
   88:         my %loaditems = (
   89:                     'onload' => "javascript:getViewportDims(document.$phase.width,document.$phase.height);setDisplayColumns();setFormElements(document.pickactions);",
   90:                         );
   91:         $additem = {'add_entries' => \%loaditems,};
   92:     } else {
   93:         my %loaditems = (
   94:                     'onload' => "javascript:getViewportDims(document.$phase.width,document.$phase.height);",
   95:                         );
   96:         $additem = {'add_entries' => \%loaditems,};
   97:     }
   98:     $r->print(&Apache::loncommon::start_page($pagetitle,$js,$additem));
   99:     $r->print(&Apache::lonhtmlcommon::breadcrumbs($brcrumtitle));
  100:     $r->print('
  101: <form name="parmform" action="">
  102: <input type="hidden" name="pres_marker" />
  103: <input type="hidden" name="pres_type" />
  104: <input type="hidden" name="pres_value" />
  105: </form>
  106: ');
  107:     $r->print('<form method="post" name="'.$phase.'" action="'.$action.'"'.
  108:               ' enctype="multipart/form-data">');
  109:     return;
  110: }
  111: 
  112: sub print_footer {
  113:     my ($r,$phase,$newphase,$button_text,$actions) = @_;
  114:     $button_text = &mt($button_text);
  115:     $r->print('<input type="hidden" name="phase" value="" />'.
  116:               '<input type="hidden" name="width" value="'.
  117:               $env{'form.width'}.'" />'.
  118:               '<input type="hidden" name="height" value="'.
  119:               $env{'form.height'}.'" />');
  120:     if (($phase eq 'display') || ($phase eq 'process')) {
  121:         if (ref($actions) eq 'ARRAY') {
  122:             foreach my $item (@{$actions}) {
  123:                 $r->print('<input type="hidden" name="actions" value="'.$item.'" />')."\n";
  124:             }
  125:         }
  126:         $r->print('<input type="hidden" name="numcols" value="'.$env{'form.numcols'}.'" />');
  127:     }
  128:     my $dest='"javascript:changePage(document.'.$phase.','."'$newphase'".')"';
  129:     if ($phase eq 'process') {
  130:         $r->print('<p><a href='.$dest.'>'.$button_text.'</a></p>');
  131:     } else {
  132:         my $onclick;
  133:         if ($phase eq 'display') {
  134:             $onclick = '"javascript:changePage(document.'.$phase.','."'$newphase'".')"';
  135:         } else {
  136:             $onclick = '"javascript:changePage(document.'.$phase.','."'$newphase'".')"';
  137:         }
  138:         $r->print('<p><input type="button" name="store" value="'.
  139:                   $button_text.'" onclick='.$onclick.' /></p>');
  140:     }
  141:     if ($phase eq 'process') {
  142:         $r->print('</form>'.&Apache::loncommon::end_page());
  143:     }
  144:     return;
  145: }
  146: 
  147: sub make_changes {
  148:     my ($r,$dom,$phase,$context,$prefs_order,$prefs,$values,$confname,$roles) = @_;
  149:     my %brcrumtext = &get_crumb_text();
  150:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  151:     &Apache::lonhtmlcommon::add_breadcrumb
  152:       ({href=>"javascript:changePage(document.$phase,'display')",
  153:         text=>$brcrumtext{$context}},
  154:        {href=>"javascript:changePage(document.$phase,'$phase')",
  155:         text=>"Updated"});
  156:     &print_header($r,$phase,$context);
  157:     if ((ref($prefs_order) eq 'ARRAY') && (ref($prefs) eq 'HASH') && 
  158:         (ref($prefs) eq 'HASH')) {
  159:         foreach my $item (@{$prefs_order}) {
  160:             if (grep(/^\Q$item\E$/,@actions)) {
  161:                 $r->print('<h3>'.&mt($prefs->{$item}{'text'}).'</h3>');
  162:                 if ($context eq 'domain') {
  163:                     $r->print(&Apache::domainprefs::process_changes($r,$dom,$confname,$item,$roles,$values));
  164:                 } else {
  165:                     $r->print(&Apache::domainprefs::process_changes($r,$dom,$item,$values));
  166:                 }
  167:             }
  168:         }
  169:     }
  170:     $r->print('<p>');
  171:     &print_footer($r,$phase,'display','Back to configuration display',\@actions);
  172:     $r->print('</p>');
  173: }
  174: 
  175: sub display_settings {
  176:     my ($r,$dom,$phase,$context,$prefs_order,$prefs,$values,$confname) = @_;
  177:     my %brcrumtext = &get_crumb_text();
  178:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  179:     &Apache::lonhtmlcommon::add_breadcrumb
  180:         ({href=>"javascript:changePage(document.$phase,'display')",
  181:           text=>"Course Settings"});
  182:     &print_header($r,$phase,$context);
  183:     if ((ref($prefs_order) eq 'ARRAY') && (ref($prefs) eq 'HASH') && (ref($values) eq 'HASH')) { 
  184:         if (@actions > 0) {
  185:             my $rowsum = 0;
  186:             my (%output,%rowtotal,@items);
  187:             my $halfway = @actions/2;
  188:             foreach my $item (@{$prefs_order}) {
  189:                 if (grep(/^\Q$item\E$/,@actions)) {
  190:                     push(@items,$item);
  191:                     if ($context eq 'domain') {
  192:                         ($output{$item},$rowtotal{$item}) =
  193:                             &Apache::domainprefs::print_config_box($r,$dom,$confname,
  194:                                 $phase,$item,$prefs->{$item},$values->{$item});
  195:                     } else {
  196:                         ($output{$item},$rowtotal{$item}) =
  197:                             &Apache::courseprefs::print_config_box($r,$dom,$phase,
  198:                                 $item,$prefs->{$item},$values->{$item});
  199:                     }
  200:                     $rowsum += $rowtotal{$item};
  201:                 }
  202:             }
  203:             my $colend;
  204:             my $halfway = $rowsum/2;
  205:             my $aggregate = 0;
  206:             my $sumleft = 0;
  207:             my $sumright = 0;
  208:             my $crossover;
  209:             for (my $i=0; $i<@items; $i++) {
  210:                 $aggregate += $rowtotal{$items[$i]};
  211:                 if ($aggregate > $halfway) {
  212:                     $crossover = $i;
  213:                     last;
  214:                 }
  215:             }
  216:             for (my $i=0; $i<$crossover; $i++) {
  217:                 $sumleft += $rowtotal{$items[$i]};
  218:             }
  219:             for (my $i=$crossover+1; $i<@items; $i++) {
  220:                 $sumright += $rowtotal{$items[$i]};
  221:             }
  222:             if ((@items > 1) && ($env{'form.numcols'} == 2)) {
  223:                 my $sumdiff = $sumright - $sumleft;
  224:                 if ($sumdiff > 0) {
  225:                     $colend = $crossover + 1;
  226:                 } else {
  227:                     $colend = $crossover;
  228:                 }
  229:             } else {
  230:                 $colend = @items;
  231:             }
  232:             $r->print('<p><table class="LC_double_column"><tr><td class="LC_left_col">');            for (my $i=0; $i<$colend; $i++) {
  233:                 $r->print($output{$items[$i]});
  234:             }
  235:             $r->print('</td><td></td><td class="LC_right_col">');
  236:             if ($colend < @items) {
  237:                 for (my $i=$colend; $i<@items; $i++) {
  238:                     $r->print($output{$items[$i]});
  239:                 }
  240:             }
  241:             $r->print('</td></tr></table></p>');
  242:             $r->print(&print_footer($r,$phase,'process','Save',\@actions));
  243:         } else {
  244:             $r->print('<input type="hidden" name="phase" value="" />'.
  245:                   '<input type="hidden" name="numcols" value="'.
  246:                   $env{'form.numcols'}.'" />'."\n".
  247:                   '<span class="LC_error">'.&mt('No settings chosen').
  248:                   '</span>');
  249:         }
  250:         $r->print('</form>');
  251:     }
  252:     $r->print(&Apache::loncommon::end_page());
  253:     return;
  254: }
  255: 
  256: sub display_choices {
  257:     my ($r,$phase,$context,$prefs_order,$prefs) = @_;
  258:     if ($phase eq '') {
  259:         $phase = 'pickactions';
  260:     }
  261:     my %helphash;
  262:     &print_header($r,$phase,$context);
  263:     $r->print('<h3>'.&mt('Functionality to display/modify').'</h3>');
  264:     $r->print('<script type="text/javascript">'."\n".
  265:               &Apache::loncommon::check_uncheck_jscript()."\n".
  266:               '</script>'."\n".'<p><input type="button" value="'.&mt('check all').'" '.
  267:               'onclick="javascript:checkAll(document.pickactions.actions)"'.
  268:               ' />'.('&nbsp;'x2).
  269:               '<input type="button" value="'.&mt('uncheck all').'" '.
  270:               'onclick="javascript:uncheckAll(document.pickactions.actions)"'.
  271:               ' /></p><div class="LC_left_float">');
  272:     my ($numitems,$midpoint,$seconddiv,$count);
  273:     if (ref($prefs_order) eq 'ARRAY') {
  274:         $numitems = @{$prefs_order};
  275:     }
  276:     $midpoint = int($numitems/2);
  277:     if ($numitems%2) {
  278:         $midpoint ++;
  279:     }
  280:     $count = 0;
  281:     if ((ref($prefs_order) eq 'ARRAY') && (ref($prefs) eq 'HASH')) {
  282:         foreach my $item (@{$prefs_order}) {
  283:             $r->print('<h4>'.
  284:                       &Apache::loncommon::help_open_topic($prefs->{$item}->{'help'}).
  285:                       '<label><input type="checkbox" name="actions" value="'.$item.
  286:                       '" />&nbsp;'.&mt($prefs->{$item}->{'text'}).'</label></h4>');
  287:             $count ++;
  288:             if ((!$seconddiv) && ($count >= $midpoint)) {
  289:                 $r->print('</div>'."\n".'<div class="LC_left_float">'."\n");
  290:                 $seconddiv = 1;
  291:             }
  292:         }
  293:         $r->print('</div><div class="LC_clear_float_footer"></div><h3>'.
  294:                   &mt('Display options').'</h3>'."\n".
  295:                   '<p><span class="LC_nobreak">'.&mt('Display using: ')."\n".
  296:                   '<label><input type="radio" name="numcols" value="1" />'.
  297:                   &mt('one column').'</label>&nbsp;&nbsp;<label>'.
  298:                   '<input type="radio" name="numcols" value="2" />'.
  299:                   &mt('two columns').'</label></span></p>');
  300:     }
  301:     $r->print(&print_footer($r,$phase,'display','Go'));
  302:     $r->print('</form>');
  303:     $r->print(&Apache::loncommon::end_page());
  304:     return;
  305: }
  306: 
  307: sub javascript_set_colnums {
  308:     return <<END;
  309: function setDisplayColumns() {
  310:     if (document.pickactions.width.value > 1100) {
  311:         document.pickactions.numcols[1].checked = true;
  312:     } else {
  313:         document.pickactions.numcols[0].checked = true;
  314:     }
  315: }
  316: END
  317: }
  318: 
  319: sub color_pick_js {
  320:     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
  321:     my $output = <<"ENDCOL";
  322:     function pclose() {
  323:         parmwin=window.open("/adm/rat/empty.html","LONCAPAparms","height=350,width=350,scrollbars=no,menubar=no");
  324:         parmwin.close();
  325:     }
  326: 
  327:     $pjump_def
  328: 
  329:     function psub() {
  330:         pclose();
  331:         if (document.parmform.pres_marker.value!='') {
  332:             if (document.parmform.pres_type.value!='') {
  333:                 eval('document.display.'+
  334:                      document.parmform.pres_marker.value+
  335:                      '.value=document.parmform.pres_value.value;');
  336:             }
  337:         } else {
  338:             document.parmform.pres_value.value='';
  339:             document.parmform.pres_marker.value='';
  340:         }
  341:     }
  342: 
  343:     function get_id (span_id) {
  344:         if (document.getElementById) {
  345:             return document.getElementById(span_id);
  346:         }
  347:         if (document.all) {
  348:             return document.all[span_id];
  349:         }
  350:         return false;
  351:     }
  352: 
  353:     function colchg_span (span_id_str,new_color_item) {
  354:         var span_ref = get_id(span_id_str);
  355:         if (span_ref.style) { span_ref = span_ref.style; }
  356:         span_ref.background = new_color_item.value;
  357:         span_ref.backgroundColor = new_color_item.value;
  358:         span_ref.bgColor = new_color_item.value;
  359:     }
  360: 
  361: ENDCOL
  362:     return $output;
  363: }
  364: 
  365: sub get_crumb_text {
  366:     my %brcrumbtext = (
  367:                        domain => 'Domain Settings',
  368:                        course => 'Course Settings',
  369:                      );
  370:     return %brcrumbtext;
  371: }
  372: 
  373: 1;

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