File:  [LON-CAPA] / loncom / interface / lonconfigsettings.pm
Revision 1.21.4.7: download - view: text, annotated - select for diffs
Fri May 22 16:52:16 2015 UTC (9 years, 1 month ago) by raeburn
Branches: version_2_11_X
CVS tags: version_2_11_1
Diff to branchpoint 1.21: preferred, unified
- For 2.11
  - Backport 1.30.

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

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