File:  [LON-CAPA] / loncom / interface / lonconfigsettings.pm
Revision 1.21.4.9.4.2: download - view: text, annotated - select for diffs
Mon Feb 4 01:40:37 2019 UTC (5 years, 4 months ago) by raeburn
Branches: version_2_11_2_msu
Diff to branchpoint 1.21.4.9: preferred, unified
- For 2.11.2 (modified).
  - Include changes in 1.45

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

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