--- loncom/interface/londocs.pm	2016/11/26 19:40:44	1.616
+++ loncom/interface/londocs.pm	2018/01/12 01:54:56	1.650
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Documents
 #
-# $Id: londocs.pm,v 1.616 2016/11/26 19:40:44 raeburn Exp $
+# $Id: londocs.pm,v 1.650 2018/01/12 01:54:56 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -45,6 +45,7 @@ use Apache::lontemplate();
 use Apache::lonsimplepage();
 use Apache::lonhomework();
 use Apache::lonpublisher();
+use Apache::lonparmset();
 use HTML::Entities;
 use HTML::TokeParser;
 use GDBM_File;
@@ -52,6 +53,7 @@ use File::MMagic;
 use File::Copy;
 use Apache::lonlocal;
 use Cwd;
+use UUID::Tiny ':std';
 use LONCAPA qw(:DEFAULT :match);
 
 my $iconpath;
@@ -142,7 +144,43 @@ sub clean {
     return $title;
 }
 
-
+sub default_folderpath {
+    my ($coursenum,$coursedom,$navmapref) = @_;
+    return unless ($coursenum && $coursedom && ref($navmapref));
+# Check if entire course is hidden and/or encrypted
+    my ($hiddenmap,$encryptmap,$folderpath,$hiddentop);
+    my $toplevel = "uploaded/$coursedom/$coursenum/default.sequence";
+    unless (ref($$navmapref)) {
+        $$navmapref = Apache::lonnavmaps::navmap->new();
+    }
+    if (ref($$navmapref)) {
+        if (lc($$navmapref->get_mapparam(undef,$toplevel,"0.hiddenresource")) eq 'yes') {
+            my $filterFunc = sub { my $res = shift; return (!$res->randomout() && !$res->is_map()) };
+            my @resources = $$navmapref->retrieveResources($toplevel,$filterFunc,1,1);
+            unless (@resources) {
+                $hiddenmap = 1;
+                unless ($env{'request.role.adv'}) {
+                    $hiddentop = 1;
+                    if ($env{'form.folder'}) {
+                        undef($env{'form.folder'});
+                    }
+                }
+            }
+        }
+        if (lc($$navmapref->get_mapparam(undef,$toplevel,"0.encrypturl")) eq 'yes') {
+            $encryptmap = 1;
+        }
+    }
+    unless ($hiddentop) {
+        $folderpath='default&'.&escape(&mt('Main Content')).
+                    '::'.$hiddenmap.':'.$encryptmap.'::';
+    }
+    if (wantarray) {
+        return ($folderpath,$hiddentop);
+    } else {
+        return $folderpath;
+    }
+}
 
 sub dumpcourse {
     my ($r) = @_;
@@ -567,9 +605,9 @@ sub recurse_html {
                 } else {
                     $relfile = $dependency;
                     $depurl = $currurlpath;
-                    $depurl =~ s{[^/]+$}{};  
+                    $depurl =~ s{[^/]+$}{};
                     $depurl .= $dependency;
-                    ($newcontainer) = ($depurl =~ m{^\Q$prefix\E(.+)$}); 
+                    ($newcontainer) = ($depurl =~ m{^\Q$prefix\E(.+)$});
                 }
                 next if ($relfile eq '');
                 my $newname = $replacehash->{$container};
@@ -618,11 +656,12 @@ sub group_import {
             }
         }
 	if ($url) {
-            if ($url =~ m{^(/adm/$coursedom/$coursenum/(\d+)/exttool)s?\:?(.*)$}) {
+            if ($url =~ m{^(/adm/$coursedom/$coursenum/(\d+)/ext\.tool)\:?(.*)$}) {
                 $url = $1;
                 my $marker = $2;
                 my $info = $3;
                 my ($toolid,%toolhash,%toolsettings);
+                my @extras = ('linktext','explanation','crslabel','crstitle','crsappend');
                 my @toolinfo = split(/:/,$info);
                 if ($residx) {
                     %toolsettings=&Apache::lonnet::dump('exttool_'.$marker,$coursedom,$coursenum);
@@ -632,28 +671,42 @@ sub group_import {
                 }
                 $toolid =~ s/\D//g;
                 ($toolhash{'target'},$toolhash{'width'},$toolhash{'height'},
-                 $toolhash{'crslabel'},$toolhash{'crstitle'}) = @toolinfo;
-                $toolhash{'crslabel'} = &unescape($toolhash{'crslabel'});
-                $toolhash{'crstitle'} = &unescape($toolhash{'crstitle'});
+                 $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') {
-                        if ($ltitoolsref->{$toolid}->{'url'} =~ m{^https://}) {
-                            $url =~ s/exttool$/exttools/;
-                        }
+                        my @deleted;
                         $toolhash{'id'} = $toolid;
-                        if (($toolhash{'target'} eq 'iframe') || ($toolhash{'target'} eq 'window')) {
+                        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') {
-                                $toolhash{'width'} = $toolsettings{'width'};
-                                $toolhash{'height'} = $toolsettings{'height'};  
+                                foreach my $item ('width','height') {
+                                    $toolhash{$item} = $toolsettings{$item};
+                                }
                             }
                         } elsif (ref($ltitoolsref->{$toolid}->{'display'}) eq 'HASH') {
                             $toolhash{'target'} = $ltitoolsref->{$toolid}->{'display'}->{'target'};
@@ -663,40 +716,93 @@ sub group_import {
                             }
                         }
                         if ($toolhash{'target'} eq 'iframe') {
-                            delete($toolhash{'width'});
-                            delete($toolhash{'height'});
-                            if ($residx) {
-                                if ($toolsettings{'width'}) {
-                                    push(@deleted,'width');
+                            foreach my $item ('width','height','linktext','explanation') {
+                                delete($toolhash{$item});
+                                if ($residx) {
+                                    if ($toolsettings{$item}) {
+                                        push(@deleted,$item);
+                                    }
                                 }
-                                if ($toolsettings{'height'}) {
-                                    push(@deleted,'height');
+                            }
+                        } 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') { 
+                            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{'crs'.$item} =~ s/^\s+//;
-                                    $toolhash{'crs'.$item} =~ s/\s+$//;
-                                    if ($toolhash{'crs'.$item} eq '') {
-                                        delete($toolhash{'crs'.$item});
+                                    $toolhash{$crsitem} =~ s/^\s+//;
+                                    $toolhash{$crsitem} =~ s/\s+$//;
+                                    if ($toolhash{$crsitem} eq '') {
+                                        delete($toolhash{$crsitem});
                                     }
                                 } else {
-                                    delete($toolhash{'crs'.$item});
+                                    delete($toolhash{$crsitem});
                                 }
-                                if (($residx) && (exists($toolsettings{'crs'.$item}))) {
-                                    unless (exists($toolhash{'crs'.$item})) {
-                                        push(@deleted,'crs'.$item);
+                                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 (($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);
-                            } 
+                            }
+                            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);
                         }
                     }
                 }
@@ -712,8 +818,8 @@ sub group_import {
                     $donechk = 1;
                 }
                 if ($url =~ m{^/uploaded/\Q$coursedom\E/\Q$coursenum\E/(default_\d+\.)(page|sequence)$}) {
-                    &contained_map_check($url,$folder,\%removefrommap,\%removeparam,
-                                         \%addedmaps,\%hierarchy,\%titles,$allmaps);
+                    &contained_map_check($url,$folder,$coursenum,$coursedom,\%removefrommap,
+                                        \%removeparam,\%addedmaps,\%hierarchy,\%titles,$allmaps);
                     $importuploaded = 1;
                 } elsif ($url =~ m{^/res/.+\.(page|sequence)$}) {
                     next if ($allmaps->{$url});
@@ -753,7 +859,7 @@ $initialtext
 </html>
 END
                 $env{'form.output'}=$newhtml;
-                my $result = 
+                my $result =
                     &Apache::lonnet::finishuserfileupload($coursenum,$coursedom,
                                                           'output',
                                                           "$filepath/$residx/$fname.html");
@@ -879,11 +985,12 @@ sub log_docs {
 sub docs_change_log {
     my ($r,$coursenum,$coursedom,$folder,$allowed,$crstype,$iconpath,$canedit)=@_;
     my $supplementalflag=($env{'form.folderpath'}=~/^supplemental/);
+    my $navmap; 
     my $js = '<script type="text/javascript">'."\n".
              '// <![CDATA['."\n".
              &Apache::loncommon::display_filter_js('docslog')."\n".
              &editing_js($env{'user.domain'},$env{'user.name'},$supplementalflag,
-                         $coursedom,$coursenum,'','',$canedit)."\n".
+                         $coursedom,$coursenum,'','',$canedit,'',\$navmap)."\n".
              &history_tab_js()."\n".
              &Apache::lonratedt::editscript('simple')."\n".
              '// ]]>'."\n".
@@ -899,8 +1006,9 @@ sub docs_change_log {
     }
     my $folderpath=$env{'form.folderpath'};
     if ($folderpath eq '') {
-        $folderpath = 'default&'.&escape(&mt('Main Content').':::::');
+        $folderpath = &default_folderpath($coursenum,$coursedom,\$navmap);
     }
+    undef($navmap);
     $pathitem = '<input type="hidden" name="folderpath" value="'.
                 &HTML::Entities::encode($folderpath,'<>&"').'" />';
     my $readfile="/uploaded/$coursedom/$coursenum/$folder.$container";
@@ -910,7 +1018,7 @@ sub docs_change_log {
     if ($supplementalflag) {
         $tid = 2;
     }
-    my ($breadcrumbtrail) = 
+    my ($breadcrumbtrail) =
         &Apache::lonhtmlcommon::docs_breadcrumbs($allowed,$crstype,1);
     $r->print($breadcrumbtrail.
               &generate_edit_table($tid,\%orderhash,undef,$iconpath,$jumpto,
@@ -932,6 +1040,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".
@@ -1039,7 +1148,7 @@ 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') {
+	    foreach my $parameter ('randompick','hiddenresource','encrypturl','randomorder','gradable') {
 		if ($docslog{$id}{'logentry'}{'parameter_action_'.$parameter}) {
 # FIXME: internationalization seems wrong here
 		    $r->print('<li>'.
@@ -1109,7 +1218,7 @@ sub update_paste_buffer {
         foreach my $suffix (@currpaste) {
              my $cid = $env{'docs.markedcopy_crs_'.$suffix};
              my $url = $env{'docs.markedcopy_url_'.$suffix};
-             my $mapidx = $env{'docs.markedcopy_map_'.$suffix};           
+             my $mapidx = $env{'docs.markedcopy_map_'.$suffix};
              if (($cid =~ /^$match_domain(?:_)$match_courseid$/) &&
                  ($url ne '')) {
                  $pasteurls{$cid.'_'.$url.'_'.$mapidx} = 1;
@@ -1162,8 +1271,8 @@ sub update_paste_buffer {
                 $subdir = $prefix;
             }
             my (%addedmaps,%removefrommap,%removeparam,%hierarchy,%titles,%allmaps);
-            &contained_map_check($url,$folder,\%removefrommap,\%removeparam,\%addedmaps,
-                                 \%hierarchy,\%titles,\%allmaps);
+            &contained_map_check($url,$folder,$coursenum,$coursedom,\%removefrommap,
+                                 \%removeparam,\%addedmaps,\%hierarchy,\%titles,\%allmaps);
             if (ref($hierarchy{$url}) eq 'HASH') {
                 my ($nested,$nestednames);
                 &recurse_uploaded_maps($url,$subdir,\%hierarchy,\%titles,\$nested,\$nestednames);
@@ -1241,7 +1350,7 @@ sub print_paste_buffer {
             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+/exttools?$}) {
+            } elsif ($url =~ m{^/adm/$match_domain/$match_courseid/\d+/ext\.tool$}) {
                 $is_exttool = 1;
             }
             if ($folder =~ /^supplemental/) {
@@ -1275,26 +1384,34 @@ sub print_paste_buffer {
                         $is_uploaded_map = 1;
                     }
                 } elsif (($url =~ m{^/res/lib/templates/\w+\.problem$}) ||
-                         ($url =~ m{^/adm/($match_domain)/($match_username)/\d+/(bulletinboard|smppg)$})) {
+                         ($url =~ m{^/adm/($match_domain)/($match_username)/\d+/(bulletinboard|smppg|ext\.tool)$})) {
                     if ($cid ne $env{'request.course.id'}) {
                         my ($srcdom,$srcnum) = split(/_/,$cid);
                         if ($env{"user.priv.cm./$srcdom/$srcnum"} =~ /\Q:mdc&F\E/) {
-                            $othercrs = '<br />'.&mt('(from another course)');
+                            if (($is_exttool) && ($srcdom ne $coursedom)) {
+                                $canpaste = 0;
+                                $nopaste = &mt('Paste from another domain unavailable.');
+                            } else {
+                                $othercrs = '<br />'.&mt('(from another course)');
+                            }
                         } else {
                             $canpaste = 0;
                             $nopaste = &mt('Paste from another course unavailable.');
-                        }       
+                        }
                     }
                 }
                 if ($canpaste) {
                     push(@pasteable,$suffix);
-                }  
+                }
             }
             my $buffer;
-            if (($is_external) || ($is_exttool)) {
+            if ($is_external) {
                 $buffer = &mt('External Resource').': '.
                     &LONCAPA::map::qtescape($env{'docs.markedcopy_title_'.$suffix}).' ('.
                     &LONCAPA::map::qtescape($url).')';
+            } elsif ($is_exttool) {
+                $buffer = &mt('External Tool').': '.
+                    &LONCAPA::map::qtescape($env{'docs.markedcopy_title_'.$suffix});
             } else {
                 my $icon = &Apache::loncommon::icon($extension);
                 if ($extension eq 'sequence' &&
@@ -1452,7 +1569,7 @@ sub supp_pasteable {
         ($url =~ m{^/uploaded/$match_domain/$match_courseid/(docs|supplemental)/(default|\d+)/\d+/}) ||
         ($url =~ m{^/adm/$match_domain/$match_username/aboutme}) ||
         ($url =~ m{^/public/$match_domain/$match_courseid/syllabus}) ||
-        ($url =~ m{^/adm/$match_domain/$match_courseid/\d+/exttools?$})) {
+        ($url =~ m{^/adm/$match_domain/$match_courseid/\d+/ext\.tool$})) {
         return 1;
     }
     return;
@@ -1574,7 +1691,7 @@ sub do_paste_from_buffer {
         return();
     }
 
-    my (%msgs,%before,%after,@dopaste,%is_map,%notinsupp,%notincrs,%duplicate,
+    my (%msgs,%before,%after,@dopaste,%is_map,%notinsupp,%notincrs,%notindom,%duplicate,
         %prefixchg,%srcdom,%srcnum,%srcmapidx,%marktomove,$save_err,$lockerrors,$allresult);
 
     foreach my $suffix (@topaste) {
@@ -1603,7 +1720,8 @@ sub do_paste_from_buffer {
             $srcdom{$suffix} = $srcd;
             $srcnum{$suffix} = $srcn;
         } elsif (($url =~ m{^/res/lib/templates/\w+\.problem$}) ||
-                 ($url =~ m{^/adm/$match_domain/$match_username/\d+/(bulletinboard|smppg)$})) {
+                 ($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
@@ -1613,6 +1731,12 @@ sub do_paste_from_buffer {
                     next;
                 }
             }
+# 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 (($url =~ m{/ext\.tool$}) && ($srcd ne $coursedom)) {
+                $notindom{$suffix} = 1;
+                next;
+            }
             $srcdom{$suffix} = $srcd;
             $srcnum{$suffix} = $srcn;
         }
@@ -1621,7 +1745,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 
@@ -1666,6 +1789,7 @@ 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.', 
                 duplicate => 'Paste failed: only one instance of a particular published sequence or page is allowed within each course.',
             );
 
@@ -1719,8 +1843,9 @@ sub do_paste_from_buffer {
         if ($is_map{$suffix}) {
 # If pasting a map, check if map contains other maps
             my (%hierarchy,%titles);
-            &contained_map_check($url,$folder,\%removefrommap,\%removeparam,
-                                 \%addedmaps,\%hierarchy,\%titles,$allmaps);
+            &contained_map_check($url,$folder,$coursenum,$coursedom,
+                                 \%removefrommap,\%removeparam,\%addedmaps,
+                                 \%hierarchy,\%titles,$allmaps);
             if ($url=~ m{^/uploaded/}) {
                 my $newurl;
                 unless ($env{'form.docs.markedcopy_options_'.$suffix} eq 'move') {
@@ -1773,15 +1898,20 @@ sub do_paste_from_buffer {
                 }
             }
         }
-        if ($url=~ m{/(bulletinboard|smppg)$}) {
+        if ($url=~ m{/(bulletinboard|smppg|ext\.tool)$}) {
             my $prefix = $1;
-            my $fromothercrs; 
+            my $fromothercrs;
             #need to copy the db contents to a new one, unless this is a move.
             my %info = (
                          src  => $url,
                          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;
@@ -1790,7 +1920,7 @@ sub do_paste_from_buffer {
                 }
             }
             unless (($env{'form.docs.markedcopy_options_'.$suffix} eq 'move') && (!$fromothercrs)) {
-                my (%lockerr,$msg); 
+                my (%lockerr,$msg);
                 my ($newurl,$result,$errtext) =
                     &dbcopy(\%info,$coursedom,$coursenum,\%lockerr);
                 if ($result eq 'ok') {
@@ -1801,6 +1931,8 @@ sub do_paste_from_buffer {
                         $msg = &mt('Paste failed: An error occurred when copying the simple page.').' '.$errtext;
                     } elsif ($prefix eq 'bulletinboard') {
                         $msg = &mt('Paste failed: An error occurred when copying the discussion board.').' '.$errtext;
+                    } elsif ($prefix eq 'ext.tool') {
+                        $msg = &mt('Paste failed: An error occurred when copying the external tool.').' '.$errtext;
                     }
                     $results{$suffix} = $result;
                     $msgerrs{$suffix} = $msg;
@@ -1847,8 +1979,8 @@ sub do_paste_from_buffer {
                     if ($newdocsdir eq '') {
                         $newdocsdir = 'default';
                     }
-                    if (($prefixchg{$suffix}) || 
-                        ($srcdom{$suffix} ne $coursedom) || 
+                    if (($prefixchg{$suffix}) ||
+                        ($srcdom{$suffix} ne $coursedom) ||
                         ($srcnum{$suffix} ne $coursenum) ||
                         ($env{'form.docs.markedcopy_options_'.$suffix} ne 'move')) {
                         my $newpath = "$newprefix/$newdocsdir/$newidx/$rem";
@@ -1871,6 +2003,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';
@@ -2060,8 +2204,11 @@ sub dbcopy {
     my ($url,$result,$errtext);
     if (ref($dbref) eq 'HASH') {
         $url = $dbref->{'src'};
-        if ($url =~ m{/(smppg|bulletinboard)$}) {
+        if ($url =~ m{/(smppg|bulletinboard|ext\.tool)$}) {
             my $prefix = $1;
+            if ($prefix eq 'ext.tool') {
+                $prefix = 'exttool';
+            }
             if (($dbref->{'cdom'} =~ /^$match_domain$/) && 
                 ($dbref->{'cnum'} =~ /^$match_courseid$/)) {
                 my $db_name;
@@ -2072,6 +2219,8 @@ sub dbcopy {
                         &Apache::lonsimplepage::get_db_name($url,$marker,
                                                             $dbref->{'cdom'},
                                                             $dbref->{'cnum'});
+                } elsif ($dbref->{'src'} =~ m{/ext\.tool$}) {
+                    $db_name = 'exttool_'.$marker;
                 } else {
                     $db_name = 'bulletinpage_'.$marker;
                 }
@@ -2082,6 +2231,8 @@ sub dbcopy {
                 if (!$suffix) {
                     if ($prefix eq 'smppg') {
                         $errtext = &mt('Failed to acquire a unique timestamp-based suffix when copying a simple page [_1].',$url);
+                    } elsif ($prefix eq 'exttool') {
+                        $errtext = &mt('Failed to acquire a unique timestamp-based suffix when copying an external tool [_1].',$url);
                     } else {
                         $errtext = &mt('Failed to acquire a unique timestamp-based suffix when copying a discussion board [_1].',$url);
                     }
@@ -2112,10 +2263,13 @@ sub dbcopy {
                         }
                     }
                     $db_name =~ s{_\d*$ }{_$suffix}x;
+                    if (($prefix eq 'exttool') && ($dbref->{'delgradable'}) && ($contents{'gradable'})) {
+                        delete($contents{'gradable'});
+                    }
                     $result=&Apache::lonnet::put($db_name,\%contents,
                                                  $coursedom,$coursenum);
                     if ($result eq 'ok') {
-                        $url =~ s{/(\d*)/(smppg|bulletinboard)$}{/$suffix/$2}x;
+                        $url =~ s{/(\d*)/(smppg|bulletinboard|ext\.tool)$}{/$suffix/$2}x;
                     }
                 }
                 if (($freedlock ne 'ok') && (ref($lockerrorsref) eq 'HASH')) {
@@ -2125,6 +2279,9 @@ sub dbcopy {
                     if ($prefix eq 'smppg') {
                         $lockerrorsref->{$prefix} .=
                             ' '.&mt('This will prevent creation of additional simple pages in this course.');
+                    } elsif ($prefix eq 'exttool') {
+                        $lockerrorsref->{$prefix} .=
+                            ' '.&mt('This will prevent addition of more external tools to this course.');
                     } else {
                         $lockerrorsref->{$prefix} .= ' '.&mt('This will prevent creation of additional discussion boards in this course.');
                     }
@@ -2254,8 +2411,8 @@ sub uniqueness_check {
 }
 
 sub contained_map_check {
-    my ($url,$folder,$removefrommap,$removeparam,$addedmaps,$hierarchy,$titles,
-        $allmaps) = @_;
+    my ($url,$folder,$coursenum,$coursedom,$removefrommap,$removeparam,$addedmaps,
+        $hierarchy,$titles,$allmaps) = @_;
     my $content = &Apache::lonnet::getfile($url);
     unless ($content eq '-1') {
         my $parser = HTML::TokeParser->new(\$content);
@@ -2265,7 +2422,13 @@ 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 = $1;
+                    unless ($srcdom eq $coursedom) {
+                        $removefrommap->{$url}{$token->[2]->{'id'}} = $ressrc;
+                        next;
+                    }
+                } elsif ($folder =~ /^supplemental/) {
                     unless (&supp_pasteable($ressrc)) {
                         $removefrommap->{$url}{$token->[2]->{'id'}} = $ressrc;
                         next;
@@ -2284,8 +2447,8 @@ sub contained_map_check {
                             $addedmaps->{$ressrc} = [$url];
                         }
                     }
-                    &contained_map_check($ressrc,$folder,$removefrommap,$removeparam,
-                                         $addedmaps,$hierarchy,$titles,$allmaps);
+                    &contained_map_check($ressrc,$folder,$coursenum,$coursedom,$removefrommap,
+                                         $removeparam,$addedmaps,$hierarchy,$titles,$allmaps);
                 }
             } elsif ($token->[1] eq 'param') {
                 if ($folder =~ /^supplemental/) {
@@ -2387,16 +2550,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 (($rem =~ m{\d+/ext\.tool$}) &&
+                         ($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) ||
@@ -2660,7 +2845,8 @@ sub apply_fixups {
                     }
                 }
             }
-            for (my $i=0; $i<@LONCAPA::map::order; $i++) {
+            my $total = scalar(@LONCAPA::map::order) - 1;
+            for (my $i=$total; $i>=0; $i--) {
                 my $idx = $LONCAPA::map::order[$i];
                 if (defined($LONCAPA::map::resources[$idx])) {
                     my $changed;
@@ -2670,7 +2856,7 @@ sub apply_fixups {
                         splice(@LONCAPA::map::order,$i,1);
                         if (ref($currparam{$idx}) eq 'ARRAY') {
                             foreach my $name (@{$currparam{$idx}}) {
-                                &LONCAPA::map::delparameter($idx,'parameter_'.$name);
+                                &LONCAPA::map::delparameter($idx,$name);
                             }
                         }
                         next;
@@ -2712,7 +2898,7 @@ sub apply_fixups {
             foreach my $idx (keys(%remparam)) {
                 if (ref($remparam{$idx}) eq 'ARRAY') {
                     foreach my $name (@{$remparam{$idx}}) {   
-                        &LONCAPA::map::delparameter($idx,'parameter_'.$name);
+                        &LONCAPA::map::delparameter($idx,$name);
                     }
                 }
             }
@@ -2820,7 +3006,7 @@ sub update_parameter {
                          'randomorder'    => {},
                       );
         foreach my $which (keys(%allchecked)) {
-            $env{'form.all'.$which} =~ s/,$//;   
+            $env{'form.all'.$which} =~ s/,$//;
             if ($which eq 'randompick') {
                 foreach my $item (split(/,/,$env{'form.all'.$which})) {
                     my ($res,$value) = split(/:/,$item);
@@ -2923,8 +3109,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'});
 
@@ -2939,19 +3126,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/) {
@@ -2961,17 +3148,32 @@ 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;
+    } elsif ($cmd eq 'setalias') {
+        my $newvalue = $env{'form.alias'};
+        if ($newvalue ne '') {
+            unless (Apache::lonnet::get_symb_from_alias($newvalue)) {
+                &LONCAPA::map::storeparameter($idx,'parameter_0_mapalias',$newvalue,
+                                              'string');
+                &remember_parms($idx,'mapalias','set',$newvalue);
+                $haschanges = 1;
+            }
+        }
+    } elsif ($cmd eq 'delalias') {
+        my $current = (&LONCAPA::map::getparameter($idx,'parameter_0_mapalias'))[0];  
+        if ($current ne '') {
+            &LONCAPA::map::delparameter($idx,'parameter_0_mapalias');
+            &remember_parms($idx,'mapalias','del');
+            $haschanges = 1;
+        }
     }
-    return 1;
+    return $haschanges;
 }
 
 sub editor {
     my ($r,$coursenum,$coursedom,$folder,$allowed,$upload_output,$crstype,
         $supplementalflag,$orderhash,$iconpath,$pathitem,$ltitoolsref,
-        $canedit,$navmapref,$hiddentop)=@_;
+        $canedit,$hostname,$navmapref,$hiddentop)=@_;
     my ($randompick,$ishidden,$isencrypted,$plain,$is_random_order,$container);
     if ($allowed) {
         (my $breadcrumbtrail,$randompick,$ishidden,$isencrypted,$plain,
@@ -3089,7 +3291,7 @@ sub editor {
 # Rename, cut, copy or remove a single resource
 	if (&handle_edit_cmd($coursenum,$coursedom)) {
             my $contentchg;
-            if ($env{'form.cmd'} =~ m{^(remove|cut)_}) {
+            if ($env{'form.cmd'} =~ m{^(remove|cut|setalias|delalias)_}) {
                 $contentchg = 1;
             }
 	    ($errtext,$fatal)=&storemap($coursenum,$coursedom,$folder.'.'.$container,$contentchg);
@@ -3182,7 +3384,7 @@ sub editor {
                         } else {
                             return $errortxt;
                         }
-                    } elsif ($url =~ m{^/adm/$coursedom/$coursenum/new/exttool}) {
+                    } elsif ($url =~ m{^/adm/$coursedom/$coursenum/new/ext\.tool}) {
                         my ($suffix,$errortxt,$locknotfreed) =
                             &new_timebased_suffix($coursedom,$coursenum,'exttool');
                         if ($locknotfreed) {
@@ -3297,10 +3499,12 @@ sub editor {
         if ($url =~ m{/uploaded/.+\.(page|sequence)$}) {
             push(@allmapidx,$res);
         }
+
         $output .= &entryline($idx,$name,$url,$folder,$allowed,$res,
                               $coursenum,$coursedom,$crstype,
                               $pathitem,$supplementalflag,$container,
-                              \%filters,\%curr_groups,$ltitoolsref,$canedit,$isencrypted,$navmapref);
+                              \%filters,\%curr_groups,$ltitoolsref,$canedit,
+                              $isencrypted,$navmapref,$hostname);
         $idx++;
         $shown++;
     }
@@ -3327,10 +3531,10 @@ sub editor {
                           .&Apache::loncommon::start_data_table(undef,'contentlist')
                           .&Apache::loncommon::start_data_table_header_row()
                           .'<th colspan="2">'.&mt('Move').'</th>'
-                          .'<th colspan="2">'.&mt('Actions').'</th>'
+                          .'<th colspan="3">'.&mt('Actions').'</th>'
                           .'<th>'.&mt('Document').'</th>';
                 if ($folder !~ /^supplemental/) {
-                    $to_show .= '<th colspan="4">'.&mt('Settings').'</th>';
+                    $to_show .= '<th colspan="2">'.&mt('Settings').'</th>';
                 }
                 $to_show .= &Apache::loncommon::end_data_table_header_row();
                 if ($folder !~ /^supplemental/) {
@@ -3348,19 +3552,18 @@ sub editor {
                     if (@allidx > 0) {
                         my $path;
                         if ($env{'form.folderpath'}) {
-                            $path = 
+                            $path =
                                 &HTML::Entities::encode($env{'form.folderpath'},'<>&"');
                         }
                         if (@allidx > 1) {
-                            $to_show .= 
+                            $to_show .=
                                 &Apache::loncommon::continue_data_table_row().
                                 '<td colspan="2">&nbsp;</td>'.
                                 '<td>'.
                                 &multiple_check_form('actions',\%lists,$canedit).
                                 '</td>'.
-                                '<td>&nbsp;</td>'.
-                                '<td>&nbsp;</td>'.
-                                '<td colspan="4">'.
+                                '<td colspan="3">&nbsp;</td>'.
+                                '<td colspan="2">'.
                                 &multiple_check_form('settings',\%lists,$canedit).
                                 '</td>'.
                                 &Apache::loncommon::end_data_table_row();
@@ -3600,7 +3803,7 @@ sub process_file_upload {
                     my $uploadphase = 'upload_embedded';
                     my $primaryurl = &HTML::Entities::encode($url,'<>&"');
 		    my $state = &embedded_form_elems($uploadphase,$primaryurl,$newidx); 
-                    my ($embedded,$num) = 
+                    my ($embedded,$num) =
                         &Apache::loncommon::ask_for_embedded_content(
                             '/adm/coursedocs',$state,$allfiles,$codebase,{'docs_url' => $url});
                     if ($embedded) {
@@ -3681,7 +3884,7 @@ 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)=@_;
+        $ltitoolsref,$canedit,$isencrypted,$navmapref,$hostname)=@_;
     my ($foldertitle,$renametitle,$oldtitle);
     if (&is_supplemental_title($title)) {
 	($title,$foldertitle,$renametitle) = &Apache::loncommon::parse_supplemental_title($title);
@@ -3708,7 +3911,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, $curralias);
     if ($env{'form.folderpath'}) {
 	$esc_path=&escape($env{'form.folderpath'});
 	$path = &HTML::Entities::encode($env{'form.folderpath'},'<>&"');
@@ -3777,6 +3980,8 @@ END
                 'ct' => 'Cut',
 		'rn' => 'Rename',
 		'cp' => 'Copy',
+                'da' => 'Unset alias', 
+                'sa' => 'Set alias',
                 'ex' => 'External Resource',
                 'et' => 'External Tool',
                 'ed' => 'Edit',
@@ -3784,7 +3989,7 @@ END
                 'sv' => 'Save',
                 'ul' => 'URL',
                 'ti' => 'Title',
-                'er' => 'Editing rights unavailable for your current role', 
+                'er' => 'Editing rights unavailable for your current role.', 
                 );
 	my %denied = &action_restrictions($coursenum,$coursedom,$url,
                                           $env{'form.folderpath'},
@@ -3798,7 +4003,7 @@ END
 			    |/aboutme$
 			    |/navmaps$
 			    |/bulletinboard$
-                            |/exttools?$
+                            |/ext\.tool$
 			    |\.html$)}x)
              || $isexternal) {
 	    $skip_confirm = 1;
@@ -3807,6 +4012,9 @@ END
             ($url!~/$LONCAPA::assess_page_seq_re/)) {
             $confirm_removal = 1;
         }
+        if ($url =~ /$LONCAPA::assess_re/) {
+            $curralias = (&LONCAPA::map::getparameter($orderidx,'parameter_0_mapalias'))[0];
+        }
 
 	if ($denied{'copy'}) {
             $copylink=(<<ENDCOPY)
@@ -3930,7 +4138,7 @@ END
 	}
     }
 
-    my ($editlink,$extresform,$anchor);
+    my ($editlink,$extresform,$anchor,$hiddenres,$nomodal);
     my $orig_url = $url;
     $orig_url=~s{http(&colon;|:)//https(&colon;|:)//}{https$2//};
     $url=~s{^http(|s)(&colon;|:)//}{/adm/wrapper/ext/};
@@ -3948,10 +4156,23 @@ END
 	        } elsif ($url!~/\.(sequence|page)$/) {
 		    $url='/adm/coursedocs/showdoc'.$url;
 	        }
-	    } elsif ($url=~m|^/ext/|) {
-	        $url='/adm/wrapper'.$url;
-	    } elsif ($url=~m{^/adm/$coursedom/$coursenum/\d+/exttools?$}) {
+	    } elsif ($url=~m{^(|/adm/wrapper)/ext/([^#]+)}) {
+                my $wrapped = $1;
+                my $exturl = $2;
+                if ($wrapped eq '') {
+                    $url='/adm/wrapper'.$url;
+                }
+                if (($ENV{'SERVER_PORT'} == 443) && ($exturl !~ /^https:/)) {
+                    $nomodal = 1;
+                }
+	    } elsif ($url=~m{^/adm/$coursedom/$coursenum/\d+/ext\.tool$}) {
 		$url='/adm/wrapper'.$url;
+            } elsif ($url eq "/public/$coursedom/$coursenum/syllabus") {
+                if (($ENV{'SERVER_PORT'} == 443) &&
+                    ($env{'course.'.$env{'request.course.id'}.'.externalsyllabus'} =~ m{^http://})) {
+                    $url .= '?usehttp=1';
+                    $nomodal = 1;
+                }
             }
             if (&Apache::lonnet::symbverify($symb,$url)) {
                 my $shownsymb = $symb;
@@ -3964,13 +4185,24 @@ END
                         }
                     }
                 }
-                $url.=(($url=~/\?/)?'&':'?').'symb='.&HTML::Entities::encode($shownsymb,'"<>&');
+                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;
                 if (((&LONCAPA::map::getparameter($orderidx,'parameter_encrypturl'))[0]=~/^yes$/i) ||
                       $isencrypted || (&Apache::lonnet::EXT('resource.0.encrypturl',$symb) =~ /^yes$/i)) {
                     $checkencrypt = 1;
-                } else {
+                } elsif (ref($navmapref)) {
                     unless (ref($$navmapref)) {
                         $$navmapref = Apache::lonnavmaps::navmap->new();
                     }
@@ -3984,7 +4216,7 @@ END
                     my $shownsymb = &Apache::lonenc::encrypted($symb);
                     my $shownurl = &Apache::lonenc::encrypted($url);
                     if (&Apache::lonnet::symbverify($shownsymb,$shownurl)) {
-                        $url = $shownurl.(($shownurl=~/\?/)?'&':'?').'symb='.&HTML::Entities::encode($shownsymb,'"<>&');
+                        $url = $shownurl.(($shownurl=~/\?/)?'&':'?').'symb='.&escape($shownsymb);
                         if ($env{'request.enc'} ne '') {
                             delete($env{'request.enc'});
                         }
@@ -3998,15 +4230,31 @@ END
                 $url='';
             }
 	}
-    } elsif ($supplementalflag) { 
+    } elsif ($supplementalflag) {
         if ($isexternal) {
             if ($url =~ /^([^#]+)#([^#]+)$/) {
                 $url = $1;
                 $anchor = $2;
+                if (($url =~ m{^(|/adm/wrapper)/ext/(?!https:)}) && ($ENV{'SERVER_PORT'} == 443)) {
+                    if ($hostname ne '') {
+                        $url = 'http://'.$hostname.$url;
+                    }
+                    $nomodal = 1;
+                }
+            }
+        } elsif ($url =~ m{^\Q/public/$coursedom/$coursenum/syllabus\E}) {
+            if (($ENV{'SERVER_PORT'} == 443) &&
+                ($env{'course.'.$env{'request.course.id'}.'.externalsyllabus'} =~ m{^http://})) {
+                if ($hostname ne '') {
+                    $url = 'http://'.$hostname.$url;
+                }
+                $url .= (($url =~ /\?/) ? '&amp;':'?').'usehttp=1';
+                $nomodal = 1;
             }
         }
     }
     my ($rand_pick_text,$rand_order_text,$hiddenfolder);
+    my $filterFunc = sub { my $res = shift; return (!$res->randomout() && !$res->is_map()) };
     if ($isfolder || $ispage || $extension eq 'sequence' || $extension eq 'page') {
 	my $foldername=&escape($foldertitle);
 	my $folderpath=$env{'form.folderpath'};
@@ -4015,39 +4263,48 @@ END
             $folderpath.=$containerarg.'&'.$foldername;
             $url.='folderpath='.&escape($folderpath);
         } else {
-# Append randompick number, hidden, and encrypted with ":" to foldername,
-# so it gets transferred between levels
-	    $folderpath.=$containerarg.'&'.$foldername.
-                                              ':'.(&LONCAPA::map::getparameter($orderidx,
-                                              'parameter_randompick'))[0]
-                                               .':'.((&LONCAPA::map::getparameter($orderidx,
-                                              'parameter_hiddenresource'))[0]=~/^yes$/i)
-                                               .':'.((&LONCAPA::map::getparameter($orderidx,
-                                              'parameter_encrypturl'))[0]=~/^yes$/i)
-                                               .':'.((&LONCAPA::map::getparameter($orderidx,
-                                              'parameter_randomorder'))[0]=~/^yes$/i)
-                                               .':'.$ispage;
-            if (!$env{'request.role.adv'}) {
-                if (lc((&LONCAPA::map::getparameter($orderidx,'parameter_hiddenresource'))[0]) eq 'yes') {
-                    $url = '';
-                    $hiddenfolder = 1;
-                } else {
+            my $rpicknum = (&LONCAPA::map::getparameter($orderidx,
+                                                        'parameter_randompick'))[0];
+            my $randorder = ((&LONCAPA::map::getparameter($orderidx,
+                                              'parameter_randomorder'))[0]=~/^yes$/i);
+            my $hiddenmap = ((&LONCAPA::map::getparameter($orderidx,
+                                              'parameter_hiddenresource'))[0]=~/^yes$/i);
+            my $encryptmap = ((&LONCAPA::map::getparameter($orderidx,
+                                              'parameter_encrypturl'))[0]=~/^yes$/i);
+            unless ($hiddenmap) {
+                if (ref($navmapref)) {
                     unless (ref($$navmapref)) {
                         $$navmapref = Apache::lonnavmaps::navmap->new();
                     }
                     if (ref($$navmapref)) {
                         if (lc($$navmapref->get_mapparam(undef,$folderurl,"0.hiddenresource")) eq 'yes') {
-                            $url = '';
-                            $hiddenfolder = 1;
+                            my @resources = $$navmapref->retrieveResources($folderurl,$filterFunc,1,1);
+                            unless (@resources) {
+                                $hiddenmap = 1;
+                                unless ($env{'request.role.adv'}) {  
+                                    $url = '';
+                                    $hiddenfolder = 1;
+                                }
+                            }
                         }
                     }
                 }
             }
+            unless ($encryptmap) {
+                if ((ref($navmapref)) && (ref($$navmapref))) {
+                    if (lc($$navmapref->get_mapparam(undef,$folderurl,"0.encrypturl")) eq 'yes') {
+                        $encryptmap = 1;
+                    }
+                }
+            }
+
+# Append randompick number, hidden, and encrypted with ":" to foldername,
+# so it gets transferred between levels
+	    $folderpath.=$containerarg.'&'.$foldername.
+                         ':'.$rpicknum.':'.$hiddenmap.':'.$encryptmap.':'.$randorder.':'.$ispage;
             unless ($url eq '') {
                 $url.='folderpath='.&escape($folderpath);
             }
-            my $rpicknum = (&LONCAPA::map::getparameter($orderidx,
-                                                        'parameter_randompick'))[0];
             my $rpckchk;
             if ($rpicknum) {
                 $rpckchk = ' checked="checked"';
@@ -4067,7 +4324,7 @@ $form_common."\n".
             $rand_pick_text .= '</span></span>'.
                                $form_end;
             my $ro_set;
-            if ((&LONCAPA::map::getparameter($orderidx,'parameter_randomorder'))[0]=~/^yes$/i) {
+            if ($randorder) {
                 $ro_set = 'checked="checked"';
                 if (($ishash) && (ref($filtersref->{'randomorder'}) eq 'ARRAY')) {
                     push(@{$filtersref->{'randomorder'}},$orderidx);
@@ -4083,7 +4340,7 @@ $form_end;
         }
     } elsif ($supplementalflag && !$allowed) {
         my $isexttool;
-        if ($url=~m{^/adm/$coursedom/$coursenum/\d+/exttools?$}) {
+        if ($url=~m{^/adm/$coursedom/$coursenum/\d+/ext\.tool$}) {
             $url='/adm/wrapper'.$url;
             $isexttool = 1;
         }
@@ -4101,14 +4358,14 @@ $form_end;
     }
     my ($tdalign,$tdwidth);
     if ($allowed) {
-        my $fileloc = 
+        my $fileloc =
             &Apache::lonnet::declutter(&Apache::lonnet::filelocation('',$orig_url));
         if ($isexternal) {
-            ($editlink,$extresform) = 
+            ($editlink,$extresform) =
                 &Apache::lonextresedit::extedit_form(0,$residx,$orig_url,$title,$pathitem,
                                                      undef,undef,undef,undef,undef,undef,
                                                      undef,$disabled);
-        } elsif ($orig_url =~ m{^/adm/$coursedom/$coursenum/\d+/exttools?$}) {
+        } elsif ($orig_url =~ m{^/adm/$coursedom/$coursenum/\d+/ext\.tool$}) {
             ($editlink,$extresform) =
                 &Apache::lonextresedit::extedit_form(0,$residx,$orig_url,$title,$pathitem,
                                                      undef,undef,undef,'tool',$coursedom,
@@ -4121,13 +4378,14 @@ $form_end;
                 if ($supplementalflag) {
                     $suppanchor = $anchor;
                 }
-                my $jscall = 
+                my $jscall =
                     &Apache::lonhtmlcommon::jump_to_editres($cfile,$home,
                                                             $switchserver,
                                                             $forceedit,
                                                             undef,$symb,
                                                             &escape($env{'form.folderpath'}),
-                                                            $renametitle,'','',1,$suppanchor);
+                                                            $renametitle,$hostname,
+                                                            '','',1,$suppanchor);
                 if ($jscall) {
                     $editlink = '<a class="LC_docs_ext_edit" href="javascript:'.
                                 $jscall.'" >'.&mt('Edit').'</a>&nbsp;'."\n";
@@ -4144,6 +4402,18 @@ $form_end;
         $reinit = &mt('(re-initialize course to access)');
     }
     $line.='<td class="LC_docs_entry_commands"'.$tdalign.'><span class="LC_nobreak">'.$editlink.$renamelink;
+    if ($orig_url =~ /$LONCAPA::assess_re/) {
+        $line.= '<br />';
+        if ($curralias ne '') {
+            $line.='<span class="LC_nobreak"><a href="javascript:delalias('."'$esc_path','$orderidx'".');" class="LC_docs_alias">'.
+                   $lt{'da'}.'</a></span>';
+        } else {
+            $line.='<span class="LC_nobreak"><a href="javascript:setalias('."'$esc_path','$orderidx'".');" class="LC_docs_alias">'.
+                   $lt{'sa'}.'</a></span>';
+        }
+    }
+    $line.='</td><td>';
+    my $link;
     if (($url=~m{/adm/(coursedocs|supplemental)}) || (!$allowed && $url)) {
        $line.='<a href="'.$url.'"><img src="'.$icon.'" alt="" class="LC_icon" /></a>';
     } elsif ($url) {
@@ -4154,9 +4424,20 @@ $form_end;
                $anchor = '#'.&HTML::Entities::encode($anchor,'"<>&');
            }
        }
-       $line.=&Apache::loncommon::modal_link(&js_escape($url.(($url=~/\?/)?'&amp;':'?').'inhibitmenu=yes'.
-                                             (($anchor ne '')?$anchor:'')),
-                                             '<img src="'.$icon.'" alt="" class="LC_icon" />',600,500);
+       if ((!$supplementalflag) && ($nomodal) && ($hostname ne '')) {
+           $link = 'http://'.$hostname.$url;
+       } else {
+           $link = $url;
+       }
+       $link = &js_escape($link.(($url=~/\?/)?'&amp;':'?').'inhibitmenu=yes'.
+                                               (($anchor ne '')?$anchor:''));
+       if ($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 {
+           $line.=&Apache::loncommon::modal_link($link,
+                                                 '<img src="'.$icon.'" alt="" class="LC_icon" />',600,500);
+       }
     } else {
        $line.='<img src="'.$icon.'" alt="" class="LC_icon" />';
     }
@@ -4164,15 +4445,23 @@ $form_end;
     if (($url=~m{/adm/(coursedocs|supplemental)}) || (!$allowed && $url)) {
        $line.='<a href="'.$url.'">'.$title.'</a>';
     } elsif ($url) {
-       $line.=&Apache::loncommon::modal_link(&js_escape($url.(($url=~/\?/)?'&amp;':'?').'inhibitmenu=yes'.
-                                             (($anchor ne '')?$anchor:'')),
-                                             $title,600,500);
-    } elsif ($hiddenfolder) {
-       $line.=$title.' <span class="LC_warning LC_docs_reinit_warn">'.&mt('(Hidden)').'</span>';
+       if ($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 {
+           $line.=&Apache::loncommon::modal_link($link,$title,600,500);
+       }
+    } elsif (($hiddenfolder) || ($hiddenres)) {
+       $line.=$title.' <span class="LC_warning LC_docs_reinit_warn">('.&mt('Hidden').')</span>';
     } else {
        $line.=$title.' <span class="LC_docs_reinit_warn">'.$reinit.'</span>';
     }
-    $line.="$extresform</td>";
+    if (($allowed) && ($curralias ne '')) {
+        $line .= '<br /><span class="LC_docs_alias_name">('.$curralias.')</span>';
+    } else {
+        $line .= $extresform;
+    }
+    $line .= '</td>';
     $rand_pick_text = '&nbsp;' if ($rand_pick_text eq '');
     $rand_order_text = '&nbsp;' if ($rand_order_text eq '');
     if (($allowed) && ($folder!~/^supplemental/)) {
@@ -4307,6 +4596,8 @@ sub new_timebased_suffix {
             $errtext = &mt('Failed to acquire a unique timestamp-based suffix for the new folder/page.');
         } elsif ($type eq 'smppg') {
             $errtext = &mt('Failed to acquire a unique timestamp-based suffix for the new simple page.');
+        } elsif ($type eq 'exttool') {
+            $errtext = &mt('Failed to acquire a unique timestamp-based suffix for the new external tool.');
         } else {
             $errtext = &mt('Failed to acquire a unique timestamp-based suffix for the new discussion board.');
         }
@@ -4315,7 +4606,7 @@ sub new_timebased_suffix {
         }
     }
     if ($freedlock ne 'ok') {
-        $locknotfreed = 
+        $locknotfreed =
             '<div class="LC_error">'.
             &mt('There was a problem removing a lockfile.').' ';
         if ($type eq 'paste') {
@@ -4335,6 +4626,9 @@ sub new_timebased_suffix {
         } elsif ($type eq 'smppg') {
             $locknotfreed .=
                 &mt('This will prevent creation of additional simple pages in this course.');
+        } elsif ($type eq 'exttool') {
+            $locknotfreed .=
+                &mt('This will prevent creation of additional external tools in this course.');
         } else {
             $locknotfreed .=
                 &mt('This will prevent creation of additional discussion boards in this course.');
@@ -4385,7 +4679,7 @@ sub untiehash {
 
 
 sub checkonthis {
-    my ($r,$url,$level,$title)=@_;
+    my ($r,$url,$level,$title,$checkstale)=@_;
     $url=&unescape($url);
     $alreadyseen{$url}=1;
     $r->rflush();
@@ -4400,10 +4694,22 @@ sub checkonthis {
        $r->print('<a href="'.$url.'" target="cat">'.
 		 ($title?$title:$url).'</a> ');
        if ($url=~/^\/res\//) {
+          my $updated;
+          if (($checkstale) && ($url !~ m{^/res/lib/templates/}) &&
+              ($url !~ /\.\d+\.\w+$/)) {
+              $updated = &Apache::lonnet::remove_stale_resfile($url);
+          }
 	  my $result=&Apache::lonnet::repcopy(
                               &Apache::lonnet::filelocation('',$url));
           if ($result eq 'ok') {
              $r->print('<span class="LC_success">'.&mt('ok').'</span>');
+             if ($updated) {
+                 $r->print('<br />');
+                 for (my $i=0;$i<=$level*5;$i++) {
+                     $r->print('&nbsp;');
+                 }
+                 $r->print('- '.&mt('Outdated copy removed'));
+             }
              $r->rflush();
              &Apache::lonnet::countacc($url);
              $url=~/\.(\w+)$/;
@@ -4437,7 +4743,7 @@ sub checkonthis {
                 &Apache::lonnet::metadata($url,'dependencies');
              foreach my $dep (split(/\,/,$dependencies)) {
 		 if (($dep=~/^\/res\//) && (!$alreadyseen{$dep})) {
-                    &checkonthis($r,$dep,$level+1);
+                    &checkonthis($r,$dep,$level+1,'',$checkstale);
                  }
              }
           } elsif ($result eq 'unavailable') {
@@ -4451,6 +4757,9 @@ sub checkonthis {
           } else {
              $r->print('<span class="LC_error">'.&mt('access denied').'</span>');
           }
+          if (($updated) && ($result ne 'ok')) {
+              $r->print('<br />'.&mt('Outdated copy removed'));
+          }
        }
     }
 }
@@ -4503,9 +4812,29 @@ sub list_symbs {
     $r->print(&endContentScreen());
 }
 
+sub contentverifyform {
+    my ($r) = @_;
+    my $crstype = &Apache::loncommon::course_type();
+    $r->print(&Apache::loncommon::start_page('Verify '.$crstype.' Content'));
+    $r->print(&Apache::lonhtmlcommon::breadcrumbs('Verify '.$crstype.' Content'));
+    $r->print(&startContentScreen('tools'));
+    $r->print('<h4 class="LC_info">'.&mt($crstype.' content verification').'</h4>');
+    $r->print('<form method="post" action="/adm/coursedocs"><p>'.
+              &mt('Include a check if files copied from elsewhere are up to date (will increase verification time)?').
+              '&nbsp;<span class="LC_nobreak">'.
+              '<label><input type="radio" name="checkstale" value="0" checked="checked" />'.
+              &mt('No').'</label>'.('&nbsp;'x2).
+              '<label><input type="radio" name="checkstale" value="1" />'.
+              &mt('Yes').'</label></span></p><p>'.
+              '<input type="submit" value="'.&mt('Verify content').' "/>'.
+              '<input type="hidden" value="1" name="tools" />'.
+              '<input type="hidden" value="1" name="verify" /></p></form>');
+    $r->print(&endContentScreen());
+    return;
+}
 
 sub verifycontent {
-    my ($r) = @_;
+    my ($r,$checkstale) = @_;
     my $crstype = &Apache::loncommon::course_type();
     $r->print(&Apache::loncommon::start_page('Verify '.$crstype.' Content'));
     $r->print(&Apache::lonhtmlcommon::breadcrumbs('Verify '.$crstype.' Content'));
@@ -4526,7 +4855,7 @@ sub verifycontent {
 	   }
        }
        if (($key=~/^src\_(.+)$/) && (!$alreadyseen{&unescape($hash{$key})})) {
-           &checkonthis($r,$hash{$key},0,$hash{'title_'.$1});
+           &checkonthis($r,$hash{$key},0,$hash{'title_'.$1},$checkstale);
        }
    }
    &untiehash();
@@ -4534,7 +4863,6 @@ sub verifycontent {
     $r->print(&endContentScreen());
 }
 
-
 sub devalidateversioncache {
     my $src=shift;
     &Apache::lonnet::devalidate_cache_new('courseresversion',$env{'request.course.id'}.'_'.
@@ -4960,12 +5288,12 @@ sub handler {
 #
 # --------------------------------------------- Initialize help topics for this
     foreach my $topic ('Adding_Course_Doc','Main_Course_Documents',
-	               'Adding_External_Resource','Navigate_Content',
-	               'Adding_Folders','Docs_Overview', 'Load_Map',
-	               'Supplemental','Score_Upload_Form','Adding_Pages',
-	               'Importing_LON-CAPA_Resource','Importing_IMS_Course',
-                       'Uploading_From_Harddrive','Course_Roster','Web_Page',
-                       'Dropbox','Simple_Problem') {
+	               'Adding_External_Resource','Adding_External_Tool',
+                       'Navigate_Content','Adding_Folders','Docs_Overview',
+	               'Load_Map','Supplemental','Score_Upload_Form',
+	               'Adding_Pages','Importing_LON-CAPA_Resource',
+	               'Importing_IMS_Course','Uploading_From_Harddrive',
+                       'Course_Roster','Web_Page','Dropbox','Simple_Problem') {
 	$help{$topic}=&Apache::loncommon::help_open_topic('Docs_'.$topic);
     }
     # Composite help files
@@ -4997,9 +5325,25 @@ sub handler {
         $disabled = ' disabled="disabled"';
     }
     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['inhibitmenu']);
+    if ($env{'form.inhibitmenu'}) {
+        unless ($env{'form.inhibitmenu'} eq 'yes') {
+            delete($env{'form.inhibitmenu'});
+        }
+    }
+
   if ($allowed && $env{'form.verify'}) {
       &init_breadcrumbs('verify','Verify Content','Docs_Verify_Content');
-      &verifycontent($r);
+      if (!$canedit) {
+          &verifycontent($r);
+      } elsif (($env{'form.checkstale'} ne '') && ($env{'form.checkstale'} =~ /^\d$/)) {
+          &Apache::lonhtmlcommon::add_breadcrumb({href=>"/adm/coursedocs?tools=1&verify=1&checkstale=$env{'form.checkstale'}",
+                                                  text=>'Results',
+                                                  faq=>273,
+                                                  bug=>'Instructor Interface'});
+          &verifycontent($r,$env{'form.checkstale'});
+      } else {
+          &contentverifyform($r);
+      }
   } elsif ($allowed && $env{'form.listsymbs'}) {
       &init_breadcrumbs('listsymbs','List Content IDs');
       &list_symbs($r);
@@ -5079,6 +5423,26 @@ sub handler {
                                              'forcesupplement','forcestandard',
                                              'tools','symb','command','supppath']);
 
+    foreach my $item ('forcesupplement','forcestandard','tools') {
+        next if ($env{'form.'.$item} eq '');
+        unless ($env{'form.'.$item} eq '1') {
+            delete($env{'form.'.$item});
+        }
+    }
+
+    if ($env{'form.command'}) {
+        unless ($env{'form.command'} =~ /^(direct|directnav|editdocs|editsupp|contents|home)$/) {
+            delete($env{'form.command'});
+        }
+    }
+
+    if ($env{'form.symb'}) {
+        my ($mapurl,$id,$resurl) = &Apache::lonnet::decode_symb($env{'form.symb'});
+        unless (($id =~ /^\d+$/) && (&Apache::lonnet::is_on_map($resurl))) { 
+            delete($env{'form.symb'});
+        }
+    }
+
 # standard=1: this is a "new-style" course with an uploaded map as top level
 # standard=2: this is a "old-style" course, and there is nothing we can do
 
@@ -5101,6 +5465,38 @@ sub handler {
     my $toolsflag=0;
     if ($env{'form.tools'}) { $toolsflag=1; }
 
+    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'});
+        }
+    }
+
+    if ($env{'form.supppath'} ne '') {
+        my @items = split(/\&/,$env{'form.supppath'});
+        my $badpath;
+        for (my $i=0; $i<@items; $i++) {
+            my $odd = $i%2;
+            if ((!$odd) && ($items[$i] !~ /^supplemental(|_\d+)$/)) {
+                $badpath = 1; 
+            }
+            last if ($badpath);
+        }
+        if ($badpath) {
+            delete($env{'form.supppath'});
+        }
+    }
+
     my $script='';
     my $showdoc=0;
     my $addentries = {};
@@ -5108,15 +5504,15 @@ sub handler {
     my $containertag;
     my $pathitem;
     my %ltitools;
+    my $hiddentop;
     my $navmap;
-    my $hiddentop; 
+    my $filterFunc = sub { my $res = shift; return (!$res->randomout() && !$res->is_map()) };
 
 # Do we directly jump somewhere?
-
    if (($env{'form.command'} eq 'direct') || ($env{'form.command'} eq 'directnav')) {
        if ($env{'form.symb'} ne '') {
            $env{'form.folderpath'}=
-               &Apache::loncommon::symb_to_docspath($env{'form.symb'});
+               &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 '') {
@@ -5125,12 +5521,10 @@ sub handler {
                $env{'form.command'}.'_'.$env{'form.supppath'}});
        }
    } elsif ($env{'form.command'} eq 'editdocs') {
-       $env{'form.folderpath'} = 'default&'.
-                                 &escape(&mt('Main Content').':::::');
+       $env{'form.folderpath'} = &default_folderpath($coursenum,$coursedom,\$navmap);
        &Apache::lonnet::appenv({'docs.exit.'.$env{'request.course.id'} => $env{'form.command'}});
    } elsif ($env{'form.command'} eq 'editsupp') {
-       $env{'form.folderpath'} = 'supplemental&'.
-                                  &escape('Supplemental Content');
+       $env{'form.folderpath'} = &supplemental_base();
        &Apache::lonnet::appenv({'docs.exit.'.$env{'request.course.id'} => '/adm/supplemental'});
    } elsif ($env{'form.command'} eq 'contents') {
        &Apache::lonnet::appenv({'docs.exit.'.$env{'request.course.id'} => '/adm/navmaps'});
@@ -5178,22 +5572,27 @@ sub handler {
         ($env{'form.folderpath'} ne '') && (!$supplementalflag)) {
         my $folderurl;
         my @pathitems = split(/\&/,$env{'form.folderpath'});
-        my $container = $pathitems[-2];
-        if ($container ne '') {
-            $folderurl = "uploaded/$coursedom/$coursenum/$container";
+        my $folder = $pathitems[-2];
+        if ($folder eq '') {
+            undef($env{'form.folderpath'});
+        } else {
+            $folderurl = "uploaded/$coursedom/$coursenum/$folder";
             if ((split(/\:/,$pathitems[-1]))[4]) {
                 $folderurl .= '.page';
             } else {
                 $folderurl .= '.sequence';
             }
-            $navmap = Apache::lonnavmaps::navmap->new();
+            unless (ref($navmap)) {
+                $navmap = Apache::lonnavmaps::navmap->new();
+            }
             if (ref($navmap)) {
                 if (lc($navmap->get_mapparam(undef,$folderurl,"0.hiddenresource")) eq 'yes') {
-                    undef($env{'form.folderpath'});
+                    my @resources = $navmap->retrieveResources($folderurl,$filterFunc,1,1);
+                    unless (@resources) {
+                        undef($env{'form.folderpath'});
+                    }
                 }
             }
-        } else {
-            undef($env{'form.folderpath'});    
         }
     }
 
@@ -5202,29 +5601,11 @@ sub handler {
     unless ($env{'form.folderpath'}) {
        if ($supplementalflag) {
           $env{'form.folderpath'}=&supplemental_base();
-       } else {
-          $env{'form.folderpath'}='default&'.&escape(&mt('Main Content').
-                                  ':::::');
-          if (($allowed) && (!$env{'request.role.adv'})) {
-# If allowed and user's role is not advanced check entire course is not hidden
-              unless (ref($navmap)) {
-                  $navmap = Apache::lonnavmaps::navmap->new();
-              }
-              if (ref($navmap)) {
-                  if (lc($navmap->get_mapparam(undef,"uploaded/$coursedom/$coursenum/default.sequence",
-                                               "0.hiddenresource")) eq 'yes') {
-                      undef($env{'form.folderpath'});
-                      $hiddentop = 1;
-                      if ($env{'form.folder'}) {
-                          undef($env{'form.folder'});
-                      }
-                  }
-              }
-          }
+       } elsif ($allowed) {
+          ($env{'form.folderpath'},$hiddentop) = &default_folderpath($coursenum,$coursedom,\$navmap);
        }
     }
 
-
 # Store this
     unless ($toolsflag) {
         if (($allowed) && ($env{'form.folderpath'} ne '')) {
@@ -5245,8 +5626,12 @@ sub handler {
         } else {
             if ($env{'form.folder'} eq '' ||
                 $env{'form.folder'} eq 'supplemental') {
-                $folderpath='default&'.
-                            &escape(&mt('Main Content').':::::');
+                if ($env{'form.folder'} eq 'supplemental') {
+                    $folderpath=&supplemental_base();
+                } elsif (!$hiddentop) {
+                    $folderpath='default&'.
+                                 &escape(&mt('Main Content').':::::');
+                }
             }
         }
         $containertag = '<input type="hidden" name="folderpath" value="" />';
@@ -5296,10 +5681,11 @@ sub handler {
                 }
             }
             my $tabidstr = join("','",@tabids);
-            %ltitools = &Apache::lonnet::get_domain_ltitools($coursedom);
+            %ltitools = &Apache::lonnet::get_domain_lti($coursedom,'consumer');
             my $posslti = keys(%ltitools);
+            my $hostname = $r->hostname();
 	    $script .= &editing_js($udom,$uname,$supplementalflag,$coursedom,$coursenum,$posslti,
-                                   $londocroot,$canedit).
+                                   $londocroot,$canedit,$hostname,\$navmap).
                        &history_tab_js().
                        &inject_data_js().
                        &Apache::lonhtmlcommon::resize_scrollbox_js('docs',$tabidstr,$tid).
@@ -5331,7 +5717,7 @@ sub handler {
                                                 {'force_register' => $showdoc,}));
     } elsif ($toolsflag) {
         my ($breadtext,$breadtitle);
-        $breadtext = "$crstype Contents";
+        $breadtext = "$crstype Editor";
         if ($canedit) {
             $breadtitle = 'Editing '.$crstype.' Contents';
         } else {
@@ -5351,7 +5737,7 @@ sub handler {
                                                 {'bread_crumbs' => $brcrum,}));
     } else {
         my ($breadtext,$breadtitle,$helpitem);
-        $breadtext = "$crstype Contents";
+        $breadtext = "$crstype Editor";
         if ($canedit) {
             $breadtitle = 'Editing '.$crstype.' Contents';
             $helpitem = 'Docs_Adding_Course_Doc';
@@ -5383,6 +5769,7 @@ sub handler {
 	  undef($hadchanges);
           $uploadphase = &process_file_upload(\$upload_output,$coursenum,$coursedom,
                                               \%allfiles,\%codebase,$context,$crstype);
+          undef($navmap);
 	  if ($hadchanges) {
 	      &mark_hash_old();
 	  }
@@ -5398,7 +5785,7 @@ sub handler {
           my ($destination,$dir_root) = &embedded_destination();
           my $url_root = '/uploaded/'.$docudom.'/'.$docuname;
           my $actionurl = '/adm/coursedocs';
-          my ($result,$flag) = 
+          my ($result,$flag) =
               &Apache::loncommon::upload_embedded('coursedoc',$destination,
                   $docuname,$docudom,$dir_root,$url_root,undef,undef,undef,$state,
                   $actionurl);
@@ -5409,11 +5796,11 @@ sub handler {
           my $docuname=$env{'course.'.$env{'request.course.id'}.'.num'};
           my $docudom=$env{'course.'.$env{'request.course.id'}.'.domain'};
           my ($destination,$dir_root) = &embedded_destination();
-          my $result = 
+          my $result =
               &Apache::loncommon::modify_html_refs('coursedoc',$destination,
                                                    $docuname,$docudom,undef,
                                                    $dir_root);
-          $r->print($result.&return_to_editor());   
+          $r->print($result.&return_to_editor());
       } elsif ($env{'form.phase'} eq 'decompress_uploaded') {
           $uploadphase = 'decompress_phase_one';
           $r->print(&decompression_phase_one().
@@ -5439,7 +5826,7 @@ sub handler {
                 'impo' => 'Import',
 		'lnks' => 'Import from Stored Links',
                 'impm' => 'Import from Assembled Map',
-                'imcr' => 'Import from Course Resources',   
+                'imcr' => 'Import from Course Resources',
                 'extr' => 'External Resource',
                 'extt' => 'External Tool',
                 'selm' => 'Select Map',
@@ -5450,7 +5837,7 @@ sub handler {
                 'navc' => 'Table of Contents',
                 'sipa' => 'Simple Course Page',
                 'sipr' => 'Simple Problem',
-                'webp' => 'Blank Web Page (editable)', 
+                'webp' => 'Blank Web Page (editable)',
                 'stpr' => 'Standard Problem',
                 'news' => 'New sub-directory',
                 'crpr' => 'Create Problem',
@@ -5482,7 +5869,7 @@ sub handler {
                 'webctce4' => 'WebCT 4 Campus Edition',
                 'yes'      => 'Yes',
                 'no'       => 'No',
-                'er' => 'Editing rights unavailable for your current role',
+                'er' => 'Editing rights unavailable for your current role.',
         );
 # -----------------------------------------------------------------------------
 
@@ -5512,7 +5899,7 @@ sub handler {
     if ($disk_quota == 0) {
         $percent = 100.0;
     } else {
-        $percent = 100*($current_disk_usage/$disk_quota);
+        $percent = 100*($usage/$disk_quota);
     }
     $usage = sprintf("%.2f",$usage);
     $quota = sprintf("%.2f",$quota);
@@ -5679,6 +6066,11 @@ CRSFORM
    <input type="hidden" name="copyfolder" />
    $containertag
  </form>
+ <form name="aliasform" method="post" action="/adm/coursedocs">
+   <input type="hidden" name="alias" />
+   <input type="hidden" name="cmd" />
+   $containertag
+ </form>
 
 HIDDENFORM
         $r->print(&makesimpleeditform($pathitem)."\n".
@@ -5701,7 +6093,7 @@ HIDDENFORM
     }
 
 #
-
+    my $hostname = $r->hostname();
     my $savefolderpath;
 
     if ($allowed) {
@@ -5899,7 +6291,7 @@ NWEBFORM
                 $numauthor ++;
             }
         }
-        my ($pickdir,$showtitle);;
+        my ($pickdir,$showtitle);
         if ($numauthor) {
             my @order;
             my $defrole;
@@ -6113,7 +6505,7 @@ NSYLFORM
 NGFFORM
 	@specialdocumentsforma=(
 	{'<img class="LC_noBorder LC_middle" src="/res/adm/pages/page.png" alt="'.$lt{newp}.'"  onclick="javascript:makenewpage(document.newpage,\''.$pageseq.'\');" />'=>$newpageform},
-	{'<img class="LC_noBorder LC_middle" src="/res/adm/pages/syllabus.png" alt="'.$lt{syll}.'" onclick="makenew(document.newsyl);" />'=>$newsylform},
+	{'<img class="LC_noBorder LC_middle" src="/res/adm/pages/syllabus.png" alt="'.$lt{syll}.'" onclick="javascript:makenew(document.newsyl);" />'=>$newsylform},
 	{'<img class="LC_noBorder LC_middle" src="/res/adm/pages/navigation.png" alt="'.$lt{navc}.'" onclick="javascript:makenew(document.newnav);" />'=>$newnavform},
         {'<img class="LC_noBorder LC_middle" src="/res/adm/pages/simple.png" alt="'.$lt{sipa}.'" onclick="javascript:makesmppage();" />'=>$newsmppageform},
         {'<img class="LC_noBorder LC_middle" src="/res/adm/pages/webpage.png" alt="'.$lt{webp}.'" onclick="javascript:makewebpage();" />'=>$newwebpageform},
@@ -6126,7 +6518,7 @@ NGFFORM
         );
         if (keys(%ltitools)) {
             push(@importdoc,
-                {'<img class="LC_noBorder LC_middle" src="/res/adm/pages/extres.png" alt="'.$lt{extt}.'" onclick="toggleUpload(\'tool\');" />'=>$exttoolform},
+                {'<img class="LC_noBorder LC_middle" src="/res/adm/pages/exttool.png" alt="'.$lt{extt}.'" onclick="toggleUpload(\'tool\');" />'=>$exttoolform},
         );
         }
         unless ($container eq 'page') {
@@ -6171,13 +6563,16 @@ unless ($container eq 'page') {
        unless (($supplementalflag || $toolsflag)) {
           my $error = &editor($r,$coursenum,$coursedom,$folder,$allowed,'',$crstype,
                               $supplementalflag,\%orderhash,$iconpath,$pathitem,
-                              \%ltitools,$canedit,\$navmap,$hiddentop);
+                              \%ltitools,$canedit,$hostname,\$navmap,$hiddentop);
+          undef($navmap);
           if ($error) {
              $r->print('<p><span class="LC_error">'.$error.'</span></p>');
           }
           if ($hadchanges) {
-             &mark_hash_old();
-          }
+              unless (&is_hash_old()) {
+                  &mark_hash_old();
+              }
+	  }
 
           &changewarning($r,'');
         }
@@ -6207,7 +6602,7 @@ unless ($container eq 'page') {
 	<form action="/adm/coursedocs" method="post" name="supuploaddocument" enctype="multipart/form-data">
         <fieldset id="uploadsuppdocform" style="display: none;">
         <legend>$lt{'upfi'}</legend>
-	<input type="hidden" name="active" value="ee" />	
+	<input type="hidden" name="active" value="ee" />
 	$fileupload
 	<br />
 	<br />
@@ -6290,7 +6685,7 @@ SWEBFORM
 
 
 my @specialdocs = (
-		{'<img class="LC_noBorder LC_middle" src="/res/adm/pages/syllabus.png" alt="'.$lt{syll}.'" onclick="makenew(document.supnewsyl);" />'
+		{'<img class="LC_noBorder LC_middle" src="/res/adm/pages/syllabus.png" alt="'.$lt{syll}.'" onclick="javascript:makenew(document.supnewsyl);" />'
             =>$supnewsylform},
 		{'<img class="LC_noBorder LC_middle" src="/res/adm/pages/myaboutme.png" alt="'.$lt{mypi}.'" onclick="javascript:makenew(document.supnewaboutme);" />'
             =>$supnewaboutmeform},
@@ -6302,7 +6697,7 @@ my @supimportdoc = (
             =>$supextform});
         if (keys(%ltitools)) {
             push(@supimportdoc,
-                {'<img class="LC_noBorder LC_middle" src="/res/adm/pages/extres.png" alt="'.$lt{extt}.'" onclick="javascript:toggleUpload(\'supptool\')" />'
+                {'<img class="LC_noBorder LC_middle" src="/res/adm/pages/exttool.png" alt="'.$lt{extt}.'" onclick="javascript:toggleUpload(\'supptool\')" />'
             =>$supexttoolform});
         }
         push(@supimportdoc, 
@@ -6319,7 +6714,7 @@ my %suporderhash = (
         if ($supplementalflag) {
            my $error = &editor($r,$coursenum,$coursedom,$folder,$allowed,'',$crstype,
                                $supplementalflag,\%suporderhash,$iconpath,$pathitem,
-                               \%ltitools,$canedit,\$navmap);
+                               \%ltitools,$canedit,$hostname);
            if ($error) {
               $r->print('<p><span class="LC_error">'.$error.'</span></p>');
            } else {
@@ -6334,12 +6729,12 @@ my %suporderhash = (
                    }
                    &Apache::lonnet::get_numsuppfiles($coursenum,$coursedom,1);
                    undef($suppchanges);
-               }  
-           } 
+               }
+           }
         }
     } elsif ($supplementalflag) {
         my $error = &editor($r,$coursenum,$coursedom,$folder,$allowed,'',$crstype,
-                            $supplementalflag,'',$iconpath,$pathitem);
+                            $supplementalflag,'',$iconpath,$pathitem,'','',$hostname);
         if ($error) {
             $r->print('<p><span class="LC_error">'.$error.'</span></p>');
         }
@@ -6366,7 +6761,7 @@ my %suporderhash = (
                 &entryline(0,&mt("Click to download or use your browser's Save Link function"),$showdoc).'</table>');
   }
  }
- unless ($noendpage) { 
+ unless ($noendpage) {
      $r->print(&Apache::loncommon::end_page());
  }
  return OK;
@@ -6375,6 +6770,7 @@ my %suporderhash = (
 sub embedded_form_elems {
     my ($phase,$primaryurl,$newidx) = @_;
     my $folderpath = &HTML::Entities::encode($env{'form.folderpath'},'<>&"');
+    $newidx =~s /\D+//g;
     return <<STATE;
     <input type="hidden" name="folderpath" value="$folderpath" />
     <input type="hidden" name="cmd" value="upload_embedded" />
@@ -6395,7 +6791,11 @@ sub embedded_destination {
     } elsif ($folder =~ /^(default|supplemental)_(\d+)$/) {
         $destination .=  $2.'/';
     }
-    $destination .= $env{'form.newidx'};
+    my $newidx = $env{'form.newidx'};
+    $newidx =~s /\D+//g;
+    if ($newidx) {
+        $destination .= $newidx;
+    }
     my $dir_root = '/userfiles';
     return ($destination,$dir_root);
 }
@@ -6421,6 +6821,9 @@ sub decompression_info {
     }
     unshift(@hiddens,$pathitem);
     foreach my $item (@hiddens) {
+        if ($item eq 'newidx') {
+            next if ($env{'form.'.$item} =~ /\D/);
+        }
         if ($env{'form.'.$item}) {
             $hiddenelem .= '<input type="hidden" name="'.$item.'" value="'.
                            &HTML::Entities::encode($env{'form.'.$item},'<>&"').'" />'."\n";
@@ -6438,7 +6841,7 @@ sub decompression_phase_one {
         $error = &mt('Archive file "[_1]" not in the expected location.',$env{'form.archiveurl'});
     } else {
         my $file = $1;
-        $output = 
+        $output =
             &Apache::loncommon::process_decompression($docudom,$docuname,$file,
                                                       $destination,$dir_root,
                                                       $hiddenelem);
@@ -6713,7 +7116,8 @@ END
 }
 
 sub editing_js {
-    my ($udom,$uname,$supplementalflag,$coursedom,$coursenum,$posslti,$londocroot,$canedit) = @_;
+    my ($udom,$uname,$supplementalflag,$coursedom,$coursenum,$posslti,
+        $londocroot,$canedit,$hostname,$navmapref) = @_;
     my %js_lt = &Apache::lonlocal::texthash(
                                           p_mnf => 'Name of New Folder',
                                           t_mnf => 'New Folder',
@@ -6744,6 +7148,8 @@ sub editing_js {
                                           p_ctr2b => '?',
                                           p_ctr3a => 'Cut those',
                                           p_ctr3b => 'items?',
+                                          setal   => 'Enter a (unique) alias',
+                                          delal   => 'Are you sure you want to eliminate the alias?',
                                           rpck    => 'Enter number to pick (e.g., 3)',
                                           imsfile => 'You must choose an IMS package for import',
                                           imscms  => 'You must select which Course Management System was the source of the IMS package',
@@ -6757,7 +7163,7 @@ sub editing_js {
                                           nofi    => 'No file selected',
                                           tinc    => 'Title in course',
                                           sunm    => 'Sub-directory name',
-                                          edri    => 'Editing rights unavailable for your current role',
+                                          edri    => 'Editing rights unavailable for your current role.',
                                         );
     &js_escape(\%js_lt);
     my $crstype = &Apache::loncommon::course_type();
@@ -6766,11 +7172,10 @@ sub editing_js {
     if (&HTML::Entities::decode($env{'environment.internal.'.$env{'request.course.id'}.'.docs_folderpath.folderpath'}) =~ /\:1$/) {
         $main_container_page = 1;
     }
-    my $toplevelmain = 
-        &escape(&mt('Main Content').':::::');
+    my $backtourl;
+    my $toplevelmain = &escape(&default_folderpath($coursenum,$coursedom,$navmapref));
     my $toplevelsupp = &supplemental_base();
 
-    my $backtourl;
     if ($env{'docs.exit.'.$env{'request.course.id'}} =~ /^direct_(.+)$/) {
         my $caller = $1;
         if ($caller =~ /^supplemental/) {
@@ -6789,8 +7194,26 @@ sub editing_js {
                 } else {
                     $url = $res;
                 }
-                $backtourl = &HTML::Entities::encode(&Apache::lonnet::clutter($url),'<>&"').'?symb='.
-                             &HTML::Entities::encode($caller,'<>&"');
+                $backtourl = &HTML::Entities::encode(&Apache::lonnet::clutter($url),'<>&"');
+                if ($backtourl =~ m{^\Q/uploaded/$coursedom/$coursenum/\Edefault_\d+\.sequence$}) {
+                    $backtourl .= '?navmap=1';
+                } else {
+                    $backtourl .= '?symb='.
+                                  &HTML::Entities::encode($caller,'<>&"');
+                }
+                if ($backtourl =~ m{^\Q/public/$coursedom/$coursenum/syllabus\E}) {
+                    if (($ENV{'SERVER_PORT'} == 443) &&
+                        ($env{'course.'.$env{'request.course.id'}.'.externalsyllabus'} =~ m{^http://})) {
+                        if ($hostname ne '') {
+                            $backtourl = 'http://'.$hostname.$backtourl;
+                        }
+                        $backtourl .= (($backtourl =~ /\?/) ? '&amp;':'?').'usehttp=1';
+                    }
+                } elsif ($backtourl =~ m{^/adm/wrapper/ext/(?!https:)}) {
+                    if (($ENV{'SERVER_PORT'} == 443) && ($hostname ne '')) {
+                        $backtourl = 'http://'.$hostname.$backtourl;
+                    }
+                }
                 if ($anchor ne '') {
                     $backtourl .= '#'.&HTML::Entities::encode($anchor,'<>&"');
                 }
@@ -6934,6 +7357,24 @@ function changename(folderpath,index,old
     }
 }
 
+function setalias(folderpath,index) {
+    var alias = prompt('$js_lt{"setal"}');
+    if ((alias != null) && (alias != '')) {
+        this.document.forms.aliasform.alias.value=alias;
+        this.document.forms.aliasform.cmd.value='setalias_'+index;
+        this.document.forms.aliasform.folderpath.value=folderpath;
+        this.document.forms.aliasform.submit();
+    }
+}
+
+function delalias(folderpath,index) {
+    if (confirm('$js_lt{"delal"}')) {
+        this.document.forms.aliasform.cmd.value='delalias_'+index;
+        this.document.forms.aliasform.folderpath.value=folderpath;
+        this.document.forms.aliasform.submit();
+    }
+}
+
 ENDNEWSCRIPT
     } else {
         $jsmakefunctions = <<ENDNEWSCRIPT;
@@ -6978,6 +7419,14 @@ function changename() {
     alert("$js_lt{'edri'}");
 }
 
+function setalias() {
+    alert("$js_lt{'edri'}");
+}
+
+function delalias() {
+    alert("$js_lt{'edri'}");
+}
+
 function makenew() {
     alert("$js_lt{'edri'}");
 }
@@ -8472,7 +8921,9 @@ check on this
 
 Verify Content
 
-=item devalidateversioncache() & checkversions()
+=item devalidateversioncache() 
+
+=item checkversions()
 
 Check Versions