--- loncom/interface/londocs.pm	2018/07/04 13:55:27	1.656
+++ loncom/interface/londocs.pm	2020/08/27 16:21:35	1.673
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Documents
 #
-# $Id: londocs.pm,v 1.656 2018/07/04 13:55:27 raeburn Exp $
+# $Id: londocs.pm,v 1.673 2020/08/27 16:21:35 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1217,13 +1217,19 @@ sub update_paste_buffer {
 # Construct identifiers for current contents of user's paste buffer
     if (@currpaste) {
         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};
-             if (($cid =~ /^$match_domain(?:_)$match_courseid$/) &&
-                 ($url ne '')) {
-                 $pasteurls{$cid.'_'.$url.'_'.$mapidx} = 1;
-             }
+            my $cid = $env{'docs.markedcopy_crs_'.$suffix};
+            my $url = $env{'docs.markedcopy_url_'.$suffix};
+            my $mapidx = $env{'docs.markedcopy_map_'.$suffix};
+            if (($cid =~ /^$match_domain(?:_)$match_courseid$/) &&
+                ($url ne '')) {
+                if ($url eq '/res/lib/templates/simpleproblem.problem') {
+                    $pasteurls{$cid.'_'.$mapidx} = 1;
+                } elsif ($url =~ m{^/res/$match_domain/$match_username/}) {
+                    $pasteurls{$url} = 1;
+                } else {
+                    $pasteurls{$cid.'_'.$url} = 1;
+                }
+            }
         }
     }
 
