File:  [LON-CAPA] / loncom / interface / lonconfigsettings.pm
Revision 1.21.4.9.4.1: download - view: text, annotated - select for diffs
Sun Oct 15 04:41:16 2017 UTC (6 years, 9 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.34 (part), 1.37, 1.38.

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

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