File:  [LON-CAPA] / loncom / interface / lonconfigsettings.pm
Revision 1.21.4.9.6.1: download - view: text, annotated - select for diffs
Thu Apr 9 20:34:29 2020 UTC (4 years, 1 month ago) by raeburn
Branches: version_2_11_2_uiuc
Diff to branchpoint 1.21.4.9: preferred, unified
- For 2.11.2 (modified)
  Include changes in 1.39 (modified) and 1.41

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: lonconfigsettings.pm,v 1.21.4.9.6.1 2020/04/09 20:34:29 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,$instcode,$dom,$values) = @_;
   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:             if (grep(/^ltitools$/,@actions)) {
  188:                 $onload .= "toggleLTITools(document.display,'user','add');";
  189:                 if (ref($values) eq 'HASH') {
  190:                     if (ref($values->{'ltitools'}) eq 'HASH') {
  191:                         my $numltitools = scalar(keys(%{$values->{'ltitools'}}));
  192:                         for (my $i=0; $i<$numltitools; $i++) {
  193:                             $onload .= "toggleLTITools(document.display,'user','$i');";
  194:                         }
  195:                     }
  196:                 }
  197:             }
  198:         } elsif ($context eq 'course') {
  199:             if (grep(/^courseinfo$/,@actions)) {
  200:                 if (@code_order) {
  201:                     $additem = {
  202:                                    add_entries => {'onload' => "courseSet('','load');toggleCloners(document.display.cloners_instcode);"},
  203:                                };
  204:                 }
  205:             }
  206:         }
  207:     }
  208:     $r->print(&Apache::loncommon::start_page($pagetitle,$js,$additem));
  209:     $r->print(&Apache::lonhtmlcommon::breadcrumbs($brcrumtitle));
  210:     $r->print('
  211: <form name="parmform" action="">
  212: <input type="hidden" name="pres_marker" />
  213: <input type="hidden" name="pres_type" />
  214: <input type="hidden" name="pres_value" />
  215: </form>
  216: ');
  217:     $r->print('<form method="post" name="'.$phase.'" action="'.$action.'"'.
  218:               ' enctype="multipart/form-data">');
  219:     return;
  220: }
  221: 
  222: sub print_footer {
  223:     my ($r,$phase,$newphase,$button_text,$actions,$container,$parm_permission) = @_;
  224:     $button_text = &mt($button_text);
  225:     $r->print('<input type="hidden" name="phase" value="" />');
  226:     if (defined($env{'form.origin'})) {
  227:         $r->print('<input type="hidden" name="origin" value="'.$env{'form.origin'}.'" />'."\n");
  228:     }
  229:     if (($phase eq 'display') || ($phase eq 'process')) {
  230:         if (ref($actions) eq 'ARRAY') {
  231:             foreach my $item (@{$actions}) {
  232:                 $r->print('<input type="hidden" name="actions" value="'.$item.'" />'."\n");
  233:             }
  234:         }
  235:     }
  236:     my $dest='"javascript:changePage(document.'.$phase.','."'$newphase'".')"';
  237:     if ($phase eq 'process') {
  238:         $r->print(
  239:             &Apache::lonhtmlcommon::actionbox(
  240:                 ['<a href='.$dest.'>'.$button_text.'</a>']));
  241:     } else {
  242:         my $onclick;
  243:         if ($phase eq 'display') {
  244:             $onclick = '"javascript:changePage(document.'.$phase.','."'$newphase'".')"';
  245:         } else {
  246:             $onclick = '"javascript:changePage(document.'.$phase.','."'$newphase'".')"';
  247:         }
  248:         my $showbutton = 1;
  249:         if (ref($parm_permission) eq 'HASH') {
  250:             unless (($parm_permission->{'process'}) || ($newphase eq 'display')) {
  251:                 $showbutton = 0;
  252:             }
  253:         }
  254:         if ($showbutton) {
  255:             $r->print('<p><input type="button" name="store" value="'.
  256:                       $button_text.'" onclick='.$onclick.' /></p>');
  257:         }
  258:     }
  259:     if ($phase eq 'process') {
  260:         $r->print('</form>');
  261:         $r->print(&Apache::loncommon::end_page());
  262:     }
  263:     return;
  264: }
  265: 
  266: sub make_changes {
  267:     my ($r,$dom,$phase,$context,$prefs_order,$prefs,$values,$confname,$roles,
  268:         $allitems,$container,$parm_permission) = @_;
  269:     my %brcrumtext = &get_crumb_text();
  270:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  271:     my ($numchanged,%changes,%disallowed);
  272:     &Apache::lonhtmlcommon::add_breadcrumb
  273:       ({href=>"javascript:changePage(document.$phase,'display')",
  274:         text=>$brcrumtext{$context}},
  275:        {href=>"javascript:changePage(document.$phase,'$phase')",
  276:         text=>"Updated"});
  277:     &print_header($r,$phase,$context,undef,$container);
  278:     my ($crstype,%lastact);
  279:     if ($context eq 'course') {
  280:         $crstype = &Apache::loncommon::course_type();
  281:     }
  282:     if ((ref($prefs_order) eq 'ARRAY') && (ref($prefs) eq 'HASH') && 
  283:         (ref($prefs) eq 'HASH')) {
  284:         foreach my $item (@{$prefs_order}) {
  285:             if (grep(/^\Q$item\E$/,@actions)) {
  286:                 if ($context eq 'domain') {
  287:                     $r->print('<h3>'.&mt($prefs->{$item}{'text'}).'</h3>'.
  288:                               &Apache::domainprefs::process_changes($r,$dom,
  289:                                           $confname,$item,$roles,$values,\%lastact));
  290:                 } else {
  291:                     $changes{$item} = {};
  292:                     &Apache::courseprefs::process_changes($dom,$item,$values,
  293:                                                           $prefs->{$item},$changes{$item},
  294:                                                           $allitems,\%disallowed,$crstype);
  295:                     if (keys(%{$changes{$item}}) > 0) {
  296:                         $numchanged ++;
  297:                     }
  298:                 }
  299:             }
  300:         }
  301:     }
  302:     if ($context eq 'course') {
  303:         if ($numchanged) {
  304:             my $message = &Apache::courseprefs::store_changes($dom,$confname,$prefs_order,\@actions,
  305:                                                           $prefs,$values,\%changes,$crstype);
  306:             $r->print(&Apache::loncommon::confirmwrapper($message));
  307:         } else {
  308:             if ($crstype eq 'Community') {
  309:                 $r->print(&Apache::loncommon::confirmwrapper(&mt("No changes made to community configuration.")));
  310:             } else {
  311:                 $r->print(&Apache::loncommon::confirmwrapper(&mt("No changes made to course configuration.")));
  312:             }
  313:         }
  314:         if (keys(%disallowed) > 0) {
  315:             $r->print('<p>');
  316:             foreach my $item ('cloners','rolenames','feedback','discussion','localization') {
  317:                 if (ref($disallowed{$item}) eq 'HASH') {
  318:                     if (keys(%{$disallowed{$item}}) > 0) {
  319:                         $r->print(&Apache::courseprefs::display_disallowed($item,$disallowed{$item},
  320:                                                                            $prefs,$crstype));
  321:                     }
  322:                 }
  323:             }
  324:             $r->print('</p>');
  325:         }
  326:     }
  327:     $r->print('<p>');
  328:     my $footer_text = 'Back to configuration display';
  329:     if ($context eq 'course') {
  330:         $footer_text = 'Back to display/edit settings'; 
  331:     }
  332:     &print_footer($r,$phase,'display',$footer_text,\@actions,$container,$parm_permission);
  333:     $r->print('</p>');
  334:     return \%lastact;
  335: }
  336: 
  337: sub display_settings {
  338:     my ($r,$dom,$phase,$context,$prefs_order,$prefs,$values,$confname,$jscript,
  339:         $allitems,$crstype,$container,$parm_permission) = @_;
  340:     my %brcrumtext = &get_crumb_text();
  341:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  342:     &Apache::lonhtmlcommon::add_breadcrumb
  343:         ({href=>"javascript:changePage(document.$phase,'display')",
  344:           text=>"Display/Edit Settings"});
  345:     my $instcode;
  346:     if (ref($values) eq 'HASH') {
  347:         $instcode = $values->{'internal.coursecode'};
  348:     }
  349:     &print_header($r,$phase,$context,$jscript,$container,$instcode,$dom,$values);
  350:     my $divwidth = 900;
  351:     if ((ref($prefs_order) eq 'ARRAY') && (ref($prefs) eq 'HASH') && (ref($values) eq 'HASH')) { 
  352:         if (@actions > 0) {
  353:             my $rowsum = 0;
  354:             my (%output,%rowtotal,@items);
  355:             foreach my $item (@{$prefs_order}) {
  356:                 if (grep(/^\Q$item\E$/,@actions)) {
  357:                     push(@items,$item);
  358:                     if ($context eq 'domain') {
  359:                         my $settings;
  360:                         if (ref($values) eq 'HASH') {
  361:                             $settings = $values->{$item};
  362:                         }
  363:                         if ($item eq 'usersessions') {
  364:                             $r->print('<script type="text/javascript">'."\n".
  365:                                       '// <![CDATA['."\n".
  366:                                       &Apache::loncommon::check_uncheck_jscript()."\n".
  367:                                       '// ]]>'."\n".
  368:                                       '</script>'."\n");
  369:                         } elsif ($item eq 'selfcreation') {
  370:                             if (ref($values) eq 'HASH') {
  371:                                 $settings = $values->{'usercreation'};
  372:                             }
  373:                         } elsif ($item eq 'defaults') {
  374:                             if (ref($values->{'inststatus'}) eq 'HASH') {
  375:                                 if (ref($values->{'defaults'}) eq 'HASH') {
  376:                                     $settings = {%{$values->{'inststatus'}},%{$values->{'defaults'}}};
  377:                                 } else {
  378:                                     $settings = $values->{'inststatus'};
  379:                                 }
  380:                             } else {
  381:                                 my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
  382:                                 my $inststatus = {
  383:                                                    inststatustypes => $usertypes,
  384:                                                    inststatusorder => $types,
  385:                                                    inststatusguest => [],
  386:                                                  };
  387:                                 if (ref($values->{defaults}) eq 'HASH') {
  388:                                     $settings = {%{$inststatus},%{$values->{'defaults'}}};
  389:                                 } else {
  390:                                     $settings = $inststatus;
  391:                                 }
  392:                             }
  393:                         }
  394:                         ($output{$item},$rowtotal{$item}) =
  395:                             &Apache::domainprefs::print_config_box($r,$dom,$confname,
  396:                                 $phase,$item,$prefs->{$item},$settings);
  397:                     } else {
  398:                         ($output{$item},$rowtotal{$item}) =
  399:                             &Apache::courseprefs::print_config_box($r,$dom,$phase,
  400:                                 $item,$prefs->{$item},$values,$allitems,$crstype,$parm_permission);
  401:                     }
  402:                     $rowsum += $rowtotal{$item};
  403:                 }
  404:             }
  405:             $r->print('<div id="prefs" style="max-width:'.$divwidth.'px;margin: 10px auto 10px auto;">');
  406:             for (my $i=0; $i<@items; $i++) {
  407:                 $r->print($output{$items[$i]});
  408:             }
  409:             $r->print('</div>');
  410:             $r->print(&print_footer($r,$phase,'process','Save Changes',\@actions,$container,$parm_permission));
  411:         } else {
  412:             $r->print('<input type="hidden" name="phase" value="" />'.
  413:                       '<span class="LC_error">'.&mt('No settings chosen').
  414:                       '</span>');
  415:         }
  416:         $r->print('</form>');
  417:     }
  418:     $r->print(&Apache::loncommon::end_page());
  419:     return;
  420: }
  421: 
  422: sub display_choices {
  423:     my ($r,$phase,$context,$prefs_order,$prefs,$container,$parm_permission) = @_;
  424:     if ($phase eq '') {
  425:         $phase = 'pickactions';
  426:     }
  427:     my %helphash;
  428:     &print_header($r,$phase,$context,undef,$container);
  429:     $r->print('<script type="text/javascript">'."\n".
  430:               '// <![CDATA['."\n".
  431:               &Apache::loncommon::check_uncheck_jscript()."\n".
  432:               '// ]]>'."\n".
  433:               '</script>'."\n");
  434:     my $heading = &mt('Settings to display/modify');
  435:     if (ref($parm_permission) eq 'HASH') {
  436:         unless ($parm_permission->{'process'}) {
  437:             $heading = &mt('Settings to display');
  438:         }
  439:     }
  440:     $r->print('<h3>'.$heading.'</h3>'.
  441:               '<div><input type="button" value="'.&mt('check all').'" '.
  442:               'onclick="javascript:checkAll(document.pickactions.actions)"'.
  443:               ' />'.('&nbsp;'x2).
  444:               '<input type="button" value="'.&mt('uncheck all').'" '.
  445:               'onclick="javascript:uncheckAll(document.pickactions.actions)" />'.
  446:               "\n".
  447:               '</div><div class="LC_left_float">');
  448:     my ($numitems,$maxincol,$firstthird,$secondthird,$seconddiv,$thirddiv,$count);
  449:     if (ref($prefs_order) eq 'ARRAY') {
  450:         $numitems = @{$prefs_order};
  451:     }
  452:     my $numcols = 3;
  453:     $maxincol = int($numitems/$numcols);
  454:     if ($numitems%$numcols) {
  455:         $maxincol ++;
  456:     }
  457:     $firstthird = $maxincol;
  458:     $secondthird = $firstthird + $maxincol;
  459:     $count = 0;
  460:     if ((ref($prefs_order) eq 'ARRAY') && (ref($prefs) eq 'HASH')) {
  461:         foreach my $item (@{$prefs_order}) {
  462:             $r->print('<h4>'.
  463:                       &Apache::loncommon::help_open_topic($prefs->{$item}->{'help'}).
  464:                       '<label><input type="checkbox" name="actions" value="'.$item.
  465:                       '" />&nbsp;'.&mt($prefs->{$item}->{'text'}).'</label></h4>');
  466:             $count ++;
  467:             if ((!$seconddiv) && ($count >= $firstthird)) {
  468:                 $r->print('</div>'."\n".'<div class="LC_left_float">'."\n");
  469:                 $seconddiv = 1;
  470:             }
  471:             if ((!$thirddiv) && ($count >= $secondthird)) {
  472:                 $r->print('</div>'."\n".'<div class="LC_left_float">'."\n");
  473:                 $thirddiv = 1;
  474:             }
  475:         }
  476:         $r->print('</div><br clear="all" />');
  477:     }
  478:     $r->print(&print_footer($r,$phase,'display','Display',undef,$container,$parm_permission));
  479:     $r->print('</form>');
  480:     $r->print(&Apache::loncommon::end_page());
  481:     return;
  482: }
  483: 
  484: sub color_pick_js {
  485:     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
  486:     my $output = <<"ENDCOL";
  487: 
  488:     $pjump_def
  489: 
  490:     function psub() {
  491:         modalWindow.close();
  492:         if (document.parmform.pres_marker.value!='') {
  493:             if (document.parmform.pres_type.value!='') {
  494:                 eval('document.display.'+
  495:                      document.parmform.pres_marker.value+
  496:                      '.value=document.parmform.pres_value.value;');
  497:             }
  498:         } else {
  499:             document.parmform.pres_value.value='';
  500:             document.parmform.pres_marker.value='';
  501:         }
  502:     }
  503: 
  504:     function get_id (span_id) {
  505:         if (document.getElementById) {
  506:             return document.getElementById(span_id);
  507:         }
  508:         if (document.all) {
  509:             return document.all[span_id];
  510:         }
  511:         return false;
  512:     }
  513: 
  514:     function colchg_span (span_id_str,new_color_item) {
  515:         var span_ref = get_id(span_id_str);
  516:         if (span_ref.style) { span_ref = span_ref.style; }
  517:         span_ref.background = new_color_item.value;
  518:         span_ref.backgroundColor = new_color_item.value;
  519:         span_ref.bgColor = new_color_item.value;
  520:     }
  521: 
  522: ENDCOL
  523:     return $output;
  524: }
  525: 
  526: sub get_crumb_text {
  527:     my %brcrumbtext = (
  528:                        domain => 'Domain Settings',
  529:                        course => 'Display/Edit Settings',
  530:                      );
  531:     return %brcrumbtext;
  532: }
  533: 
  534: 1;

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