File:  [LON-CAPA] / loncom / interface / lonconfigsettings.pm
Revision 1.72: download - view: text, annotated - select for diffs
Sun Sep 24 03:31:18 2023 UTC (7 months, 3 weeks ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, HEAD
- Bug 2689

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: lonconfigsettings.pm,v 1.72 2023/09/24 03:31:18 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: use File::Copy;
   42: 
   43: sub print_header {
   44:     my ($r,$phase,$context,$jscript,$container,$instcode,$dom,$confname,$values) = @_;
   45:     my ($pagetitle,$brcrumtitle,$action,$call_category_check,$instcode_check,
   46:         $linkprot_check,$ltitools_check,$crstype,@actions,@code_order);
   47:     if ($phase eq 'display') {
   48:         @actions = &Apache::loncommon::get_env_multiple('form.actions');
   49:     }
   50:     if ($context eq 'domain') {
   51:         ($pagetitle, $brcrumtitle) = ('View/Modify Domain Settings','View/Modify Domain Settings');
   52:         $action = '/adm/domainprefs';
   53:         if ($phase eq 'display') {
   54:             if (grep(/^coursecategories$/,@actions)) {
   55:                 $call_category_check = qq|
   56:     if (formname == document.display) {
   57:         if (!categoryCheck(formname)) {
   58:             return;
   59:         }
   60:     }
   61: |;
   62:             }
   63:         }
   64:     } else {
   65:         $crstype = &Apache::loncommon::course_type();
   66:         if ($crstype eq 'Community') {
   67:             ($pagetitle,$brcrumtitle) = ('Community Configuration','Community Configuration');
   68:         } else {
   69:             ($pagetitle,$brcrumtitle) = ('Course Configuration','Course Configuration');
   70:         }
   71:         $action = '/adm/courseprefs';
   72:         if ($phase eq 'display') {
   73:             if (grep(/^courseinfo$/,@actions)) {
   74:                 my %codedefaults;
   75:                 &Apache::lonnet::auto_instcode_defaults($env{'request.role.domain'},\%codedefaults,
   76:                                                         \@code_order);
   77:                 if (@code_order) {
   78:                    my $noinstcodestr = &mt('You indicated cloning based on category, but did not select any categories.');
   79:                    &js_escape(\$noinstcodestr);
   80:                    $instcode_check = <<"ENDSCRIPT";
   81:     if (formname == document.display) {
   82:         if (formname.cloners_instcode.length) {
   83:             for (var j=0; j<formname.cloners_instcode.length; j++) {
   84:                 if (formname.cloners_instcode[j].checked) {
   85:                     if (formname.cloners_instcode[j].value == 1) {
   86:                         var codes;
   87:                         if (document.getElementsByClassName) {
   88:                             codes = document.getElementsByClassName('LC_cloners_instcodes');
   89:                         } else {
   90:                             codes = getElementsByClassName(document.body,'LC_cloners_instcodes');
   91:                         }
   92:                         if (codes.length) {
   93:                             var gotcode = 0;
   94:                             for (var i=0; i<codes.length; i++) {
   95:                                 if (codes[i].selectedIndex != 0) {
   96:                                      gotcode = 1;
   97:                                      break;
   98:                                 }
   99:                             }
  100:                             if (!gotcode) {
  101:                                 for (var k=0; k<formname.cloners_instcode.length; k++) {
  102:                                     if (formname.cloners_instcode[k].value == 0) {
  103:                                         formname.cloners_instcode[k].checked = true;
  104:                                     }
  105:                                 }
  106:                                 toggleCloners(document.display.cloners_instcode);
  107:                                 alert('$noinstcodestr');
  108:                                 return false;
  109:                             }
  110:                         }
  111:                     }
  112:                 }
  113:             }
  114:         }
  115:     }
  116: 
  117: ENDSCRIPT
  118:                 }
  119:             }
  120:             if (($context eq 'course') && ((grep(/^linkprot$/,@actions)) ||
  121:                 (grep(/^ltitools$/,@actions)))) {
  122:                 my $allowed;
  123:                 my $home = &Apache::lonnet::homeserver($confname,$dom);
  124:                 unless ($home eq 'no_host') {
  125:                     my @ids=&Apache::lonnet::current_machine_ids();
  126:                     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
  127:                 }
  128:                 if ($allowed) {
  129:                     if (grep((/^linkprot$/,@actions))) {
  130:                         $linkprot_check = &ltisecret_js('linkprot',$dom,$values);
  131:                     }
  132:                     if (grep((/^ltitools$/,@actions))) {
  133:                         $ltitools_check = &ltisecret_js('ltitools',$dom,$values);
  134:                     }
  135:                 }
  136:             }
  137:         }
  138:     }
  139:     my $alert = &mt('You must select at least one functionality type to display.');
  140:     &js_escape(\$alert);
  141:     my $js = '
  142: <script type="text/javascript">
  143: // <![CDATA[
  144: 
  145: function changePage(formname,newphase) {
  146:     formname.phase.value = newphase;
  147:     numchecked = 0;
  148:     if (formname == document.pickactions) {
  149:         if (formname.actions.length > 0) {
  150:             for (var i = 0; i<formname.actions.length; i++) {
  151:                 if (formname.actions[i].checked) {
  152:                     numchecked ++;
  153:                 }
  154:             }
  155:         } else {
  156:             if (formname.actions.checked) {
  157:                 numchecked ++;
  158:             }
  159:         }
  160:         if (numchecked > 0) {
  161:             formname.submit();
  162:         } else {
  163:             alert("'.$alert.'");
  164:             return;
  165:         }
  166:     }
  167:     '.$instcode_check.$call_category_check.$linkprot_check.$ltitools_check.'
  168:     formname.submit();
  169: }'."\n";
  170:     if ($phase eq 'pickactions') {
  171: 	$js .= &Apache::lonhtmlcommon::color_picker();
  172:         $js .=
  173:             &Apache::lonhtmlcommon::set_form_elements({actions => 'checkbox'})."\n";
  174:     } elsif ($phase eq 'display') {
  175: 	$js .= &Apache::lonhtmlcommon::color_picker();
  176:         $js .= &color_pick_js()."\n";
  177:         if ($context eq 'domain') {
  178:             if (grep(/^(lti|ltitools)$/,@actions)) {
  179:                  $js .= &ltisec_javascript($dom)."\n";
  180:             }
  181:         }
  182:     }
  183:     $js .= &Apache::loncommon::viewport_size_js().'
  184: 
  185: // ]]>
  186: </script>
  187: ';
  188:     if ($jscript) {
  189:         $js .= "
  190: 
  191: $jscript
  192: 
  193: ";
  194:     }
  195:     my $additem;
  196:     if ($phase eq 'pickactions') {
  197:         my %loaditems = (
  198:                     'onload' => "setFormElements(document.pickactions);",
  199:                         );
  200:         $additem = {'add_entries' => \%loaditems,};
  201:     } elsif ($phase eq 'display') {
  202:         if ($context eq 'domain') {
  203:             my $onload;
  204:             if (grep(/^coursedefaults$/,@actions)) {
  205:                 $onload = "toggleDisplay(document.display,'cloneinstcode');".
  206:                           "toggleDisplay(document.display,'credits');".
  207:                           "toggleDisplay(document.display,'studentsubmission');";
  208:             }
  209:             if (grep(/^selfcreation$/,@actions)) {
  210:                 my $prefix = 'cancreate_emailverified';
  211:                 my $customclass = 'LC_selfcreate_email';
  212:                 my $classprefix = 'LC_canmodify_emailusername_';
  213:                 my $optionsprefix = 'LC_options_emailusername_';
  214:                 $onload .= "toggleRows(document.display,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
  215:                 my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
  216:                 my $hascustom;
  217:                 my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
  218:                 if (ref($emailrules) eq 'HASH') {
  219:                     if (keys(%{$emailrules}) > 0) {
  220:                         $hascustom = 'cancreate_emailrule';
  221:                     }
  222:                 }
  223:                 my @posstypes;
  224:                 if (ref($types) eq 'ARRAY') {
  225:                     @posstypes = @{$types};
  226:                     push(@posstypes,'default');
  227:                     foreach my $type (@posstypes) {
  228:                         $onload .= "toggleEmailOptions(document.display,'cancreate_emailoptions','$hascustom',".
  229:                                                                "'cancreate_emaildomain','$type');";
  230:                     }
  231:                 } else {
  232:                     $onload .= "toggleEmailOptions(document.display,'cancreate_emailoptions','$hascustom',".
  233:                                                        "'cancreate_emaildomain','default');";
  234:                 }
  235:             }
  236:             if (grep(/^contacts$/,@actions)) {
  237:                 my $customclass = 'LC_helpdesk_override';
  238:                 my $optionsprefix = 'LC_options_helpdesk_';
  239:                 $onload .= "toggleHelpdeskRow(document.display,'overrides','$customclass','$optionsprefix');";
  240:             }
  241:             if (grep(/^lti$/,@actions)) {
  242:                 $onload .= "toggleLTI(document.display,'user','add');".
  243:                            "toggleLTI(document.display,'crs','add');".
  244:                            "toggleLTI(document.display,'sec','add');".
  245:                            "toggleLTI(document.display,'lcauth','add');".
  246:                            "toggleLTI(document.display,'lcmenu','add');".
  247:                            "toggleLTI(document.display,'passback','add');".
  248:                            "toggleLTI(document.display,'callback','add');";
  249:                 if (ref($values) eq 'HASH') {
  250:                     if (ref($values->{'lti'}) eq 'HASH') {
  251:                         my $numlti = scalar(keys(%{$values->{'lti'}}));
  252:                         for (my $i=0; $i<$numlti; $i++) {
  253:                             $onload .= "toggleLTI(document.display,'user','$i');".
  254:                                        "toggleLTI(document.display,'crs','$i');".
  255:                                        "toggleLTI(document.display,'sec','$i');".
  256:                                        "toggleLTI(document.display,'lcauth','$i');".
  257:                                        "toggleLTI(document.display,'lcmenu','$i');".
  258:                                        "toggleLTI(document.display,'passback','$i');".
  259:                                        "toggleLTI(document.display,'callback','$i');";
  260:                         }
  261:                     }
  262:                 }
  263:                 my %servers = &Apache::lonnet::get_servers($dom,'library');
  264:                 foreach my $server (keys(%servers)) {
  265:                     $onload .= "togglePrivKey(document.display,'ltisec','$server');";
  266:                 }
  267:                 $onload .= "toggleLTIEncKey(document.display,'ltisec');";
  268:             }
  269:             if (grep(/^ltitools$/,@actions)) {
  270:                 $onload .= "toggleLTITools(document.display,'passback','add');".
  271:                            "toggleLTITools(document.display,'roster','add');".
  272:                            "toggleLTITools(document.display,'user','add');";
  273:                 if (ref($values) eq 'HASH') {
  274:                     if (ref($values->{'ltitools'}) eq 'HASH') {
  275:                         my $numltitools = scalar(keys(%{$values->{'ltitools'}}));
  276:                         for (my $i=0; $i<$numltitools; $i++) {
  277:                             $onload .= "toggleLTITools(document.display,'passback','$i');".
  278:                                        "toggleLTITools(document.display,'roster','$i');".
  279:                                        "toggleLTITools(document.display,'user','$i');";
  280:                         }
  281:                     }
  282:                 }
  283:                 my %servers = &Apache::lonnet::get_servers($dom,'library');
  284:                 foreach my $server (keys(%servers)) {
  285:                     $onload .= "togglePrivKey(document.display,'toolsec','$server');";
  286:                 }
  287:                 $onload .= "toggleLTIEncKey(document.display,'toolsec');";
  288:             }
  289:             if (grep(/^wafproxy$/,@actions)) {
  290:                 $onload .= "toggleWAF();checkWAF();updateWAF();";
  291:             }
  292:             if (grep(/^proctoring$/,@actions)) {
  293:                 $onload .= "toggleProctoring(document.display,'proctorio');".
  294:                            "toggleProctoring(document.display,'examity');";
  295:             }
  296:             if (grep(/^scantron$/,@actions)) {
  297:                 $onload .= "toggleScantron(document.display);";
  298:             }
  299:             if (grep(/^autoupdate$/,@actions)) {
  300:                 $onload .= "toggleLastActiveDays(document.display);";
  301:             }
  302:             if (grep(/^autoenroll$/,@actions)) {
  303:                 $onload .= "toggleFailsafe(document.display);";
  304:             }
  305:             if (grep(/^login$/,@actions)) {
  306:                 my %domservers = &Apache::lonnet::get_servers($dom);
  307:                 foreach my $server (sort(keys(%domservers))) {
  308:                     $onload .= "toggleSamlOptions(document.display,'$server');";
  309:                 }
  310:             }
  311:             if ($onload) {
  312:                 my %loaditems = (
  313:                                   'onload' => $onload,
  314:                                 );
  315:                 $additem = {'add_entries' => \%loaditems,};
  316:             }
  317:         } elsif ($context eq 'course') {
  318:             my ($onload,$ishome,$crshome_checked);
  319:             if (grep(/^courseinfo$/,@actions)) {
  320:                 if (@code_order) {
  321:                     $onload = "courseSet('','load');toggleCloners(document.display.cloners_instcode);";
  322:                 }
  323:             }
  324:             if (grep(/^linkprot$/,@actions)) {
  325:                 if (ref($values) eq 'HASH') {
  326:                     if (ref($values->{'linkprot'}) eq 'HASH') {
  327:                         my $ltiauth;
  328:                         if (exists($env{'course.'.$env{'request.course.id'}.'.internal.ltiauth'})) {
  329:                             $ltiauth = $env{'course.'.$env{'request.course.id'}.'.internal.ltiauth'};
  330:                         } else {
  331:                             my %domdefs = &Apache::lonnet::get_domain_defaults($dom);
  332:                             $ltiauth = $domdefs{'crsltiauth'};
  333:                         }
  334:                         unless ($crshome_checked) {
  335:                             $ishome = &is_home();
  336:                             $crshome_checked = 1;
  337:                         }
  338:                         my $posslti = scalar(keys(%{$values->{'linkprot'}}));
  339:                         for (my $i=0; $i<=$posslti; $i++) {
  340:                             my $num = $i;
  341:                             if ($i == $posslti) {
  342:                                 $num = 'add';
  343:                             }
  344:                             if (ref($values->{'linkprot'}->{$i}) eq 'HASH') {
  345:                                 if ($values->{'linkprot'}->{$i}->{'usable'}) {
  346:                                     $onload .= "toggleChgSecret(document.display,'$num','secret','linkprot');";
  347:                                 }
  348:                             }
  349:                             $onload .= "toggleLinkProtExtra(document.display,'returnurl','divurlparam','1','inline-block','$num');".
  350:                                        "toggleLinkProtExtra(document.display,'passback','passbackparam','1','inline-block','$num');";
  351:                             if ($ltiauth) {
  352:                                 $onload .= "toggleLinkProtExtra(document.display,'requser','optional','1','block','$num');".
  353:                                            "toggleLinkProtExtra(document.display,'mapuser','userfield','other','inline-block','$num');";
  354:                             }
  355:                             if ($ishome) {
  356:                                 $onload .= "uncheckLinkProtMakeVis('linkprot','visible','$num');";
  357:                             }
  358:                         }
  359:                     }
  360:                 }
  361:             }
  362:             if (grep(/^ltitools$/,@actions)) {
  363:                 if (ref($values) eq 'HASH') {
  364:                     if (ref($values->{'ltitools'}) eq 'HASH') {
  365:                         unless ($crshome_checked) {
  366:                             $ishome = &is_home();
  367:                             $crshome_checked = 1;
  368:                         }
  369:                         my $possltitools = scalar(keys(%{$values->{'ltitools'}}));
  370:                         for (my $i=0; $i<=$possltitools; $i++) {
  371:                             my $num = $i;
  372:                             if ($i == $possltitools) {
  373:                                 $num = 'add';
  374:                             }
  375:                             if (ref($values->{'ltitools'}->{$i}) eq 'HASH') {
  376:                                 if ($values->{'ltitools'}->{$i}->{'usable'}) {
  377:                                     $onload .= "toggleChgSecret(document.display,'$num','secret','ltitools');";
  378:                                 }
  379:                             }
  380:                             if ($ishome) {
  381:                                 $onload .= "uncheckLinkProtMakeVis('ltitools','visible','$num');";
  382:                             }
  383:                         }
  384:                     }
  385:                 }
  386:             }
  387:             if (grep(/^appearance$/,@actions)) {
  388:                 $onload .= "toggleExtRes();";
  389:             }
  390:             if (grep(/^localization$/,@actions)) {
  391:                 $onload .= "toggleTimeZone();";
  392:             }
  393:             if (grep(/^grading$/,@actions)) {
  394:                 $onload .= 'toggleGrading(document.display);toggleHiddenTotalsSec(document.display);';
  395:             }
  396:             if ($onload) {
  397:                 my %loaditems = (
  398:                                   'onload' => $onload,
  399:                                 );
  400:                 $additem = {'add_entries' => \%loaditems,};
  401:             }
  402:         }
  403:     }
  404:     $r->print(&Apache::loncommon::start_page($pagetitle,$js,$additem));
  405:     $r->print(&Apache::lonhtmlcommon::breadcrumbs($brcrumtitle));
  406:     $r->print('
  407: <form name="parmform" action="">
  408: <input type="hidden" name="pres_marker" />
  409: <input type="hidden" name="pres_type" />
  410: <input type="hidden" name="pres_value" />
  411: </form>
  412: ');
  413:     if ($container) {
  414:        &Apache::lonparmset::startSettingsScreen($r,$container,$crstype);
  415:     }
  416:     $r->print('<form method="post" name="'.$phase.'" action="'.$action.'"'.
  417:               ' enctype="multipart/form-data">');
  418:     return;
  419: }
  420: 
  421: sub ltisecret_js {
  422:     my ($name,$dom,$values) = @_;
  423:     return unless (ref($values) eq 'HASH');
  424:     my $js;
  425:     if (($name eq 'linkprot') || ($name eq 'ltitools')) {
  426:         my (@changeable,@settable);
  427:         if (ref($values->{$name}) eq 'HASH') {
  428:             if (keys(%{$values->{$name}})) {
  429:                 my @current = sort { $a <=> $b } keys(%{$values->{$name}});
  430:                 if (@current) {
  431:                     for (my $i=0; $i<@current; $i++) {
  432:                         my $num = $current[$i];
  433:                         if (ref($values->{$name}->{$num}) eq 'HASH') {
  434:                             if ($values->{$name}->{$num}->{'usable'}) {
  435:                                 push(@changeable,$i);
  436:                             } else {
  437:                                 push(@settable,$i);
  438:                             }
  439:                         }
  440:                     }
  441:                 }
  442:             }
  443:         }
  444:         my ($numrules,$intargjs);
  445: $js .= <<ENDJS;
  446: 
  447: var ${name}radio = '';
  448: var ${name}secretinput = '';
  449: var ${name}posscheck = '';
  450: 
  451: ENDJS
  452:         if (@changeable) {
  453:             foreach my $num (@changeable) {
  454:                 ($numrules,$intargjs) =
  455:                     &Apache::loncommon::passwd_validation_js($name.'secretinput',$dom,$name,$num);
  456:                 $js .= <<"ENDJS";
  457: ${name}posscheck = '';
  458: ${name}radio = document.display.elements['${name}_changesecret_$num'];
  459: if (${name}radio.length) {
  460:     for (var i=0; i<${name}radio.length; i++) {
  461:         if (${name}radio[i].checked) {
  462:             if (${name}radio[i].value == 1) {
  463:                 ${name}posscheck = 1;
  464:             }
  465:         }
  466:     }
  467: }
  468: 
  469: ENDJS
  470:                 if ($numrules) {
  471:                     $js .= <<"ENDJS";
  472: if (${name}posscheck) {
  473:     ${name}secretinput = document.display.elements['${name}_secret_$num'].value;
  474:     $intargjs
  475: }
  476: 
  477: ENDJS
  478:                 }
  479:                 $js .= <<"ENDJS";
  480: if (${name}posscheck) {
  481:     uncheckLinkProtMakeVis('$name','visible','$num');
  482:     document.display.elements['${name}_secret_$num'].type = 'password';
  483: }
  484: 
  485: ENDJS
  486:             }
  487:         }
  488:         if (@settable) {
  489:             foreach my $num (@settable) {
  490:                 ($numrules,$intargjs) =
  491:                     &Apache::loncommon::passwd_validation_js($name.'secretinput',$dom,$name,$num);
  492:                 if ($numrules) {
  493:                     $js .= <<ENDJS;
  494: ${name}secretinput = document.display.elements['${name}_secret_$num'].value;
  495: $intargjs
  496: 
  497: ENDJS
  498:                 }
  499:                 $js .= <<ENDJS;
  500: uncheckLinkProtMakeVis('$name','visible','$num');
  501: document.display.elements['${name}_secret_$num'].type = 'password';
  502: 
  503: ENDJS
  504:             }
  505:         }
  506:         ($numrules,$intargjs) =
  507:             &Apache::loncommon::passwd_validation_js($name.'secretinput',$dom,$name,'add');
  508:         if ($numrules) {
  509:             $js .= <<ENDJS
  510: ${name}secretinput = document.display.elements['${name}_secret_add'].value;
  511: if (document.display.elements['${name}_add'].checked) {
  512:     $intargjs
  513: }
  514: 
  515: ENDJS
  516:         }
  517:         $js .= <<ENDJS;
  518: uncheckLinkProtMakeVis('$name','visible','add');
  519: document.display.elements['${name}_secret_add'].type = 'password';
  520: 
  521: ENDJS
  522:     }
  523:     return $js;
  524: }
  525: 
  526: sub is_home {
  527:     my $ishome;
  528:     my $chome = $env{'course.'.$env{'request.course.id'}.'.home'};
  529:     unless (($chome eq 'no_host') || ($chome eq '')) {
  530:         my @ids=&Apache::lonnet::current_machine_ids();
  531:         foreach my $id (@ids) {
  532:             if ($id eq $chome) {
  533:                 $ishome=1;
  534:                 last;
  535:             }
  536:         }
  537:     }
  538:     return $ishome;
  539: }
  540: 
  541: sub print_footer {
  542:     my ($r,$phase,$newphase,$button_text,$actions,$container,$parm_permission) = @_;
  543:     $button_text = &mt($button_text);
  544:     $r->print('<input type="hidden" name="phase" value="" />');
  545:     if (defined($env{'form.origin'})) {
  546:         $r->print('<input type="hidden" name="origin" value="'.$env{'form.origin'}.'" />'."\n");
  547:     }
  548:     if (($phase eq 'display') || ($phase eq 'process')) {
  549:         if (ref($actions) eq 'ARRAY') {
  550:             foreach my $item (@{$actions}) {
  551:                 $r->print('<input type="hidden" name="actions" value="'.$item.'" />'."\n");
  552:             }
  553:         }
  554:     }
  555:     my $dest='"javascript:changePage(document.'.$phase.','."'$newphase'".')"';
  556:     if ($phase eq 'process') {
  557:         $r->print(
  558:             &Apache::lonhtmlcommon::actionbox(
  559:                 ['<a href='.$dest.'>'.$button_text.'</a>']));
  560:     } else {
  561:         my $onclick;
  562:         if ($phase eq 'display') {
  563:             $onclick = '"javascript:changePage(document.'.$phase.','."'$newphase'".')"';
  564:         } else {
  565:             $onclick = '"javascript:changePage(document.'.$phase.','."'$newphase'".')"';
  566:         }
  567:         my $showbutton = 1;
  568:         if (ref($parm_permission) eq 'HASH') {
  569:             unless (($parm_permission->{'process'}) || ($newphase eq 'display')) {
  570:                 $showbutton = 0;
  571:             }
  572:         }
  573:         if ($showbutton) {
  574:             $r->print('<p><input type="button" name="store" value="'.
  575:                       $button_text.'" onclick='.$onclick.' /></p>');
  576:         }
  577:     }
  578:     if ($phase eq 'process') {
  579:         $r->print('</form>');
  580:         if ($container) {
  581:            &Apache::lonparmset::endSettingsScreen($r);
  582:         }
  583:         $r->print(&Apache::loncommon::end_page());
  584:     }
  585:     return;
  586: }
  587: 
  588: sub make_changes {
  589:     my ($r,$dom,$phase,$context,$prefs_order,$prefs,$values,$confname,$roles,
  590:         $allitems,$container,$parm_permission) = @_;
  591:     my %brcrumtext = &get_crumb_text();
  592:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  593:     my ($numchanged,%changes,%disallowed);
  594:     &Apache::lonhtmlcommon::add_breadcrumb
  595:       ({href=>"javascript:changePage(document.$phase,'display')",
  596:         text=>$brcrumtext{$context}},
  597:        {href=>"javascript:changePage(document.$phase,'$phase')",
  598:         text=>"Updated"});
  599:     &print_header($r,$phase,$context,undef,$container);
  600:     my ($crstype,%lastact,$errors);
  601:     if ($context eq 'course') {
  602:         $crstype = &Apache::loncommon::course_type();
  603:     }
  604:     if ((ref($prefs_order) eq 'ARRAY') && (ref($prefs) eq 'HASH') && 
  605:         (ref($prefs) eq 'HASH')) {
  606:         foreach my $item (@{$prefs_order}) {
  607:             if (grep(/^\Q$item\E$/,@actions)) {
  608:                 if ($context eq 'domain') {
  609:                     $r->print('<h3>'.&mt($prefs->{$item}{'text'}).'</h3>'.
  610:                               &Apache::domainprefs::process_changes($r,$dom,
  611:                                           $confname,$item,$roles,$values,\%lastact));
  612:                 } else {
  613:                     $changes{$item} = {};
  614:                     $errors =
  615:                         &Apache::courseprefs::process_changes($dom,$confname,$item,$values,
  616:                                                               $prefs->{$item},$changes{$item},
  617:                                                               $allitems,\%disallowed,$crstype,
  618:                                                               \%lastact);
  619:                     if (keys(%{$changes{$item}}) > 0) {
  620:                         $numchanged ++;
  621:                     }
  622:                 }
  623:             }
  624:         }
  625:     }
  626:     if ($context eq 'course') {
  627:         if ($numchanged) {
  628:             my $message = &Apache::courseprefs::store_changes($dom,$confname,$prefs_order,\@actions,
  629:                                                           $prefs,$values,\%changes,$crstype);
  630:             $r->print(&Apache::loncommon::confirmwrapper($message));
  631:         } else {
  632:             if ($crstype eq 'Community') {
  633:                 $r->print(&Apache::loncommon::confirmwrapper(&mt("No changes made to community configuration.")));
  634:             } else {
  635:                 $r->print(&Apache::loncommon::confirmwrapper(&mt("No changes made to course configuration.")));
  636:             }
  637:         }
  638:         if (keys(%disallowed) > 0) {
  639:             $r->print('<p>');
  640:             foreach my $item ('cloners','rolenames','feedback','discussion','localization') {
  641:                 if (ref($disallowed{$item}) eq 'HASH') {
  642:                     if (keys(%{$disallowed{$item}}) > 0) {
  643:                         $r->print(&Apache::courseprefs::display_disallowed($item,$disallowed{$item},
  644:                                                                            $prefs,$crstype));
  645:                     }
  646:                 }
  647:             }
  648:             $r->print('</p>');
  649:         }
  650:         if ($errors) {
  651:             $r->print('<p>'.$errors.'</p>');
  652:         }
  653:     }
  654:     $r->print('<p>');
  655:     my $footer_text = 'Back to configuration display';
  656:     if ($context eq 'course') {
  657:         $footer_text = 'Back to display/edit settings'; 
  658:     }
  659:     &print_footer($r,$phase,'display',$footer_text,\@actions,$container,$parm_permission);
  660:     $r->print('</p>');
  661:     return \%lastact;
  662: }
  663: 
  664: sub display_settings {
  665:     my ($r,$dom,$phase,$context,$prefs_order,$prefs,$values,$confname,$jscript,
  666:         $allitems,$crstype,$container,$parm_permission) = @_;
  667:     my %brcrumtext = &get_crumb_text();
  668:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  669:     &Apache::lonhtmlcommon::add_breadcrumb
  670:         ({href=>"javascript:changePage(document.$phase,'display')",
  671:           text=>"Display/Edit Settings"});
  672:     my $instcode;
  673:     if (ref($values) eq 'HASH') {
  674:         $instcode = $values->{'internal.coursecode'};
  675:     }
  676:     &print_header($r,$phase,$context,$jscript,$container,$instcode,$dom,$confname,$values);
  677:     my $divwidth = 900;
  678:     if ((ref($prefs_order) eq 'ARRAY') && (ref($prefs) eq 'HASH') && (ref($values) eq 'HASH')) { 
  679:         if (@actions > 0) {
  680:             my $rowsum = 0;
  681:             my (%output,%rowtotal,@items,$got_check_uncheck,$got_change_secret);
  682:             foreach my $item (@{$prefs_order}) {
  683:                 if (grep(/^\Q$item\E$/,@actions)) {
  684:                     push(@items,$item);
  685:                     if ($context eq 'domain') {
  686:                         my $settings;
  687:                         if (ref($values) eq 'HASH') {
  688:                             $settings = $values->{$item};
  689:                         }
  690:                         if (($item eq 'usersessions') || ($item eq 'ssl')) {
  691:                             unless ($got_check_uncheck) {
  692:                                 $r->print('<script type="text/javascript">'."\n".
  693:                                           '// <![CDATA['."\n".
  694:                                           &Apache::loncommon::check_uncheck_jscript()."\n".
  695:                                           '// ]]>'."\n".
  696:                                           '</script>'."\n");
  697:                                 $got_check_uncheck = 1;
  698:                             }
  699:                         } elsif ($item eq 'selfcreation') {
  700:                             if (ref($values) eq 'HASH') {
  701:                                 $settings = $values->{'usercreation'};
  702:                             }
  703:                         } elsif ($item eq 'defaults') {
  704:                             if (ref($values->{'inststatus'}) eq 'HASH') {
  705:                                 if (ref($values->{'defaults'}) eq 'HASH') {
  706:                                     $settings = {%{$values->{'inststatus'}},%{$values->{'defaults'}}};
  707:                                 } else {
  708:                                     $settings = $values->{'inststatus'};
  709:                                 }
  710:                             } else {
  711:                                 my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
  712:                                 my $inststatus = {
  713:                                                    inststatustypes => $usertypes,
  714:                                                    inststatusorder => $types,
  715:                                                    inststatusguest => [],
  716:                                                  };
  717:                                 if (ref($values->{defaults}) eq 'HASH') {
  718:                                     $settings = {%{$inststatus},%{$values->{'defaults'}}};
  719:                                 } else {
  720:                                     $settings = $inststatus;
  721:                                 }
  722:                             }
  723:                         } elsif (($item eq 'lti') || ($item eq 'ltitools')) {
  724:                             unless ($got_change_secret) {
  725:                                 $r->print('<script type="text/javascript">'."\n".
  726:                                           '// <![CDATA['."\n".
  727:                                           &change_secret_js()."\n".
  728:                                           '// ]]>'."\n".
  729:                                           '</script>'."\n");
  730:                                 $got_change_secret = 1;
  731:                             }
  732:                             if ($item eq 'lti') {
  733:                                 if (ref($values->{'ltisec'}) eq 'HASH') {
  734:                                     if (ref($values->{'lti'}) eq 'HASH') {
  735:                                         $settings = {%{$values->{'lti'}},%{$values->{'ltisec'}}};
  736:                                     } else {
  737:                                         $settings = $values->{'ltisec'};
  738:                                     }
  739:                                 } elsif (ref($values->{'lti'}) eq 'HASH') {
  740:                                     $settings = $values->{'lti'};
  741:                                 }
  742:                             } elsif ($item eq 'ltitools') {
  743:                                 if (ref($values->{'toolsec'}) eq 'HASH') {
  744:                                     if (ref($values->{'ltitools'}) eq 'HASH') {
  745:                                         $settings = {%{$values->{'ltitools'}},%{$values->{'toolsec'}}};
  746:                                     } else {
  747:                                         $settings = $values->{'toolsec'};
  748:                                     }
  749:                                 }
  750:                             }
  751:                         }
  752:                         ($output{$item},$rowtotal{$item}) =
  753:                             &Apache::domainprefs::print_config_box($r,$dom,$confname,
  754:                                 $phase,$item,$prefs->{$item},$settings);
  755:                     } else {
  756:                         unless ($got_change_secret) {
  757:                             $r->print('<script type="text/javascript">'."\n".
  758:                                       '// <![CDATA['."\n".
  759:                                       &change_secret_js()."\n".
  760:                                       '// ]]>'."\n".
  761:                                       '</script>'."\n");
  762:                             $got_change_secret = 1;
  763:                         }
  764:                         ($output{$item},$rowtotal{$item}) =
  765:                             &Apache::courseprefs::print_config_box($r,$dom,$confname,$phase,
  766:                                 $item,$prefs->{$item},$values,$allitems,$crstype,$parm_permission);
  767:                     }
  768:                     $rowsum += $rowtotal{$item};
  769:                 }
  770:             }
  771:             $r->print('<div id="prefs" style="max-width:'.$divwidth.'px;margin: 10px auto 10px auto;">');
  772:             for (my $i=0; $i<@items; $i++) {
  773:                 $r->print($output{$items[$i]});
  774:             }
  775:             $r->print('</div>');
  776:             $r->print(&print_footer($r,$phase,'process','Save Changes',\@actions,$container,$parm_permission));
  777:         } else {
  778:             $r->print('<input type="hidden" name="phase" value="" />'.
  779:                       '<span class="LC_error">'.&mt('No settings chosen').
  780:                       '</span>');
  781:         }
  782:         $r->print('</form>');
  783:     }
  784:     if ($container) {
  785:         &Apache::lonparmset::endSettingsScreen($r);
  786:     }
  787:     $r->print(&Apache::loncommon::end_page());
  788:     return;
  789: }
  790: 
  791: sub display_choices {
  792:     my ($r,$phase,$context,$prefs_order,$prefs,$container,$parm_permission) = @_;
  793:     if ($phase eq '') {
  794:         $phase = 'pickactions';
  795:     }
  796:     my %helphash;
  797:     &print_header($r,$phase,$context,undef,$container);
  798:     $r->print('<script type="text/javascript">'."\n".
  799:               '// <![CDATA['."\n".
  800:               &Apache::loncommon::check_uncheck_jscript()."\n".
  801:               '// ]]>'."\n".
  802:               '</script>'."\n");
  803:     my $heading = &mt('Settings to display/modify');
  804:     if (ref($parm_permission) eq 'HASH') {
  805:         unless ($parm_permission->{'process'}) {
  806:             $heading = &mt('Settings to display');
  807:         }
  808:     }
  809:     $r->print('<h3>'.$heading.'</h3>'.
  810:               '<div><input type="button" value="'.&mt('check all').'" '.
  811:               'onclick="javascript:checkAll(document.pickactions.actions)"'.
  812:               ' />'.('&nbsp;'x2).
  813:               '<input type="button" value="'.&mt('uncheck all').'" '.
  814:               'onclick="javascript:uncheckAll(document.pickactions.actions)" />'.
  815:               "\n".
  816:               '</div><div class="LC_left_float">');
  817:     my ($numitems,$maxincol,$firstthird,$secondthird,$seconddiv,$thirddiv,$count);
  818:     if (ref($prefs_order) eq 'ARRAY') {
  819:         $numitems = @{$prefs_order};
  820:     }
  821:     my $numcols = 3;
  822:     $maxincol = int($numitems/$numcols);
  823:     if ($numitems%$numcols) {
  824:         $maxincol ++;
  825:     }
  826:     $firstthird = $maxincol;
  827:     $secondthird = $firstthird + $maxincol;
  828:     $count = 0;
  829:     if ((ref($prefs_order) eq 'ARRAY') && (ref($prefs) eq 'HASH')) {
  830:         foreach my $item (@{$prefs_order}) {
  831:             $r->print('<h4>'.
  832:                       &Apache::loncommon::help_open_topic($prefs->{$item}->{'help'}).
  833:                       '<label><input type="checkbox" name="actions" value="'.$item.
  834:                       '" />&nbsp;'.&mt($prefs->{$item}->{'text'}).'</label></h4>');
  835:             $count ++;
  836:             if ((!$seconddiv) && ($count >= $firstthird)) {
  837:                 $r->print('</div>'."\n".'<div class="LC_left_float">'."\n");
  838:                 $seconddiv = 1;
  839:             }
  840:             if ((!$thirddiv) && ($count >= $secondthird)) {
  841:                 $r->print('</div>'."\n".'<div class="LC_left_float">'."\n");
  842:                 $thirddiv = 1;
  843:             }
  844:         }
  845:     }
  846:     $r->print('</div><div style="padding:0;clear:both;margin:0;border:0"></div>');
  847:     $r->print(&print_footer($r,$phase,'display','Display',undef,$container,$parm_permission));
  848:     $r->print('</form>');
  849:     if ($container) {
  850:         &Apache::lonparmset::endSettingsScreen($r);
  851:     }
  852:     $r->print(&Apache::loncommon::end_page());
  853:     return;
  854: }
  855: 
  856: sub color_pick_js {
  857:     my $pjump_def = &Apache::lonhtmlcommon::pjump_javascript_definition();
  858:     my $output = <<"ENDCOL";
  859: 
  860:     $pjump_def
  861: 
  862:     function psub() {
  863:         modalWindow.close();
  864:         if (document.parmform.pres_marker.value!='') {
  865:             if (document.parmform.pres_type.value!='') {
  866:                 eval('document.display.'+
  867:                      document.parmform.pres_marker.value+
  868:                      '.value=document.parmform.pres_value.value;');
  869:             }
  870:         } else {
  871:             document.parmform.pres_value.value='';
  872:             document.parmform.pres_marker.value='';
  873:         }
  874:     }
  875: 
  876:     function get_id (span_id) {
  877:         if (document.getElementById) {
  878:             return document.getElementById(span_id);
  879:         }
  880:         if (document.all) {
  881:             return document.all[span_id];
  882:         }
  883:         return false;
  884:     }
  885: 
  886:     function colchg_span (span_id_str,new_color_item) {
  887:         var span_ref = get_id(span_id_str);
  888:         if (span_ref.style) { span_ref = span_ref.style; }
  889:         span_ref.background = new_color_item.value;
  890:         span_ref.backgroundColor = new_color_item.value;
  891:         span_ref.bgColor = new_color_item.value;
  892:     }
  893: 
  894: ENDCOL
  895:     return $output;
  896: }
  897: 
  898: sub ltisec_javascript {
  899:     my ($dom) = @_;
  900:     my %servers = &Apache::lonnet::get_servers($dom,'library');
  901:     my $primary = &Apache::lonnet::domain($dom,'primary');
  902:     my $course_servers = "'".join("','",keys(%servers))."'";
  903:     my $output = <<"ENDJS";
  904: 
  905: function toggleLTIEncKey(form,context) {
  906:     var shownhosts = new Array();
  907:     var hiddenhosts = new Array();
  908:     var forcourse = new Array($course_servers);
  909:     var fromdomain = '$primary';
  910:     var crsradio;
  911:     if (context == 'ltisec') {
  912:         crsradio = form.elements['ltisec_crslinkprot'];
  913:     } else {
  914:         crsradio = form.elements['toolsec_crs'];
  915:     }
  916:     if (crsradio.length) {
  917:         for (var i=0; i<crsradio.length; i++) {
  918:             if (crsradio[i].checked) {
  919:                 if (crsradio[i].value == 1) {
  920:                     if (forcourse.length > 0) {
  921:                         for (var j=0; j<forcourse.length; j++) {
  922:                             if (!shownhosts.includes(forcourse[j])) {
  923:                                 shownhosts.push(forcourse[j]);
  924:                             }
  925:                         }
  926:                     }
  927:                 } else {
  928:                     if (forcourse.length > 0) {
  929:                         for (var j=0; j<forcourse.length; j++) {
  930:                             if (!hiddenhosts.includes(forcourse[j])) {
  931:                                 hiddenhosts.push(forcourse[j]);
  932:                             }
  933:                         }
  934:                     }
  935:                 }
  936:             }
  937:         }
  938:     }
  939:     var domradio;
  940:     if (context == 'ltisec') {
  941:         domradio = form.elements['ltisec_domlinkprot'];
  942:     } else {
  943:         domradio = form.elements['toolsec_dom'];
  944:     }
  945:     if (domradio.length) {
  946:         for (var i=0; i<domradio.length; i++) {
  947:             if (domradio[i].checked) {
  948:                 if (domradio[i].value == 1) {
  949:                     if (!shownhosts.includes(fromdomain)) {
  950:                         shownhosts.push(fromdomain);
  951:                     }
  952:                 } else {
  953:                     if (!hiddenhosts.includes(fromdomain)) {
  954:                         hiddenhosts.push(fromdomain);
  955:                     }
  956:                 }
  957:             }
  958:         }
  959:     }
  960:     if (context == 'ltisec') {
  961:         var consumersradio = form.elements['ltisec_consumers'];
  962:         if (consumersradio.length) {
  963:             for (var i=0; i<consumersradio.length; i++) {
  964:                 if (consumersradio[i].checked) {
  965:                     if (consumersradio[i].value == 1) {
  966:                         if (!shownhosts.includes(fromdomain)) {
  967:                             shownhosts.push(fromdomain);
  968:                         }
  969:                     } else {
  970:                         if (!hiddenhosts.includes(fromdomain)) {
  971:                             hiddenhosts.push(fromdomain);
  972:                         }
  973:                     }
  974:                 }
  975:             }
  976:         }
  977:     }
  978:     if (shownhosts.length > 0) {
  979:         for (var i=0; i<shownhosts.length; i++) {
  980:             if (document.getElementById(context+'_info_'+shownhosts[i])) {
  981:                 document.getElementById(context+'_info_'+shownhosts[i]).style.display = 'block';
  982:             }
  983:         }
  984:         if (document.getElementById(context+'_noprivkey')) {
  985:             document.getElementById(context+'_noprivkey').style.display = 'none';
  986:         }
  987:     } else {
  988:         if (document.getElementById(context+'_noprivkey')) {
  989:             document.getElementById(context+'_noprivkey').style.display = 'inline-block';
  990:         }
  991:     }
  992:     if (hiddenhosts.length > 0) {
  993:         for (var i=0; i<hiddenhosts.length; i++) {
  994:             if (!shownhosts.includes(hiddenhosts[i])) {
  995:                 if (document.getElementById(context+'_info_'+hiddenhosts[i])) {
  996:                     document.getElementById(context+'_info_'+hiddenhosts[i]).style.display = 'none';
  997:                 }
  998:             }
  999:         }
 1000:     }
 1001:     return;
 1002: }
 1003: 
 1004: function togglePrivKey(form,context,hostid) {
 1005:     var radioname = '';
 1006:     var currdivid = '';
 1007:     var newdivid = '';
 1008:     if ((document.getElementById(context+'_divcurrprivkey_'+hostid)) &&
 1009:         (document.getElementById(context+'_divchgprivkey_'+hostid))) {
 1010:         currdivid = document.getElementById(context+'_divcurrprivkey_'+hostid);
 1011:         newdivid = document.getElementById(context+'_divchgprivkey_'+hostid);
 1012:         radioname = form.elements[context+'_changeprivkey_'+hostid];
 1013:         if (radioname) {
 1014:             if (radioname.length > 0) {
 1015:                 var setvis;
 1016:                 for (var i=0; i<radioname.length; i++) {
 1017:                     if (radioname[i].checked == true) {
 1018:                         if (radioname[i].value == 1) {
 1019:                             newdivid.style.display = 'inline-block';
 1020:                             currdivid.style.display = 'none';
 1021:                             setvis = 1;
 1022:                         }
 1023:                         break;
 1024:                     }
 1025:                 }
 1026:                 if (!setvis) {
 1027:                     newdivid.style.display = 'none';
 1028:                     currdivid.style.display = 'inline-block';
 1029:                 }
 1030:             }
 1031:         }
 1032:     }
 1033:     return;
 1034: }
 1035: 
 1036: ENDJS
 1037: 
 1038: }
 1039: 
 1040: sub ltitools_javascript {
 1041:     my ($settings) = @_;
 1042:     my $togglejs = &ltitools_toggle_js();
 1043:     unless (ref($settings) eq 'HASH') {
 1044:         return $togglejs;
 1045:     }
 1046:     my (%ordered,$total,%jstext);
 1047:     $total = 0;
 1048:     foreach my $item (keys(%{$settings})) {
 1049:         if (ref($settings->{$item}) eq 'HASH') {
 1050:             my $num = $settings->{$item}{'order'};
 1051:             $ordered{$num} = $item;
 1052:         }
 1053:     }
 1054:     $total = scalar(keys(%{$settings}));
 1055:     my @jsarray = ();
 1056:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 1057:         push(@jsarray,$ordered{$item});
 1058:     }
 1059:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 1060:     return <<"ENDSCRIPT";
 1061: <script type="text/javascript">
 1062: // <![CDATA[
 1063: function reorderLTITools(form,item) {
 1064:     var changedVal;
 1065: $jstext
 1066:     var newpos = 'ltitools_add_pos';
 1067:     var maxh = 1 + $total;
 1068:     var current = new Array;
 1069:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 1070:     if (item == newpos) {
 1071:         changedVal = newitemVal;
 1072:     } else {
 1073:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 1074:         current[newitemVal] = newpos;
 1075:     }
 1076:     for (var i=0; i<ltitools.length; i++) {
 1077:         var elementName = 'ltitools_'+ltitools[i];
 1078:         if (elementName != item) {
 1079:             if (form.elements[elementName]) {
 1080:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 1081:                 current[currVal] = elementName;
 1082:             }
 1083:         }
 1084:     }
 1085:     var oldVal;
 1086:     for (var j=0; j<maxh; j++) {
 1087:         if (current[j] == undefined) {
 1088:             oldVal = j;
 1089:         }
 1090:     }
 1091:     if (oldVal < changedVal) {
 1092:         for (var k=oldVal+1; k<=changedVal ; k++) {
 1093:            var elementName = current[k];
 1094:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 1095:         }
 1096:     } else {
 1097:         for (var k=changedVal; k<oldVal; k++) {
 1098:             var elementName = current[k];
 1099:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 1100:         }
 1101:     }
 1102:     return;
 1103: }
 1104: 
 1105: // ]]>
 1106: </script>
 1107: 
 1108: $togglejs
 1109: 
 1110: ENDSCRIPT
 1111: }
 1112: 
 1113: sub ltitools_toggle_js {
 1114:     return <<"ENDSCRIPT";
 1115: <script type="text/javascript">
 1116: // <![CDATA[
 1117: 
 1118: function toggleLTITools(form,setting,item) {
 1119:     var radioname = '';
 1120:     var divid = '';
 1121:     if ((setting == 'passback') || (setting == 'roster')) {
 1122:         radioname = 'ltitools_'+setting+'_'+item;
 1123:         divid = 'ltitools_'+setting+'time_'+item;
 1124:         var num = form.elements[radioname].length;
 1125:         if (num) {
 1126:             var setvis = '';
 1127:             for (var i=0; i<num; i++) {
 1128:                 if (form.elements[radioname][i].checked) {
 1129:                     if (form.elements[radioname][i].value == '1') {
 1130:                         if (document.getElementById(divid)) {
 1131:                             document.getElementById(divid).style.display = 'inline-block';
 1132:                         }
 1133:                         setvis = 1;
 1134:                     }
 1135:                     break;
 1136:                 }
 1137:             }
 1138:         }
 1139:         if (!setvis) {
 1140:             if (document.getElementById(divid)) {
 1141:                 document.getElementById(divid).style.display = 'none';
 1142:             }
 1143:         }
 1144:     }
 1145:     if (setting == 'user') {
 1146:         divid = 'ltitools_'+setting+'_div_'+item;
 1147:         var checkid = 'ltitools_'+setting+'_field_'+item;
 1148:         if (document.getElementById(divid)) {
 1149:             if (document.getElementById(checkid)) {
 1150:                 if (document.getElementById(checkid).checked) {
 1151:                     document.getElementById(divid).style.display = 'inline-block';
 1152:                 } else {
 1153:                     document.getElementById(divid).style.display = 'none';
 1154:                 }
 1155:             }
 1156:         }
 1157:     }
 1158:     return;
 1159: }
 1160: // ]]>
 1161: </script>
 1162: 
 1163: ENDSCRIPT
 1164: }
 1165: 
 1166: sub get_crumb_text {
 1167:     my %brcrumbtext = (
 1168:                        domain => 'Domain Settings',
 1169:                        course => 'Display/Edit Settings',
 1170:                      );
 1171:     return %brcrumbtext;
 1172: }
 1173: 
 1174: sub publishlogo {
 1175:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,
 1176:         $savefileas,$modified) = @_;
 1177:     my ($output,$fname,$logourl,$madethumb);
 1178:     if ($action eq 'upload') {
 1179:         $fname=$env{'form.'.$formname.'.filename'};
 1180:         chop($env{'form.'.$formname});
 1181:     } else {
 1182:         ($fname) = ($formname =~ /([^\/]+)$/);
 1183:     }
 1184:     if ($savefileas ne '') {
 1185:         $fname = $savefileas;
 1186:     }
 1187:     $fname=&Apache::lonnet::clean_filename($fname);
 1188: # See if there is anything left
 1189:     unless ($fname) { return ('error: no uploaded file'); }
 1190:     $fname="$subdir/$fname";
 1191:     my $docroot=$r->dir_config('lonDocRoot');
 1192:     my $filepath="$docroot/priv";
 1193:     my $relpath = "$dom/$confname";
 1194:     my ($fnamepath,$file,$fetchthumb);
 1195:     $file=$fname;
 1196:     if ($fname=~m|/|) {
 1197:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 1198:     }
 1199:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 1200:     my $count;
 1201:     for ($count=5;$count<=$#parts;$count++) {
 1202:         $filepath.="/$parts[$count]";
 1203:         if ((-e $filepath)!=1) {
 1204:             mkdir($filepath,02770);
 1205:         }
 1206:     }
 1207:     # Check for bad extension and disallow upload
 1208:     if ($file=~/\.(\w+)$/ &&
 1209:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 1210:         $output =
 1211:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1);
 1212:     } elsif ($file=~/\.(\w+)$/ &&
 1213:         !defined(&Apache::loncommon::fileembstyle($1))) {
 1214:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 1215:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 1216:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 1217:     } elsif (-d "$filepath/$file") {
 1218:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 1219:     } else {
 1220:         my $source = $filepath.'/'.$file;
 1221:         my $logfile;
 1222:         if (!open($logfile,">>",$source.'.log')) {
 1223:             return (&mt('No write permission to Authoring Space'));
 1224:         }
 1225:         print $logfile
 1226: "\n================= Publish ".localtime()." ================\n".
 1227: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 1228: # Save the file
 1229:         if (!open(FH,">",$source)) {
 1230:             &Apache::lonnet::logthis('Failed to create '.$source);
 1231:             return (&mt('Failed to create file'));
 1232:         }
 1233:         if ($action eq 'upload') {
 1234:             if (!print FH ($env{'form.'.$formname})) {
 1235:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 1236:                 return (&mt('Failed to write file'));
 1237:             }
 1238:         } else {
 1239:             my $original = &Apache::lonnet::filelocation('',$formname);
 1240:             if(!copy($original,$source)) {
 1241:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 1242:                 return (&mt('Failed to write file'));
 1243:             }
 1244:         }
 1245:         close(FH);
 1246:         chmod(0660, $source); # Permissions to rw-rw---.
 1247: 
 1248:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 1249:         my $copyfile=$targetdir.'/'.$file;
 1250: 
 1251:         my @parts=split(/\//,$targetdir);
 1252:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 1253:         for (my $count=5;$count<=$#parts;$count++) {
 1254:             $path.="/$parts[$count]";
 1255:             if (!-e $path) {
 1256:                 print $logfile "\nCreating directory ".$path;
 1257:                 mkdir($path,02770);
 1258:             }
 1259:         }
 1260:         my $versionresult;
 1261:         if (-e $copyfile) {
 1262:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 1263:         } else {
 1264:             $versionresult = 'ok';
 1265:         }
 1266:         if ($versionresult eq 'ok') {
 1267:             if (copy($source,$copyfile)) {
 1268:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 1269:                 $output = 'ok';
 1270:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 1271:                 if (ref($modified) eq 'ARRAY') {
 1272:                     push(@{$modified},[$copyfile,$source]);
 1273:                 }
 1274:                 my $metaoutput =
 1275:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 1276:             } else {
 1277:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 1278:                 $output = &mt('Failed to copy file to RES space').", $!";
 1279:             }
 1280:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 1281:                 my $inputfile = $filepath.'/'.$file;
 1282:                 my $outfile = $filepath.'/'.'tn-'.$file;
 1283:                 my ($fullwidth,$fullheight) = &Apache::lonnet::check_dimensions($inputfile);
 1284:                 if ($fullwidth ne '' && $fullheight ne '') {
 1285:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 1286:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 1287:                         my @args = ('convert','-sample',$thumbsize,$inputfile,$outfile);
 1288:                         system({$args[0]} @args);
 1289:                         chmod(0660, $filepath.'/tn-'.$file);
 1290:                         if (-e $outfile) {
 1291:                             my $copyfile=$targetdir.'/tn-'.$file;
 1292:                             if (copy($outfile,$copyfile)) {
 1293:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 1294:                                 my $thumb_metaoutput =
 1295:                                     &write_metadata($dom,$confname,$formname,
 1296:                                                     $targetdir,'tn-'.$file,$logfile);
 1297:                                 if (ref($modified) eq 'ARRAY') {
 1298:                                     push(@{$modified},[$copyfile,$outfile]);
 1299:                                 }
 1300:                                 $madethumb = 1;
 1301:                             } else {
 1302:                                 print $logfile "\nUnable to write ".$copyfile.
 1303:                                                ':'.$!."\n";
 1304:                             }
 1305:                         }
 1306:                     }
 1307:                 }
 1308:             }
 1309:         } else {
 1310:             $output = $versionresult;
 1311:         }
 1312:     }
 1313:     return ($output,$logourl,$madethumb);
 1314: }
 1315: 
 1316: sub logo_versioning {
 1317:     my ($targetdir,$file,$logfile) = @_;
 1318:     my $target = $targetdir.'/'.$file;
 1319:     my ($maxversion,$fn,$extn,$output);
 1320:     $maxversion = 0;
 1321:     if ($file =~ /^(.+)\.(\w+)$/) {
 1322:         $fn=$1;
 1323:         $extn=$2;
 1324:     }
 1325:     opendir(DIR,$targetdir);
 1326:     while (my $filename=readdir(DIR)) {
 1327:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 1328:             $maxversion=($1>$maxversion)?$1:$maxversion;
 1329:         }
 1330:     }
 1331:     $maxversion++;
 1332:     print $logfile "\nCreating old version ".$maxversion."\n";
 1333:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 1334:     if (copy($target,$copyfile)) {
 1335:         print $logfile "Copied old target to ".$copyfile."\n";
 1336:         $copyfile=$copyfile.'.meta';
 1337:         if (copy($target.'.meta',$copyfile)) {
 1338:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 1339:             $output = 'ok';
 1340:         } else {
 1341:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 1342:             $output = &mt('Failed to copy old meta').", $!, ";
 1343:         }
 1344:     } else {
 1345:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 1346:         $output = &mt('Failed to copy old target').", $!, ";
 1347:     }
 1348:     return $output;
 1349: }
 1350: 
 1351: sub write_metadata {
 1352:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 1353:     my (%metadatafields,%metadatakeys,$output);
 1354:     $metadatafields{'title'}=$formname;
 1355:     $metadatafields{'creationdate'}=time;
 1356:     $metadatafields{'lastrevisiondate'}=time;
 1357:     $metadatafields{'copyright'}='public';
 1358:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 1359:                                          $env{'user.domain'};
 1360:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 1361:     $metadatafields{'domain'}=$dom;
 1362:     {
 1363:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 1364:         my $mfh;
 1365:         if (open($mfh,">",$targetdir.'/'.$file.'.meta')) {
 1366:             foreach (sort(keys(%metadatafields))) {
 1367:                 unless ($_=~/\./) {
 1368:                     my $unikey=$_;
 1369:                     $unikey=~/^([A-Za-z]+)/;
 1370:                     my $tag=$1;
 1371:                     $tag=~tr/A-Z/a-z/;
 1372:                     print $mfh "\n\<$tag";
 1373:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 1374:                         my $value=$metadatafields{$unikey.'.'.$_};
 1375:                         $value=~s/\"/\'\'/g;
 1376:                         print $mfh ' '.$_.'="'.$value.'"';
 1377:                     }
 1378:                     print $mfh '>'.
 1379:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 1380:                             .'</'.$tag.'>';
 1381:                 }
 1382:             }
 1383:             $output = 'ok';
 1384:             print $logfile "\nWrote metadata";
 1385:             close($mfh);
 1386:         } else {
 1387:             print $logfile "\nFailed to open metadata file";
 1388:             $output = &mt('Could not write metadata');
 1389:         }
 1390:     }
 1391:     return $output;
 1392: }
 1393: 
 1394: sub change_secret_js {
 1395:     return <<"ENDSCRIPT";
 1396: function toggleChgSecret(form,num,item,name) {
 1397:     var radioname = '';
 1398:     var currdivid = '';
 1399:     var newdivid = '';
 1400:     if ((document.getElementById(name+'_divcurr'+item+'_'+num)) &&
 1401:         (document.getElementById(name+'_divchg'+item+'_'+num))) {
 1402:         currdivid = document.getElementById(name+'_divcurr'+item+'_'+num);
 1403:         newdivid = document.getElementById(name+'_divchg'+item+'_'+num);
 1404:         radioname = form.elements[name+'_change'+item+'_'+num];
 1405:         if (radioname) {
 1406:             if (radioname.length > 0) {
 1407:                 var setvis;
 1408:                 for (var i=0; i<radioname.length; i++) {
 1409:                     if (radioname[i].checked == true) {
 1410:                         if (radioname[i].value == 1) {
 1411:                             newdivid.style.display = 'inline-block';
 1412:                             currdivid.style.display = 'none';
 1413:                             setvis = 1;
 1414:                         }
 1415:                         break;
 1416:                     }
 1417:                 }
 1418:                 if (!setvis) {
 1419:                     newdivid.style.display = 'none';
 1420:                     currdivid.style.display = 'inline-block';
 1421:                 }
 1422:             }
 1423:         }
 1424:     }
 1425:     return;
 1426: }
 1427: 
 1428: function uncheckLinkProtMakeVis(name,item,num) {
 1429:     if (document.getElementById(name+'_'+item+'_'+num)) {
 1430:         var currtype = document.getElementById(name+'_'+item+'_'+num).type;
 1431:         if (currtype.toLowerCase() == 'checkbox') {
 1432:             document.getElementById(name+'_'+item+'_'+num).checked = false;
 1433:         }
 1434:     }
 1435:     return;
 1436: }
 1437: ENDSCRIPT
 1438: 
 1439: }
 1440: 
 1441: 1;

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