--- loncom/interface/courseprefs.pm	2022/03/12 21:24:17	1.106
+++ loncom/interface/courseprefs.pm	2022/04/06 14:35:27	1.111
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Handler to set configuration settings for a course
 #
-# $Id: courseprefs.pm,v 1.106 2022/03/12 21:24:17 raeburn Exp $
+# $Id: courseprefs.pm,v 1.111 2022/04/06 14:35:27 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -601,10 +601,14 @@ sub handler {
     );
     if (($phase eq 'process') && ($parm_permission->{'process'})) {
         my @allitems = &get_allitems(%prefs);
-        &Apache::lonconfigsettings::make_changes($r,$cdom,$phase,$context,
-                                                 \@prefs_order,\%prefs,\%values,
-                                                  $cnum,undef,\@allitems,
-                                                  'coursepref',$parm_permission);
+        my $result = &Apache::lonconfigsettings::make_changes($r,$cdom,$phase,$context,
+                                                              \@prefs_order,\%prefs,\%values,
+                                                              $cnum,undef,\@allitems,
+                                                              'coursepref',$parm_permission);
+        if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
+            $r->rflush();
+            &devalidate_remote_courseprefs($cdom,$cnum,$result);
+        }
     } elsif (($phase eq 'display') && ($parm_permission->{'display'})) {
         my $noedit;
         if (ref($parm_permission) eq 'HASH') {
@@ -812,7 +816,7 @@ sub print_config_box {
 }
 
 sub process_changes {
-    my ($cdom,$cnum,$action,$values,$item,$changes,$allitems,$disallowed,$crstype) = @_;
+    my ($cdom,$cnum,$action,$values,$item,$changes,$allitems,$disallowed,$crstype,$lastactref) = @_;
     my (%newvalues,$errors);
     if (ref($item) eq 'HASH') {
         if (ref($changes) eq 'HASH') {
@@ -981,7 +985,7 @@ sub process_changes {
                     }
                 } elsif ($action eq 'linkprot') {
                     if (ref($values) eq 'HASH') {
-                        $errors = &process_linkprot($cdom,$cnum,$values->{$action},$changes,'course');
+                        $errors = &process_linkprot($cdom,$cnum,$values->{$action},$changes,'course',$lastactref);
                     }
                 } else {
                     foreach my $entry (@ordered) {
@@ -1477,7 +1481,7 @@ sub process_changes {
 }
 
 sub process_linkprot {
-    my ($cdom,$cnum,$values,$changes,$context) = @_;
+    my ($cdom,$cnum,$values,$changes,$context,$lastactref) = @_;
     my ($home,$dest,$ltiauth,$privkey,$privnum,$cipher,$errors,%linkprot);
     if (ref($values) eq 'HASH') {
         foreach my $id (keys(%{$values})) {
@@ -1544,7 +1548,7 @@ sub process_linkprot {
     if (ref($values) eq 'HASH') {
         my @todelete = &Apache::loncommon::get_env_multiple('form.linkprot_del');
         my $maxnum = $env{'form.linkprot_maxnum'};
-        for (my $i=0; $i<=$maxnum; $i++) {
+        for (my $i=0; $i<$maxnum; $i++) {
             my $itemid = $env{'form.linkprot_id_'.$i};
             $itemid =~ s/\D+//g;
             if ($itemid) {
@@ -1656,6 +1660,7 @@ sub process_linkprot {
                     }
                 } else {
                     $linkprot{$itemid}{'secret'} = $current{'secret'};
+                    $linkprot{$itemid}{'cipher'} = $current{'cipher'};
                 }
             } elsif ($env{$secretitem} ne '') {
                 if ($privnum && $cipher) {
@@ -1672,6 +1677,9 @@ sub process_linkprot {
         foreach my $entry (keys(%haschanges)) {
             $changes->{$entry} = $linkprot{$entry};
         }
+        if (ref($lastactref) eq 'HASH') {
+            $lastactref->{'courselti'} = 1;
+        }
     }
     return $errors;
 }
@@ -2205,6 +2213,9 @@ sub store_linkprot {
                     if (exists($oldlinkprot->{$id}{'usable'})) {
                         $changes->{$id}->{'usable'} = 1;
                     }
+                    if (exists($oldlinkprot->{$id}{'cipher'})) {
+                        $changes->{$id}->{'cipher'} = $oldlinkprot->{$id}{'cipher'};
+                    }
                 }
             }
         }
@@ -5763,6 +5774,7 @@ sub print_linkprotection {
                     } else {
                         $datatable .= '<span class="LC_nobreak">'.&mt('Secret required').' - '.$switchmessage.'</span>'."\n";
                     }
+                    $datatable .= '<input type="hidden" name="linkprot_id_'.$i.'" value="'.$num.'" />';
                 } else {
                     if ($values{'usable'} ne '') {
                         $datatable .= '<div id="linkprot_divcurrsecret_'.$i.'" style="display:inline-block" /><span class="LC_nobreak">'.
@@ -6550,6 +6562,30 @@ sub change_clone {
             }
         }
     }
+    return;
+}
+
+sub devalidate_remote_courseprefs {
+    my ($cdom,$cnum,$cachekeys) = @_;
+    return unless (ref($cachekeys) eq 'HASH');
+    my %servers = &Apache::lonnet::internet_dom_servers($cdom);
+    my %thismachine;
+    map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
+    my @posscached = ('courselti');
+    if (keys(%servers)) {
+        foreach my $server (keys(%servers)) {
+            next if ($thismachine{$server});
+            my @cached;
+            foreach my $name (@posscached) {
+                if ($cachekeys->{$name}) {
+                    push(@cached,&escape($name).':'.&escape($cdom.'_'.$cnum));
+                }
+            }
+            if (@cached) {
+                &Apache::lonnet::remote_devalidate_cache($server,\@cached);
+            }
+        }
+    }
     return;
 }