@@ -1232,7 +1238,7 @@ sub update_paste_buffer {
 
     my @pathitems = split(/\&/,$env{'form.folderpath'});
     my @folderconf = split(/\:/,$pathitems[-1]);
-    my $ispage = $folderconf[4];
+    my $ispage = $folderconf[5];
 
     foreach my $item (@possibles) {
         my ($orderidx,$cmd) = split(/:/,$item);
@@ -1245,7 +1251,13 @@ sub update_paste_buffer {
                                           $env{'form.folderpath'},\%curr_groups);
         next if ($denied{'copy'});
         $url=~s{http(:|:)//https(:|:)//}{https$2//};
-        next if (exists($pasteurls{$coursedom.'_'.$coursenum.'_'.$mapidx}));
+        if ($url eq '/res/lib/templates/simpleproblem.problem') {
+            next if (exists($pasteurls{$coursedom.'_'.$coursenum.'_'.$mapidx}));
+        } elsif ($url =~ m{^/res/$match_domain/$match_username/}) {
+            next if (exists($pasteurls{$url}));
+        } else {
+            next if (exists($pasteurls{$coursedom.'_'.$coursenum.'_'.$url}));
+        }
         my ($suffix,$errortxt,$locknotfreed) =
             &new_timebased_suffix($env{'user.domain'},$env{'user.name'},'paste');
         if ($suffix ne '') {
@@ -1790,7 +1802,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.', 
+                notindom  => 'Paste failed: Item is an external tool from a course in a different domain.',
                 duplicate => 'Paste failed: only one instance of a particular published sequence or page is allowed within each course.',
             );
 
@@ -1819,14 +1831,7 @@ sub do_paste_from_buffer {
 # Retrieve information about all course maps in main content area 
 
     my $allmaps = {};
-    if ($folder =~ /^default/) {
-        $allmaps =
-            &Apache::loncommon::allmaps_incourse($coursedom,$coursenum,
-                                                 $env{"course.$env{'request.course.id'}.home"},
-                                                 $env{'request.course.id'});
-    }
-
-    my (@toclear,%mapurls,%lockerrs,%msgerrs,%results);
+    my (@toclear,%mapurls,%lockerrs,%msgerrs,%results,$donechk);
 
 # Loop over the items to paste
     foreach my $suffix (@dopaste) {
@@ -1844,6 +1849,13 @@ sub do_paste_from_buffer {
         if ($is_map{$suffix}) {
 # If pasting a map, check if map contains other maps
             my (%hierarchy,%titles);
+            if (($folder =~ /^default/) && (!$donechk)) {
+                $allmaps =
+                    &Apache::loncommon::allmaps_incourse($coursedom,$coursenum,
+                                                         $env{"course.$env{'request.course.id'}.home"},
+                                                         $env{'request.course.id'});
+                $donechk = 1;
+            }
             &contained_map_check($url,$folder,$coursenum,$coursedom,
                                  \%removefrommap,\%removeparam,\%addedmaps,
                                  \%hierarchy,\%titles,$allmaps);
@@ -2329,7 +2341,9 @@ sub copy_templated_files {
         my @simpleprobqtypes = qw(radio option string essay numerical);
         my $qtype=$srcparms{$srcprefix.'questiontype'};
         if (grep(/^\Q$qtype\E$/,@simpleprobqtypes)) {
-            my %newdata;
+            my %newdata = (
+                $newprefix.'questiontype' => $qtype,
+            );
             foreach my $type (@simpleprobqtypes) {
                 if ($type eq $qtype) {
                     $newdata{"$weightprefix.$type.weight"}=1;
@@ -2803,9 +2817,7 @@ sub apply_fixups {
                 }
             }
             if (ref($resdatacopy{$key}) eq 'HASH') {
-                if ($newsubdir{$key}) {
-
-                }
+                my ($gotnewmapname,$newmapname,$srcfolder,$srccontainer);
                 foreach my $idx (keys(%{$resdatacopy{$key}})) {
                     if (ref($resdatacopy{$key}{$idx}) eq 'HASH') {
                         my $srcurl = $resdatacopy{$key}{$idx}{'src'};
@@ -2815,15 +2827,18 @@ sub apply_fixups {
                                 ($resdatacopy{$key}{$idx}{'cnum'} =~ /^$match_courseid$/)) {
                                 my $srcdom = $resdatacopy{$key}{$idx}{'cdom'};
                                 my $srcnum = $resdatacopy{$key}{$idx}{'cnum'};
-                                my ($newmapname) = ($key =~ m{/([^/]+)$});
-                                my ($srcfolder,$srccontainer) = split(/\./,$newmapname);
+                                unless ($gotnewmapname) {
+                                    ($newmapname) = ($key =~ m{/([^/]+)$});
+                                    ($srcfolder,$srccontainer) = split(/\./,$newmapname);
+                                    if ($newsubdir{$key}) {
+                                        $newmapname =~ s/^((?:default|supplemental)_)(\d+)/$1$newsubdir{$key}/;
+                                    }
+                                    $gotnewmapname = 1;
+                                }
                                 my $srcmapinfo = $srcfolder.':'.$idx;
                                 if ($srccontainer eq 'page') {
                                     $srcmapinfo .= ':1';
                                 }
-                                if ($newsubdir{$key}) {
-                                    $newmapname =~ s/^((?:default|supplemental)_)(\d+)/$1$newsubdir{$key}/;
-                                }
                                 &copy_templated_files($srcurl,$srcdom,$srcnum,$srcmapinfo,$cdom,
                                                       $cnum,$template,$idx,$newmapname);
                             }
@@ -4142,11 +4157,19 @@ END
     my ($editlink,$extresform,$anchor,$hiddenres,$nomodal);
     my $orig_url = $url;
     $orig_url=~s{http(:|:)//https(:|:)//}{https$2//};
-    $url=~s{^http(|s)(:|:)//}{/adm/wrapper/ext/};
+    if ($container eq 'page') {
+        $url=~s{^http(|s)(:|:)//}{/ext/};
+    } else {
+        $url=~s{^http(|s)(:|:)//}{/adm/wrapper/ext/};
+    }
     if (!$supplementalflag && $residx && $symb) {
         if ((!$isfolder) && (!$ispage)) {
 	    (undef,undef,$url)=&Apache::lonnet::decode_symb($symb);
-	    $url=&Apache::lonnet::clutter($url);
+            if (($url =~ m{^ext/}) && ($container eq 'page')) {
+                $url=&Apache::lonnet::clutter_with_no_wrapper($url);
+            } else {
+                $url=&Apache::lonnet::clutter($url);
+            } 
 	    if ($url=~/^\/*uploaded\//) {
 	        $url=~/\.(\w+)$/;
 	        my $embstyle=&Apache::loncommon::fileembstyle($1);
@@ -4160,7 +4183,7 @@ END
 	    } elsif ($url=~m{^(|/adm/wrapper)/ext/([^#]+)}) {
                 my $wrapped = $1;
                 my $exturl = $2;
-                if ($wrapped eq '') {
+                if (($wrapped eq '') && ($container ne 'page')) { 
                     $url='/adm/wrapper'.$url;
                 }
                 if (($ENV{'SERVER_PORT'} == 443) && ($exturl !~ /^https:/)) {
@@ -4171,19 +4194,18 @@ END
             } elsif ($url eq "/public/$coursedom/$coursenum/syllabus") {
                 if (($ENV{'SERVER_PORT'} == 443) &&
                     ($env{'course.'.$env{'request.course.id'}.'.externalsyllabus'} =~ m{^http://})) {
-                    $url .= '?usehttp=1';
+                    unless (&Apache::lonnet::uses_sts()) {
+                        $url .= '?usehttp=1';
+                    }
                     $nomodal = 1;
                 }
             }
             if (&Apache::lonnet::symbverify($symb,$url)) {
                 my $shownsymb = $symb;
                 if ($isexternal) {
-                    if ($url =~ /^([^#]+)#([^#]+)$/) {
-                        $url = $1;
-                        $anchor = $2;
-                        if ($symb =~ m{^([^#]+)\Q#$anchor\E$}) {
-                            $shownsymb = $1.&escape('#').$anchor;
-                        }
+                    $url =~ s/\#[^#]+$//;
+                    if ($container eq 'page') {
+                        $url = &Apache::lonnet::clutter($url);
                     }
                 }
                 unless ($env{'request.role.adv'}) {
@@ -4237,8 +4259,11 @@ END
                 $url = $1;
                 $anchor = $2;
                 if (($url =~ m{^(|/adm/wrapper)/ext/(?!https:)}) && ($ENV{'SERVER_PORT'} == 443)) {
-                    if ($hostname ne '') {
-                        $url = 'http://'.$hostname.$url;
+                    unless (&Apache::lonnet::uses_sts()) {
+                        if ($hostname ne '') {
+                            $url = 'http://'.$hostname.$url;
+                        }
+                        $url .= (($url =~ /\?/) ? '&':'?').'usehttp=1';
                     }
                     $nomodal = 1;
                 }
@@ -4246,10 +4271,12 @@ END
         } 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;
+                unless (&Apache::lonnet::uses_sts()) {
+                    if ($hostname ne '') {
+                        $url = 'http://'.$hostname.$url;
+                    }
+                    $url .= (($url =~ /\?/) ? '&':'?').'usehttp=1';
                 }
-                $url .= (($url =~ /\?/) ? '&':'?').'usehttp=1';
                 $nomodal = 1;
             }
         }
@@ -4430,8 +4457,7 @@ $form_end;
        } else {
            $link = $url;
        }
-       $link = &js_escape($link.(($url=~/\?/)?'&':'?').'inhibitmenu=yes'.
-                                               (($anchor ne '')?$anchor:''));
+       $link = &js_escape($link.(($url=~/\?/)?'&':'?').'inhibitmenu=yes'.$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>';
@@ -4834,7 +4860,7 @@ sub short_urls {
         $r->rflush();
         my $readonly;
         if ($canedit) {
-            my ($numnew,$errors) = &Apache::loncommon::make_short_symbs($cdom,$cnum,$navmap);
+            my ($numnew,$errors) = &Apache::loncommon::get_requested_shorturls($cdom,$cnum,$navmap);
             if ($numnew) {
                 $r->print('<p class="LC_info">'.&mt('Created [quant,_1,URL]',$numnew).'</p>');
             }
@@ -5629,7 +5655,7 @@ sub handler {
             undef($env{'form.folderpath'});
         } else {
             $folderurl = "uploaded/$coursedom/$coursenum/$folder";
-            if ((split(/\:/,$pathitems[-1]))[4]) {
+            if ((split(/\:/,$pathitems[-1]))[5]) {
                 $folderurl .= '.page';
             } else {
                 $folderurl .= '.sequence';
@@ -5962,8 +5988,8 @@ sub handler {
 	my $fileupload=(<<FIUP);
         $quotainfo
 	$lt{'file'}:<br />
-	<input type="file" name="uploaddoc" class="flUpload" size="40" $disabled />
-        <input type="hidden" id="free_space" value="$free_space" />
+	<input type="file" name="uploaddoc" class="LC_flUpload" size="40" $disabled />
+        <input type="hidden" id="LC_free_space" value="$free_space" />
 FIUP
 
 	my $checkbox=(<<CHBO);
@@ -6559,13 +6585,19 @@ NSYLFORM
 	$help{'Group Portfolio'}
 	</form>
 NGFFORM
-	@specialdocumentsforma=(
+        if ($container eq 'page') {
+            @specialdocumentsforma=(
+        {'<img class="LC_noBorder LC_middle" src="/res/adm/pages/webpage.png" alt="'.$lt{webp}.'" onclick="javascript:makewebpage();" />'=>$newwebpageform},
+            );
+        } else {
+	    @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="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},
-        );
+            );
+        }
         $specialdocumentsform = &create_form_ul(&create_list_elements(@specialdocumentsforma));
 
         my @external = (
@@ -6611,11 +6643,11 @@ my %orderhash = (
                 'bb' => ['Import',$importpubform],
                 'cc' => ['External',$externalform],
                 'dd' => ['Grading',$gradingform],
+                'ff' => ['Other',$specialdocumentsform],
                 );
 unless ($container eq 'page') {
     $orderhash{'00'} = ['Newfolder',$newfolderform];
     $orderhash{'ee'} = ['Collaboration',$communityform];
-    $orderhash{'ff'} = ['Other',$specialdocumentsform];
 }
 
  $hadchanges=0;
@@ -7272,14 +7304,21 @@ sub editing_js {
                 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;
+                        unless (&Apache::lonnet::uses_sts()) {
+                            if ($hostname ne '') {
+                                $backtourl = 'http://'.$hostname.$backtourl;
+                            }
+                            $backtourl .= (($backtourl =~ /\?/) ? '&amp;':'?').'usehttp=1';
                         }
-                        $backtourl .= (($backtourl =~ /\?/) ? '&amp;':'?').'usehttp=1';
                     }
                 } elsif ($backtourl =~ m{^/adm/wrapper/ext/(?!https:)}) {
                     if (($ENV{'SERVER_PORT'} == 443) && ($hostname ne '')) {
-                        $backtourl = 'http://'.$hostname.$backtourl;
+                        unless (&Apache::lonnet::uses_sts()) {
+                            if ($hostname ne '') {
+                                $backtourl = 'http://'.$hostname.$backtourl;
+                            }
+                            $backtourl .= (($backtourl =~ /\?/) ? '&amp;':'?').'usehttp=1';
+                        }
                     }
                 }
                 if ($anchor ne '') {