File:  [LON-CAPA] / loncom / interface / lonconfigsettings.pm
Revision 1.10: download - view: text, annotated - select for diffs
Fri Jan 8 00:52:06 2010 UTC (14 years, 5 months ago) by faziophi
Branches: MAIN
CVS tags: HEAD
Additional style for elements within an accordion, and adds jQuery
javascript to support advanced elements.  If any <tr> elements are found
inside an accordion container with the class "advanced", they are hidden
by default and a "Show advanced options" checkbox is created.  Toggling
this causes these additional options to fade in/out as desired.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: lonconfigsettings.pm,v 1.10 2010/01/08 00:52:06 faziophi 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,$jscript) = @_;
   41:     my ($pagetitle,$brcrumtitle,$action,$call_category_check);
   42:     if ($context eq 'domain') {
   43:         ($pagetitle, $brcrumtitle) = ('View/Modify Domain Settings','Domain Settings');
   44:         $action = '/adm/domainprefs';
   45:         if ($phase eq 'display') {
   46:             my @actions = &Apache::loncommon::get_env_multiple('form.actions');
   47:             if (grep(/^coursecategories$/,@actions)) {
   48:                 $call_category_check = qq|
   49:     if (formname == document.display) {
   50:         if (!categoryCheck(formname)) {
   51:             return;
   52:         }
   53:     }
   54: |;
   55:             }
   56:         }
   57:     } else {
   58:         if (&Apache::loncommon::course_type() eq 'Community') {
   59:             ($pagetitle,$brcrumtitle) = ('Community Configuration','Community Configuration');
   60:         } else {
   61:             ($pagetitle,$brcrumtitle) = ('Course Configuration','Course Configuration');
   62:         }
   63:         $action = '/adm/courseprefs';
   64:     }
   65:     my $alert = &mt('You must select at least one functionality type to display.');
   66:     my $js = '
   67: <script type="text/javascript">
   68: // <![CDATA[
   69: 
   70: function changePage(formname,newphase) {
   71:     formname.phase.value = newphase;
   72:     numchecked = 0;
   73:     if (formname == document.pickactions) {
   74:         if (formname.actions.length > 0) {
   75:             for (var i = 0; i<formname.actions.length; i++) {
   76:                 if (formname.actions[i].checked) {
   77:                     numchecked ++;
   78:                 }
   79:             }
   80:         } else {
   81:             if (formname.actions.checked) {
   82:                 numchecked ++;
   83:             }
   84:         }
   85:         if (numchecked > 0) {
   86:             formname.submit();
   87:         } else {
   88:             alert("'.$alert.'");
   89:             return;
   90:         }
   91:     }
   92:     '.$call_category_check.'
   93:     formname.submit();
   94: }'."\n";
   95:     if ($phase eq 'pickactions') {
   96:         $js .=
   97:             &Apache::lonhtmlcommon::set_form_elements({actions => 'checkbox',numcols => 'radio',})."\n".
   98:             &javascript_set_colnums();
   99:     } elsif ($phase eq 'display') {
  100:         $js .= &color_pick_js()."\n";
  101:     }
  102:     $js .= &Apache::loncommon::viewport_size_js().'
  103: 
  104: // ]]>
  105: </script>
  106: ';
  107:     if ($jscript) {
  108:         $js .= "
  109: 
  110: $jscript
  111: 
  112: ";
  113:     }
  114:     my $additem;
  115:     if ($phase eq 'pickactions') {
  116:         my %loaditems = (
  117:                     'onload' => "javascript:getViewportDims(document.$phase.width,document.$phase.height);setDisplayColumns();setFormElements(document.pickactions);",
  118:                         );
  119:         $additem = {'add_entries' => \%loaditems,};
  120:     } else {
  121:         my %loaditems = (
  122:                     'onload' => "javascript:getViewportDims(document.$phase.width,document.$phase.height);",
  123:                         );
  124:         $additem = {'add_entries' => \%loaditems,};
  125:     }
  126:     $r->print(&Apache::loncommon::start_page($pagetitle,$js,$additem));
  127:     $r->print(&Apache::lonhtmlcommon::breadcrumbs($brcrumtitle));
  128:     $r->print('
  129: <form name="parmform" action="">
  130: <input type="hidden" name="pres_marker" />
  131: <input type="hidden" name="pres_type" />
  132: <input type="hidden" name="pres_value" />
  133: </form>
  134: ');
  135:     $r->print('<form method="post" name="'.$phase.'" action="'.$action.'"'.
  136:               ' enctype="multipart/form-data">');
  137:     return;
  138: }
  139: 
  140: sub print_footer {
  141:     my ($r,$phase,$newphase,$button_text,$actions) = @_;
  142:     $button_text = &mt($button_text);
  143:     $r->print('<input type="hidden" name="phase" value="" />'.
  144:               '<input type="hidden" name="width" value="'.
  145:               $env{'form.width'}.'" />'.
  146:               '<input type="hidden" name="height" value="'.
  147:               $env{'form.height'}.'" />');
  148:     if (defined($env{'form.origin'})) {
  149:         $r->print('<input type="hidden" name="origin" value="'.$env{'form.origin'}.'" />'."\n");
  150:     }
  151:     if (($phase eq 'display') || ($phase eq 'process')) {
  152:         if (ref($actions) eq 'ARRAY') {
  153:             foreach my $item (@{$actions}) {
  154:                 $r->print('<input type="hidden" name="actions" value="'.$item.'" />')."\n";
  155:             }
  156:         }
  157:         $r->print('<input type="hidden" name="numcols" value="'.$env{'form.numcols'}.'" />');
  158:     }
  159:     my $dest='"javascript:changePage(document.'.$phase.','."'$newphase'".')"';
  160:     if ($phase eq 'process') {
  161:         $r->print('<p><a href='.$dest.'>'.$button_text.'</a></p>');
  162:     } else {
  163:         my $onclick;
  164:         if ($phase eq 'display') {
  165:             $onclick = '"javascript:changePage(document.'.$phase.','."'$newphase'".')"';
  166:         } else {
  167:             $onclick = '"javascript:changePage(document.'.$phase.','."'$newphase'".')"';
  168:         }
  169:         $r->print('<p><input type="button" name="store" value="'.
  170:                   $button_text.'" onclick='.$onclick.' /></p>');
  171:     }
  172:     if ($phase eq 'process') {
  173:         $r->print('</form>'.&Apache::loncommon::end_page());
  174:     }
  175:     return;
  176: }
  177: 
  178: sub make_changes {
  179:     my ($r,$dom,$phase,$context,$prefs_order,$prefs,$values,$confname,$roles,$allitems) = @_;
  180:     my %brcrumtext = &get_crumb_text();
  181:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  182:     my ($numchanged,%changes,%disallowed);
  183:     &Apache::lonhtmlcommon::add_breadcrumb
  184:       ({href=>"javascript:changePage(document.$phase,'display')",
  185:         text=>$brcrumtext{$context}},
  186:        {href=>"javascript:changePage(document.$phase,'$phase')",
  187:         text=>"Updated"});
  188:     &print_header($r,$phase,$context);
  189:     my $crstype;
  190:     if ($context eq 'course') {
  191:         $crstype = &Apache::loncommon::course_type();
  192:     }
  193:     if ((ref($prefs_order) eq 'ARRAY') && (ref($prefs) eq 'HASH') && 
  194:         (ref($prefs) eq 'HASH')) {
  195:         foreach my $item (@{$prefs_order}) {
  196:             if (grep(/^\Q$item\E$/,@actions)) {
  197:                 if ($context eq 'domain') {
  198:                     $r->print('<h3>'.&mt($prefs->{$item}{'text'}).'</h3>'.
  199:                               &Apache::domainprefs::process_changes($r,$dom,
  200:                                           $confname,$item,$roles,$values));
  201:                 } else {
  202:                     $changes{$item} = {};
  203:                     &Apache::courseprefs::process_changes($dom,$item,$values,
  204:                                                           $prefs->{$item},$changes{$item},
  205:                                                           $allitems,\%disallowed,$crstype);
  206:                     if (keys(%{$changes{$item}}) > 0) {
  207:                         $numchanged ++;
  208:                     }
  209:                 }
  210:             }
  211:         }
  212:     }
  213:     if ($context eq 'course') {
  214:         if ($numchanged) {
  215:             $r->print(&Apache::courseprefs::store_changes($dom,$confname,$prefs_order,\@actions,
  216:                                                           $prefs,$values,\%changes,$crstype));
  217:         } else {
  218:             if ($crstype eq 'Community') {
  219:                 $r->print(&mt("No changes made to community configuration."));
  220:             } else {
  221:                 $r->print(&mt("No changes made to course configuration."));
  222:             }
  223:         }
  224:         if (keys(%disallowed) > 0) {
  225:             $r->print('<p>');
  226:             foreach my $item ('cloners','rolenames','feedback','discussion','localization') {
  227:                 if (ref($disallowed{$item}) eq 'HASH') {
  228:                     if (keys(%{$disallowed{$item}}) > 0) {
  229:                         $r->print(&Apache::courseprefs::display_disallowed($item,$disallowed{$item},
  230:                                                                            $prefs,$crstype));
  231:                     }
  232:                 }
  233:             }
  234:             $r->print('</p>');
  235:         }
  236:     }
  237:     $r->print('<p>');
  238:     my $footer_text = 'Back to configuration display';
  239:     if ($context eq 'course') {
  240:         $footer_text = 'Back to display/edit settings'; 
  241:     }
  242:     &print_footer($r,$phase,'display',$footer_text,\@actions);
  243:     $r->print('</p>');
  244: }
  245: 
  246: sub display_settings {
  247:     my ($r,$dom,$phase,$context,$prefs_order,$prefs,$values,$confname,$jscript,
  248:         $allitems,$crstype) = @_;
  249:     my %brcrumtext = &get_crumb_text();
  250:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  251:     &Apache::lonhtmlcommon::add_breadcrumb
  252:         ({href=>"javascript:changePage(document.$phase,'display')",
  253:           text=>"Display/Edit Settings"});
  254:     &print_header($r,$phase,$context,$jscript);
  255:     if ((ref($prefs_order) eq 'ARRAY') && (ref($prefs) eq 'HASH') && (ref($values) eq 'HASH')) { 
  256:         if (@actions > 0) {
  257:             my $rowsum = 0;
  258:             my (%output,%rowtotal,@items);
  259:             my $halfway = @actions/2;
  260:             foreach my $item (@{$prefs_order}) {
  261:                 if (grep(/^\Q$item\E$/,@actions)) {
  262:                     push(@items,$item);
  263:                     if ($context eq 'domain') {
  264:                         ($output{$item},$rowtotal{$item}) =
  265:                             &Apache::domainprefs::print_config_box($r,$dom,$confname,
  266:                                 $phase,$item,$prefs->{$item},$values->{$item});
  267:                     } else {
  268:                         ($output{$item},$rowtotal{$item}) =
  269:                             &Apache::courseprefs::print_config_box($r,$dom,$phase,
  270:                                 $item,$prefs->{$item},$values,$allitems,$crstype);
  271:                     }
  272:                     $rowsum += $rowtotal{$item};
  273:                 }
  274:             }
  275:             my $colend;
  276:             my $halfway = $rowsum/2;
  277:             my $aggregate = 0;
  278:             my $sumleft = 0;
  279:             my $sumright = 0;
  280:             my $crossover;
  281:             for (my $i=0; $i<@items; $i++) {
  282:                 $aggregate += $rowtotal{$items[$i]};
  283:                 if ($aggregate > $halfway) {
  284:                     $crossover = $i;
  285:                     last;
  286:                 }
  287:             }
  288:             for (my $i=0; $i<$crossover; $i++) {
  289:                 $sumleft += $rowtotal{$items[$i]};
  290:             }
  291:             for (my $i=$crossover+1; $i<@items; $i++) {
  292:                 $sumright += $rowtotal{$items[$i]};
  293:             }
  294:             if ((@items > 1) && ($env{'form.numcols'} == 2)) {
  295:                 my $sumdiff = $sumright - $sumleft;
  296:                 if ($sumdiff > 0) {
  297:                     $colend = $crossover + 1;
  298:                 } else {
  299:                     $colend = $crossover;
  300:                 }
  301:             } else {
  302:                 $colend = @items;
  303:             }
  304:             if ($context ne 'course') {
  305:             	$r->print('<p><table class="LC_double_column"><tr><td class="LC_left_col">');            
  306:             }
  307:             else {
  308:             	$r->print('<div id="prefs" style="max-width:900px;margin: 10px auto 10px auto;">');
  309:             }
  310:             for (my $i=0; $i<$colend; $i++) {
  311:                 $r->print($output{$items[$i]});
  312:             }
  313:             if ($context ne 'course') {
  314:             	$r->print('</td><td></td><td class="LC_right_col">');
  315:             }
  316:             if ($colend < @items) {
  317:                 for (my $i=$colend; $i<@items; $i++) {
  318:                     $r->print($output{$items[$i]});
  319:                 }
  320:             }
  321:             if ($context ne 'course') {
  322:             	$r->print('</td></tr></table></p>');
  323:             }
  324:             else {
  325:             	$r->print('</div>');
  326:             }
  327:             $r->print(&print_footer($r,$phase,'process','Save Changes',\@actions));
  328:         } else {
  329:             $r->print('<input type="hidden" name="phase" value="" />'.
  330:                   '<input type="hidden" name="numcols" value="'.
  331:                   $env{'form.numcols'}.'" />'."\n".
  332:                   '<span class="LC_error">'.&mt('No settings chosen').
  333:                   '</span>');
  334:         }
  335:         $r->print('</form>');
  336:     }
  337:     if ($context eq 'course') {
  338:     	$r->print('
  339: <script type="text/javascript">
  340: 	$(document).ready(function(){
  341: 		$("#prefs").accordion({
  342: 			autoHeight: false
  343: 		});
  344: 		if ($(".LC_nested tr.advanced").get(0)) {
  345: 			$(".LC_nested tr.advanced").each(function() {
  346: 				$(this).hide();
  347: 				p = $(this).parents("div.ui-accordion-content")
  348: 				if (p.find(".LC_advanced_toggle").length) {
  349: 					return;
  350: 				}
  351: 				p.prepend("<span class=\'LC_advanced_toggle\'><input type=\"checkbox\"/>Show advanced options</span>");
  352: 			});
  353: 			$(".LC_advanced_toggle input").change(function() {
  354: 				if($(this).is(":checked")) {
  355: 					$(this).parents("div.ui-accordion-content").find("tr.advanced").fadeIn("normal");
  356: 				} else {
  357: 					$(this).parents("div.ui-accordion-content").find("tr.advanced").fadeOut("normal");		
  358: 				}
  359: 			});
  360: 		}
  361: 	});
  362: </script>
  363: ');
  364:     }
  365:     $r->print(&Apache::loncommon::end_page());
  366:     return;
  367: }
  368: 
  369: sub display_choices {
  370:     my ($r,$phase,$context,$prefs_order,$prefs) = @_;
  371:     if ($phase eq '') {
  372:         $phase = 'pickactions';
  373:     }
  374:     my %helphash;
  375:     &print_header($r,$phase,$context);
  376:     $r->print('<h3>'.&mt('Settings to display/modify').'</h3>');
  377:     $r->print('<script type="text/javascript">'."\n".
  378:               '// <![CDATA['."\n".
  379:               &Apache::loncommon::check_uncheck_jscript()."\n".
  380:               '// ]]>'."\n".
  381:               '</script>'."\n".'<p><input type="button" value="'.&mt('check all').'" '.
  382:               'onclick="javascript:checkAll(document.pickactions.actions)"'.
  383:               ' />'.('&nbsp;'x2).
  384:               '<input type="button" value="'.&mt('uncheck all').'" '.
  385:               'onclick="javascript:uncheckAll(document.pickactions.actions)"'.
  386:               ' /></p><div class="LC_left_float">');
  387:     my ($numitems,$midpoint,$seconddiv,$count);
  388:     if (ref($prefs_order) eq 'ARRAY') {
  389:         $numitems = @{$prefs_order};
  390:     }
  391:     $midpoint = int($numitems/2);
  392:     if ($numitems%2) {
  393:         $midpoint ++;
  394:     }
  395:     $count = 0;
  396:     if ((ref($prefs_order) eq 'ARRAY') && (ref($prefs) eq 'HASH')) {
  397:         foreach my $item (@{$prefs_order}) {
  398:             $r->print('<h4>'.
  399:                       &Apache::loncommon::help_open_topic($prefs->{$item}->{'help'}).
  400:                       '<label><input type="checkbox" name="actions" value="'.$item.
  401:                       '" />&nbsp;'.&mt($prefs->{$item}->{'text'}).'</label></h4>');
  402:             $count ++;
  403:             if ((!$seconddiv) && ($count >= $midpoint)) {
  404:                 $r->print('</div>'."\n".'<div class="LC_left_float">'."\n");
  405:                 $seconddiv = 1;
  406:             }
  407:         }
  408:         $r->print('</div><div class="LC_clear_float_footer"></div><h3>'.
  409:                   &mt('Display options').'</h3>'."\n".
  410:                   '<p><span class="LC_nobreak">'.&mt('Display using: ')."\n".
  411:                   '<label><input type="radio" name="numcols" value="1" />'.
  412:                   &mt('one column').'</label>&nbsp;&nbsp;<label>'.
  413:                   '<input type="radio" name="numcols" value="2" />'.
  414:                   &mt('two columns').'</label></span></p>');
  415:     }
  416:     $r->print(&print_footer($r,$phase,'display','Go'));
  417:     $r->print('</form>');
  418:     $r->print(&Apache::loncommon::end_page());
  419:     return;
  420: }
  421: 
  422: sub javascript_set_colnums {
  423:     return <<END;
  424: function setDisplayColumns() {
  425:     if (document.pickactions.width.value > 1100) {
  426:         document.pickactions.numcols[1].checked = true;
  427:     } else {
  428:         document.pickactions.numcols[0].checked = true;
  429:     }
  430: }
  431: END
  432: }
  433: 
  434: sub color_pick_js {
  435:     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
  436:     my $output = <<"ENDCOL";
  437:     function pclose() {
  438:         parmwin=window.open("/adm/rat/empty.html","LONCAPAparms","height=350,width=350,scrollbars=no,menubar=no");
  439:         parmwin.close();
  440:     }
  441: 
  442:     $pjump_def
  443: 
  444:     function psub() {
  445:         pclose();
  446:         if (document.parmform.pres_marker.value!='') {
  447:             if (document.parmform.pres_type.value!='') {
  448:                 eval('document.display.'+
  449:                      document.parmform.pres_marker.value+
  450:                      '.value=document.parmform.pres_value.value;');
  451:             }
  452:         } else {
  453:             document.parmform.pres_value.value='';
  454:             document.parmform.pres_marker.value='';
  455:         }
  456:     }
  457: 
  458:     function get_id (span_id) {
  459:         if (document.getElementById) {
  460:             return document.getElementById(span_id);
  461:         }
  462:         if (document.all) {
  463:             return document.all[span_id];
  464:         }
  465:         return false;
  466:     }
  467: 
  468:     function colchg_span (span_id_str,new_color_item) {
  469:         var span_ref = get_id(span_id_str);
  470:         if (span_ref.style) { span_ref = span_ref.style; }
  471:         span_ref.background = new_color_item.value;
  472:         span_ref.backgroundColor = new_color_item.value;
  473:         span_ref.bgColor = new_color_item.value;
  474:     }
  475: 
  476: ENDCOL
  477:     return $output;
  478: }
  479: 
  480: sub get_crumb_text {
  481:     my %brcrumbtext = (
  482:                        domain => 'Domain Settings',
  483:                        course => 'Display/Edit Settings',
  484:                      );
  485:     return %brcrumbtext;
  486: }
  487: 
  488: 1;

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