--- loncom/interface/londocs.pm	2022/01/05 00:48:06	1.484.2.93.2.2
+++ loncom/interface/londocs.pm	2023/09/06 13:35:14	1.484.2.93.2.15
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Documents
 #
-# $Id: londocs.pm,v 1.484.2.93.2.2 2022/01/05 00:48:06 raeburn Exp $
+# $Id: londocs.pm,v 1.484.2.93.2.15 2023/09/06 13:35:14 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -50,6 +50,7 @@ use GDBM_File;
 use File::MMagic;
 use Apache::lonlocal;
 use Cwd;
+use UUID::Tiny ':std';
 use LONCAPA qw(:DEFAULT :match);
 
 my $iconpath;
@@ -86,7 +87,7 @@ sub storemap {
 
     if ($map =~ /^default/) {
         $hadchanges=1;
-    } else {
+    } elsif ($contentchg) {
         $suppchanges=1;
     }
     return ($errtext,0);
@@ -178,43 +179,62 @@ sub default_folderpath {
     }
 }
 
-sub validate_folderpath {
-    my ($supplementalflag) = @_;
-    if ($env{'form.folderpath'} ne '') {
-        my @items = split(/\&/,$env{'form.folderpath'});
-        my $badpath;
-        for (my $i=0; $i<@items; $i++) {
-            my $odd = $i%2;
-            if (($odd) && (!$supplementalflag) && ($items[$i] !~ /^[^:]*:(|\d+):(|1):(|1):(|1):(|1)$/)) {
-                $badpath = 1;
-            } elsif ((!$odd) && ($items[$i] !~ /^(default|supplemental)(|_\d+)$/)) {
-                $badpath = 1;
-            }
-            last if ($badpath);
-        }
-        if ($badpath) {
-            delete($env{'form.folderpath'});
-        }
-    }
-    return;
-}
-
-sub validate_suppath {
+sub validate_supppath {
+    my ($coursenum,$coursedom) = @_;
+    my $backto;
     if ($env{'form.supppath'} ne '') {
         my @items = split(/\&/,$env{'form.supppath'});
-        my $badpath;
+        my ($badpath,$got_supp,$supppath,%supphidden,%suppids);
         for (my $i=0; $i<@items; $i++) {
             my $odd = $i%2;
             if ((!$odd) && ($items[$i] !~ /^supplemental(|_\d+)$/)) {
                 $badpath = 1;
+                last;
+            } elsif ($odd) {
+                my $suffix;
+                my $idx = $i-1;
+                if ($items[$i] =~ /^([^:]*)::(|1):::$/) {
+                    $backto .= '&'.$1;
+                } elsif ($items[$idx] eq 'supplemental') {
+                    $backto .= '&'.$items[$i];
+                } else {
+                    $backto .= '&'.$items[$i];
+                    my $is_hidden;
+                    unless ($got_supp) {
+                        my ($supplemental) = &Apache::loncommon::get_supplemental($coursenum,$coursedom);
+                        if (ref($supplemental) eq 'HASH') {
+                            if (ref($supplemental->{'hidden'}) eq 'HASH') {
+                                %supphidden = %{$supplemental->{'hidden'}};
+                            }
+                            if (ref($supplemental->{'ids'}) eq 'HASH') {
+                                %suppids = %{$supplemental->{'ids'}};
+                            }
+                        }
+                        $got_supp = 1;
+                    }
+                    if (ref($suppids{"/uploaded/$coursedom/$coursenum/$items[$idx].sequence"}) eq 'ARRAY') {
+                        my $mapid = $suppids{"/uploaded/$coursedom/$coursenum/$items[$idx].sequence"}->[0];
+                        if ($supphidden{$mapid}) {
+                            $is_hidden = 1;
+                        }
+                    }
+                    $suffix = '::'.$is_hidden.':::';
+                }
+                $supppath .= '&'.$items[$i].$suffix;
+            } else {
+                $supppath .= '&'.$items[$i];
+                $backto .= '&'.$items[$i];
             }
-            last if ($badpath);
         }
         if ($badpath) {
             delete($env{'form.supppath'});
+        } else {
+            $supppath =~ s/^\&//;
+            $backto =~ s/^\&//;
+            $env{'form.supppath'} = $supppath;
         }
     }
-    return;
+    return $backto;
 }
 
 sub dumpcourse {
@@ -695,7 +715,7 @@ sub group_import {
                 $url = $1;
                 my $marker = $2;
                 my $info = $3;
-                my ($toolid,%toolhash,%toolsettings);
+                my ($toolid,$toolprefix,$tooltype,%toolhash,%toolsettings);
                 my @extras = ('linktext','explanation','crslabel','crstitle','crsappend');
                 my @toolinfo = split(/:/,$info);
                 if ($residx) {
@@ -704,97 +724,149 @@ sub group_import {
                 } else {
                     $toolid = shift(@toolinfo);
                 }
+                if ($toolid =~ /^c/) {
+                    $tooltype = 'crs';
+                    $toolprefix = 'c';
+                } else {
+                    $tooltype = 'dom';
+                }
                 $toolid =~ s/\D//g;
                 ($toolhash{'target'},$toolhash{'width'},$toolhash{'height'},
-                 $toolhash{'linktext'},$toolhash{'explanation'},
-                 $toolhash{'crslabel'},$toolhash{'crstitle'},$toolhash{'crsappend'}) = @toolinfo;
+                 $toolhash{'linktext'},$toolhash{'explanation'},$toolhash{'crslabel'},
+                 $toolhash{'crstitle'},$toolhash{'crsappend'},$toolhash{'gradable'}) = @toolinfo;
                 foreach my $item (@extras) {
                     $toolhash{$item} = &unescape($toolhash{$item});
                 }
+                if ($folder =~ /^supplemental/) {
+                    delete($toolhash{'gradable'});
+                } else {
+                    $toolhash{'gradable'} =~ s/\D+//g;
+                }
                 if (ref($ltitoolsref) eq 'HASH') {
-                    my @deleted;
-                    if (ref($ltitoolsref->{$toolid}) eq 'HASH') {
-                        $toolhash{'id'} = $toolid;
-                        if (($toolhash{'target'} eq 'iframe') || ($toolhash{'target'} eq 'tab') ||
-                            ($toolhash{'target'} eq 'window')) {
-                            if ($toolhash{'target'} eq 'window') {
-                                foreach my $item ('width','height') {
-                                    $toolhash{$item} =~ s/^\s+//;
-                                    $toolhash{$item} =~ s/\s+$//;
-                                    if ($toolhash{$item} =~ /\D/) {
-                                        delete($toolhash{$item});
-                                        if ($residx) {
-                                            if ($toolsettings{$item}) {
-                                                push(@deleted,$item);
+                    if (ref($ltitoolsref->{$tooltype}) eq 'HASH') {
+                        if (ref($ltitoolsref->{$tooltype}->{$toolid}) eq 'HASH') {
+                            my %tools = %{$ltitoolsref->{$tooltype}->{$toolid}};
+                            my @deleted;
+                            $toolhash{'id'} = $toolprefix.$toolid;
+                            if (($toolhash{'target'} eq 'iframe') || ($toolhash{'target'} eq 'tab') ||
+                                ($toolhash{'target'} eq 'window')) {
+                                if ($toolhash{'target'} eq 'window') {
+                                    foreach my $item ('width','height') {
+                                        $toolhash{$item} =~ s/^\s+//;
+                                        $toolhash{$item} =~ s/\s+$//;
+                                        if ($toolhash{$item} =~ /\D/) {
+                                            delete($toolhash{$item});
+                                            if ($residx) {
+                                                if ($toolsettings{$item}) {
+                                                    push(@deleted,$item);
+                                                }
                                             }
                                         }
                                     }
                                 }
-                            }
-                        } elsif ($residx) {
-                            $toolhash{'target'} = $toolsettings{'target'};
-                            if ($toolhash{'target'} eq 'window') {
-                                foreach my $item ('width','height') {
-                                    $toolhash{$item} = $toolsettings{$item};
+                            } elsif ($residx) {
+                                $toolhash{'target'} = $toolsettings{'target'};
+                                if ($toolhash{'target'} eq 'window') {
+                                    foreach my $item ('width','height') {
+                                        $toolhash{$item} = $toolsettings{$item};
+                                    }
+                                }
+                            } elsif (ref($tools{'display'}) eq 'HASH') {
+                                $toolhash{'target'} = $tools{'display'}{'target'};
+                                if ($toolhash{'target'} eq 'window') {
+                                    $toolhash{'width'} = $tools{'display'}{'width'};
+                                    $toolhash{'height'} = $tools{'display'}{'height'};
                                 }
                             }
-                        } elsif (ref($ltitoolsref->{$toolid}->{'display'}) eq 'HASH') {
-                            $toolhash{'target'} = $ltitoolsref->{$toolid}->{'display'}->{'target'};
-                            if ($toolhash{'target'} eq 'window') {
-                                $toolhash{'width'} = $ltitoolsref->{$toolid}->{'display'}->{'width'};
-                                $toolhash{'height'} = $ltitoolsref->{$toolid}->{'display'}->{'height'};
-                            }
-                        }
-                        if ($toolhash{'target'} eq 'iframe') {
-                            foreach my $item ('width','height','linktext','explanation') {
-                                delete($toolhash{$item});
-                                if ($residx) {
-                                    if ($toolsettings{$item}) {
-                                        push(@deleted,$item);
+                            if ($toolhash{'target'} eq 'iframe') {
+                                foreach my $item ('width','height','linktext','explanation') {
+                                    delete($toolhash{$item});
+                                    if ($residx) {
+                                        if ($toolsettings{$item}) {
+                                            push(@deleted,$item);
+                                        }
                                     }
                                 }
-                            }
-                        } elsif ($toolhash{'target'} eq 'tab') {
-                            foreach my $item ('width','height') {
-                                delete($toolhash{$item});
-                                if ($residx) {
-                                    if ($toolsettings{$item}) {
-                                        push(@deleted,$item);
+                            } elsif ($toolhash{'target'} eq 'tab') {
+                                foreach my $item ('width','height') {
+                                    delete($toolhash{$item});
+                                    if ($residx) {
+                                        if ($toolsettings{$item}) {
+                                            push(@deleted,$item);
+                                        }
                                     }
                                 }
                             }
-                        }
-                        if (ref($ltitoolsref->{$toolid}->{'crsconf'}) eq 'HASH') {
-                            foreach my $item ('label','title','linktext','explanation') {
-                                my $crsitem;
-                                if (($item eq 'label') || ($item eq 'title')) {
-                                    $crsitem = 'crs'.$item;
-                                } else {
-                                    $crsitem = $item;
-                                }
-                                if ($ltitoolsref->{$toolid}->{'crsconf'}->{$item}) {
-                                    $toolhash{$crsitem} =~ s/^\s+//;
-                                    $toolhash{$crsitem} =~ s/\s+$//;
-                                    if ($toolhash{$crsitem} eq '') {
+                            if (ref($tools{'crsconf'}) eq 'HASH') {
+                                foreach my $item ('label','title','linktext','explanation') {
+                                    my $crsitem;
+                                    if (($item eq 'label') || ($item eq 'title')) {
+                                        $crsitem = 'crs'.$item;
+                                    } else {
+                                        $crsitem = $item;
+                                    }
+                                    if ($tools{'crsconf'}{$item}) {
+                                        $toolhash{$crsitem} =~ s/^\s+//;
+                                        $toolhash{$crsitem} =~ s/\s+$//;
+                                        if ($toolhash{$crsitem} eq '') {
+                                            delete($toolhash{$crsitem});
+                                        }
+                                    } else {
                                         delete($toolhash{$crsitem});
                                     }
-                                } else {
-                                    delete($toolhash{$crsitem});
+                                    if (($residx) && (exists($toolsettings{$crsitem}))) {
+                                        unless (exists($toolhash{$crsitem})) {
+                                            push(@deleted,$crsitem);
+                                        }
+                                    }
+                                }
+                            }
+                            if ($toolhash{'passback'}) {
+                                my $gradesecret = UUID::Tiny::create_uuid_as_string(UUID_V4);
+                                $toolhash{'gradesecret'} = $gradesecret;
+                                $toolhash{'gradesecretdate'} = time;
+                            }
+                            if ($toolhash{'roster'}) {
+                                my $rostersecret = UUID::Tiny::create_uuid_as_string(UUID_V4);
+                                $toolhash{'rostersecret'} = $rostersecret;
+                                $toolhash{'rostersecretdate'} = time;
+                            }
+                            my $changegradable;
+                            if (($residx) && ($folder =~ /^default/)) {
+                                if ($toolsettings{'gradable'}) {
+                                    unless (($toolhash{'gradable'}) || (defined($LONCAPA::map::zombies[$residx]))) {
+                                        push(@deleted,'gradable');
+                                        $changegradable = 1;
+                                    }
+                                } elsif ($toolhash{'gradable'}) {
+                                    $changegradable = 1;
                                 }
-                                if (($residx) && (exists($toolsettings{$crsitem}))) {
-                                    unless (exists($toolhash{$crsitem})) {
-                                        push(@deleted,$crsitem);
+                                if (($caller eq 'londocs') && (defined($LONCAPA::map::zombies[$residx]))) {
+                                    $changegradable = 1;
+                                    if ($toolsettings{'gradable'}) {
+                                        $toolhash{'gradable'} = 1;
                                     }
                                 }
                             }
-                        }
-                        my $putres = &Apache::lonnet::put('exttool_'.$marker,\%toolhash,$coursedom,$coursenum);
-                        if ($putres eq 'ok') {
-                            if (@deleted) {
-                                &Apache::lonnet::del('exttool_'.$marker,\@deleted,$coursedom,$coursenum);
+                            my $putres = &Apache::lonnet::put('exttool_'.$marker,\%toolhash,$coursedom,$coursenum);
+                            if ($putres eq 'ok') {
+                                if (@deleted) {
+                                    &Apache::lonnet::del('exttool_'.$marker,\@deleted,$coursedom,$coursenum);
+                                }
+                                if (($changegradable) && ($folder =~ /^default/)) {
+                                    my $val;
+                                    if ($toolhash{'gradable'}) {
+                                        $val = 'yes';
+                                    } else {
+                                        $val = 'no';
+                                    }
+                                    &LONCAPA::map::storeparameter($residx,'parameter_0_gradable',$val,
+                                                                  'string_yesno');
+                                    &remember_parms($residx,'gradable','set',$val);
+                                }
+                            } else {
+                                return (&mt('Failed to save update to external tool.'),1);
                             }
-                        } else {
-                            return (&mt('Failed to save update to external tool.'),1);
                         }
                     }
                 }
@@ -907,7 +979,6 @@ END
         &storemap($coursenum, $coursedom, $folder.'.'.$container,1);
     unless ($fatal) {
         if ($folder =~ /^supplemental/) {
-            &Apache::lonnet::get_numsuppfiles($coursenum,$coursedom,1);
             my ($errtext,$fatal) = &mapread($coursenum,$coursedom,
                                             $folder.'.'.$container);
         }
@@ -984,7 +1055,7 @@ sub docs_change_log {
              '// <![CDATA['."\n".
              &Apache::loncommon::display_filter_js('docslog')."\n".
              &editing_js($env{'user.domain'},$env{'user.name'},$supplementalflag,
-                         $coursedom,$coursenum,'',$canedit,'',\$navmap)."\n".
+                         $coursedom,$coursenum,'','',$canedit,'',\$navmap)."\n".
              &history_tab_js()."\n".
              &Apache::lonratedt::editscript('simple')."\n".
              '// ]]>'."\n".
@@ -1034,6 +1105,7 @@ sub docs_change_log {
 	    'encrypturl'     => 'URL hidden',
 	    'randompick'     => 'Randomly pick',
 	    'randomorder'    => 'Randomly ordered',
+            'gradable'       => 'Grade can be assigned to External Tool',
 	    'set'            => 'set to',
 	    'del'            => 'deleted');
     my $filter = &Apache::loncommon::display_filter('docslog')."\n".
@@ -1140,8 +1212,14 @@ sub docs_change_log {
 	}
 	$r->print('</ul>');
 	if ($docslog{$id}{'logentry'}{'parameter_res'}) {
-	    $r->print(&LONCAPA::map::qtescape((split(/\:/,$docslog{$id}{'logentry'}{'parameter_res'}))[0]).':<ul>');
-	    foreach my $parameter ('randompick','hiddenresource','encrypturl','randomorder') {
+            my ($title,$url) = split(/\:/,$docslog{$id}{'logentry'}{'parameter_res'},3);
+            if ($title eq '') {
+                ($title) = ($url =~ m{/([^/]+)$});
+            } elsif ($is_supp) {
+                $title = &Apache::loncommon::parse_supplemental_title($title);
+            }
+            $r->print(&LONCAPA::map::qtescape($title).':<ul>');
+	    foreach my $parameter ('randompick','hiddenresource','encrypturl','randomorder','gradable') {
 		if ($docslog{$id}{'logentry'}{'parameter_action_'.$parameter}) {
 # FIXME: internationalization seems wrong here
 		    $r->print('<li>'.
@@ -1338,7 +1416,7 @@ sub print_paste_buffer {
     }
 
     my @currpaste = split(/,/,$env{'docs.markedcopies'});
-    my ($pasteitems,@pasteable);
+    my ($pasteitems,@pasteable,$same_institution,$checkedsameinst);
     my $clipboardcount = 0;
 
 # Construct identifiers for current contents of user's paste buffer
@@ -1351,11 +1429,13 @@ sub print_paste_buffer {
             ($url ne '')) {
             $clipboardcount ++;
             my ($is_external,$othercourse,$fromsupp,$is_uploaded_map,$parent,
-                $canpaste,$nopaste,$othercrs,$areachange,$is_exttool);
+                $canpaste,$nopaste,$othercrs,$areachange,$is_exttool,$toolcdom,
+                $toolcnum,$marker);
             my $extension = (split(/\./,$env{'docs.markedcopy_url_'.$suffix}))[-1];
             if ($url =~ m{^(?:/adm/wrapper/ext|(?:http|https)(?:&colon;|:))//} ) {
                 $is_external = 1;
-            } elsif ($url =~ m{^/adm/$match_domain/$match_courseid/\d+/ext\.tool$}) {
+            } elsif ($url =~ m{^/adm/($match_domain)/($match_courseid)/(\d+)/ext\.tool$}) {
+                ($toolcdom,$toolcnum,$marker) = ($1,$2,$3);
                 $is_exttool = 1;
             }
             if ($folder =~ /^supplemental/) {
@@ -1393,11 +1473,42 @@ sub print_paste_buffer {
                     if ($cid ne $env{'request.course.id'}) {
                         my ($srcdom,$srcnum) = split(/_/,$cid);
                         if ($env{"user.priv.cm./$srcdom/$srcnum"} =~ /\Q:mdc&F\E/) {
-                            if (($is_exttool) && ($srcdom ne $coursedom)) {
-                                $canpaste = 0;
-                                $nopaste = &mt('Paste from another domain unavailable.');
-                            } else {
-                                $othercrs = '<br />'.&mt('(from another course)');
+                            if ($is_exttool) {
+                                if ($toolcdom ne $coursedom) {
+                                    $canpaste = 0;
+                                    $nopaste = &mt('Paste from another domain unavailable.');
+                                } elsif ($toolcnum ne $coursenum) {
+                                    my %toolsettings =
+                                        &Apache::lonnet::dump('exttool_'.$marker,$toolcdom,$toolcnum);
+                                    my %tooltypes = &Apache::loncommon::usable_exttools();
+                                    if ((($toolsettings{'id'} =~ /^c\d+$/) && (!$tooltypes{'crs'})) ||
+                                        (($toolsettings{'id'} =~ /^\d+$/) && (!$tooltypes{'dom'}))) {
+                                        $canpaste = 0;
+                                        $nopaste = &mt('Paste from another course unavailable.');
+                                    } elsif ($toolsettings{'id'} =~ /^c\d+$/) {
+                                        unless ($checkedsameinst) {
+                                            my $primary_id = &Apache::lonnet::domain($coursedom,'primary');
+                                            my $intdom = &Apache::lonnet::internet_dom($primary_id);
+                                            if ($intdom ne '') {
+                                                my $internet_names =
+                                                    &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
+                                                if (ref($internet_names) eq 'ARRAY') {
+                                                    if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
+                                                        $same_institution = 1;
+                                                    }
+                                                }
+                                            }
+                                            $checkedsameinst = 1;
+                                        }
+                                        if ($same_institution) {
+                                            $othercrs = '<br />'.&mt('(from another course)');
+                                        } else {
+                                            $nopaste = &mt('Paste from another course unavailable.');
+                                        }
+                                    } else {
+                                        $othercrs = '<br />'.&mt('(from another course)');
+                                    }
+                                }
                             }
                         } else {
                             $canpaste = 0;
@@ -1446,7 +1557,7 @@ sub print_paste_buffer {
             }
             $pasteitems .= '<label><input type="checkbox" name="pasting" id="pasting_'.$suffix.'" value="'.$suffix.'" '.$onclick.'/>'.$buffer.'</label>';
             if ($nopaste) {
-                 $pasteitems .= $nopaste;   
+                 $pasteitems .= ' <span class="LC_cusr_emph">'.$nopaste.'</span>';   
             } else {
                 if ($othercrs) {
                     $pasteitems .= $othercrs;
@@ -1696,9 +1807,12 @@ sub do_paste_from_buffer {
         return();
     }
 
-    my (%msgs,%before,%after,@dopaste,%is_map,%notinsupp,%notincrs,%notindom,%duplicate,
-        %prefixchg,%srcdom,%srcnum,%srcmapidx,%marktomove,$save_err,$lockerrors,$allresult);
-
+    my (%msgs,%before,%after,@dopaste,%is_map,%notinsupp,%notincrs,%notindom,
+        %othcrstool,%duplicate,%prefixchg,%srcdom,%srcnum,%srcmapidx,
+        %marktomove,$save_err,$lockerrors,$allresult,%currcrsltitools,
+        %currltititles,$currltimax,$gotcrsltitools);
+    $currltimax = 0;
+    $gotcrsltitools = 0;
     foreach my $suffix (@topaste) {
         my $url=&LONCAPA::map::qtescape($env{'docs.markedcopy_url_'.$suffix});
         my $cid=&LONCAPA::map::qtescape($env{'docs.markedcopy_crs_'.$suffix});
@@ -1724,7 +1838,9 @@ sub do_paste_from_buffer {
             }
             $srcdom{$suffix} = $srcd;
             $srcnum{$suffix} = $srcn;
-        } elsif ($url =~ m{^/adm/$match_domain/$match_courseid/\d+/ext\.tool$}) {
+        } elsif (($url =~ m{^/res/lib/templates/\w+\.problem$}) ||
+                 ($url =~ m{^/adm/$match_domain/$match_username/\d+/(bulletinboard|smppg)$}) ||
+                 ($url =~ m{^/adm/$match_domain/$match_courseid/\d+/ext\.tool$})) {
             my ($srcd,$srcn) = split(/_/,$cid);
 # When paste buffer was populated using an active role in a different course
 # check for mdc privilege in the course from which the resource was pasted
@@ -1735,22 +1851,39 @@ sub do_paste_from_buffer {
                 }
             }
 # When buffer was populated using an active role in a different course
-# disallow pasting of External Tool if course is in a different domain.
-            if ($srcd ne $coursedom) {
-                $notindom{$suffix} = 1;
-                next;
-            }
-            $srcdom{$suffix} = $srcd;
-            $srcnum{$suffix} = $srcn;
-        } elsif (($url =~ m{^/res/lib/templates/\w+\.problem$}) ||
-                 ($url =~ m{^/adm/$match_domain/$match_username/\d+/(bulletinboard|smppg)$})) {
-            my ($srcd,$srcn) = split(/_/,$cid);
-# When paste buffer was populated using an active role in a different course
-# check for mdc privilege in the course from which the resource was pasted
-            if (($srcd ne $coursedom) || ($srcn ne $coursenum)) {
-                unless ($env{"user.priv.cm./$srcd/$srcn"} =~ /\Q:mdc&F\E/) {
-                    $notincrs{$suffix} = 1;
+# disallow pasting of External Tool if course is in a different domain,
+# or if External Tool use is not permitted in this course.
+            if ($url =~ m{^/adm/($match_domain)/($match_courseid)/(\d+)/ext\.tool$}) {
+                my ($toolcdom,$toolcnum,$marker) = ($1,$2,$3);
+                if ($toolcdom ne $coursedom) {
+                    $notindom{$suffix} = 1;
                     next;
+                } elsif ($toolcnum ne $coursenum) {
+                    my %toolsettings =
+                        &Apache::lonnet::dump('exttool_'.$marker,$toolcdom,$toolcnum);
+                    my %tooltypes = &Apache::loncommon::usable_exttools();
+                    if ((($toolsettings{'id'} =~ /^c\d+$/) && (!$tooltypes{'crs'})) ||
+                        (($toolsettings{'id'} =~ /^\d+$/) && (!$tooltypes{'dom'}))) {
+                        $othcrstool{$suffix} = 1;
+                        next;
+                    }
+                    if ($toolsettings{'id'} =~ /^c\d+$/) {
+                        unless ($gotcrsltitools) {
+                            %currcrsltitools =
+                                &Apache::lonnet::get_course_lti($coursenum,$coursedom,'consumer');
+                            foreach my $item (sort(keys(%currcrsltitools))) {
+                                if (ref($currcrsltitools{$item}) eq 'HASH') {
+                                    $currltimax ++;
+                                    if (ref($currltititles{$currcrsltitools{$item}{'title'}}) eq 'ARRAY') {
+                                        push(@{$currltititles{$currcrsltitools{$item}{'title'}}},$item);
+                                    } else {
+                                        $currltititles{$currcrsltitools{$item}{'title'}} = [$item];
+                                    }
+                                }
+                            }
+                            $gotcrsltitools = 1;
+                        }
+                    }
                 }
             }
             $srcdom{$suffix} = $srcd;
@@ -1761,7 +1894,6 @@ sub do_paste_from_buffer {
         if ($url=~/\.(page|sequence)$/) {
             $is_map{$suffix} = 1; 
         }
-
         if ($url =~ m{^/uploaded/$match_domain/$match_courseid/([^/]+)}) {
             my $oldprefix = $1;
 # When pasting content from Main Content to Supplemental Content and vice versa 
@@ -1806,7 +1938,8 @@ sub do_paste_from_buffer {
     %msgs = &Apache::lonlocal::texthash (
                 notinsupp => 'Paste failed: content type is not supported within Supplemental Content',
                 notincrs  => 'Paste failed: Item is from a different course which you do not have rights to edit.',
-                notindom  => 'Paste failed: Item is an external tool from a course in a different donain.',
+                notindom  => 'Paste failed: Item is an external tool from a course in a different domain.',
+                othcrstool => 'Paste failed: Item is an external tool from a different course, for which use is not allowed in this course.',
                 duplicate => 'Paste failed: only one instance of a particular published sequence or page is allowed within each course.',
             );
 
@@ -1835,7 +1968,9 @@ sub do_paste_from_buffer {
 # Retrieve information about all course maps in main content area 
 
     my $allmaps = {};
-    my (@toclear,%mapurls,%lockerrs,%msgerrs,%results,$donechk);
+    my (@toclear,%mapurls,%lockerrs,%msgerrs,%results,$donechk,
+        @updatetoolsenc,$updatetoolscache,$checkedsameinst,
+        $same_institution);
 
 # Loop over the items to paste
     foreach my $suffix (@dopaste) {
@@ -1924,17 +2059,37 @@ sub do_paste_from_buffer {
                          cdom => $coursedom,
                          cnum => $coursenum,
                        );
+            if ($prefix eq 'ext.tool') {
+                if ($prefixchg{$suffix} eq 'docstosupp') {
+                    $info{'delgradable'} = 1;
+                }
+            }
             if (($srcdom{$suffix} =~ /^$match_domain$/) && ($srcnum{$suffix} =~ /^$match_courseid$/)) {
                 unless (($srcdom{$suffix} eq $coursedom) && ($srcnum{$suffix} eq $coursenum)) {
                     $fromothercrs = 1;
                     $info{'cdom'} = $srcdom{$suffix};
                     $info{'cnum'} = $srcnum{$suffix};
+                    unless ($checkedsameinst) {
+                        my $primary_id = &Apache::lonnet::domain($coursedom,'primary');
+                        my $intdom = &Apache::lonnet::internet_dom($primary_id);
+                        if ($intdom ne '') {
+                            my $internet_names =
+                                &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
+                            if (ref($internet_names) eq 'ARRAY') {
+                                if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
+                                    $same_institution = 1;
+                                }
+                            }
+                        }
+                        $checkedsameinst = 1;
+                    }
                 }
             }
             unless (($env{'form.docs.markedcopy_options_'.$suffix} eq 'move') && (!$fromothercrs)) {
                 my (%lockerr,$msg); 
                 my ($newurl,$result,$errtext) =
-                    &dbcopy(\%info,$coursedom,$coursenum,\%lockerr);
+                    &dbcopy(\%info,$coursedom,$coursenum,\%lockerr,\%currltititles,
+                            \$currltimax,\@updatetoolsenc,\$updatetoolscache,$same_institution);
                 if ($result eq 'ok') {
                     $url = $newurl;
                     $title=&mt('Copy of').' '.$title;
@@ -2015,6 +2170,18 @@ sub do_paste_from_buffer {
                     &copy_templated_files($url,$srcdom{$suffix},$srcnum{$suffix},$srcmapidx{$suffix},
                                           $coursedom,$coursenum,$template,$newidx,"$folder.$container");
                 }
+            } elsif ($url =~ /ext\.tool$/) {
+                if (($newidx) && ($folder=~/^default/)) {
+                    my $marker = (split(m{/},$url))[4];
+                    my %toolsettings = &Apache::lonnet::dump('exttool_'.$marker,$coursedom,$coursenum);
+                    my $val = 'no';
+                    if ($toolsettings{'gradable'}) {
+                        $val = 'yes';
+                    }
+                    &LONCAPA::map::storeparameter($newidx,'parameter_0_gradable',$val,
+                                                  'string_yesno');
+                    &remember_parms($newidx,'gradable','set',$val);
+                }
             }
             $LONCAPA::map::resources[$newidx]=$title.':'.&LONCAPA::map::qtunescape($url).
                                               ':'.$ext.':normal:res';
@@ -2091,6 +2258,10 @@ sub do_paste_from_buffer {
             }
         }
     }
+    if (($updatetoolscache) || (@updatetoolsenc)) {
+        &update_ltitools_caches($coursedom,$coursenum,$updatetoolscache,
+                                \@updatetoolsenc);
+    }
     &clear_from_buffer(\@toclear,\@currpaste);
     my $msgsarray;
     foreach my $suffix (keys(%msgs)) {
@@ -2139,6 +2310,30 @@ sub clear_from_buffer {
     return $numdel;
 }
 
+sub update_ltitools_caches {
+    my ($coursedom,$coursenum,$updatetoolscache,$updatetoolsenc) = @_;
+    my $hashid=$coursedom.'_'.$coursenum;
+    if ($updatetoolscache) {
+        &Apache::lonnet::devalidate_cache_new('courseltitools',$hashid);
+    }
+    if ((ref($updatetoolsenc) eq 'ARRAY') &&
+        (@{$updatetoolsenc})) {
+        my @ids=&Apache::lonnet::current_machine_ids();
+        my $updatedone;
+        foreach my $lonhost (@{$updatetoolsenc}) {
+            if (grep(/^\Q$lonhost\E$/,@ids)) {
+                unless ($updatedone) {
+                    &Apache::lonnet::devalidate_cache_new('crsltitoolsenc',$hashid);
+                }
+                $updatedone = 1;
+            } else {
+                &Apache::lonnet::remote_devalidate_cache($lonhost,["crsltitoolsenc:$hashid"]);
+            }
+        }
+    }
+    return;
+}
+
 sub get_newmap_url {
     my ($url,$folder,$prefixchg,$coursedom,$coursenum,$srcdom,$srcnum,
         $titleref,$allmaps,$newurls) = @_;
@@ -2200,7 +2395,8 @@ sub get_newmap_url {
 }
 
 sub dbcopy {
-    my ($dbref,$coursedom,$coursenum,$lockerrorsref) = @_;
+    my ($dbref,$coursedom,$coursenum,$lockerrorsref,$currltititles,
+        $currltimax,$updatetoolsenc,$updatetoolscache,$same_institution) = @_;
     my ($url,$result,$errtext);
     if (ref($dbref) eq 'HASH') {
         $url = $dbref->{'src'};
@@ -2244,6 +2440,117 @@ sub dbcopy {
                     my %contents=&Apache::lonnet::dump($db_name,
                                                        $dbref->{'cdom'},
                                                        $dbref->{'cnum'});
+                    my ($toolcopyerror,$toolpassback,$toolroster,%toolinfo,$oldtoolid,$defincrs);
+                    if ($url eq '/adm/'.$dbref->{'cdom'}.'/'.$dbref->{'cnum'}."/$marker/ext.tool") {
+                        if ($contents{'id'} =~ /^(|c)(\d+)$/) {
+                            $oldtoolid = $2;
+                            if ($1 eq 'c') {
+                                $defincrs = 1;
+                                %toolinfo =
+                                    &Apache::lonnet::get('ltitools',[$oldtoolid],$dbref->{'cdom'},$dbref->{'cnum'});
+                            } else {
+                                %toolinfo= &Apache::lonnet::get_domain_lti($dbref->{'cdom'},'consumer');
+                            }
+                            if (ref($toolinfo{$oldtoolid}) eq 'HASH') {
+                                if ($toolinfo{$oldtoolid}{'passback'}) {
+                                    $toolpassback = 1;
+                                }
+                                if ($toolinfo{$oldtoolid}{'roster'}) {
+                                    $toolroster = 1;
+                                }
+                            } else {
+                                $toolcopyerror = 1;
+                                $errtext = &mt('Could not retrieve original settings for pasted external tool.');
+                            }
+                        }
+                        unless (($dbref->{'cnum'} eq $coursenum) && ($dbref->{'cdom'} eq $coursedom)) {
+                            $url = "/adm/$coursedom/$coursenum/$marker/ext.tool";
+                            if ($contents{'crstitle'} ne '') {
+                                $contents{'crstitle'} = $env{'course.'.$coursedom.'_'.$coursenum.'.description'};
+                            }
+                            if (($defincrs) && (!$toolcopyerror)) {
+                                my %newtool;
+                                my $oldcdom = $dbref->{'cdom'};
+                                my $oldcnum = $dbref->{'cnum'};
+                                my $title = $toolinfo{$oldtoolid}{'title'};
+                                if (ref($currltititles) eq 'HASH') {
+                                    if (exists($currltititles->{$title})) {
+                                        $title .= ' (copied from another course)';
+                                    }
+                                }
+                                my ($newid,$iderror) =
+                                    &Apache::lonnet::get_ltitools_id('course',$coursedom,$coursenum,$title);
+                                if ($newid =~ /^\d+$/) {
+                                    %{$newtool{$newid}} = %{$toolinfo{$oldtoolid}};
+                                    $newtool{$newid}{'title'} = $title;
+                                    if (ref($currltimax)) {
+                                        $newtool{$newid}{'order'} = $$currltimax;
+                                    }
+                                    if ($newtool{$newid}{'image'} =~ m{^\Q/uploaded/$oldcdom/$oldcnum/toollogo/$oldtoolid/\E([^/]+)$}) {
+                                        my $fname = $1;
+                                        my $content = &Apache::lonnet::getfile($newtool{$newid}{'image'});
+                                        if ($content eq '-1') {
+                                            delete($newtool{$newid}{'image'});
+                                        } else {
+                                            $env{'form.'.$suffix.'.image'} = $content;
+                                            my $newlogo =
+                                                &Apache::lonnet::finishuserfileupload($coursenum,$coursedom,$suffix.'.image',"toollogo/$newid/$fname");
+                                            delete($env{'form.'.$suffix.'.image'});
+                                            if ($newlogo =~ m{^/uploaded/}) {
+                                                $newtool{$newid}{'image'} = $newlogo;
+                                            } else {
+                                                delete($newtool{$newid}{'image'});
+                                            }
+                                        }
+                                    }
+                                    my $newusable;
+                                    if ($same_institution) {
+                                        my %oldtoolsenc = &Apache::lonnet::eget('nohist_toolsenc',[$oldtoolid],$oldcdom,$oldcnum);
+                                        if (ref($oldtoolsenc{$oldtoolid}) eq 'HASH') {
+                                            my %newtoolsenc;
+                                            %{$newtoolsenc{$newid}} = %{$oldtoolsenc{$oldtoolid}};
+                                            my $putres = &Apache::lonnet::put('nohist_toolsenc',\%newtoolsenc,$coursedom,$coursenum,1);
+                                            if ($putres eq 'ok') {
+                                                if (ref($updatetoolsenc) eq 'ARRAY') {
+                                                    my $newhome = &Apache::lonnet::homeserver($coursenum,$coursedom);
+                                                    unless (grep(/^\Q$newhome\E$/,@{$updatetoolsenc})) {
+                                                        push(@{$updatetoolsenc},$newhome);
+                                                    }
+                                                }
+                                                $newusable = 1;
+                                            }
+                                        }
+                                    }
+                                    if ($newtool{$newid}{'usable'}) {
+                                        unless ($newusable) {
+                                            delete($newtool{$newid}{'usable'});
+                                        }
+                                    }
+                                    my $putres = &Apache::lonnet::put('ltitools',\%newtool,$coursedom,$coursenum);
+                                    if ($putres eq 'ok') {
+                                        $contents{'id'} = "c$newid";
+                                        if (ref($updatetoolscache)) {
+                                            $$updatetoolscache ++;
+                                        }
+                                        if (ref($currltititles->{$title}) eq 'ARRAY') {
+                                            push(@{$currltititles->{$title}},$newid);
+                                        } else {
+                                            $currltititles->{$title} = [$newid];
+                                        }
+                                        if (ref($currltimax)) {
+                                            $$currltimax ++;
+                                        }
+                                    } else {
+                                        $toolcopyerror = 1;
+                                        $errtext = &mt('Unable to save external tool definition in Course Settings.');
+                                    }
+                                } else {
+                                    $toolcopyerror = 1;
+                                    $errtext = &mt('Unable to retrieve new tool ID when adding external tool definition to Course Settings.');
+                                }
+                            }
+                        }
+                    }
                     if (exists($contents{'uploaded.photourl'})) {
                         my $photo = $contents{'uploaded.photourl'};
                         my ($subdir,$fname) =
@@ -2263,10 +2570,40 @@ sub dbcopy {
                         }
                     }
                     $db_name =~ s{_\d*$ }{_$suffix}x;
-                    $result=&Apache::lonnet::put($db_name,\%contents,
-                                                 $coursedom,$coursenum);
-                    if ($result eq 'ok') {
-                        $url =~ s{/(\d*)/(smppg|bulletinboard|ext\.tool)$}{/$suffix/$2}x;
+                    if ($prefix eq 'exttool') {
+                        unless ($toolcopyerror) {
+                            foreach my $key ('oldgradesecret','gradesecret','gradesecretdate','oldrostersecret','rostersecret','rostersecretdate') {
+                                if (exists($contents{$key})) {
+                                    delete($contents{$key});
+                                }
+                            }
+                            if ($dbref->{'delgradable'}) {
+                                if (exists($contents{'gradable'})) {
+                                    delete($contents{'gradable'});
+                                }
+                            }
+                            if ($toolpassback) {
+                                if ($contents{'gradable'}) {
+                                    my $gradesecret = UUID::Tiny::create_uuid_as_string(UUID_V4);
+                                    $contents{'gradesecret'} = $gradesecret;
+                                    $contents{'gradesecretdate'} = time;
+                                }
+                            }
+                            if ($toolroster) {
+                                my $rostersecret = UUID::Tiny::create_uuid_as_string(UUID_V4);
+                                $contents{'rostersecret'} = $rostersecret;
+                                $contents{'rostersecretdate'} = time;
+                            }
+                        }
+                    }
+                    if (($prefix eq 'exttool') && ($toolcopyerror)) {
+                        $result = 'error';
+                    } else {
+                        $result=&Apache::lonnet::put($db_name,\%contents,
+                                                     $coursedom,$coursenum);
+                        if ($result eq 'ok') {
+                            $url =~ s{/(\d*)/(smppg|bulletinboard|ext\.tool)$}{/$suffix/$2}x;
+                        }
                     }
                 }
                 if (($freedlock ne 'ok') && (ref($lockerrorsref) eq 'HASH')) {
@@ -2421,7 +2758,23 @@ sub contained_map_check {
             if ($token->[1] eq 'resource') {
                 next if ($token->[2]->{'type'} eq 'zombie');
                 my $ressrc = $token->[2]->{'src'};
-                if ($folder =~ /^supplemental/) {
+                if ($ressrc =~ m{^/adm/($match_domain)/($match_courseid)/(\d+)/ext\.tool$}) {
+                    my ($srcdom,$srcnum,$marker) = ($1,$2,$3);
+                    unless ($srcdom eq $coursedom) {
+                        $removefrommap->{$url}{$token->[2]->{'id'}} = $ressrc;
+                        next;
+                    }
+                    unless ($srcnum eq $coursenum) {
+                        my %toolsettings =
+                            &Apache::lonnet::dump('exttool_'.$marker,$srcdom,$srcnum);
+                        my %tooltypes = &Apache::loncommon::usable_exttools();
+                        if ((($toolsettings{'id'} =~ /^c\d+$/) && (!$tooltypes{'crs'})) ||
+                            (($toolsettings{'id'} =~ /^\d+$/) && (!$tooltypes{'dom'}))) {
+                            $removefrommap->{$url}{$token->[2]->{'id'}} = $ressrc;
+                            next;
+                        }
+                    }
+                } elsif ($folder =~ /^supplemental/) {
                     unless (&supp_pasteable($ressrc)) {
                         $removefrommap->{$url}{$token->[2]->{'id'}} = $ressrc;
                         next;
@@ -2544,16 +2897,38 @@ sub url_paste_fixups {
                         $changed = 1;
                     }
                 }
-            } elsif ($ressrc =~ m{^/adm/($match_domain)/($match_courseid)/.+$}) {
+            } elsif ($ressrc =~ m{^/adm/($match_domain)/($match_courseid)/(.+)$}) {
                 next if ($skip);
                 my $srcdom = $1;
                 my $srcnum = $2;
+                my $rem = $3;
+                my ($is_exttool,$exttoolchg);
+                if ($rem =~ m{\d+/ext\.tool$}) {
+                    $is_exttool = 1;
+                }
                 if (($srcdom ne $cdom) || ($srcnum ne $cnum)) {
                     $rewrites->{$oldurl}{$id} = $ressrc;
                     $dbcopies->{$oldurl}{$id}{'src'} = $ressrc;
                     $dbcopies->{$oldurl}{$id}{'cdom'} = $srcdom;
                     $dbcopies->{$oldurl}{$id}{'cnum'} = $srcnum;
                     $changed = 1;
+                    if ($is_exttool) {
+                        $exttoolchg = 1;
+                    }
+                } elsif (($is_exttool) &&
+                         ($env{'form.docs.markedcopy_options'} ne 'move')) {
+                    $dbcopies->{$oldurl}{$id}{'src'} = $ressrc;
+                    $dbcopies->{$oldurl}{$id}{'cdom'} = $srcdom;
+                    $dbcopies->{$oldurl}{$id}{'cnum'} = $srcnum;
+                    $changed = 1;
+                    $exttoolchg = 1;
+                }
+                if (($is_exttool) && ($prefixchg)) {
+                    if ($oldurl =~ m{^/uploaded/$match_domain/$match_courseid/default}) {
+                        if ($exttoolchg) {
+                            $dbcopies->{$oldurl}{$id}{'delgradable'} = 1;
+                        }
+                    }
                 }
             } elsif ($ressrc =~ m{^/adm/$match_domain/$match_username/\d+/(smppg|bulletinboard)$}) {
                 if (($fromcdom ne $cdom) || ($fromcnum ne $cnum) ||
@@ -2600,7 +2975,9 @@ sub apply_fixups {
         $oldurl,$url,$caller) = @_;
     my (%rewrites,%zombies,%removefrommap,%removeparam,%dbcopies,%retitles,
         %params,%newsubdir,%before,%after,%copies,%docmoves,%mapmoves,@msgs,
-        %resdatacopy,%lockerrors,$lockmsg);
+        %resdatacopy,%lockerrors,$lockmsg,%currcrsltitools,$gotcrsltitools,
+        %currltititles,$currltimax);
+    $currltimax = 0;
     if (ref($updated) eq 'HASH') {
         if (ref($updated->{'rewrites'}) eq 'HASH') {
             %rewrites = %{$updated->{'rewrites'}};
@@ -2716,6 +3093,15 @@ sub apply_fixups {
             $storefn =~ s/^((?:default|supplemental)_)(\d+)/$1$newsubdir{$key}/;
         }
         my $mapcontent = &Apache::lonnet::getfile($key);
+        if (($mapcontent eq '-1') && ($before{'map'} eq 'supplemental') &&
+            ($after{'map'} eq 'default') &&
+            ($key =~ m{^/uploaded/$match_domain/$match_courseid/supplemental_\d+\.sequence$})) {
+            $mapcontent = '<map>'."\n".
+                          '<resource id="1" src="" type="start" />'."\n".
+                          '<link from="1" to="2" index="1" />'."\n".
+                          '<resource id="2" src="" type="finish" />'."\n".
+                          '</map>';
+        }
         if ($mapcontent eq '-1') {
             if (ref($errors) eq 'HASH') {
                 $errors->{$key} = 1;
@@ -2742,6 +3128,7 @@ sub apply_fixups {
                 }
             }
         }
+        my ($updatetoolscache,@updatetoolsenc,$same_institution,$checkedsameinst);
         foreach my $key (keys(%updates)) {
             my (%torewrite,%toretitle,%toremove,%remparam,%currparam,%zombie,%newdb);
             if (ref($rewrites{$key}) eq 'HASH') {
@@ -2762,10 +3149,63 @@ sub apply_fixups {
             if (ref($dbcopies{$key}) eq 'HASH') {
                 foreach my $idx (keys(%{$dbcopies{$key}})) {
                     if (ref($dbcopies{$key}{$idx}) eq 'HASH') {
+                        my $oldurl = $dbcopies{$key}{$idx}{'src'};
+                        my $oldcdom = $dbcopies{$key}{$idx}{'cdom'};
+                        my $oldcnum = $dbcopies{$key}{$idx}{'cnum'};
+                        my $oldmarker;
+                        if ($oldurl =~ m{^\Q/adm/$oldcdom/$oldcnum/\E(\d+)/ext\.tool$}) {
+                            $oldmarker = $1;
+                            unless (($gotcrsltitools) ||
+                                    (($oldcnum eq $cnum) && ($oldcdom eq $cdom))) {
+                                my %oldtoolsettings=&Apache::lonnet::dump('exttool_'.$oldmarker,$oldcdom,$oldcnum);
+                                if ($oldtoolsettings{'id'} =~ /^c\d+$/) {
+                                    unless ($gotcrsltitools) {
+                                        %currcrsltitools =
+                                            &Apache::lonnet::get_course_lti($cnum,$cdom,'consumer');
+                                        foreach my $item (sort(keys(%currcrsltitools))) {
+                                            if (ref($currcrsltitools{$item}) eq 'HASH') {
+                                                $currltimax ++;
+                                                if (ref($currltititles{$currcrsltitools{$item}{'title'}}) eq 'ARRAY') {
+                                                    push(@{$currltititles{$currcrsltitools{$item}{'title'}}},$item);
+                                                } else {
+                                                    $currltititles{$currcrsltitools{$item}{'title'}} = [$item];
+                                                }
+                                            }
+                                        }
+                                        $gotcrsltitools = 1;
+                                    }
+                                    unless ($checkedsameinst) {
+                                        my $primary_id = &Apache::lonnet::domain($cdom,'primary');
+                                        my $intdom = &Apache::lonnet::internet_dom($primary_id);
+                                        if ($intdom ne '') {
+                                            my $internet_names =
+                                                &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
+                                            if (ref($internet_names) eq 'ARRAY') {
+                                                if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
+                                                    $same_institution = 1;
+                                                }
+                                            }
+                                        }
+                                        $checkedsameinst = 1;
+                                    }
+                                }
+                            }
+                        }
                         my ($newurl,$result,$errtext) =
-                            &dbcopy($dbcopies{$key}{$idx},$cdom,$cnum,\%lockerrors);
+                            &dbcopy($dbcopies{$key}{$idx},$cdom,$cnum,\%lockerrors,\%currltititles,
+                                    \$currltimax,\@updatetoolsenc,\$updatetoolscache,$same_institution);
                         if ($result eq 'ok') {
                             $newdb{$idx} = $newurl;
+                            if ($newurl =~ /ext\.tool$/) {
+                                if ($torewrite{$idx} eq "/adm/$oldcdom/$oldcnum/$oldmarker/ext.tool") {
+                                    if ($newurl =~ m{^\Q/adm/$cdom/$cnum/\E(\d+)/ext.tool$}) {
+                                        my $newmarker = $1;
+                                        unless ($oldmarker eq $newmarker) {
+                                            $torewrite{$idx} = "/adm/$oldcdom/$oldcnum/$newmarker/ext.tool";
+                                        }
+                                    }
+                                }
+                            }
                         } elsif (ref($errors) eq 'HASH') {
                             $errors->{$key} = 1;
                         }
@@ -2904,6 +3344,10 @@ sub apply_fixups {
                 }
             }
         }
+        if (($updatetoolscache) || (@updatetoolsenc)) {
+            &update_ltitools_caches($cdom,$cnum,$updatetoolscache,
+                                    \@updatetoolsenc);
+        }
     }
     return ('ok',\@msgs,$lockmsg);
 }
@@ -2998,7 +3442,7 @@ sub update_parameter {
             my ($name,$url)=split(/\:/,$LONCAPA::map::resources[$res]);
             $name=&LONCAPA::map::qtescape($name);
             $url=&LONCAPA::map::qtescape($url);
-            next unless ($name && $url);
+            next unless $url;
             my $is_map;
             if ($url =~ m{/uploaded/.+\.(page|sequence)$}) {
                 $is_map = 1;
@@ -3083,8 +3527,9 @@ sub update_parameter {
 
 sub handle_edit_cmd {
     my ($coursenum,$coursedom) =@_;
+    my $haschanges = 0;
     if ($env{'form.cmd'} eq '') {
-        return 0;
+        return $haschanges;
     }
     my ($cmd,$idx)=split('_',$env{'form.cmd'});
 
@@ -3099,19 +3544,19 @@ sub handle_edit_cmd {
 	    &LONCAPA::map::makezombie($LONCAPA::map::order[$idx]);
 	}
 	splice(@LONCAPA::map::order, $idx, 1);
-
+        $haschanges = 1;
     } elsif ($cmd eq 'cut') {
 	&LONCAPA::map::makezombie($LONCAPA::map::order[$idx]);
 	splice(@LONCAPA::map::order, $idx, 1);
-
+        $haschanges = 1;
     } elsif ($cmd eq 'up'
 	     && ($idx) && (defined($LONCAPA::map::order[$idx-1]))) {
 	@LONCAPA::map::order[$idx-1,$idx] = @LONCAPA::map::order[$idx,$idx-1];
-
+        $haschanges = 1;
     } elsif ($cmd eq 'down'
 	     && defined($LONCAPA::map::order[$idx+1])) {
 	@LONCAPA::map::order[$idx+1,$idx] = @LONCAPA::map::order[$idx,$idx+1];
-
+        $haschanges = 1;
     } elsif ($cmd eq 'rename') {
 	my $comment = &LONCAPA::map::qtunescape($env{'form.title'});
 	if ($comment=~/\S/) {
@@ -3121,11 +3566,9 @@ sub handle_edit_cmd {
 # Devalidate title cache
 	my $renamed_url=&LONCAPA::map::qtescape($url);
 	&Apache::lonnet::devalidate_title_cache($renamed_url);
-
-    } else {
-	return 0;
+        $haschanges = 1;
     }
-    return 1;
+    return $haschanges;
 }
 
 sub editor {
@@ -3435,6 +3878,21 @@ sub editor {
         $r->print('</div>');
     }
 
+    if ((!$allowed) && ($folder =~ /^supplemental_\d+$/)) {
+        my ($supplemental) = &Apache::loncommon::get_supplemental($coursenum,$coursedom);
+        if (ref($supplemental) eq 'HASH') {
+            if ((ref($supplemental->{'hidden'}) eq 'HASH') &&
+                (ref($supplemental->{'ids'}) eq 'HASH')) {
+                if (ref($supplemental->{'ids'}->{"/uploaded/$coursedom/$coursenum/$folder.$container"}) eq 'ARRAY') {
+                    my $mapnum = $supplemental->{'ids'}->{"/uploaded/$coursedom/$coursenum/$folder.$container"}->[0];
+                    if ($supplemental->{'hidden'}->{$mapnum}) {
+                        $ishidden = 1;
+                    }
+                }
+            }
+        }
+    }
+
     my ($to_show,$output,@allidx,@allmapidx,%filters,%lists,%curr_groups);
     %filters =  (
                   canremove      => [],
@@ -3458,11 +3916,17 @@ sub editor {
             push(@allmapidx,$res);
         }
 
+        if (($supplementalflag) && (!$allowed) && (!$env{'request.role.adv'})) {
+            if (($ishidden) || ((&LONCAPA::map::getparameter($res,'parameter_hiddenresource'))[0]=~/^yes$/i)) {
+                $idx++;
+                next;
+            }
+        }
         $output .= &entryline($idx,$name,$url,$folder,$allowed,$res,
                               $coursenum,$coursedom,$crstype,
                               $pathitem,$supplementalflag,$container,
                               \%filters,\%curr_groups,$ltitoolsref,$canedit,
-                              $isencrypted,$navmapref,$hostname);
+                              $isencrypted,$ishidden,$navmapref,$hostname);
         $idx++;
         $shown++;
     }
@@ -3471,10 +3935,14 @@ sub editor {
     my $need_save;
     if ($allowed || ($supplementalflag && $folder eq 'supplemental')) {
         my $toolslink;
-        if ($allowed) {
+        if ($allowed || $canedit) {
+            my $helpitem = 'Navigation_Screen';
+            if (!$allowed) {
+                $helpitem = 'Supplemental_Navigation';
+            }
             $toolslink = '<table><tr><td>'
                        .&Apache::loncommon::help_open_menu('Navigation Screen',
-                                                           'Navigation_Screen',undef,'RAT')
+                                                           $helpitem,undef,'RAT')
                        .'</td><td class="LC_middle">'.&mt('Tools:').'</td>'
                        .'<td align="left"><ul id="LC_toolbar">'
                        .'<li><a href="/adm/coursedocs?forcesupplement=1&amp;command=editsupp" '
@@ -3490,11 +3958,9 @@ sub editor {
                           .&Apache::loncommon::start_data_table_header_row()
                           .'<th colspan="2">'.&mt('Move').'</th>'
                           .'<th colspan="2">'.&mt('Actions').'</th>'
-                          .'<th>'.&mt('Document').'</th>';
-                if ($folder !~ /^supplemental/) {
-                    $to_show .= '<th colspan="4">'.&mt('Settings').'</th>';
-                }
-                $to_show .= &Apache::loncommon::end_data_table_header_row();
+                          .'<th>'.&mt('Document').'</th>'
+                          .'<th colspan="2">'.&mt('Settings').'</th>'
+                          .&Apache::loncommon::end_data_table_header_row();
                 if ($folder !~ /^supplemental/) {
                     $lists{'canhide'} = join(',',@allidx);
                     $lists{'canrandomlyorder'} = join(',',@allmapidx);
@@ -3522,7 +3988,7 @@ sub editor {
                                 '</td>'.
                                 '<td>&nbsp;</td>'.
                                 '<td>&nbsp;</td>'.
-                                '<td colspan="4">'.
+                                '<td colspan="2">'.
                                 &multiple_check_form('settings',\%lists,$canedit).
                                 '</td>'.
                                 &Apache::loncommon::end_data_table_row();
@@ -3591,7 +4057,7 @@ sub multiple_check_form {
     return unless (ref($listsref) eq 'HASH');
     my $disabled;
     unless ($canedit) {
-        $disabled = 'disabled="disabled"';
+        $disabled = ' disabled="disabled"';
     }
     my $output =
     '<form action="/adm/coursedocs" method="post" name="togglemult'.$caller.'">'.
@@ -3841,12 +4307,14 @@ sub is_supplemental_title {
 sub entryline {
     my ($index,$title,$url,$folder,$allowed,$residx,$coursenum,$coursedom,
         $crstype,$pathitem,$supplementalflag,$container,$filtersref,$currgroups,
-        $ltitoolsref,$canedit,$isencrypted,$navmapref,$hostname)=@_;
-    my ($foldertitle,$renametitle,$oldtitle);
+        $ltitoolsref,$canedit,$isencrypted,$ishidden,$navmapref,$hostname)=@_;
+    my ($foldertitle,$renametitle,$oldtitle,$encodedtitle);
     if (&is_supplemental_title($title)) {
 	($title,$foldertitle,$renametitle) = &Apache::loncommon::parse_supplemental_title($title);
+        $encodedtitle=$title;
     } else {
 	$title=&HTML::Entities::encode($title,'"<>&\'');
+        $encodedtitle=$title;
 	$renametitle=$title;
 	$foldertitle=$title;
     }
@@ -3868,7 +4336,7 @@ sub entryline {
     my $line=&Apache::loncommon::start_data_table_row();
     my ($form_start,$form_end,$form_common,$form_param);
 # Edit commands
-    my ($esc_path, $path, $symb);
+    my ($esc_path, $path, $symb, $shownsymb);
     if ($env{'form.folderpath'}) {
 	$esc_path=&escape($env{'form.folderpath'});
 	$path = &HTML::Entities::encode($env{'form.folderpath'},'<>&"');
@@ -4068,6 +4536,7 @@ END
     my $ispage;
     my $containerarg;
     my $folderurl;
+    my $plainurl;
     if ($uploaded) {
         if (($extension eq 'sequence') || ($extension eq 'page')) {
             $url=~/\Q$coursenum\E\/([\/\w]+)\.\Q$extension\E$/;
@@ -4086,7 +4555,7 @@ END
                 $url='/adm/supplemental?';
             }
 	} else {
-	    &Apache::lonnet::allowuploaded('/adm/coursedoc',$url);
+            $plainurl = $url;
 	}
     }
 
@@ -4136,30 +4605,10 @@ END
                     $nomodal = 1;
                 }
 	    }
-            if (&Apache::lonnet::symbverify($symb,$url)) {
-                my $shownsymb = $symb;
-                if ($isexternal) {
-                    $url =~ s/\#[^#]+$//;
-                    if ($container eq 'page') {
-                        $url = &Apache::lonnet::clutter($url);
-                    }
-                }
-                unless ($env{'request.role.adv'}) {
-                    if ((&LONCAPA::map::getparameter($orderidx,'parameter_hiddenresource'))[0]=~/^yes$/i) {
-                        $url = '';
-                    }
-                    if (&Apache::lonnet::EXT('resource.0.hiddenresource',$symb) =~ /^yes$/i) {
-                        $url = '';
-                        $hiddenres = 1;
-                    }
-                }
-                if ($url ne '') {
-                    $url.=(($url=~/\?/)?'&':'?').'symb='.&escape($shownsymb);
-                }
-            } elsif (!$env{'request.role.adv'}) {
-                my $checkencrypt;
+            my ($checkencrypt,$shownurl);
+            if (!$env{'request.role.adv'}) {
                 if (((&LONCAPA::map::getparameter($orderidx,'parameter_encrypturl'))[0]=~/^yes$/i) ||
-                      $isencrypted || (&Apache::lonnet::EXT('resource.0.encrypturl',$symb) =~ /^yes$/i)) {
+                    ($isencrypted) || (&Apache::lonnet::EXT('resource.0.encrypturl',$symb) =~ /^yes$/i)) {
                     $checkencrypt = 1;
                 } elsif (ref($navmapref)) {
                     unless (ref($$navmapref)) {
@@ -4171,22 +4620,39 @@ END
                         }
                     }
                 }
-                if ($checkencrypt) {
-                    my $shownsymb = &Apache::lonenc::encrypted($symb);
-                    my $shownurl = &Apache::lonenc::encrypted($url);
-                    if (&Apache::lonnet::symbverify($shownsymb,$shownurl)) {
-                        $url = $shownurl.(($shownurl=~/\?/)?'&':'?').'symb='.&escape($shownsymb);
-                        if ($env{'request.enc'} ne '') {
-                            delete($env{'request.enc'});
-                        }
-                    } else {
-                        $url='';
-                    }
+            }
+            if ($checkencrypt) {
+                my $currenc = $env{'request.enc'};
+                $env{'request.enc'} = 1;
+                $shownsymb = &Apache::lonenc::encrypted($symb);
+                $shownurl = &Apache::lonenc::encrypted($url);
+                if (&Apache::lonnet::symbverify($symb,$url)) {
+                    $url = $shownurl;
                 } else {
-                    $url='';
+                    $url = '';
                 }
-            } else {
-                $url='';
+                $env{'request.enc'} = $currenc;
+            } elsif (&Apache::lonnet::symbverify($symb,$url)) {
+                $shownsymb = $symb;
+                if ($isexternal) {
+                    $url =~ s/\#[^#]+$//;
+                    if ($container eq 'page') {
+                        $url = &Apache::lonnet::clutter($url);
+                    }
+                }
+                $shownurl = $url;
+            }
+            unless ($env{'request.role.adv'}) {
+                if ((&LONCAPA::map::getparameter($orderidx,'parameter_hiddenresource'))[0]=~/^yes$/i) {
+                    $url = '';
+                }
+                if (&Apache::lonnet::EXT('resource.0.hiddenresource',$symb) =~ /^yes$/i) {
+                    $url = '';
+                    $hiddenres = 1;
+                }
+            }
+            if ($url ne '') {
+                $url = $shownurl.(($shownurl=~/\?/)?'&':'?').'symb='.&escape($shownsymb);
             }
 	}
     } elsif ($supplementalflag) {
@@ -4215,6 +4681,22 @@ END
                 }
                 $nomodal = 1;
             }
+        } elsif (($uploaded) && (!$allowed) && ($url ne '/adm/supplemental?')) {
+            my $embstyle=&Apache::loncommon::fileembstyle($extension);
+            unless ($embstyle eq 'ssi') {
+                if (($embstyle eq 'img')
+                 || ($embstyle eq 'emb')
+                 || ($embstyle eq 'wrp')) {
+                    $url='/adm/wrapper'.$url;
+                } elsif ($url !~ /\.(sequence|page)$/) {
+                    $url='/adm/coursedocs/showdoc'.$url;
+                }
+            }
+        }
+        unless ($allowed && $env{'request.role.adv'}) {
+            if ($ishidden || (&LONCAPA::map::getparameter($orderidx,'parameter_hiddenresource'))[0]=~/^yes$/i) {
+                $hiddenres = 1;
+            }
         }
     }
     my ($rand_pick_text,$rand_order_text,$hiddenfolder);
@@ -4226,6 +4708,9 @@ END
         if (!$allowed && $supplementalflag) {
             $folderpath.=$containerarg.'&'.$foldername;
             $url.='folderpath='.&escape($folderpath);
+            if ($ishidden || (&LONCAPA::map::getparameter($orderidx,'parameter_hiddenresource'))[0]=~/^yes$/i) {
+                $hiddenfolder = 1;
+            }
         } else {
             my $rpicknum = (&LONCAPA::map::getparameter($orderidx,
                                                         'parameter_randompick'))[0];
@@ -4311,7 +4796,7 @@ $form_end;
         $url .= ($url =~ /\?/) ? '&amp;':'?';
         $url .= 'folderpath='.&HTML::Entities::encode($esc_path,'<>&"');
         if ($title) {
-            $url .= '&amp;title='.&HTML::Entities::encode($renametitle,'<>&"');
+            $url .= '&amp;title='.$encodedtitle;
         }
         if ((($isexternal) || ($isexttool)) && $orderidx) {
             $url .= '&amp;idx='.$orderidx;
@@ -4346,7 +4831,7 @@ $form_end;
                     &Apache::lonhtmlcommon::jump_to_editres($cfile,$home,
                                                             $switchserver,
                                                             $forceedit,
-                                                            undef,$symb,
+                                                            undef,$symb,$shownsymb,
                                                             &escape($env{'form.folderpath'}),
                                                             $renametitle,$hostname,
                                                             '','',1,$suppanchor);
@@ -4366,9 +4851,18 @@ $form_end;
         $reinit = &mt('(re-initialize course to access)');
     }
     $line.='<td class="LC_docs_entry_commands"'.$tdalign.'><span class="LC_nobreak">'.$editlink.$renamelink;
-    my $link;
+    my ($link,$nolink);
     if (($url=~m{/adm/(coursedocs|supplemental)}) || (!$allowed && $url)) {
-       $line.='<a href="'.$url.'"><img src="'.$icon.'" alt="" class="LC_icon" /></a>';
+        if ($allowed && !$env{'request.role.adv'} && !$isfolder && !$ispage) {
+            if ((&LONCAPA::map::getparameter($orderidx,'parameter_hiddenresource'))[0]=~/^yes$/i) {
+                $nolink = 1;
+            }
+        }
+        if ($nolink) {
+            $line .= '<img src="'.$icon.'" alt="" class="LC_icon" /></a>';
+        } else {
+            $line.='<a href="'.$url.'"><img src="'.$icon.'" alt="" class="LC_icon" /></a>';
+        }
     } elsif ($url) {
        if ($anchor ne '') {
            if ($supplementalflag) {
@@ -4377,14 +4871,20 @@ $form_end;
                $anchor = '#'.&HTML::Entities::encode($anchor,'"<>&');
            }
        }
-
        if ((!$supplementalflag) && ($nomodal) && ($hostname ne '')) {
            $link = 'http://'.$hostname.$url;
        } else {
            $link = $url;
        }
        $link = &js_escape($link.(($url=~/\?/)?'&amp;':'?').'inhibitmenu=yes'.$anchor);
-       if ($nomodal) {
+       if ($allowed && !$env{'request.role.adv'} && !$isfolder && !$ispage && !$uploaded) {
+           if ((&LONCAPA::map::getparameter($orderidx,'parameter_hiddenresource'))[0]=~/^yes$/i) {
+               $nolink = 1;
+           }
+       }
+       if ($nolink) {
+           $line.='<img src="'.$icon.'" alt="" class="LC_icon" />';
+       } elsif ($nomodal) {
            $line.='<a href="#" onclick="javascript:window.open('."'$link','syllabuspreview','height=400,width=500,scrollbars=1,resizable=1,menubar=0,location=1')".'; return false;" />'.
                   '<img src="'.$icon.'" alt="" class="LC_icon" border="0" /></a>';
        } else {
@@ -4396,9 +4896,26 @@ $form_end;
     }
     $line.='</span></td><td'.$tdwidth.'>';
     if (($url=~m{/adm/(coursedocs|supplemental)}) || (!$allowed && $url)) {
-       $line.='<a href="'.$url.'">'.$title.'</a>';
+       if ($nolink) {
+           $line.=$title;
+       } else {
+           $line.='<a href="'.$url.'">'.$title.'</a>';
+       }
+       if (!$allowed && $supplementalflag && $canedit && $isfolder) {
+           my $editicon = &Apache::loncommon::lonhttpdurl('/res/adm/pages').'/editmap.png';
+           my $editurl = $url;
+           $editurl =~ s{^\Q/adm/supplemental?\E}{/adm/coursedocs?command=direct&amp;forcesupplement=1&amp;};
+           $line .= '&nbsp;'.'<a href="'.$editurl.'">'.
+                    '<img src="'.$editicon.'" alt="'.&mt('Edit Content').'" title="'.&mt('Edit Content').'" />'.
+                    '</a>';
+       }
+       if ((($hiddenfolder) || ($hiddenres)) && (!$allowed) && ($supplementalflag))  {
+           $line.= ' <span class="LC_warning">('.&mt('hidden').')</span> ';
+       }
     } elsif ($url) {
-       if ($nomodal) {
+       if ($nolink) {
+           $line.=$title;
+       } elsif ($nomodal) {
            $line.='<a href="#" onclick="javascript:window.open('."'$link','syllabuspreview','height=400,width=500,scrollbars=1,resizable=1,menubar=0,location=1')".'; return false;" />'.
                   $title.'</a>';
        } else {
@@ -4412,32 +4929,42 @@ $form_end;
     $line.="$extresform</td>";
     $rand_pick_text = '&nbsp;' if ($rand_pick_text eq '');
     $rand_order_text = '&nbsp;' if ($rand_order_text eq '');
-    if (($allowed) && ($folder!~/^supplemental/)) {
- 	my %lt=&Apache::lonlocal::texthash(
- 			      'hd' => 'Hidden',
- 			      'ec' => 'URL hidden');
-        my ($enctext,$hidtext);
-        if ((&LONCAPA::map::getparameter($orderidx,'parameter_encrypturl'))[0]=~/^yes$/i) {
-            $enctext = ' checked="checked"';
-            if (($ishash) && (ref($filtersref->{'encrypturl'}) eq 'ARRAY')) {
-                push(@{$filtersref->{'encrypturl'}},$orderidx);
-            }
+    if ($uploaded && $url && !$isfolder && !$ispage) {
+        if (($plainurl ne '') && ($env{'request.role.adv'} || $allowed || !$hiddenres)) {
+            &Apache::lonnet::allowuploaded('/adm/coursedoc',$plainurl);
         }
+    }
+    if ($allowed) {
+        my %lt=&Apache::lonlocal::texthash(
+                              'hd' => 'Hidden',
+                              'ec' => 'URL hidden');
+        my ($enctext,$hidtext,$formhidden,$formurlhidden);
         if ((&LONCAPA::map::getparameter($orderidx,'parameter_hiddenresource'))[0]=~/^yes$/i) {
             $hidtext = ' checked="checked"';
-            if (($ishash) && (ref($filtersref->{'randomorder'}) eq 'ARRAY')) {
+            if (($ishash) && (ref($filtersref->{'hiddenresource'}) eq 'ARRAY')) {
                 push(@{$filtersref->{'hiddenresource'}},$orderidx);
             }
         }
-        my $formhidden = 'edit_hiddenresource_'.$orderidx;
-        my $formurlhidden = 'edit_encrypturl_'.$orderidx;
-	$line.=(<<ENDPARMS);
+        $formhidden = 'edit_hiddenresource_'.$orderidx;
+        $line.=(<<ENDPARMS);
   <td class="LC_docs_entry_parameter">
     <form action="/adm/coursedocs" method="post" name="$formhidden">
     $form_param
     $form_common
     <label><input type="checkbox" name="hiddenresource_$orderidx" id="hiddenresource_$orderidx" onclick="checkForSubmit(this.form,'hiddenresource','settings');" $hidtext $disabled /> $lt{'hd'}</label>
     $form_end
+ENDPARMS
+        if ($folder =~/^supplemental/) {
+            $line.= "\n    <td>";
+        } else {
+            if ((&LONCAPA::map::getparameter($orderidx,'parameter_encrypturl'))[0]=~/^yes$/i) {
+                $enctext = ' checked="checked"';
+                if (($ishash) && (ref($filtersref->{'encrypturl'}) eq 'ARRAY')) {
+                    push(@{$filtersref->{'encrypturl'}},$orderidx);
+                }
+            }
+            $formurlhidden = 'edit_encrypturl_'.$orderidx;
+	    $line.=(<<ENDPARMS);
     <br />
     <form action="/adm/coursedocs" method="post" name="$formurlhidden">
     $form_param
@@ -4448,6 +4975,7 @@ $form_end;
   <td class="LC_docs_entry_parameter">$rand_pick_text<br />
                                       $rand_order_text</td>
 ENDPARMS
+        }
     }
     $line.=&Apache::loncommon::end_data_table_row();
     return $line;
@@ -5173,13 +5701,17 @@ sub changewarning {
     if (!defined($message)) {
 	$message='Changes will become active for your current session after [_1], or the next time you log in.';
     }
+    my $windowname = 'loncapaclient';
+    if ($env{'request.lti.login'}) {
+        $windowname .= 'lti';
+    }
     $r->print("\n\n".
 '<script type="text/javascript">'."\n".
 '// <![CDATA['."\n".
 'function reinit(tf) { tf.submit();'.$postexec.' }'."\n".
 '// ]]>'."\n".
 '</script>'."\n".
-'<form name="reinitform" method="post" action="/adm/roles" target="loncapaclient">'.
+'<form name="reinitform" method="post" action="/adm/roles" target="'.$windowname.'">'.
 '<input type="hidden" name="orgurl" value="'.$url.
 '" /><input type="hidden" name="selectrole" value="1" /><p class="LC_warning">'.
 &mt($message,' <input type="hidden" name="'.
@@ -5296,17 +5828,20 @@ sub handler {
     $help{'Caching'} = &Apache::loncommon::help_open_topic('Caching');
  
     my ($allowed,$canedit,$canview,$disabled);
+# does this user have privileges to modify content.
+    if (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
 # URI is /adm/supplemental when viewing supplemental docs in non-edit mode.
-    unless ($r->uri eq '/adm/supplemental') {
-        # does this user have privileges to modify content.  
-        if (&Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
+        unless ($r->uri eq '/adm/supplemental') {
             $allowed = 1;
-            $canedit = 1;
-            $canview = 1;
-        } elsif (&Apache::lonnet::allowed('cev',$env{'request.course.id'})) {
+        }
+        $canedit = 1;
+        $canview = 1;
+    } elsif (&Apache::lonnet::allowed('cev',$env{'request.course.id'})) {
+# URI is /adm/supplemental when viewing supplemental docs in non-edit mode.
+        unless ($r->uri eq '/adm/supplemental') {
             $allowed = 1;
-            $canview = 1;
         }
+        $canview = 1;
     }
     unless ($canedit) {
         $disabled = ' disabled="disabled"';
@@ -5360,7 +5895,7 @@ sub handler {
 # Get the parameters that may be needed
 #
     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
-                                            ['folderpath',
+                                            ['folderpath','title',
                                              'forcesupplement','forcestandard',
                                              'tools','symb','command','supppath']);
 
@@ -5407,11 +5942,14 @@ sub handler {
     if ($env{'form.tools'}) { $toolsflag=1; }
 
     if ($env{'form.folderpath'} ne '') {
-        &validate_folderpath($supplementalflag);
+        &Apache::loncommon::validate_folderpath($supplementalflag,$allowed,$coursenum,$coursedom);
     }
 
+    my $backto_supppath;
     if ($env{'form.supppath'} ne '') {
-        &validate_suppath();
+        if ($supplementalflag && $allowed) {
+            $backto_supppath = &validate_supppath($coursenum,$coursedom);
+        }
     }
 
     my $script='';
@@ -5421,6 +5959,7 @@ sub handler {
     my $containertag;
     my $pathitem;
     my %ltitools;
+    my $posslti;
     my $hiddentop;
     my $navmap;
     my $filterFunc = sub { my $res = shift; return (!$res->randomout() && !$res->is_map()) };
@@ -5432,10 +5971,10 @@ sub handler {
                &Apache::loncommon::symb_to_docspath($env{'form.symb'},\$navmap);
            &Apache::lonnet::appenv({'docs.exit.'.$env{'request.course.id'} =>
                $env{'form.command'}.'_'.$env{'form.symb'}});
-       } elsif ($env{'form.supppath'} ne '') {
+       } elsif (($env{'form.supppath'} ne '') && $supplementalflag && $allowed) {
            $env{'form.folderpath'}=$env{'form.supppath'};
            &Apache::lonnet::appenv({'docs.exit.'.$env{'request.course.id'} =>
-               $env{'form.command'}.'_'.$env{'form.supppath'}});
+               $env{'form.command'}.'_'.$backto_supppath});
        }
    } elsif ($env{'form.command'} eq 'editdocs') {
        $env{'form.folderpath'} = &default_folderpath($coursenum,$coursedom,\$navmap);
@@ -5471,7 +6010,7 @@ sub handler {
             undef($env{'form.folderpath'});
         }
         if ($env{'form.folderpath'} ne '') {
-            &validate_folderpath($supplementalflag);
+            &Apache::loncommon::validate_folderpath($supplementalflag,$allowed,$coursenum,$coursedom);
         }
     }
    
@@ -5488,9 +6027,9 @@ sub handler {
                                   $env{'form.folderpath'};
     }
 # If allowed and user's role is not advanced check folderpath is not hidden
-    if (($allowed) && (!$env{'request.role.adv'}) &&
-        ($env{'form.folderpath'} ne '') && (!$supplementalflag)) {
-        my $folderurl;
+    my $hidden_and_empty;
+    if (($allowed) && (!$env{'request.role.adv'}) && ($env{'form.folderpath'} ne '')) {
+        my ($folderurl,$foldername,$hiddenfolder);
         my @pathitems = split(/\&/,$env{'form.folderpath'});
         my $folder = $pathitems[-2];
         if ($folder eq '') {
@@ -5502,21 +6041,74 @@ sub handler {
             } else {
                 $folderurl .= '.sequence';
             }
-            unless (ref($navmap)) {
-                $navmap = Apache::lonnavmaps::navmap->new();
-            }
-            if (ref($navmap)) {
-                if (lc($navmap->get_mapparam(undef,$folderurl,"0.hiddenresource")) eq 'yes') {
-                    my @resources = $navmap->retrieveResources($folderurl,$filterFunc,1,1);
-                    unless (@resources) {
-                        undef($env{'form.folderpath'});
+            if ($supplementalflag) {
+                ($foldername,$hiddenfolder) = ($pathitems[-1] =~ /^([^:]*)::(|1):::$/);
+                $foldername = &HTML::Entities::decode(&unescape($foldername));
+                my ($supplemental) = &Apache::loncommon::get_supplemental($coursenum,$coursedom);
+                if (ref($supplemental) eq 'HASH') {
+                    my ($suppmap,$suppmapnum);
+                    if ($folder eq 'supplemental') {
+                        $suppmap = 'default';
+                        $suppmapnum = 0;
+                    } elsif ($folder =~ /^supplemental_(\d+)$/) {
+                        $suppmap = $1;
+                        $suppmapnum = $suppmap;
+                    }
+                    if ($hiddenfolder) {
+                        my $hascontent;
+                        foreach my $key (reverse(sort(keys(%{$supplemental->{'ids'}})))) {
+                            if ($key =~ m{^\Q/uploaded/$coursedom/$coursenum/supplemental/$suppmap/\E}) {
+                                $hascontent = 1;
+                            } elsif (ref($supplemental->{'ids'}->{$key}) eq 'ARRAY') {
+                                foreach my $id (@{$supplemental->{'ids'}->{$key}}) {
+                                    if ($id =~ /^$suppmapnum\:/) {
+                                        $hascontent = 1;
+                                        last;
+                                    }
+                                }
+                            }
+                            last if ($hascontent);
+                        }
+                        unless ($hascontent) {
+                            if ($foldername ne '') {
+                                $hidden_and_empty = $foldername;
+                            } else {
+                                $hidden_and_empty = $folder;
+                            }
+                        }
+                    }
+                }
+            } else {
+                unless (ref($navmap)) {
+                    $navmap = Apache::lonnavmaps::navmap->new();
+                }
+                ($foldername,$hiddenfolder) = ($pathitems[-1] =~ /^([^:]*):|\d+:|1:(|1):|1:|1$/);
+                $foldername = &HTML::Entities::decode(&unescape($foldername));
+                if (ref($navmap)) {
+                    if ($hiddenfolder ||
+                        (lc($navmap->get_mapparam(undef,$folderurl,"0.hiddenresource")) eq 'yes')) {
+                        my @resources = $navmap->retrieveResources($folderurl,$filterFunc,1,1);
+                        unless (@resources) {
+                            if ($foldername ne '') {
+                                $hidden_and_empty = $foldername;
+                            } else {
+                                $hidden_and_empty = $folder;
+                            }
+                        }
                     }
                 }
             }
+            if ($hidden_and_empty ne '') {
+                splice(@pathitems,-2);
+                if (@pathitems) {
+                    $env{'form.folderpath'} = join('&',@pathitems);
+                } else {
+                    undef($env{'form.folderpath'});
+                }
+            }
         }
     }
 
-
 # If after all of this, we still don't have any paths, make them
     unless ($env{'form.folderpath'}) {
        if ($supplementalflag) {
@@ -5601,8 +6193,19 @@ sub handler {
                 }
             }
             my $tabidstr = join("','",@tabids);
-            %ltitools = &Apache::lonnet::get_domain_lti($coursedom,'consumer');
-            my $posslti = keys(%ltitools);
+            my (%domtools,%crstools);
+            my %tooltypes = &Apache::loncommon::usable_exttools();
+            if ($tooltypes{'dom'}) {
+                %domtools = &Apache::lonnet::get_domain_lti($coursedom,'consumer');
+            }
+            if ($tooltypes{'crs'}) {
+                %crstools = &Apache::lonnet::get_course_lti($coursenum,$coursedom,'consumer');
+            }
+            %ltitools = (
+                          dom => \%domtools,
+                          crs => \%crstools,
+                        );
+            $posslti = scalar(keys(%domtools)) + scalar(keys(%crstools));
             my $hostname = $r->hostname();
             $script .= &editing_js($udom,$uname,$supplementalflag,$coursedom,$coursenum,$posslti,
                                    $canedit,$hostname,\$navmap).
@@ -5610,8 +6213,14 @@ sub handler {
                        &inject_data_js().
                        &Apache::lonhtmlcommon::resize_scrollbox_js('docs',$tabidstr,$tid).
                        &Apache::lonextresedit::extedit_javascript(\%ltitools);
+            my $onload = "javascript:resize_scrollbox('contentscroll','1','1');";
+            if ($hidden_and_empty ne '') {
+                my $alert = &mt("Additional privileges required to edit empty and hidden folder: '[_1]'",
+                                $hidden_and_empty);
+                $onload .= "javascript:alert('".&js_escape($alert)."');";
+            }
             $addentries = {
-                            onload   => "javascript:resize_scrollbox('contentscroll','1','1');",
+                            onload => $onload,
                           };
         }
         $script .= &paste_popup_js(); 
@@ -5632,8 +6241,15 @@ sub handler {
     &Apache::lonhtmlcommon::clear_breadcrumbs();
 
     if ($showdoc) {
-        $r->print(&Apache::loncommon::start_page("$crstype documents",undef,
-                                                {'force_register' => $showdoc,}));
+        my $args;
+        if ($supplementalflag) {
+            my $title = &HTML::Entities::encode($env{'form.title'},'\'"<>&');
+            my $brcrum = &Apache::lonhtmlcommon::docs_breadcrumbs(undef,$crstype,undef,$title,1);
+            $args = {'bread_crumbs' => $brcrum};
+        } else {
+            $args = {'force_register' => $showdoc};
+        }
+        $r->print(&Apache::loncommon::start_page("$crstype documents",undef,$args));
     } elsif ($toolsflag) {
         my ($breadtext,$breadtitle);
         $breadtext = "$crstype Editor";
@@ -5651,6 +6267,12 @@ sub handler {
                      $breadtitle)
                  );
     } elsif ($r->uri eq '/adm/supplemental') {
+        unless ($env{'request.role.adv'}) {
+            unless (&Apache::lonnet::has_unhidden_suppfiles($coursenum,$coursedom)) {
+                $r->internal_redirect('/adm/navmaps');
+                return OK;
+            }
+        }
         my $brcrum = &Apache::lonhtmlcommon::docs_breadcrumbs(undef,$crstype);
         $r->print(&Apache::loncommon::start_page("Supplemental $crstype Content",undef,
                                                 {'bread_crumbs' => $brcrum,}));
@@ -5681,6 +6303,7 @@ sub handler {
   my %codebase = ();
   my ($upload_result,$upload_output,$uploadphase);
   if ($canedit) {
+      undef($suppchanges);
       if (($env{'form.uploaddoc.filename'}) &&
 	  ($env{'form.cmd'}=~/^upload_(\w+)/)) {
           my $context = $1; 
@@ -5692,6 +6315,10 @@ sub handler {
 	  if ($hadchanges) {
 	      &mark_hash_old();
 	  }
+          if ($suppchanges) {
+              &Apache::lonnet::update_supp_caches($coursedom,$coursenum);
+              undef($suppchanges);
+          }
           $r->print($upload_output);
       } elsif ($env{'form.phase'} eq 'upload_embedded') {
           # Process file upload - phase two - upload embedded objects 
@@ -5813,10 +6440,7 @@ sub handler {
 	my $fileupload=(<<FIUP);
         $quotainfo
 	$lt{'file'}:<br />
-	<input type="file" name="uploaddoc" class="LC_flUpload" size="40" $disabled />
-        <input type="hidden" id="LC_free_space" value="$free_space" />
 FIUP
-
 	my $checkbox=(<<CHBO);
 	<!-- <label>$lt{'parse'}?
 	<input type="checkbox" name="parserflag" />
@@ -5836,6 +6460,8 @@ CHBO
         <fieldset id="uploadimsform" style="display: none;">
         <legend>$lt{'imsf'}</legend>
         $fileupload
+        <input type="file" name="uploaddoc" id="uploaddocims" class="LC_flUpload LC_uploaddoc" size="40" $disabled />
+        <input type="hidden" id="LC_free_space_ims" value="$free_space" />
         <br />
         <p>
         $lt{'cms'}:&nbsp; 
@@ -5862,6 +6488,8 @@ IMSFORM
         <legend>$lt{'upfi'}</legend>
 	<input type="hidden" name="active" value="aa" />
 	$fileupload
+        <input type="file" name="uploaddoc" class="LC_flUpload" size="40" $disabled />
+        <input type="hidden" id="LC_free_space" value="$free_space" />
 	<br />
 	$lt{'title'}:<br />
 	<input type="text" size="60" name="comment" $disabled />
@@ -5980,9 +6608,13 @@ HIDDENFORM
        }
        my $postexec='';
        if ($folder eq 'default') {
+           my $windowname = 'loncapaclient';
+           if ($env{'request.lti.login'}) {
+               $windowname .= 'lti';
+           }
            $r->print('<script type="text/javascript">'."\n"
                     .'// <![CDATA['."\n"
-                    .'this.window.name="loncapaclient";'."\n"
+                    .'this.window.name="$windowname";'."\n"
                     .'// ]]>'."\n"
                     .'</script>'."\n"
        );
@@ -6178,7 +6810,7 @@ NGFFORM
         my @importdoc = (
         {'<img class="LC_noBorder LC_middle" src="/res/adm/pages/extres.png" alt="'.$lt{extr}.'" onclick="toggleUpload(\'ext\');" />'=>$extresourcesform}
         );
-        if (keys(%ltitools)) {
+        if ($posslti) {
             push(@importdoc,
                 {'<img class="LC_noBorder LC_middle" src="/res/adm/pages/exttool.png" alt="'.$lt{extt}.'" onclick="toggleUpload(\'tool\');" />'=>$exttoolform},
         );
@@ -6246,7 +6878,7 @@ unless ($container eq 'page') {
        unless ($supplementalflag) {
 	   $folder='supplemental';
        }
-       if ($folder =~ /^supplemental$/ &&
+       if (($folder eq 'supplemental') &&
 	   (($env{'form.folderpath'} =~ /^default\&/) || ($env{'form.folderpath'} eq ''))) {
           $env{'form.folderpath'} = &supplemental_base();
        } elsif ($allowed) {
@@ -6266,6 +6898,8 @@ unless ($container eq 'page') {
         <legend>$lt{'upfi'}</legend>
 	<input type="hidden" name="active" value="ee" />	
 	$fileupload
+        <input type="file" name="uploaddoc" id="uploaddocsupp" class="LC_flUpload LC_uploaddoc" size="40" $disabled />
+        <input type="hidden" id="LC_free_space_supp" value="$free_space" />
 	<br />
 	<br />
 	<span class="LC_nobreak">
@@ -6278,6 +6912,7 @@ unless ($container eq 'page') {
 	$pathitem
 	<input type="hidden" name="cmd" value="upload_supplemental" />
         <input type='submit' value="$lt{'upld'}" />
+        </fieldset>
         </form>
 SUPDOCFORM
 
@@ -6372,29 +7007,22 @@ my %suporderhash = (
                 'ff' => ['Other',&create_form_ul(&create_list_elements(@specialdocs))]
                 );
         if ($supplementalflag) {
-           my $error = &editor($r,$coursenum,$coursedom,$folder,$allowed,'',$crstype,
-                               $supplementalflag,\%suporderhash,$iconpath,$pathitem,
-                               \%ltitools,$canedit,$hostname);
-           if ($error) {
-              $r->print('<p><span class="LC_error">'.$error.'</span></p>');
-           } else {
-               if ($suppchanges) {
-                   my %servers = &Apache::lonnet::internet_dom_servers($coursedom);
-                   my @ids=&Apache::lonnet::current_machine_ids();
-                   foreach my $server (keys(%servers)) {
-                       next if (grep(/^\Q$server\E$/,@ids));
-                       my $hashid=$coursenum.':'.$coursedom;
-                       my $cachekey = &escape('suppcount').':'.&escape($hashid);
-                       &Apache::lonnet::remote_devalidate_cache($server,[$cachekey]);
-                   }
-                   &Apache::lonnet::get_numsuppfiles($coursenum,$coursedom,1);
-                   undef($suppchanges);
-               }
-           }
+            $suppchanges = 0;
+            my $error = &editor($r,$coursenum,$coursedom,$folder,$allowed,'',$crstype,
+                                $supplementalflag,\%suporderhash,$iconpath,$pathitem,
+                                \%ltitools,$canedit,$hostname);
+            if ($error) {
+                $r->print('<p><span class="LC_error">'.$error.'</span></p>');
+            }
+            if ($suppchanges) {
+                &Apache::lonnet::update_supp_caches($coursedom,$coursenum);
+                undef($suppchanges);
+            }
         }
     } elsif ($supplementalflag) {
         my $error = &editor($r,$coursenum,$coursedom,$folder,$allowed,'',$crstype,
-                            $supplementalflag,'',$iconpath,$pathitem,'',$hostname);
+                            $supplementalflag,'',$iconpath,$pathitem,'',$canedit,
+                            $hostname);
         if ($error) {
             $r->print('<p><span class="LC_error">'.$error.'</span></p>');
         }
@@ -6556,6 +7184,10 @@ sub remove_archive {
             if ($url eq $env{'form.archiveurl'}) {
                 if (&handle_edit_cmd($docuname,$docudom)) {
                     ($errtext,$fatal) = &storemap($docuname,$docudom,$map,1);
+                    if ($suppchanges) {
+                        &Apache::lonnet::update_supp_caches($docudom,$docuname);
+                        undef($suppchanges);
+                    }
                     if ($fatal) {
                         if ($container eq 'page') {
                             $delwarning = &mt('An error occurred updating the contents of the current page.');
@@ -6893,7 +7525,12 @@ sub editing_js {
     } elsif ($env{'docs.exit.'.$env{'request.course.id'}} eq '/adm/menu') {
         $backtourl = '/adm/menu';
     } elsif ($supplementalflag) {
-        $backtourl = '/adm/supplemental';
+        if (($env{'request.role.adv'}) ||
+            (&Apache::lonnet::has_unhidden_suppfiles($coursenum,$coursedom))) {
+            $backtourl = '/adm/supplemental';
+        } else {
+            $backtourl = '/adm/navmaps';
+        }
     } else {
         $backtourl = '/adm/navmaps';
     }