File:  [LON-CAPA] / loncom / interface / lonconfigsettings.pm
Revision 1.8.2.1: download - view: text, annotated - select for diffs
Sat Feb 13 19:32:51 2010 UTC (14 years, 4 months ago) by raeburn
Branches: version_2_9_X
Diff to branchpoint 1.8: preferred, unified
- Backport 1.11.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: lonconfigsettings.pm,v 1.8.2.1 2010/02/13 19:32:51 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,$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:             my $message = &Apache::courseprefs::store_changes($dom,$confname,$prefs_order,\@actions,
  216:                                                           $prefs,$values,\%changes,$crstype);
  217:             $r->print(&Apache::loncommon::confirmwrapper($message));
  218:         } else {
  219:             if ($crstype eq 'Community') {
  220:                 $r->print(&Apache::loncommon::confirmwrapper(&mt("No changes made to community configuration.")));
  221:             } else {
  222:                 $r->print(&Apache::loncommon::confirmwrapper(&mt("No changes made to course configuration.")));
  223:             }
  224:         }
  225:         if (keys(%disallowed) > 0) {
  226:             $r->print('<p>');
  227:             foreach my $item ('cloners','rolenames','feedback','discussion','localization') {
  228:                 if (ref($disallowed{$item}) eq 'HASH') {
  229:                     if (keys(%{$disallowed{$item}}) > 0) {
  230:                         $r->print(&Apache::courseprefs::display_disallowed($item,$disallowed{$item},
  231:                                                                            $prefs,$crstype));
  232:                     }
  233:                 }
  234:             }
  235:             $r->print('</p>');
  236:         }
  237:     }
  238:     $r->print('<p>');
  239:     my $footer_text = 'Back to configuration display';
  240:     if ($context eq 'course') {
  241:         $footer_text = 'Back to display/edit settings'; 
  242:     }
  243:     &print_footer($r,$phase,'display',$footer_text,\@actions);
  244:     $r->print('</p>');
  245: }
  246: 
  247: sub display_settings {
  248:     my ($r,$dom,$phase,$context,$prefs_order,$prefs,$values,$confname,$jscript,
  249:         $allitems,$crstype) = @_;
  250:     my %brcrumtext = &get_crumb_text();
  251:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  252:     &Apache::lonhtmlcommon::add_breadcrumb
  253:         ({href=>"javascript:changePage(document.$phase,'display')",
  254:           text=>"Display/Edit Settings"});
  255:     &print_header($r,$phase,$context,$jscript);
  256:     if ((ref($prefs_order) eq 'ARRAY') && (ref($prefs) eq 'HASH') && (ref($values) eq 'HASH')) { 
  257:         if (@actions > 0) {
  258:             my $rowsum = 0;
  259:             my (%output,%rowtotal,@items);
  260:             my $halfway = @actions/2;
  261:             foreach my $item (@{$prefs_order}) {
  262:                 if (grep(/^\Q$item\E$/,@actions)) {
  263:                     push(@items,$item);
  264:                     if ($context eq 'domain') {
  265:                         ($output{$item},$rowtotal{$item}) =
  266:                             &Apache::domainprefs::print_config_box($r,$dom,$confname,
  267:                                 $phase,$item,$prefs->{$item},$values->{$item});
  268:                     } else {
  269:                         ($output{$item},$rowtotal{$item}) =
  270:                             &Apache::courseprefs::print_config_box($r,$dom,$phase,
  271:                                 $item,$prefs->{$item},$values,$allitems,$crstype);
  272:                     }
  273:                     $rowsum += $rowtotal{$item};
  274:                 }
  275:             }
  276:             my $colend;
  277:             my $halfway = $rowsum/2;
  278:             my $aggregate = 0;
  279:             my $sumleft = 0;
  280:             my $sumright = 0;
  281:             my $crossover;
  282:             for (my $i=0; $i<@items; $i++) {
  283:                 $aggregate += $rowtotal{$items[$i]};
  284:                 if ($aggregate > $halfway) {
  285:                     $crossover = $i;
  286:                     last;
  287:                 }
  288:             }
  289:             for (my $i=0; $i<$crossover; $i++) {
  290:                 $sumleft += $rowtotal{$items[$i]};
  291:             }
  292:             for (my $i=$crossover+1; $i<@items; $i++) {
  293:                 $sumright += $rowtotal{$items[$i]};
  294:             }
  295:             if ((@items > 1) && ($env{'form.numcols'} == 2)) {
  296:                 my $sumdiff = $sumright - $sumleft;
  297:                 if ($sumdiff > 0) {
  298:                     $colend = $crossover + 1;
  299:                 } else {
  300:                     $colend = $crossover;
  301:                 }
  302:             } else {
  303:                 $colend = @items;
  304:             }
  305:             $r->print('<p><table class="LC_double_column"><tr><td class="LC_left_col">');            for (my $i=0; $i<$colend; $i++) {
  306:                 $r->print($output{$items[$i]});
  307:             }
  308:             $r->print('</td><td></td><td class="LC_right_col">');
  309:             if ($colend < @items) {
  310:                 for (my $i=$colend; $i<@items; $i++) {
  311:                     $r->print($output{$items[$i]});
  312:                 }
  313:             }
  314:             $r->print('</td></tr></table></p>');
  315:             $r->print(&print_footer($r,$phase,'process','Save',\@actions));
  316:         } else {
  317:             $r->print('<input type="hidden" name="phase" value="" />'.
  318:                   '<input type="hidden" name="numcols" value="'.
  319:                   $env{'form.numcols'}.'" />'."\n".
  320:                   '<span class="LC_error">'.&mt('No settings chosen').
  321:                   '</span>');
  322:         }
  323:         $r->print('</form>');
  324:     }
  325:     $r->print(&Apache::loncommon::end_page());
  326:     return;
  327: }
  328: 
  329: sub display_choices {
  330:     my ($r,$phase,$context,$prefs_order,$prefs) = @_;
  331:     if ($phase eq '') {
  332:         $phase = 'pickactions';
  333:     }
  334:     my %helphash;
  335:     &print_header($r,$phase,$context);
  336:     $r->print('<h3>'.&mt('Settings to display/modify').'</h3>');
  337:     $r->print('<script type="text/javascript">'."\n".
  338:               '// <![CDATA['."\n".
  339:               &Apache::loncommon::check_uncheck_jscript()."\n".
  340:               '// ]]>'."\n".
  341:               '</script>'."\n".'<p><input type="button" value="'.&mt('check all').'" '.
  342:               'onclick="javascript:checkAll(document.pickactions.actions)"'.
  343:               ' />'.('&nbsp;'x2).
  344:               '<input type="button" value="'.&mt('uncheck all').'" '.
  345:               'onclick="javascript:uncheckAll(document.pickactions.actions)"'.
  346:               ' /></p><div class="LC_left_float">');
  347:     my ($numitems,$midpoint,$seconddiv,$count);
  348:     if (ref($prefs_order) eq 'ARRAY') {
  349:         $numitems = @{$prefs_order};
  350:     }
  351:     $midpoint = int($numitems/2);
  352:     if ($numitems%2) {
  353:         $midpoint ++;
  354:     }
  355:     $count = 0;
  356:     if ((ref($prefs_order) eq 'ARRAY') && (ref($prefs) eq 'HASH')) {
  357:         foreach my $item (@{$prefs_order}) {
  358:             $r->print('<h4>'.
  359:                       &Apache::loncommon::help_open_topic($prefs->{$item}->{'help'}).
  360:                       '<label><input type="checkbox" name="actions" value="'.$item.
  361:                       '" />&nbsp;'.&mt($prefs->{$item}->{'text'}).'</label></h4>');
  362:             $count ++;
  363:             if ((!$seconddiv) && ($count >= $midpoint)) {
  364:                 $r->print('</div>'."\n".'<div class="LC_left_float">'."\n");
  365:                 $seconddiv = 1;
  366:             }
  367:         }
  368:         $r->print('</div><div class="LC_clear_float_footer"></div><h3>'.
  369:                   &mt('Display options').'</h3>'."\n".
  370:                   '<p><span class="LC_nobreak">'.&mt('Display using: ')."\n".
  371:                   '<label><input type="radio" name="numcols" value="1" />'.
  372:                   &mt('one column').'</label>&nbsp;&nbsp;<label>'.
  373:                   '<input type="radio" name="numcols" value="2" />'.
  374:                   &mt('two columns').'</label></span></p>');
  375:     }
  376:     $r->print(&print_footer($r,$phase,'display','Go'));
  377:     $r->print('</form>');
  378:     $r->print(&Apache::loncommon::end_page());
  379:     return;
  380: }
  381: 
  382: sub javascript_set_colnums {
  383:     return <<END;
  384: function setDisplayColumns() {
  385:     if (document.pickactions.width.value > 1100) {
  386:         document.pickactions.numcols[1].checked = true;
  387:     } else {
  388:         document.pickactions.numcols[0].checked = true;
  389:     }
  390: }
  391: END
  392: }
  393: 
  394: sub color_pick_js {
  395:     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
  396:     my $output = <<"ENDCOL";
  397:     function pclose() {
  398:         parmwin=window.open("/adm/rat/empty.html","LONCAPAparms","height=350,width=350,scrollbars=no,menubar=no");
  399:         parmwin.close();
  400:     }
  401: 
  402:     $pjump_def
  403: 
  404:     function psub() {
  405:         pclose();
  406:         if (document.parmform.pres_marker.value!='') {
  407:             if (document.parmform.pres_type.value!='') {
  408:                 eval('document.display.'+
  409:                      document.parmform.pres_marker.value+
  410:                      '.value=document.parmform.pres_value.value;');
  411:             }
  412:         } else {
  413:             document.parmform.pres_value.value='';
  414:             document.parmform.pres_marker.value='';
  415:         }
  416:     }
  417: 
  418:     function get_id (span_id) {
  419:         if (document.getElementById) {
  420:             return document.getElementById(span_id);
  421:         }
  422:         if (document.all) {
  423:             return document.all[span_id];
  424:         }
  425:         return false;
  426:     }
  427: 
  428:     function colchg_span (span_id_str,new_color_item) {
  429:         var span_ref = get_id(span_id_str);
  430:         if (span_ref.style) { span_ref = span_ref.style; }
  431:         span_ref.background = new_color_item.value;
  432:         span_ref.backgroundColor = new_color_item.value;
  433:         span_ref.bgColor = new_color_item.value;
  434:     }
  435: 
  436: ENDCOL
  437:     return $output;
  438: }
  439: 
  440: sub get_crumb_text {
  441:     my %brcrumbtext = (
  442:                        domain => 'Domain Settings',
  443:                        course => 'Display/Edit Settings',
  444:                      );
  445:     return %brcrumbtext;
  446: }
  447: 
  448: 1;

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