File:  [LON-CAPA] / loncom / interface / lonconfigsettings.pm
Revision 1.21.4.8: download - view: text, annotated - select for diffs
Thu Aug 4 17:34:35 2016 UTC (7 years, 9 months ago) by raeburn
Branches: version_2_11_X
Diff to branchpoint 1.21: preferred, unified
- For 2.11
  Backport 1.31

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

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