File:  [LON-CAPA] / loncom / interface / lonconfigsettings.pm
Revision 1.30: download - view: text, annotated - select for diffs
Thu May 21 23:26:35 2015 UTC (9 years ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- General course settings: users allowed to clone course.
  - Show if domain default is set.
  - Adding any course-specific setting will cause domain default to
    be ignored.
  - For official courses, where localenroll::instcode_defaults()
    has been customized, can also allow cloning by specific
    partial institutional codes (e.g., department + number: such as
    phy184 for example).

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

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