--- loncom/interface/londocs.pm	2021/04/23 13:35:26	1.484.2.85.2.9
+++ loncom/interface/londocs.pm	2017/05/10 20:32:27	1.629
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Documents
 #
-# $Id: londocs.pm,v 1.484.2.85.2.9 2021/04/23 13:35:26 raeburn Exp $
+# $Id: londocs.pm,v 1.629 2017/05/10 20:32:27 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -43,11 +43,13 @@ use Apache::lonnavdisplay();
 use Apache::lonextresedit();
 use Apache::lontemplate();
 use Apache::lonsimplepage();
-use Apache::loncourserespicker();
+use Apache::lonhomework();
+use Apache::lonpublisher();
 use HTML::Entities;
 use HTML::TokeParser;
 use GDBM_File;
 use File::MMagic;
+use File::Copy;
 use Apache::lonlocal;
 use Cwd;
 use LONCAPA qw(:DEFAULT :match);
@@ -178,45 +180,6 @@ sub default_folderpath {
     }
 }
 
-sub validate_folderpath {
-    my ($supplementalflag) = @_;
-    if ($env{'form.folderpath'} ne '') {
-        my @items = split(/\&/,$env{'form.folderpath'});
-        my $badpath;
-        for (my $i=0; $i<@items; $i++) {
-            my $odd = $i%2;
-            if (($odd) && (!$supplementalflag) && ($items[$i] !~ /^[^:]*:(|\d+):(|1):(|1):(|1):(|1)$/)) {
-                $badpath = 1;
-            } elsif ((!$odd) && ($items[$i] !~ /^(default|supplemental)(|_\d+)$/)) {
-                $badpath = 1;
-            }
-            last if ($badpath);
-        }
-        if ($badpath) {
-            delete($env{'form.folderpath'});
-        }
-    }
-    return;
-}
-
-sub validate_suppath {
-    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'});
-        }
-    }
-    return;
-}
-
 sub dumpcourse {
     my ($r) = @_;
     my $crstype = &Apache::loncommon::course_type();
@@ -272,7 +235,7 @@ ENDJS
         } else {
             &Apache::loncourserespicker::enumerate_course_contents($navmap,\%maps,\%resources,\%titles,
                                                                    'dumpdocs',$cdom,$cnum);
-        }
+	}
         my @todump = &Apache::loncommon::get_env_multiple('form.archive');
         my (%tocopy,%replacehash,%lookup,%deps,%display,%result,%depresult,%simpleproblems,%simplepages,
             %newcontent,%has_simpleprobs);
@@ -283,7 +246,7 @@ ENDJS
                 if ($res =~ m{^uploaded/$cdom/$cnum/\E((?:docs|supplemental)/.+)$}) {
                     $tocopy{$1} = $name;
                     $display{$item} = $1;
-                    $lookup{$1} = $item;
+                    $lookup{$1} = $item; 
                 } elsif ($res eq 'lib/templates/simpleproblem.problem') {
                     $simpleproblems{$item} = {
                                                 symb => $resources{$item},
@@ -383,7 +346,7 @@ $contents{content}.'
 </div>';
                 }
                 if ($contents{webreferences}) {
-                    $content .= '
+                    $content .= ' 
 <div class="LC_Box">
 <h4 class="LC_hcell">'.&mt('Web References').'</h4>'.
 $contents{webreferences}.'
@@ -393,10 +356,10 @@ $contents{webreferences}.'
 </body>
 </html>
 ';
-                $newcontent{'/'.$simplepages{$item}{res}} = $content;
+                $newcontent{'/'.$simplepages{$item}{res}} = $content; 
             }
         }
-        foreach my $item (keys(%tocopy)) {
+	foreach my $item (keys(%tocopy)) {
             unless ($item=~/\.(sequence|page)$/) {
                 my $currurlpath = $prefix.$item;
                 my $currdirpath = &Apache::lonnet::filelocation('',$currurlpath);
@@ -427,39 +390,39 @@ $contents{webreferences}.'
                 if ($simpleproblems{$num}) {
                     $newfilename=$title.'/'.$simpleproblems{$num}{'name'};
                 } else {
-                    $newfilename=$title.'/'.$replacehash{$item};
+	            $newfilename=$title.'/'.$replacehash{$item};
                 }
-                $newfilename=~s/\.(\w+)$//;
-               my $ext=$1;
-                $newfilename=&clean($newfilename);
-                $newfilename.='.'.$ext;
-                my ($newrelpath) = ($newfilename =~ m{^\Q$title/\E(.+)$});
+	        $newfilename=~s/\.(\w+)$//;
+	        my $ext=$1;
+	        $newfilename=&clean($newfilename);
+	        $newfilename.='.'.$ext;
+                my ($newrelpath) = ($newfilename =~ m{^\Q$title/\E(.+)$}); 
                 if ($newrelpath ne $replacehash{$item}) {
                     $replacehash{$item} = $newrelpath;
                 }
-                my @dirs=split(/\//,$newfilename);
-                my $path=$r->dir_config('lonDocRoot')."/priv/$cd/$ca";
-                my $makepath=$path;
-                my $fail;
+	        my @dirs=split(/\//,$newfilename);
+	        my $path=$r->dir_config('lonDocRoot')."/priv/$cd/$ca";
+	        my $makepath=$path;
+	        my $fail;
                 my $origin;
-                for (my $i=0;$i<$#dirs;$i++) {
-                    $makepath.='/'.$dirs[$i];
-                    unless (-e $makepath) {
-                        unless(mkdir($makepath,0755)) {
+	        for (my $i=0;$i<$#dirs;$i++) {
+		    $makepath.='/'.$dirs[$i];
+		    unless (-e $makepath) {
+		        unless(mkdir($makepath,0755)) { 
                             $fail = &mt('Directory creation failed.');
                         }
-                    }
-                }
+		    }
+	        }
                 if ($i == 0) {
-                    $result = '<br /><tt>'.$item.'</tt> => <tt>'.$newfilename.'</tt>: ';
+	            $result = '<br /><tt>'.$item.'</tt> => <tt>'.$newfilename.'</tt>: ';
                 } else {
                     $depresult .= '<li><tt>'.$item.'</tt> => <tt>'.$newfilename.'</tt> '.
                                   '<span class="LC_fontsize_small" style="font-weight: bold;">'.
                                   &mt('(dependency)').'</span>: ';
                 }
                 if (-e $path.'/'.$newfilename) {
-                    $fail = &mt('Destination already exists -- not overwriting.');
-                } else {
+                    $fail = &mt('Destination already exists -- not overwriting.'); 
+	        } else {
                     if (my $fh=Apache::File->new('>'.$path.'/'.$newfilename)) {
                         if (($item =~ m{^/adm/$match_domain/$match_username/\d+/smppg}) ||
                             ($item =~ /^simpleproblem_/)) {
@@ -481,18 +444,18 @@ $contents{webreferences}.'
                                         while (my $token = $parser->get_token) {
                                             if ($token->[0] eq 'S') {
                                                 if (($token->[1] eq 'resource') &&
-                                                    ($token->[2]->{'src'} eq '/res/lib/templates/simpleproblem.problem') &&
+                                                    ($token->[2]->{'src'} eq '/res/lib/templates/simpleproblem.problem') && 
                                                     ($changes{$token->[2]->{'id'}})) {
                                                     my $id = $token->[2]->{'id'};
                                                     $updatedcontent .= '<'.$token->[1];
                                                     foreach my $attrib (@{$token->[3]}) {
-                                                       next unless ($attrib =~ /^(src|type|title|id)$/);
+                                                        next unless ($attrib =~ /^(src|type|title|id)$/);
                                                         if ($attrib eq 'src') {
-                                                            my ($file) = ($display{$changes{$id}} =~ /^\Qsimpleproblem_\E(.+)$/);
+                                                            my ($file) = ($display{$changes{$id}} =~ /^\Qsimpleproblem_\E(.+)$/); 
                                                             if ($file) {
                                                                 $updatedcontent .= ' '.$attrib.'="'.$file.'"';
                                                             } else {
-                                                                $updatedcontent .= ' '.$attrib.'="'.$token->[2]->{$attrib}.'"';
+                                                                $updatedcontent .= ' '.$attrib.'="'.$token->[2]->{$attrib}.'"'; 
                                                             }
                                                         } else {
                                                             $updatedcontent .= ' '.$attrib.'="'.$token->[2]->{$attrib}.'"';
@@ -507,36 +470,36 @@ $contents{webreferences}.'
                                              }
                                          }
                                          print $fh $updatedcontent;
-                                    } else {
-                                        print $fh &Apache::lonclonecourse::rewritefile(
+                                    } else {  
+		                        print $fh &Apache::lonclonecourse::rewritefile(
                      &Apache::lonclonecourse::readfile($env{'request.course.id'},$item),
-                                                      (%replacehash,$crs => '')
-                                                                                      );
+		                                      (%replacehash,$crs => '')
+							                              );
                                     }
                                 } else {
-                                    print $fh
+		                    print $fh
                                         &Apache::lonclonecourse::readfile($env{'request.course.id'},$item);
-                                }
+		                }
                             } else {
-                                $fail = &mt('Source does not exist.');
+                                $fail = &mt('Source does not exist.');  
                             }
                         }
                         $fh->close();
-                    } else {
-                        $fail = &mt('Could not write to destination.');
+	            } else {
+		        $fail = &mt('Could not write to destination.');
                     }
-                }
+	        }
                 my $text;
-                if ($fail) {
+	        if ($fail) {
                     $text = '<span class="LC_error">'.&mt('fail').('&nbsp;'x3).$fail.'</span>';
-                } else {
+	        } else {
                     $text = '<span class="LC_success">'.&mt('ok').'</span>';
                 }
                 if ($i == 0) {
                     $result .= $text;
                 } else {
                     $depresult .= $text.'</li>';
-                }
+	        }
             }
             $r->print($result);
             if ($depresult) {
@@ -553,63 +516,63 @@ $contents{webreferences}.'
             $r->rflush();
             my ($preamble,$formname);
             $formname = 'dumpdoc';
-            unless ($home==1) {
-                $preamble = '<div class="LC_left_float">'.
-                            '<fieldset><legend>'.
+	    unless ($home==1) {
+	        $preamble = '<div class="LC_left_float">'.
+		            '<fieldset><legend>'.
                             &mt('Select the Authoring Space').
                             '</legend><select name="authorspace">';
-            }
+	    }
             my @orderspaces = ();
-            foreach my $key (sort(keys(%outhash))) {
+	    foreach my $key (sort(keys(%outhash))) {
                 if ($key=~/^home_(.+)$/) {
                     if ($1 eq $env{'user.name'}.':'.$env{'user.domain'}) {
                         unshift(@orderspaces,$1);
                     } else {
                         push(@orderspaces,$1);
                     }
-                }
+                } 
             }
             if ($home>1) {
                 $preamble .= '<option value="" selected="selected">'.&mt('Select').'</option>';
             }
             foreach my $user (@orderspaces) {
-                if ($home==1) {
-                    $preamble .= '<input type="hidden" name="authorspace" value="'.$user.'" />';
-                } else {
-                    $preamble .= '<option value="'.$user.'">'.$user.' - '.
-                                 &Apache::loncommon::plainname(split(/\:/,$user)).'</option>';
-                }
-            }
-            unless ($home==1) {
-                $preamble .= '</select></fieldset></div>'."\n";
-            }
-            my $title=$origcrsdata{'description'};
-            $title=~s/[\/\s]+/\_/gs;
-            $title=&clean($title);
-            $preamble .= '<div class="LC_left_float">'.
+		if ($home==1) {
+		    $preamble .= '<input type="hidden" name="authorspace" value="'.$user.'" />';
+		} else {
+		    $preamble .= '<option value="'.$user.'">'.$user.' - '.
+			         &Apache::loncommon::plainname(split(/\:/,$user)).'</option>';
+	        }
+	    }
+	    unless ($home==1) {
+	        $preamble .= '</select></fieldset></div>'."\n";
+	    }
+	    my $title=$origcrsdata{'description'};
+	    $title=~s/[\/\s]+/\_/gs;
+	    $title=&clean($title);
+	    $preamble .= '<div class="LC_left_float">'.
                          '<fieldset><legend>'.&mt('Folder in Authoring Space').'</legend>'.
                          '<input type="text" size="50" name="authorfolder" value="'.
                          $title.'" />'.
                          '</fieldset></div><div style="padding:0;clear:both;margin:0;border:0"></div>'."\n";
             my %uploadedfiles;
-            &tiehash();
-            foreach my $file (&Apache::lonclonecourse::crsdirlist($origcrsid,'userfiles')) {
-                my ($ext)=($file=~/\.(\w+)$/);
+	    &tiehash();
+	    foreach my $file (&Apache::lonclonecourse::crsdirlist($origcrsid,'userfiles')) {
+	        my ($ext)=($file=~/\.(\w+)$/);
 # FIXME Check supplemental here
-                my $title=$hash{'title_'.$hash{
-                                'ids_/uploaded/'.$origcrsdata{'domain'}.'/'.$origcrsdata{'num'}.'/'.$file}};
-                if (!$title) {
-                    $title=$file;
-                } else {
-                    $title=~s|/|_|g;
-                }
-                $title=~s/\.(\w+)$//;
-                $title=&clean($title);
-                $title.='.'.$ext;
-#           $r->print("\n<td><input type='text' size='60' name='namefor_".$file."' value='".$title."' /></td>"
+	        my $title=$hash{'title_'.$hash{
+		                'ids_/uploaded/'.$origcrsdata{'domain'}.'/'.$origcrsdata{'num'}.'/'.$file}};
+	        if (!$title) {
+		    $title=$file;
+	        } else {
+		    $title=~s|/|_|g;
+	        }
+	        $title=~s/\.(\w+)$//;
+	        $title=&clean($title);
+	        $title.='.'.$ext;
+#	    $r->print("\n<td><input type='text' size='60' name='namefor_".$file."' value='".$title."' /></td>"
                 $uploadedfiles{$file} = $title;
-            }
-            &untiehash();
+	    }
+	    &untiehash();
             $r->print(&Apache::loncourserespicker::create_picker($navmap,'dumpdocs',$formname,$crstype,undef,
                                                                  undef,undef,$preamble,$home,\%uploadedfiles));
         }
@@ -640,16 +603,16 @@ 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};
                 $newname =~ s{[^/]+$}{};
                 $replacehash->{$newcontainer} = $newname.$relfile;
                 $deps->{$item}{$newcontainer} = 1;
-                my ($newurlpath) = ($depurl =~ m{^(.*)/[^/]+$});
+                my ($newurlpath) = ($depurl =~ m{^(.*)/[^/]+$});  
                 my $depfile = &Apache::lonnet::filelocation('',$depurl);
                 my $type = $mm->checktype_filename($depfile);
                 if ($type eq 'text/html') {
@@ -696,7 +659,7 @@ sub group_import {
                 my $marker = $2;
                 my $info = $3;
                 my ($toolid,%toolhash,%toolsettings);
-                my @extras = ('linktext','explanation','crslabel','crstitle','crsappend');
+                my @extras = ('linktext','explanation','crslabel','crstitle');
                 my @toolinfo = split(/:/,$info);
                 if ($residx) {
                     %toolsettings=&Apache::lonnet::dump('exttool_'.$marker,$coursedom,$coursenum);
@@ -707,7 +670,7 @@ sub group_import {
                 $toolid =~ s/\D//g;
                 ($toolhash{'target'},$toolhash{'width'},$toolhash{'height'},
                  $toolhash{'linktext'},$toolhash{'explanation'},
-                 $toolhash{'crslabel'},$toolhash{'crstitle'},$toolhash{'crsappend'}) = @toolinfo;
+                 $toolhash{'crslabel'},$toolhash{'crstitle'}) = @toolinfo;
                 foreach my $item (@extras) {
                     $toolhash{$item} = &unescape($toolhash{$item});
                 }
@@ -734,7 +697,7 @@ sub group_import {
                         } elsif ($residx) {
                             $toolhash{'target'} = $toolsettings{'target'};
                             if ($toolhash{'target'} eq 'window') {
-                                foreach my $item ('width','height') {
+                                foreach my $item ('width','height') { 
                                     $toolhash{$item} = $toolsettings{$item};
                                 }
                             }
@@ -792,7 +755,7 @@ sub group_import {
                         if ($putres eq 'ok') {
                             if (@deleted) {
                                 &Apache::lonnet::del('exttool_'.$marker,\@deleted,$coursedom,$coursenum);
-                            }
+                            } 
                         }
                     }
                 }
@@ -809,7 +772,7 @@ sub group_import {
                 }
                 if ($url =~ m{^/uploaded/\Q$coursedom\E/\Q$coursenum\E/(default_\d+\.)(page|sequence)$}) {
                     &contained_map_check($url,$folder,$coursenum,$coursedom,\%removefrommap,
-                                         \%removeparam,\%addedmaps,\%hierarchy,\%titles,$allmaps);
+                                        \%removeparam,\%addedmaps,\%hierarchy,\%titles,$allmaps);
                     $importuploaded = 1;
                 } elsif ($url =~ m{^/res/.+\.(page|sequence)$}) {
                     next if ($allmaps->{$url});
@@ -822,27 +785,26 @@ sub group_import {
 	    }
 	    my $ext = 'false';
 	    if ($url=~m{^http://} || $url=~m{^https://}) { $ext = 'true'; }
+	    $name = &LONCAPA::map::qtunescape($name);
+            if ($name eq '') {
+                $name = &LONCAPA::map::qtunescape(&mt('Web Page'));
+            }
             if ($url =~ m{^/uploaded/$coursedom/$coursenum/((?:docs|supplemental)/(?:default|\d+))/new\.html$}) {
                 my $filepath = $1;
-                my $fname;
-                if ($name eq '') {
-                    $name = &mt('Web Page');
+                my $fname = $name;
+                if ($fname =~ /^\W+$/) {
                     $fname = 'web';
                 } else {
-                    $fname = $name;
-                    $fname=&Apache::lonnet::clean_filename($fname);
-                    if ($fname eq '') {
-                        $fname = 'web';
-                    } elsif (length($fname) > 15) {
-                        $fname = substr($fname,0,14);
-                    }
+                    $fname =~ s/\W/_/g;
+                }
+                if (length($fname) > 15) {
+                    $fname = substr($fname,0,14);
                 }
-                my $title = &Apache::loncommon::cleanup_html($name);
                 my $initialtext = &mt('Replace with your own content.');
                 my $newhtml = <<END;
 <html>
 <head>
-<title>$title</title>
+<title>$name</title>
 </head>
 <body bgcolor="#ffffff">
 $initialtext
@@ -850,7 +812,7 @@ $initialtext
 </html>
 END
                 $env{'form.output'}=$newhtml;
-                my $result =
+                my $result = 
                     &Apache::lonnet::finishuserfileupload($coursenum,$coursedom,
                                                           'output',
                                                           "$filepath/$residx/$fname.html");
@@ -864,7 +826,6 @@ END
                     return (&mt('Failed to save new web page.'),1);
                 }
             }
-            $name = &LONCAPA::map::qtunescape($name);
             $url  = &LONCAPA::map::qtunescape($url);
 	    $LONCAPA::map::resources[$residx] =
 		join(':', ($name, $url, $ext, 'normal', 'res'));
@@ -876,7 +837,7 @@ END
                           removefrommap => \%removefrommap,
                           removeparam   => \%removeparam,
                       );
-        my ($result,$msgsarray,$lockerror) =
+        my ($result,$msgsarray,$lockerror) = 
             &apply_fixups($folder,1,$coursedom,$coursenum,\%import_errors,\%updated);
         if (keys(%import_errors) > 0) {
             $fixuperrors =
@@ -977,12 +938,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 $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,'',\$navmap)."\n".
+                         $coursedom,$coursenum,'','',$canedit,'',\$navmap)."\n".
              &history_tab_js()."\n".
              &Apache::lonratedt::editscript('simple')."\n".
              '// ]]>'."\n".
@@ -1207,19 +1168,13 @@ 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 '')) {
-                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;
-                }
-            }
+             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;
+             }
         }
     }
 
@@ -1228,7 +1183,7 @@ sub update_paste_buffer {
 
     my @pathitems = split(/\&/,$env{'form.folderpath'});
     my @folderconf = split(/\:/,$pathitems[-1]);
-    my $ispage = $folderconf[5];
+    my $ispage = $folderconf[4];
 
     foreach my $item (@possibles) {
         my ($orderidx,$cmd) = split(/:/,$item);
@@ -1241,13 +1196,7 @@ sub update_paste_buffer {
                                           $env{'form.folderpath'},\%curr_groups);
         next if ($denied{'copy'});
         $url=~s{http(&colon;|:)//https(&colon;|:)//}{https$2//};
-        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}));
-        }
+        next if (exists($pasteurls{$coursedom.'_'.$coursenum.'_'.$mapidx}));
         my ($suffix,$errortxt,$locknotfreed) =
             &new_timebased_suffix($env{'user.domain'},$env{'user.name'},'paste');
         if ($suffix ne '') {
@@ -1344,7 +1293,7 @@ sub print_paste_buffer {
         next if ($suffix =~ /\D/);
         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 '')) {
             $clipboardcount ++;
@@ -1467,7 +1416,7 @@ sub print_paste_buffer {
             my $value = &mt('Paste to current folder');
             if ($container eq 'page') {
                 $value = &mt('Paste to current page');
-            }
+            } 
             $buttons = '<input type="submit" name="pastemarked" value="'.$value.'" />'.('&nbsp;'x2);
         }
         $buttons .= '<input type="submit" name="clearmarked" value="'.&mt('Remove from clipboard').'" />'.('&nbsp;'x2);
@@ -1644,7 +1593,7 @@ function checkClipboard() {
     if (document.pasteform.pasting.length > 1) {
         for (var i=0; i<document.pasteform.pasting.length; i++) {
             document.pasteform.pasting[i].checked = true;
-        }
+        } 
     }
     return;
 }
@@ -1700,7 +1649,7 @@ sub do_paste_from_buffer {
     foreach my $suffix (@topaste) {
         my $url=&LONCAPA::map::qtescape($env{'docs.markedcopy_url_'.$suffix});
         my $cid=&LONCAPA::map::qtescape($env{'docs.markedcopy_crs_'.$suffix});
-        my $mapidx=&LONCAPA::map::qtescape($env{'docs.markedcopy_map_'.$suffix});  
+        my $mapidx=&LONCAPA::map::qtescape($env{'docs.markedcopy_map_'.$suffix}); 
 # Supplemental content may only include certain types of content
 # Early out if pasted content is not supported in Supplemental area
         if ($folder =~ /^supplemental/) {
@@ -1722,7 +1671,9 @@ sub do_paste_from_buffer {
             }
             $srcdom{$suffix} = $srcd;
             $srcnum{$suffix} = $srcn;
-        } elsif ($url =~ m{^/adm/$match_domain/$match_courseid/\d+/ext\.tool$}) {
+        } elsif (($url =~ m{^/res/lib/templates/\w+\.problem$}) ||
+                 ($url =~ m{^/adm/$match_domain/$match_username/\d+/(bulletinboard|smppg|ext\.tool)$})) {
+            my $srctype= $1;
             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
@@ -1732,39 +1683,23 @@ 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 ($srcd ne $coursedom) {
+            if (($srctype eq 'ext.tool') && ($srcd ne $coursedom)) {
                 $notindom{$suffix} = 1;
                 next;
             }
             $srcdom{$suffix} = $srcd;
             $srcnum{$suffix} = $srcn;
-        } elsif (($url =~ m{^/res/lib/templates/\w+\.problem$}) ||
-                 ($url =~ m{^/adm/$match_domain/$match_username/\d+/(bulletinboard|smppg)$})) {
-            my ($srcd,$srcn) = split(/_/,$cid);
-# When paste buffer was populated using an active role in a different course
-# check for mdc privilege in the course from which the resource was pasted
-            if (($srcd ne $coursedom) || ($srcn ne $coursenum)) {
-                unless ($env{"user.priv.cm./$srcd/$srcn"} =~ /\Q:mdc&F\E/) {
-                    $notincrs{$suffix} = 1;
-                    next;
-                }
-            }
-            $srcdom{$suffix} = $srcd;
-            $srcnum{$suffix} = $srcn;
         }
-        $srcmapidx{$suffix} = $mapidx; 
+        $srcmapidx{$suffix} = $mapidx;
         push(@dopaste,$suffix);
         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 
 # URLs will contain different paths (which depend on whether pasted item is
-# a folder/page or a document).   
+# a folder/page or a document).
             if (($folder =~ /^supplemental/) && (($oldprefix =~ /^default/) || ($oldprefix eq 'docs'))) {
                 $prefixchg{$suffix} = 'docstosupp';
             } elsif (($folder =~ /^default/) && ($oldprefix =~ /^supplemental/)) {
@@ -1804,7 +1739,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 donain.', 
                 duplicate => 'Paste failed: only one instance of a particular published sequence or page is allowed within each course.',
             );
 
@@ -1833,7 +1768,14 @@ sub do_paste_from_buffer {
 # Retrieve information about all course maps in main content area 
 
     my $allmaps = {};
-    my (@toclear,%mapurls,%lockerrs,%msgerrs,%results,$donechk);
+    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);
 
 # Loop over the items to paste
     foreach my $suffix (@dopaste) {
@@ -1846,18 +1788,11 @@ sub do_paste_from_buffer {
         }
         my $url=&LONCAPA::map::qtescape($env{'docs.markedcopy_url_'.$suffix});
         my $title=&LONCAPA::map::qtescape($env{'docs.markedcopy_title_'.$suffix});
-        my $cid=&LONCAPA::map::qtescape($env{'docs.markedcopy_crs_'.$suffix});
+        my $cid=&LONCAPA::map::qtescape($env{'docs.markedcopy_crs_'.$suffix}); 
         my $oldurl = $url;
         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);
@@ -1899,7 +1834,7 @@ sub do_paste_from_buffer {
                                       $coursenum,$srcdom{$suffix},$srcnum{$suffix},
                                       $allmaps,\%rewrites,\%retitles,\%copies,\%dbcopies,
                                       \%zombies,\%params,\%mapmoves,\%mapchanges,
-                                      \%tomove,\%newsubdir,\%newurls,\%resdatacopy); 
+                                      \%tomove,\%newsubdir,\%newurls,\%resdatacopy);
                 }
             } elsif ($url=~m {^/res/}) {
 # published map can only exist once, so remove from paste buffer when done
@@ -1915,7 +1850,7 @@ sub do_paste_from_buffer {
         }
         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,
@@ -1950,7 +1885,7 @@ sub do_paste_from_buffer {
                     next;
 	        }
                 if ($lockerr{$prefix}) {
-                    $lockerrs{$suffix} = $lockerr{$prefix};
+                    $lockerrs{$suffix} = $lockerr{$prefix};  
                 }
             }
         }
@@ -1989,7 +1924,7 @@ sub do_paste_from_buffer {
                     if ($newdocsdir eq '') {
                         $newdocsdir = 'default';
                     }
-                    if (($prefixchg{$suffix}) ||
+                    if (($prefixchg{$suffix}) || 
                         ($srcdom{$suffix} ne $coursedom) || 
                         ($srcnum{$suffix} ne $coursenum) ||
                         ($env{'form.docs.markedcopy_options_'.$suffix} ne 'move')) {
@@ -2026,8 +1961,8 @@ sub do_paste_from_buffer {
             }
         }
 
-# Apply any changes to maps, or copy dependencies for uploaded HTML pages, or update 
-# resourcedata for simpleproblems copied from another course
+# Apply any changes to maps, or copy dependencies for uploaded HTML pages, or update
+# resourcedata for simpleproblems copied from another course 
         unless ($allresult eq 'fail') {
             my %updated = (
                             rewrites      => \%rewrites,
@@ -2207,7 +2142,7 @@ sub dbcopy {
             if ($prefix eq 'ext.tool') {
                 $prefix = 'exttool';
             }
-            if (($dbref->{'cdom'} =~ /^$match_domain$/) &&
+            if (($dbref->{'cdom'} =~ /^$match_domain$/) && 
                 ($dbref->{'cnum'} =~ /^$match_courseid$/)) {
                 my $db_name;
                 my $marker = (split(m{/},$url))[4];
@@ -2229,8 +2164,6 @@ 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);
                     }
@@ -2251,7 +2184,7 @@ sub dbcopy {
                             my $content = &Apache::lonnet::getfile($photo);
                             unless ($content eq '-1') {
                                 $env{'form.'.$suffix.'.photourl'} = $content;
-                                $newphoto =
+                                $newphoto = 
                                     &Apache::lonnet::finishuserfileupload($coursenum,$coursedom,$suffix.'.photourl',"$subdir/$suffix/$fname");
                                 delete($env{'form.'.$suffix.'.photourl'});
                             }
@@ -2323,9 +2256,7 @@ 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 = (
-                $newprefix.'questiontype' => $qtype,
-            );
+            my %newdata;
             foreach my $type (@simpleprobqtypes) {
                 if ($type eq $qtype) {
                     $newdata{"$weightprefix.$type.weight"}=1;
@@ -2419,7 +2350,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;
@@ -2438,9 +2375,8 @@ sub contained_map_check {
                             $addedmaps->{$ressrc} = [$url];
                         }
                     }
-                    &contained_map_check($ressrc,$folder,$coursenum,$coursedom,
-                                         $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/) {
@@ -2491,7 +2427,7 @@ sub url_paste_fixups {
             }
             next if ($token->[2]->{'type'} eq 'external');
             if ($token->[2]->{'type'} eq 'zombie') {
-                next if ($skip);
+                next if ($skip);  
                 $zombies->{$oldurl}{$id} = $ressrc;
                 $changed = 1;
             } elsif ($ressrc =~ m{^/uploaded/($match_domain)/($match_courseid)/(.+)$}) {
@@ -2772,7 +2708,9 @@ sub apply_fixups {
                 }
             }
             if (ref($resdatacopy{$key}) eq 'HASH') {
-                my ($gotnewmapname,$newmapname,$srcfolder,$srccontainer);
+                if ($newsubdir{$key}) {
+
+                }
                 foreach my $idx (keys(%{$resdatacopy{$key}})) {
                     if (ref($resdatacopy{$key}{$idx}) eq 'HASH') {
                         my $srcurl = $resdatacopy{$key}{$idx}{'src'};
@@ -2782,18 +2720,15 @@ sub apply_fixups {
                                 ($resdatacopy{$key}{$idx}{'cnum'} =~ /^$match_courseid$/)) {
                                 my $srcdom = $resdatacopy{$key}{$idx}{'cdom'};
                                 my $srcnum = $resdatacopy{$key}{$idx}{'cnum'};
-                                unless ($gotnewmapname) {
-                                    ($newmapname) = ($key =~ m{/([^/]+)$});
-                                    ($srcfolder,$srccontainer) = split(/\./,$newmapname);
-                                    if ($newsubdir{$key}) {
-                                        $newmapname =~ s/^((?:default|supplemental)_)(\d+)/$1$newsubdir{$key}/;
-                                    }
-                                    $gotnewmapname = 1;
-                                }
+                                my ($newmapname) = ($key =~ m{/([^/]+)$});
+                                my ($srcfolder,$srccontainer) = split(/\./,$newmapname);
                                 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);
                             }
@@ -2816,8 +2751,7 @@ sub apply_fixups {
                     }
                 }
             }
-            my $total = scalar(@LONCAPA::map::order) - 1;
-            for (my $i=$total; $i>=0; $i--) {
+            for (my $i=0; $i<@LONCAPA::map::order; $i++) {
                 my $idx = $LONCAPA::map::order[$i];
                 if (defined($LONCAPA::map::resources[$idx])) {
                     my $changed;
@@ -2827,7 +2761,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,$name);
+                                &LONCAPA::map::delparameter($idx,'parameter_'.$name);
                             }
                         }
                         next;
@@ -2869,7 +2803,7 @@ sub apply_fixups {
             foreach my $idx (keys(%remparam)) {
                 if (ref($remparam{$idx}) eq 'ARRAY') {
                     foreach my $name (@{$remparam{$idx}}) {   
-                        &LONCAPA::map::delparameter($idx,$name);
+                        &LONCAPA::map::delparameter($idx,'parameter_'.$name);
                     }
                 }
             }
@@ -2977,7 +2911,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);
@@ -3004,7 +2938,7 @@ sub update_parameter {
             foreach my $which (keys(%allchecked)) {
                 if (($which eq 'randompick' || $which eq 'randomorder')) {
                     next if (!$is_map);
-                }
+                } 
                 my $oldvalue = 0;
                 my $newvalue = 0;
                 if ($allchecked{$which}{$res}) {
@@ -3056,8 +2990,8 @@ sub update_parameter {
             $oldvalue = 1;
         }
         if ($env{'form.'.$which.'_'.$idx}) {
-            $newvalue = ($which eq 'randompick') ? $env{'form.rpicknum_'.$idx}
-                                                 : 1;
+	    $newvalue = ($which eq 'randompick') ? $env{'form.rpicknum_'.$idx}
+	                                         : 1;
         }
         if ($oldvalue ne $newvalue) {
             $haschanges = 1;
@@ -3066,17 +3000,16 @@ sub update_parameter {
                 if ($which eq 'randompick') {
                     $storeval = $newvalue;
                 }
-                &LONCAPA::map::storeparameter($idx, 'parameter_'.$which, $storeval,
-                                              $parameter_type{$which});
-                &remember_parms($idx,$which,'set',$storeval);
+	        &LONCAPA::map::storeparameter($idx, 'parameter_'.$which, $storeval,
+				              $parameter_type{$which});
+	        &remember_parms($idx,$which,'set',$storeval);
             } else {
-                &LONCAPA::map::delparameter($idx,'parameter_'.$which);
-                &remember_parms($idx,$which,'del');
+	        &LONCAPA::map::delparameter($idx,'parameter_'.$which);
+	        &remember_parms($idx,$which,'del');
             }
         }
         return $haschanges;
     }
-    return;
 }
 
 sub handle_edit_cmd {
@@ -3161,10 +3094,10 @@ sub editor {
             undef(@LONCAPA::map::zombies);
         }
         $folder = 'default';
-        $container = 'sequence';
+        $container = 'sequence'; 
     } else {
         ($errtext,$fatal) = &mapread($coursenum,$coursedom,
-                                     $folder.'.'.$container);
+				     $folder.'.'.$container);
         return $errtext if ($fatal);
     }
 
@@ -3508,11 +3441,11 @@ 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>'.
@@ -3544,7 +3477,7 @@ sub editor {
             }
             my $noresmsg;
             if ($allowed && $hiddentop && !$supplementalflag) {
-                $noresmsg = &mt('Main Content Hidden');
+                $noresmsg = &mt('Main Content Hidden'); 
             } else {
                 $noresmsg = &mt('Currently empty');
             }
@@ -3589,7 +3522,7 @@ sub multiple_check_form {
     return unless (ref($listsref) eq 'HASH');
     my $disabled;
     unless ($canedit) {
-        $disabled = 'disabled="disabled"';
+        $disabled = 'disabled="disabled"'; 
     }
     my $output =
     '<form action="/adm/coursedocs" method="post" name="togglemult'.$caller.'">'.
@@ -3627,7 +3560,7 @@ sub multiple_check_form {
             '</label></span></td>'."\n".
             '<td class="LC_docs_entry_parameter">'.
             '<span class="LC_nobreak LC_docs_copy">'.
-            '<label><input type="checkbox" name="copyall" id="copyall" onclick="propagateState(this.form,'."'copy'".')"'.$disabled.' />'.&mt('Copy').
+            '<label><input type="checkbox" name="copyall" id="copyall" onclick="propagateState(this.form,'."'copy'".')"'. $disabled.' />'.&mt('Copy').
             '</label></span></td>'.
             '</tr></table>'."\n";
     }
@@ -3669,6 +3602,8 @@ sub process_file_upload {
     my $quotatype = 'unofficial';
     if ($crstype eq 'Community') {
         $quotatype = 'community';
+    } elsif ($crstype eq 'Placement') {
+        $quotatype = 'placement';
     } elsif ($env{'course.'.$coursedom.'_'.$coursenum.'.internal.coursecode'}) {
         $quotatype = 'official';
     } elsif ($env{'course.'.$coursedom.'_'.$coursenum.'.internal.textbook'}) {
@@ -3862,7 +3797,7 @@ sub entryline {
     $renametitle=~s/"/%22/g;
     $renametitle=~s/ /%20/g;
     $oldtitle = $renametitle;
-    $renametitle=~s/\&#39\;/\\\'/g;
+    $renametitle=~s/\&#39;/\\\'/g;
     my $line=&Apache::loncommon::start_data_table_row();
     my ($form_start,$form_end,$form_common,$form_param);
 # Edit commands
@@ -3942,7 +3877,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'},
@@ -4091,19 +4026,11 @@ END
     my ($editlink,$extresform,$anchor,$hiddenres,$nomodal);
     my $orig_url = $url;
     $orig_url=~s{http(&colon;|:)//https(&colon;|:)//}{https$2//};
-    if ($container eq 'page') {
-        $url=~s{^http(|s)(&colon;|:)//}{/ext/};
-    } else {
-        $url=~s{^http(|s)(&colon;|:)//}{/adm/wrapper/ext/};
-    }
+    $url=~s{^http(|s)(&colon;|:)//}{/adm/wrapper/ext/};
     if (!$supplementalflag && $residx && $symb) {
         if ((!$isfolder) && (!$ispage)) {
 	    (undef,undef,$url)=&Apache::lonnet::decode_symb($symb);
-            if (($url =~ m{^ext/}) && ($container eq 'page')) {
-                $url=&Apache::lonnet::clutter_with_no_wrapper($url);
-            } else {
-                $url=&Apache::lonnet::clutter($url);
-            }
+	    $url=&Apache::lonnet::clutter($url);
 	    if ($url=~/^\/*uploaded\//) {
 	        $url=~/\.(\w+)$/;
 	        my $embstyle=&Apache::loncommon::fileembstyle($1);
@@ -4114,32 +4041,33 @@ END
 	        } elsif ($url!~/\.(sequence|page)$/) {
 		    $url='/adm/coursedocs/showdoc'.$url;
 	        }
-            } elsif ($url=~m{^(|/adm/wrapper)/ext/([^#]+)}) {
+	    } elsif ($url=~m{^(|/adm/wrapper)/ext/([^#]+)}) {
                 my $wrapped = $1;
                 my $exturl = $2;
-                if (($wrapped eq '') && ($container ne 'page')) {
+                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=~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://})) {
-                    unless (&Apache::lonnet::uses_sts()) {
-                        $url .= '?usehttp=1';
-                    }
+                    $url .= '?usehttp=1';
                     $nomodal = 1;
                 }
-	    }
+            }
             if (&Apache::lonnet::symbverify($symb,$url)) {
                 my $shownsymb = $symb;
                 if ($isexternal) {
-                    $url =~ s/\#[^#]+$//;
-                    if ($container eq 'page') {
-                        $url = &Apache::lonnet::clutter($url);
+                    if ($url =~ /^([^#]+)#([^#]+)$/) {
+                        $url = $1;
+                        $anchor = $2;
+                        if ($symb =~ m{^([^#]+)\Q#$anchor\E$}) {
+                            $shownsymb = $1.&escape('#').$anchor;
+                        }
                     }
                 }
                 unless ($env{'request.role.adv'}) {
@@ -4152,7 +4080,7 @@ END
                     }
                 }
                 if ($url ne '') {
-                    $url.=(($url=~/\?/)?'&':'?').'symb='.&escape($shownsymb);
+                    $url.=(($url=~/\?/)?'&':'?').'symb='.&HTML::Entities::encode($shownsymb,'"<>&');
                 }
             } elsif (!$env{'request.role.adv'}) {
                 my $checkencrypt;
@@ -4165,7 +4093,7 @@ END
                     }
                     if (ref($$navmapref)) {
                         if (lc($$navmapref->get_mapparam($symb,undef,"0.encrypturl")) eq 'yes') {
-                            $checkencrypt = 1;
+                            $checkencrypt = 1;       
                         }
                     }
                 }
@@ -4173,7 +4101,7 @@ END
                     my $shownsymb = &Apache::lonenc::encrypted($symb);
                     my $shownurl = &Apache::lonenc::encrypted($url);
                     if (&Apache::lonnet::symbverify($shownsymb,$shownurl)) {
-                        $url = $shownurl.(($shownurl=~/\?/)?'&':'?').'symb='.&escape($shownsymb);
+                        $url = $shownurl.(($shownurl=~/\?/)?'&':'?').'symb='.&HTML::Entities::encode($shownsymb,'"<>&');
                         if ($env{'request.enc'} ne '') {
                             delete($env{'request.enc'});
                         }
@@ -4193,11 +4121,8 @@ END
                 $url = $1;
                 $anchor = $2;
                 if (($url =~ m{^(|/adm/wrapper)/ext/(?!https:)}) && ($ENV{'SERVER_PORT'} == 443)) {
-                    unless (&Apache::lonnet::uses_sts()) {
-                        if ($hostname ne '') {
-                            $url = 'http://'.$hostname.$url;
-                        }
-                        $url .= (($url =~ /\?/) ? '&amp;':'?').'usehttp=1';
+                    if ($hostname ne '') {
+                        $url = 'http://'.$hostname.$url;
                     }
                     $nomodal = 1;
                 }
@@ -4205,12 +4130,10 @@ END
         } elsif ($url =~ m{^\Q/public/$coursedom/$coursenum/syllabus\E}) {
             if (($ENV{'SERVER_PORT'} == 443) &&
                 ($env{'course.'.$env{'request.course.id'}.'.externalsyllabus'} =~ m{^http://})) {
-                unless (&Apache::lonnet::uses_sts()) {
-                    if ($hostname ne '') {
-                        $url = 'http://'.$hostname.$url;
-                    }
-                    $url .= (($url =~ /\?/) ? '&amp;':'?').'usehttp=1';
+                if ($hostname ne '') {
+                    $url = 'http://'.$hostname.$url;
                 }
+                $url .= (($url =~ /\?/) ? '&amp;':'?').'usehttp=1';
                 $nomodal = 1;
             }
         }
@@ -4243,7 +4166,7 @@ END
                             my @resources = $$navmapref->retrieveResources($folderurl,$filterFunc,1,1);
                             unless (@resources) {
                                 $hiddenmap = 1;
-                                unless ($env{'request.role.adv'}) {
+                                unless ($env{'request.role.adv'}) {  
                                     $url = '';
                                     $hiddenfolder = 1;
                                 }
@@ -4259,10 +4182,10 @@ END
                     }
                 }
             }
-
+            
 # Append randompick number, hidden, and encrypted with ":" to foldername,
 # so it gets transferred between levels
-            $folderpath.=$containerarg.'&'.$foldername.
+	    $folderpath.=$containerarg.'&'.$foldername.
                          ':'.$rpicknum.':'.$hiddenmap.':'.$encryptmap.':'.$randorder.':'.$ispage;
             unless ($url eq '') {
                 $url.='folderpath='.&escape($folderpath);
@@ -4298,7 +4221,7 @@ $form_common."\n".
 $form_param."\n".
 $form_common."\n".
 '<span class="LC_nobreak"><label><input type="checkbox" name="randomorder_'.$orderidx.'" id="randomorder_'.$orderidx.'" onclick="checkForSubmit(this.form,'."'randomorder','settings'".');" '.$ro_set.$disabled.' /> '.&mt('Random Order').' </label></span>'.
-$form_end;
+$form_end; 
         }
     } elsif ($supplementalflag && !$allowed) {
         my $isexttool;
@@ -4375,13 +4298,13 @@ $form_end;
                $anchor = '#'.&HTML::Entities::encode($anchor,'"<>&');
            }
        }
-
        if ((!$supplementalflag) && ($nomodal) && ($hostname ne '')) {
            $link = 'http://'.$hostname.$url;
        } else {
            $link = $url;
        }
-       $link = &js_escape($link.(($url=~/\?/)?'&amp;':'?').'inhibitmenu=yes'.$anchor);
+       $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>';
@@ -4403,7 +4326,7 @@ $form_end;
            $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>';
+       $line.=$title.' <span class="LC_warning LC_docs_reinit_warn">'.&mt('(Hidden)').'</span>';
     } else {
        $line.=$title.' <span class="LC_docs_reinit_warn">'.$reinit.'</span>';
     }
@@ -4552,7 +4475,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') {
@@ -4560,9 +4483,9 @@ sub new_timebased_suffix {
                 $locknotfreed =
                     '<div class="LC_error">'.
                     &mt('A lockfile was not released when you added content to the clipboard earlier in this session.').' '.
-
+ 
                     &mt('As a result addition of items to the clipboard will be unavailable until your next log-in.');
-            } else {
+            } else { 
                 $locknotfreed .=
                     &mt('This will prevent addition of items to the clipboard until your next log-in.');
             }
@@ -4625,7 +4548,7 @@ sub untiehash {
 
 
 sub checkonthis {
-    my ($r,$url,$level,$title,$checkstale)=@_;
+    my ($r,$url,$level,$title)=@_;
     $url=&unescape($url);
     $alreadyseen{$url}=1;
     $r->rflush();
@@ -4640,22 +4563,10 @@ 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+)$/;
@@ -4689,7 +4600,7 @@ sub checkonthis {
                 &Apache::lonnet::metadata($url,'dependencies');
              foreach my $dep (split(/\,/,$dependencies)) {
 		 if (($dep=~/^\/res\//) && (!$alreadyseen{$dep})) {
-                    &checkonthis($r,$dep,$level+1,'',$checkstale);
+                    &checkonthis($r,$dep,$level+1);
                  }
              }
           } elsif ($result eq 'unavailable') {
@@ -4703,9 +4614,6 @@ 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'));
-          }
        }
     }
 }
@@ -4758,71 +4666,9 @@ sub list_symbs {
     $r->print(&endContentScreen());
 }
 
-sub short_urls {
-    my ($r,$canedit) = @_;
-    my $crstype = &Apache::loncommon::course_type();
-    my $formname = 'shortenurl';
-    $r->print(&Apache::loncommon::start_page('Display/Set Shortened URLs'));
-    $r->print(&Apache::lonhtmlcommon::breadcrumbs('Shortened URLs'));
-    $r->print(&startContentScreen('tools'));
-    my ($navmap,$errormsg) =
-        &Apache::loncourserespicker::get_navmap_object($crstype,'shorturls');
-    my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
-    my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
-    my (%maps,%resources,%titles);
-    if (!ref($navmap)) {
-        $r->print($errormsg.
-                  &endContentScreen());
-        return '';
-    } else {
-        $r->print('<h4 class="LC_info">'.&mt('Tiny URLs for deep-linking into course').'</h4>'."\n");
-        $r->rflush();
-        my $readonly;
-        if ($canedit) {
-            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>');
-            }
-            if ((ref($errors) eq 'ARRAY') && (@{$errors} > 0)) {
-                $r->print(&mt('The following errors occurred when processing your request to create shortened URLs:').'<br /><ul>');
-                foreach my $error (@{$errors}) {
-                    $r->print('<li>'.$error.'</li>');
-                }
-                $r->print('</ul><br />');
-            }
-        } else {
-            $readonly = 1;
-        }
-        my %currtiny = &Apache::lonnet::dump('tiny',$cdom,$cnum);
-        $r->print(&Apache::loncourserespicker::create_picker($navmap,'shorturls',$formname,$crstype,undef,
-                                                             undef,undef,undef,undef,undef,\%currtiny,$readonly));
-    }
-    $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,$checkstale) = @_;
+    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'));
@@ -4843,7 +4689,7 @@ sub verifycontent {
 	   }
        }
        if (($key=~/^src\_(.+)$/) && (!$alreadyseen{&unescape($hash{$key})})) {
-           &checkonthis($r,$hash{$key},0,$hash{'title_'.$1},$checkstale);
+           &checkonthis($r,$hash{$key},0,$hash{'title_'.$1});
        }
    }
    &untiehash();
@@ -4915,8 +4761,8 @@ sub checkversions {
         }
         if ($haschanged) {
             if (&Apache::lonnet::put('resourceversions',\%newsetversions,
-			      $env{'course.'.$env{'request.course.id'}.'.domain'},
-			      $env{'course.'.$env{'request.course.id'}.'.num'}) eq 'ok') {
+			             $env{'course.'.$env{'request.course.id'}.'.domain'},
+			             $env{'course.'.$env{'request.course.id'}.'.num'}) eq 'ok') {
 	        $r->print(&Apache::loncommon::confirmwrapper(
                     &Apache::lonhtmlcommon::confirm_success(&mt('Your Version Settings have been Saved'))));
 	    } else {
@@ -5048,7 +4894,7 @@ ENDHEADERS
         return;
     }
     $r->print(
-        '<input type="submit" name="setversions" value="'.$lt{'save'}.'"'.$disabled.' />'.
+       '<input type="submit" name="setversions" value="'.$lt{'save'}.'"'.$disabled.' />'.
         &Apache::loncommon::start_data_table().
         &Apache::loncommon::start_data_table_header_row().
         '<th>'.&mt('Resources').'</th>'.
@@ -5091,9 +4937,9 @@ ENDHEADERS
                       $setversions{$linkurl},
                       'set_version_'.$linkurl,
                       {'select_form_order' => ['',1..$currentversion,'mostrecent'],
-                       '' => '',
-                       'mostrecent' => &mt('most recent'),
-                       map {$_,$_} (1..$currentversion)},'',$readonly));
+                      '' => '',
+                      'mostrecent' => &mt('most recent'),
+                      map {$_,$_} (1..$currentversion)},'',$readonly));
             my $lastold=1;
             for (my $prevvers=1;$prevvers<$currentversion;$prevvers++) {
                 my $url=$root.'.'.$prevvers.'.'.$extension;
@@ -5230,6 +5076,7 @@ sub startContentScreen {
     if (($mode eq 'navmaps') || ($mode eq 'supplemental')) {
         $output .= '<li'.(($mode eq 'navmaps')?' class="active"':'').'><a href="/adm/navmaps"><b>&nbsp;&nbsp;&nbsp;&nbsp;'.&mt('Content Overview').'&nbsp;&nbsp;&nbsp;&nbsp;</b></a></li>'."\n";
         $output .= '<li'.(($mode eq 'coursesearch')?' class="active"':'').'><a href="/adm/searchcourse"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.&mt('Content Search').'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></a></li>'."\n";
+        $output .= '<li'.(($mode eq 'courseindex')?' class="active"':'').'><a href="/adm/indexcourse"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.&mt('Content Index').'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></a></li>'."\n";
         $output .= '<li '.(($mode eq 'suppdocs')?' class="active"':'').'><a href="/adm/supplemental"><b>'.&mt('Supplemental Content').'</b></a></li>';
     } else {
         $output .= '<li '.(($mode eq 'docs')?' class="active"':'').' id="tabbededitor"><a href="/adm/coursedocs?forcestandard=1"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.&mt('Main Content Editor').'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b></a></li>'."\n";
@@ -5267,17 +5114,20 @@ sub handler {
     my $coursenum=$env{'course.'.$env{'request.course.id'}.'.num'};
     my $coursedom=$env{'course.'.$env{'request.course.id'}.'.domain'};
 
+# get docroot
+    my $londocroot = $r->dir_config('lonDocRoot');
+
 # graphics settings
     $iconpath = &Apache::loncommon::lonhttpdurl($r->dir_config('lonIconsURL').'/');
 
 #
 # --------------------------------------------- Initialize help topics for this
     foreach my $topic ('Adding_Course_Doc','Main_Course_Documents',
-                       'Adding_External_Resource','Adding_External_Tool',
+	               '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',
+	               '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);
     }
@@ -5293,7 +5143,7 @@ sub handler {
     $help{'Group Portfolio'} = &Apache::loncommon::help_open_topic('Docs_About_Group_Files');
     $help{'Caching'} = &Apache::loncommon::help_open_topic('Caching');
  
-    my ($allowed,$canedit,$canview,$disabled);
+    my ($allowed,$canedit,$canview,$noendpage,$disabled);
 # URI is /adm/supplemental when viewing supplemental docs in non-edit mode.
     unless ($r->uri eq '/adm/supplemental') {
         # does this user have privileges to modify content.  
@@ -5310,31 +5160,12 @@ 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');
-      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);
-      }
+      &verifycontent($r);
   } elsif ($allowed && $env{'form.listsymbs'}) {
       &init_breadcrumbs('listsymbs','List Content IDs');
       &list_symbs($r);
-  } elsif ($allowed && $env{'form.shorturls'}) {
-      &init_breadcrumbs('shorturls','Set/Display Shortened URLs','Docs_Short_URLs');
-      &short_urls($r,$canedit);
   } elsif ($allowed && $env{'form.docslog'}) {
       &init_breadcrumbs('docslog','Show Log');
       my $folder = $env{'form.folder'};
@@ -5348,10 +5179,59 @@ sub handler {
   } elsif ($canedit && $env{'form.dumpcourse'}) {
       &init_breadcrumbs('dumpcourse','Copy '.&Apache::loncommon::course_type().' Content to Authoring Space');
       &dumpcourse($r);
-  } elsif ($allowed && $env{'form.exportcourse'}) {
+  } elsif ($canedit && $env{'form.exportcourse'}) {
       &init_breadcrumbs('exportcourse','IMS Export');
       &Apache::imsexport::exportcourse($r);
   } else {
+      if ($canedit && $env{'form.authorrole'}) {
+          $noendpage = 1;
+          my ($redirect,$error) = &makenewproblem($r,$coursedom,$coursenum);
+          if ($redirect) {
+              if (($env{'form.newresourceadd'}) && ($env{'form.folderpath'})) {
+                  my $container = 'sequence'; 
+                  my ($breadcrumbtrail,$randompick,$ishidden,$isencrypted,$plain,
+                      $is_random_order,$container) =
+                      &Apache::lonhtmlcommon::docs_breadcrumbs($allowed,$crstype,1);
+                  my (@folders)=split('&',$env{'form.folderpath'});
+                  $env{'form.foldername'}=&unescape(pop(@folders));
+                  my $folder=pop(@folders);
+                  my ($errtext,$fatal) = &mapread($coursenum,$coursedom,
+                                                  $folder.'.'.$container);
+                  my $warning;
+                  if ($fatal) {
+                      if ($container eq 'page') {
+                          $warning = &mt('An error occurred retrieving the contents of the current page.');
+                      } else {
+                          $warning = &mt('An error occurred retrieving the contents of the current folder.');
+                      }
+                  } else {
+                      my $url = $redirect;
+                      my $srcfile = $londocroot.$url;
+                      $url =~ s{^/priv/}{/res/};
+                      my $targetfile = $londocroot.$url;
+                      my $nokeyref = &Apache::lonpublisher::getnokey($r->dir_config('lonIncludes'));
+                      my $output = &Apache::lonpublisher::batchpublish($r,$srcfile,$targetfile,$nokeyref,1);
+                      $env{'form.folder'} = $folder;
+                      &snapshotbefore();
+                      my $title = &LONCAPA::map::qtunescape($env{'form.newresourcetitle'});
+                      my $ext = 'false';
+                      my $newidx = &LONCAPA::map::getresidx(&LONCAPA::map::qtunescape($url));
+                      $LONCAPA::map::resources[$newidx]=$title.':'.&LONCAPA::map::qtunescape($url).
+                                                        ':'.$ext.':normal:res';
+                      push(@LONCAPA::map::order,$newidx);
+                      &LONCAPA::map::storeparameter($newidx,'parameter_hiddenresource','yes',
+                                                   'string_yesno');
+                      &remember_parms($newidx,'hiddenresource','set','yes');
+                      ($errtext,$fatal) =
+                          &storemap($coursenum, $coursedom, $folder.'.'.$container,1);
+                      &log_differences($plain);
+                      &mark_hash_old();
+                      $r->internal_redirect($redirect);
+                      return OK;
+                  }
+              }
+          }
+      }
 #
 # Done catching special calls
 # The whole rest is for course and supplemental documents and utilities menu
@@ -5362,26 +5242,6 @@ 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
 
@@ -5404,14 +5264,6 @@ sub handler {
     my $toolsflag=0;
     if ($env{'form.tools'}) { $toolsflag=1; }
 
-    if ($env{'form.folderpath'} ne '') {
-        &validate_folderpath($supplementalflag);
-    }
-
-    if ($env{'form.supppath'} ne '') {
-        &validate_suppath();
-    }
-
     my $script='';
     my $showdoc=0;
     my $addentries = {};
@@ -5461,16 +5313,13 @@ sub handler {
 
         if (&unescape($env{'form.folderpath'}) =~ m{^(default|supplemental)&}) {
             if ($supplementalflag) {
-                undef($env{'form.folderpath'}) if ($1 eq 'default');
+                undef($env{'form.folderpath'}) if ($1 eq 'default'); 
             } else {
                 undef($env{'form.folderpath'}) if ($1 eq 'supplemental');
             }
         } else {
             undef($env{'form.folderpath'});
         }
-        if ($env{'form.folderpath'} ne '') {
-            &validate_folderpath($supplementalflag);
-        }
     }
    
 # If we are not allowed to make changes, all we can see are supplemental docs
@@ -5485,8 +5334,8 @@ sub handler {
                                   .'&'.
                                   $env{'form.folderpath'};
     }
-# If allowed and user's role is not advanced check folderpath is not hidden
-    if (($allowed) && (!$env{'request.role.adv'}) &&
+# If allowed and user's role is not advanced check folderpath is not hidden  
+    if (($allowed) && (!$env{'request.role.adv'}) && 
         ($env{'form.folderpath'} ne '') && (!$supplementalflag)) {
         my $folderurl;
         my @pathitems = split(/\&/,$env{'form.folderpath'});
@@ -5495,7 +5344,7 @@ sub handler {
             undef($env{'form.folderpath'});
         } else {
             $folderurl = "uploaded/$coursedom/$coursenum/$folder";
-            if ((split(/\:/,$pathitems[-1]))[5]) {
+            if ((split(/\:/,$pathitems[-1]))[4]) {
                 $folderurl .= '.page';
             } else {
                 $folderurl .= '.sequence';
@@ -5599,11 +5448,11 @@ sub handler {
                 }
             }
             my $tabidstr = join("','",@tabids);
-            %ltitools = &Apache::lonnet::get_domain_lti($coursedom,'consumer');
+            %ltitools = &Apache::lonnet::get_domain_ltitools($coursedom);
             my $posslti = keys(%ltitools);
             my $hostname = $r->hostname();
-            $script .= &editing_js($udom,$uname,$supplementalflag,$coursedom,$coursenum,$posslti,
-                                   $canedit,$hostname,\$navmap).
+	    $script .= &editing_js($udom,$uname,$supplementalflag,$coursedom,$coursenum,$posslti,
+                                   $londocroot,$canedit,$hostname,\$navmap).
                        &history_tab_js().
                        &inject_data_js().
                        &Apache::lonhtmlcommon::resize_scrollbox_js('docs',$tabidstr,$tid).
@@ -5624,7 +5473,8 @@ sub handler {
               .$script."\n"
               .'// ]]>'."\n"
               .'</script>'."\n"
-              .'<script type="text/javascript" src="/res/adm/includes/file_upload.js"></script>'."\n";
+              .'<script type="text/javascript" 
+                src="/res/adm/includes/file_upload.js"></script>'."\n";
 
     # Breadcrumbs
     &Apache::lonhtmlcommon::clear_breadcrumbs();
@@ -5686,7 +5536,6 @@ sub handler {
 	  undef($hadchanges);
           $uploadphase = &process_file_upload(\$upload_output,$coursenum,$coursedom,
                                               \%allfiles,\%codebase,$context,$crstype);
-          undef($navmap);
 	  if ($hadchanges) {
 	      &mark_hash_old();
 	  }
@@ -5743,8 +5592,9 @@ sub handler {
                 'impo' => 'Import',
 		'lnks' => 'Import from Stored Links',
                 'impm' => 'Import from Assembled Map',
+                'imcr' => 'Import from Course Resources',   
                 'extr' => 'External Resource',
-                'extt' => 'External Tool', 
+                'extt' => 'External Tool',
                 'selm' => 'Select Map',
                 'load' => 'Load Map',
                 'newf' => 'New Folder',
@@ -5753,7 +5603,10 @@ 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',
                 'drbx' => 'Drop Box',
                 'scuf' => 'External Scores (handgrade, upload, clicker)',
                 'bull' => 'Discussion Board',
@@ -5767,20 +5620,32 @@ sub handler {
                 'se'   => 'Select',
                 'file' =>  'File',
                 'title' => 'Title',
+                'addp' => 'Add Placeholder to course?',
+                'uste' => 'Use Template?',
+                'fnam' => 'File Name:',
+                'loca' => 'Location:',
+                'dire' => 'Directory:',
+                'cate' => 'Category:',
+                'tmpl' => 'Template:',
                 'comment' => 'Comment',
                 'parse' => 'Upload embedded images/multimedia files if HTML file',
                 'bb5'      => 'Blackboard 5',
                 'bb6'      => 'Blackboard 6',
                 'angel5'   => 'ANGEL 5.5',
                 'webctce4' => 'WebCT 4 Campus Edition',
+                'yes'      => 'Yes',
+                'no'       => 'No',
                 'er' => 'Editing rights unavailable for your current role.',
         );
 # -----------------------------------------------------------------------------
+
     # Calculate free quota space for a user or course. A javascript function checks
     # file size to determine if upload should be allowed.
     my $quotatype = 'unofficial';
     if ($crstype eq 'Community') {
         $quotatype = 'community';
+    } elsif ($crstype eq 'Placement') {
+        $quotatype = 'placement';
     } elsif ($env{'course.'.$coursedom.'_'.$coursenum.'.internal.coursecode'}) {
         $quotatype = 'official';
     } elsif ($env{'course.'.$coursedom.'_'.$coursenum.'.internal.textbook'}) {
@@ -5811,8 +5676,8 @@ sub handler {
 	my $fileupload=(<<FIUP);
         $quotainfo
 	$lt{'file'}:<br />
-	<input type="file" name="uploaddoc" class="LC_flUpload" size="40" $disabled />
-        <input type="hidden" id="LC_free_space" value="$free_space" />
+	<input type="file" name="uploaddoc" class="flUpload" size="40" $disabled />
+        <input type="hidden" id="free_space" value="$free_space" />
 FIUP
 
 	my $checkbox=(<<CHBO);
@@ -5859,7 +5724,7 @@ IMSFORM
         <fieldset id="uploaddocform" style="display: none;">
         <legend>$lt{'upfi'}</legend>
 	<input type="hidden" name="active" value="aa" />
-	$fileupload
+    $fileupload
 	<br />
 	$lt{'title'}:<br />
 	<input type="text" size="60" name="comment" $disabled />
@@ -5877,7 +5742,7 @@ FUFORM
 
         my $mapimportjs;
         if ($canedit) {
-            $mapimportjs = "javascript:openbrowser('mapimportform','importmap','sequence,page','');";
+            $mapimportjs = "javascript:openbrowser('mapimportform','importmap','sequence,page','');"; 
         } else {
             $mapimportjs = "javascript:alert('".&js_escape($lt{'er'})."');";
         }
@@ -5898,10 +5763,33 @@ FUFORM
         </form>
 
 SEDFFORM
+        my $importcrsresform;
+        my ($numdirs,$pickfile) = 
+            &Apache::loncommon::import_crsauthor_form('crsresimportform','coursepath','coursefile',
+                                                      "resize_scrollbox('contentscroll','1','0');",
+                                                      undef,'res');
+        if ($pickfile) {
+            $importcrsresform=(<<CRSFORM);
+        <a class="LC_menubuttons_link" href="javascript:toggleImportCrsres('res','$numdirs');">
+        $lt{'imcr'}</a>$help{'Course_Resources'}
+        <form action="/adm/coursedocs" method="post" name="crsresimportform" onsubmit="return validImportCrsRes();">
+        <fieldset id="importcrsresform" style="display: none;">
+        <legend>$lt{'imcr'}</legend>
+        <input type="hidden" name="active" value="bb" />
+        $pickfile
+        <p>
+        $lt{'title'}: <input type="textbox" name="crsrestitle" value="" $disabled />
+        </p>
+        <input type="hidden" name="importdetail" value="" />
+        <input type="submit" name="crsres" value="$lt{'impo'}" $disabled />
+        </fieldset>
+        </form>
+CRSFORM
+        }
 
         my $fromstoredjs;
         if ($canedit) {
-            $fromstoredjs = 'open_StoredLinks_Import()';
+            $fromstoredjs = 'open_StoredLinks_Import()'; 
         } else {
             $fromstoredjs = "alert('".&js_escape($lt{'er'})."')";
         }
@@ -5910,8 +5798,11 @@ SEDFFORM
 	{ '<img class="LC_noBorder LC_middle" src="/res/adm/pages/src.png" alt="'.$lt{srch}.'"  onclick="javascript:groupsearch()" />' => $pathitem."<a class='LC_menubuttons_link' href='javascript:groupsearch()'>$lt{'srch'}</a>" },
 	{ '<img class="LC_noBorder LC_middle" src="/res/adm/pages/res.png" alt="'.$lt{impo}.'"  onclick="javascript:groupimport();"/>' => "<a class='LC_menubuttons_link' href='javascript:groupimport();'>$lt{'impo'}</a>$help{'Importing_LON-CAPA_Resource'}" },
 	{ '<img class="LC_noBorder LC_middle" src="/res/adm/pages/wishlist.png" alt="'.$lt{lnks}.'" onclick="javascript:'.$fromstoredjs.';" />' => '<a class="LC_menubuttons_link" href="javascript:'.$fromstoredjs.';">'.$lt{'lnks'}.'</a>' },
-        { '<img class="LC_noBorder LC_middle" src="/res/adm/pages/sequence.png" alt="'.$lt{impm}.'" onclick="javascript:toggleMap(\'map\');" />' => $importpubform }
-	);
+        { '<img class="LC_noBorder LC_middle" src="/res/adm/pages/sequence.png" alt="'.$lt{impm}.'" onclick="javascript:toggleMap(\'map\');" />' => $importpubform },
+        );
+        if ($pickfile) {
+            push(@importpubforma,{ '<img class="LC_noBorder LC_middle" src="/res/adm/pages/res.png" alt="'.$lt{imcr}.'"  onclick="javascript:toggleImportCrsres(\'res\','."'$numdirs'".');"/>' => $importcrsresform});
+	}
 	$importpubform = &create_form_ul(&create_list_elements(@importpubforma));
         my $extresourcesform =
             &Apache::lonextresedit::extedit_form(0,0,undef,undef,$pathitem,
@@ -5963,9 +5854,8 @@ HIDDENFORM
     }
 
 #
-
-    my $savefolderpath;
     my $hostname = $r->hostname();
+    my $savefolderpath;
 
     if ($allowed) {
        my $folder=$env{'form.folder'};
@@ -6102,7 +5992,224 @@ NROSTFORM
         $help{'Web_Page'}
         </form>
 NWEBFORM
- 
+
+        my @ids=&Apache::lonnet::current_machine_ids();
+        my %select_menus;
+        my $numauthor = 0;
+        my $numcrsdirs = 0;
+        my $toppath = "/priv/$env{'user.domain'}/$env{'user.name'}"; 
+        if ($env{'user.author'}) {
+            $numauthor ++;
+            $select_menus{'author'}->{'text'} = &Apache::lonnet::plaintext('au');
+            if (grep(/^\Q$env{'user.home'}\E$/,@ids)) {
+                my $is_home = 1;
+                my %subdirs;
+                &Apache::lonnet::recursedirs($is_home,'priv',$londocroot,$toppath,'',\%subdirs);
+                $select_menus{'author'}->{'default'} = '/'; 
+                $select_menus{'author'}->{'select2'}->{'/'} = '/';
+                my @ordered = ('/');
+                foreach my $relpath (sort { lc($a) cmp lc($b) } (keys(%subdirs))) {
+                    $select_menus{'author'}->{'select2'}->{$relpath} = $relpath;
+                    push(@ordered,$relpath);
+                }
+                $select_menus{'author'}->{'order'} = \@ordered;
+            } else {
+                $select_menus{'author'}->{'select2'}->{'switch'} = &mt('Switch server required');
+                $select_menus{'author'}->{'default'} = 'switch';
+                $select_menus{'author'}->{'order'} = ['switch'];
+            }
+        }
+        my %roleshash = &Apache::lonnet::get_my_roles($env{'user.name'},$env{'user.domain'},'userroles',
+                                                      ['active'],['ca','aa']);
+        my $crshome = $env{'course.'.$env{'request.course.id'}.'.home'};
+        my %by_roletype;
+        if (keys(%roleshash)) {
+            foreach my $entry (keys(%roleshash)) {
+                my ($auname,$audom,$roletype) = split(/:/,$entry);
+                my $key = $entry;
+                $key =~ s/:/___/g;
+                $by_roletype{$roletype}{$auname.'___'.$audom} = 1;
+                $select_menus{$key}->{'text'} = &Apache::lonnet::plaintext($roletype)." ($audom/$auname)";
+                my $rolehome = &Apache::lonnet::homeserver($auname,$audom);
+                if (grep(/^\Q$rolehome\E$/,@ids)) {    
+                    my $is_home = 1;
+                    my (%subdirs,@ordered);
+                    my $toppath="/priv/$audom/$auname";
+                    &Apache::lonnet::recursedirs($is_home,'priv',$londocroot,$toppath,'',\%subdirs);
+                    $select_menus{$key}->{'default'} = '/';
+                    $select_menus{$key}->{'select2'}->{'/'} = '/';
+                    my @ordered = ('/');
+                    foreach my $relpath (sort { lc($a) cmp lc($b) } (keys(%subdirs))) {
+                        $select_menus{$key}->{'select2'}->{$relpath} = $relpath;
+                        push(@ordered,$relpath);
+                    }
+                    $select_menus{$key}->{'order'} = \@ordered;
+                } else {
+                    $select_menus{$key}->{'select2'}->{'switch'} = &mt('Switch server required');
+                    $select_menus{$key}->{'default'} = 'switch';
+                    $select_menus{$key}->{'order'} = ['switch'];
+                }
+                $numauthor ++;
+            }
+        }
+        my ($pickdir,$showtitle);;
+        if ($numauthor) {
+            my @order;
+            my $defrole;
+            if ($env{'user.author'}) {
+                push(@order,'author');
+                $defrole = 'author';
+            }
+            if (keys(%by_roletype)) {
+                foreach my $possrole ('ca','aa') {
+                    if (ref($by_roletype{$possrole}) eq 'HASH') {
+                        foreach my $author (sort { lc($a) cmp lc($b) } (keys(%{$by_roletype{$possrole}}))) {
+                            unless ($defrole) {
+                                $defrole = $author;
+                            }
+                            push(@order,$author.'___'.$possrole);
+                        }
+                    }
+                }
+            }
+            $select_menus{'course'}->{'text'} = &mt('Course Resource');
+            if (grep(/^\Q$crshome\E$/,@ids)) {
+                my $is_home = 1;
+                my %subdirs;
+                my $toppath="/priv/$coursedom/$coursenum";
+                &Apache::lonnet::recursedirs($is_home,'priv',$londocroot,$toppath,'',\%subdirs);
+                $numcrsdirs = keys(%subdirs);
+                $select_menus{'course'}->{'default'} = '/';
+                $select_menus{'course'}->{'select2'}->{'/'} = '/';
+                my @ordered = ('/');
+                foreach my $relpath (sort { lc($a) cmp lc($b) } (keys(%subdirs))) {
+                    $select_menus{'course'}->{'select2'}->{$relpath} = $relpath;
+                    push(@ordered,$relpath);
+                }
+                $select_menus{'course'}->{'order'} = \@ordered;
+            } else {
+                $select_menus{'course'}->{'select2'}->{'switch'} = &mt('Switch server required');
+                $select_menus{'course'}->{'default'} = 'switch';
+                $select_menus{'course'}->{'order'} = ['switch'];
+            }
+            push(@order,'course');
+            $pickdir = $lt{'loca'}.
+                       &Apache::loncommon::linked_select_forms('courseresform','<br />'.$lt{'dire'},
+                                                               $defrole,'authorrole','authorpath',
+                                                               \%select_menus,\@order,'toggleCrsResTitle();',
+                                                               '','priv').'<br />';
+            $showtitle = 'none';
+        } else {
+            my $is_home;
+            $showtitle = 'inline';
+            if (grep(/^\Q$crshome\E$/,@ids)) {
+                $is_home = 1;
+                $pickdir .= '<input type="hidden" name="authorrole" value="course" />'; 
+                my $toppath="/priv/$coursedom/$coursenum'}";
+                my %subdirs;
+                &Apache::lonnet::recursedirs($is_home,'priv',$londocroot,$toppath,'',\%subdirs);
+                $numcrsdirs = keys(%subdirs); 
+                if ($numcrsdirs) {
+                    $pickdir .= &mt('Directory: ').'<select name="authorpath">'."\n".
+                                 '<option value="/">/</option>'."\n";
+                    foreach my $key (sort { lc($a) cmp lc($b) } (keys(%subdirs))) {
+                        $pickdir .= '<option value="'.$key.'">'.$key.'</option>'."\n";
+                    }
+                    $pickdir .= '</select>';
+                } else {
+                    $pickdir .= '<input type="hidden" name="authorpath" value="/" />'."\n";   
+                }
+            }
+        }
+
+        my %seltemplate_menus;
+        my @files = &Apache::lonhomework::get_template_list('problem');
+        my @noexamplelink = ('blank.problem','blank.library','script.library');
+        my $currentcategory = '';
+        my @ordered = ('');
+        my %templatehelp;
+        my $defcategory = '';
+        my @catorder = ($defcategory);
+        $seltemplate_menus{$defcategory}->{'order'} = [''];
+        $seltemplate_menus{$defcategory}->{'text'} = '';
+        foreach my $file (@files) {
+            if (ref($file) eq 'ARRAY') {
+                my ($path,$title,$category,$help) = @{$file};
+                next if ($title !~ /\S/);
+                if (&js_escape($category) ne $currentcategory) {
+                    $currentcategory = &js_escape($category);
+                    push(@catorder,&js_escape($currentcategory));
+                    $seltemplate_menus{$currentcategory}->{'text'} = $category;
+                    $seltemplate_menus{$currentcategory}->{'default'} = '';
+                    $seltemplate_menus{$currentcategory}->{'select2'}->{''} = '';
+                    push(@{$seltemplate_menus{$currentcategory}->{'order'}},'');
+                }
+                if ($path) {
+                    $seltemplate_menus{$currentcategory}->{'select2'}->{&js_escape($path)} = $title;
+                    push(@{$seltemplate_menus{$currentcategory}->{'order'}},&js_escape($path));
+                    if ($help) {
+                        $templatehelp{$path} = $help;
+                    }
+                }
+            }
+        }
+
+        my $templates = $lt{'cate'}.' '.
+                        &Apache::loncommon::linked_select_forms('courseresform','<br />'.$lt{'tmpl'}.' ',
+                                                                $defcategory,'tempcategory','template',
+                                                                \%seltemplate_menus,\@catorder,
+                                                                "resize_scrollbox('contentscroll','1','0');",
+                                                                "toggleExampleText();",'template').'<br />';
+        my $templatepreview =  '<a href="#" target="sample" onclick="javascript:getExample(600,420,\'yes\',true);  return false;">'.
+                               '<span id="newresexample">'.&mt('Example').'<span></a>';
+        my $crsresform=(<<RESFORM);
+        <a class="LC_menubuttons_link" href="javascript:toggleCrsRes('res','$numauthor','$numcrsdirs');">
+        $lt{'stpr'}</a>$help{'Course_Resource'}
+        <form action="/adm/coursedocs" method="post" name="courseresform">
+        <fieldset id="crsresform" style="display:none;">
+        <legend>$lt{'stpr'}</legend>
+        <input type="hidden" name="active" value="ee" />
+        <p>
+        $pickdir
+        <span class="LC_nobreak">$lt{'news'}?&nbsp;
+        <label><input type="radio" name="newsubdir" value="0" onclick="toggleNewsubdir(this.form);" checked="checked" $disabled />No</label>
+        &nbsp;
+        <label><input type="radio" name="newsubdir" value="1" onclick="toggleNewsubdir(this.form);" $disabled />Yes</label>
+        </span><span id="newsubdir"></span>
+        <input type="hidden" name="newsubdirname" id="newsubdirname" value="" autocomplete="off" />
+        </p>
+        $lt{'fnam'}
+        <input type="text" size="20" name="newresourcename" autocomplete="off" $disabled />
+        <p>
+        <div id="newresource" style="display:$showtitle">
+        $lt{'addp'}
+        <label><input type="radio" name="newresourceadd" value="0" checked="checked" onclick="toggleNewInCourse(this.form);" $disabled />
+        $lt{'no'}</label>&nbsp;&nbsp;
+        <label><input type="radio" name="newresourceadd" value="1" onclick="toggleNewInCourse(this.form);" $disabled />
+        $lt{'yes'}</label>
+        <span id="newrestitle"></span>
+        <input type="hidden" size="20" name="newresourcetitle" id="newresourcetitle" autocomplete="off" $disabled />
+        </div>
+        </p>
+        <p>
+        $lt{'uste'}
+        <label><input type="radio" name="newresusetemp" value="0" checked="checked" onclick="toggleWithTemplate(this.form);" $disabled />
+        $lt{'no'}</label>&nbsp;&nbsp;
+        <label><input type="radio" name="newresusetemp" value="1" onclick="toggleWithTemplate(this.form);" $disabled />
+        $lt{'yes'}</label>
+        <div id="newrestemplate" style="display:none">
+        $templates
+        $templatepreview
+        </div>
+        </p>
+        <span class="LC_nobreak">
+        <input type="hidden" name="folderpath" value="$env{'form.folderpath'}" />
+        <input type="submit" name="newcrs" value="$lt{'crpr'}" $disabled />
+        </span>
+        </fieldset>
+        </form>
+
+RESFORM
 
 my $specialdocumentsform;
 my @specialdocumentsforma;
@@ -6157,19 +6264,13 @@ NSYLFORM
 	$help{'Group Portfolio'}
 	</form>
 NGFFORM
-        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=(
+	@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));
 
 
@@ -6195,7 +6296,7 @@ NGFFORM
         {'<img class="LC_noBorder LC_middle" src="/res/adm/pages/simpprob.png" alt="'.$lt{sipr}.'" onclick="javascript:makesmpproblem();" />'=>$newsmpproblemform},
         {'<img class="LC_noBorder LC_middle" src="/res/adm/pages/dropbox.png" alt="'.$lt{drbx}.'" onclick="javascript:makedropbox();" />'=>$newdropboxform},
         {'<img class="LC_noBorder LC_middle" src="/res/adm/pages/scoreupfrm.png" alt="'.$lt{scuf}.'" onclick="javascript:makeexamupload();" />'=>$newexuploadform},
-
+        {'<img class="LC_noBorder LC_middle" src="/res/adm/pages/simpprob.png" alt="'.$lt{stpr}.'" onclick="javascript:toggleCrsRes(\'res\','."'$numauthor','$numcrsdirs'".');" />'=>$crsresform},
         );
         $gradingform = &create_form_ul(&create_list_elements(@gradingforma));
 
@@ -6212,11 +6313,11 @@ my %orderhash = (
                 'aa' => ['Upload',$fileuploadform],
                 'bb' => ['Import',$importpubform],
                 'cc' => ['Grading',$gradingform],
-                'ee' => ['Other',$specialdocumentsform],
                 );
 unless ($container eq 'page') {
     $orderhash{'00'} = ['Newfolder',$newfolderform];
     $orderhash{'dd'} = ['Collaboration',$communityform];
+    $orderhash{'ee'} = ['Other',$specialdocumentsform];
 }
 
  $hadchanges=0;
@@ -6229,9 +6330,7 @@ unless ($container eq 'page') {
              $r->print('<p><span class="LC_error">'.$error.'</span></p>');
           }
           if ($hadchanges) {
-             unless (&is_hash_old()) {
-                 &mark_hash_old();
-             }
+             &mark_hash_old();
           }
 
           &changewarning($r,'');
@@ -6292,7 +6391,9 @@ SNFFORM
         my $supextform =
             &Apache::lonextresedit::extedit_form(1,0,undef,undef,$pathitem,
                                                  $help{'Adding_External_Resource'},
-                                                 undef,undef,$disabled);
+                                                 undef,undef,undef,undef,undef,undef,
+                                                 $disabled);
+
         my $supexttoolform =
             &Apache::lonextresedit::extedit_form(1,0,undef,undef,$pathitem,
                                                  $help{'Adding_External_Tool'},
@@ -6358,8 +6459,8 @@ my @supimportdoc = (
                 {'<img class="LC_noBorder LC_middle" src="/res/adm/pages/exttool.png" alt="'.$lt{extt}.'" onclick="javascript:toggleUpload(\'supptool\')" />'
             =>$supexttoolform});
         }
-        push(@supimportdoc,
-                {'<img class="LC_noBorder LC_middle" src="/res/adm/pages/pdfupload.png" alt="'.$lt{upl}.'" onclick="javascript:toggleUpload(\'suppdoc\');" />'
+        push(@supimportdoc, 
+            {'<img class="LC_noBorder LC_middle" src="/res/adm/pages/pdfupload.png" alt="'.$lt{upl}.'" onclick="javascript:toggleUpload(\'suppdoc\');" />'
             =>$supupdocform},
         );
 
@@ -6387,12 +6488,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,'',$hostname);
+                            $supplementalflag,'',$iconpath,$pathitem,'','',$hostname);
         if ($error) {
             $r->print('<p><span class="LC_error">'.$error.'</span></p>');
         }
@@ -6419,14 +6520,15 @@ my %suporderhash = (
                 &entryline(0,&mt("Click to download or use your browser's Save Link function"),$showdoc).'</table>');
   }
  }
- $r->print(&Apache::loncommon::end_page());
+ unless ($noendpage) { 
+     $r->print(&Apache::loncommon::end_page());
+ }
  return OK;
 }
 
 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" />
@@ -6447,11 +6549,7 @@ sub embedded_destination {
     } elsif ($folder =~ /^(default|supplemental)_(\d+)$/) {
         $destination .=  $2.'/';
     }
-    my $newidx = $env{'form.newidx'};
-    $newidx =~s /\D+//g;
-    if ($newidx) {
-        $destination .= $newidx;
-    }
+    $destination .= $env{'form.newidx'};
     my $dir_root = '/userfiles';
     return ($destination,$dir_root);
 }
@@ -6477,9 +6575,6 @@ 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";
@@ -6592,7 +6687,6 @@ sub generate_admin_menu {
                                          'vc'   => 'Verify Content',
                                          'cv'   => 'Check/Set Resource Versions',
                                          'ls'   => 'List Resource Identifiers',
-                                         'ct'   => 'Display/Set Shortened URLs for Deep-linking',
                                          'imse' => 'Export contents to IMS Archive',
                                          'dcd'  => "Copy $crstype Content to Authoring Space",
             );
@@ -6643,13 +6737,6 @@ sub generate_admin_menu {
                     icon       => 'symbs.png',
                     linktitle  => "List the unique identifier used for each resource instance in your $lc_crstype"
                 },
-                {   linktext   => $lt{'ct'},
-                    url        => "javascript:injectData(document.courseverify,'dummy','shorturls','$lt{'ct'}')",
-                    permission => 'F',
-                    help       => 'Docs_Short_URLs',
-                    icon       => 'shorturls.png',
-                    linktitle  => "Set shortened URLs for a resource or folder in your $lc_crstype for use in deep-linking"
-                },
                 ]
         });
     if ($canedit) {
@@ -6781,7 +6868,7 @@ END
 
 sub editing_js {
     my ($udom,$uname,$supplementalflag,$coursedom,$coursenum,$posslti,
-        $canedit,$hostname,$navmapref) = @_;
+        $londocroot,$canedit,$hostname,$navmapref) = @_;
     my %js_lt = &Apache::lonlocal::texthash(
                                           p_mnf => 'Name of New Folder',
                                           t_mnf => 'New Folder',
@@ -6793,6 +6880,7 @@ sub editing_js {
                                           p_mdb => 'Title for the Drop Box',
                                           p_mbb => 'Title for the Discussion Board',
                                           p_mwp => 'Title for Web Page',
+                                          p_mnr => 'Title for the Resource',
                                           p_mab => "Enter user:domain for User's Personal Information Page",
                                           p_mab2 => 'Personal Information Page of ',
                                           p_mab_alrt1 => 'Not a valid user:domain',
@@ -6821,9 +6909,11 @@ sub editing_js {
                                           noor    => 'No actions selected or changes to settings specified.',
                                           noch    => 'No changes to settings specified.',
                                           noac    => 'No actions selected.',
+                                          nofi    => 'No file selected',
+                                          tinc    => 'Title in course',
+                                          sunm    => 'Sub-directory name',
                                           edri    => 'Editing rights unavailable for your current role.',
                                         );
-
     &js_escape(\%js_lt);
     my $crstype = &Apache::loncommon::course_type();
     my $docs_folderpath = &HTML::Entities::encode($env{'environment.internal.'.$env{'request.course.id'}.'.docs_folderpath.folderpath'},'<>&"');
@@ -6853,37 +6943,25 @@ sub editing_js {
                 } else {
                     $url = $res;
                 }
-                $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,'<>&"');
-                }
+                $backtourl = &HTML::Entities::encode(&Apache::lonnet::clutter($url),'<>&"').'?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://})) {
-                        unless (&Apache::lonnet::uses_sts()) {
-                            if ($hostname ne '') {
-                                $backtourl = 'http://'.$hostname.$backtourl;
-                            }
-                            $backtourl .= (($backtourl =~ /\?/) ? '&amp;':'?').'usehttp=1';
+                        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 '')) {
-                        unless (&Apache::lonnet::uses_sts()) {
-                            if ($hostname ne '') {
-                                $backtourl = 'http://'.$hostname.$backtourl;
-                            }
-                            $backtourl .= (($backtourl =~ /\?/) ? '&amp;':'?').'usehttp=1';
-                        }
+                        $backtourl = 'http://'.$hostname.$backtourl;
                     }
                 }
                 if ($anchor ne '') {
                     $backtourl .= '#'.&HTML::Entities::encode($anchor,'<>&"');
                 }
-                $backtourl = &Apache::loncommon::escape_single($backtourl); 
+                $backtourl = &Apache::loncommon::escape_single($backtourl);
             } else {
                 $backtourl = '/adm/navmaps';
             }
@@ -6909,14 +6987,14 @@ sub editing_js {
             $fieldsets .= ",'supptool'";
         }
     }
-
+    
     my $jsmakefunctions;
     if ($canedit) {
         $jsmakefunctions = <<ENDNEWSCRIPT;
 function makenewfolder(targetform,folderseq) {
     var foldername=prompt('$js_lt{"p_mnf"}','$js_lt{"t_mnf"}');
     if (foldername) {
-       targetform.importdetail.value=encodeURIComponent(foldername)+"="+folderseq;
+       targetform.importdetail.value=escape(foldername)+"="+folderseq;
         targetform.submit();
     }
 }
@@ -6924,7 +7002,7 @@ function makenewfolder(targetform,folder
 function makenewpage(targetform,folderseq) {
     var pagename=prompt('$js_lt{"p_mnp"}','$js_lt{"t_mnp"}');
     if (pagename) {
-        targetform.importdetail.value=encodeURIComponent(pagename)+"="+folderseq;
+        targetform.importdetail.value=escape(pagename)+"="+folderseq;
         targetform.submit();
     }
 }
@@ -6933,7 +7011,7 @@ function makeexamupload() {
    var title=prompt('$js_lt{"p_mxu"}');
    if (title) {
     this.document.forms.newexamupload.importdetail.value=
-	encodeURIComponent(title)+'=/res/lib/templates/examupload.problem';
+	escape(title)+'=/res/lib/templates/examupload.problem';
     this.document.forms.newexamupload.submit();
    }
 }
@@ -6942,7 +7020,7 @@ function makesmppage() {
    var title=prompt('$js_lt{"p_msp"}');
    if (title) {
     this.document.forms.newsmppg.importdetail.value=
-	encodeURIComponent(title)+'=/adm/$udom/$uname/new/smppg';
+	escape(title)+'=/adm/$udom/$uname/new/smppg';
     this.document.forms.newsmppg.submit();
    }
 }
@@ -6956,8 +7034,8 @@ function makewebpage(type) {
        formname = this.document.forms.newwebpage;
    }
    if (title) {
-       var webpage = formname.importdetail.value;
-       formname.importdetail.value = encodeURIComponent(title)+'='+webpage;
+       var webpage = formname.importdetail.value; 
+       formname.importdetail.value = escape(title)+'='+webpage;
        formname.submit();
    }
 }
@@ -6966,7 +7044,7 @@ function makesmpproblem() {
    var title=prompt('$js_lt{"p_msb"}');
    if (title) {
     this.document.forms.newsmpproblem.importdetail.value=
-	encodeURIComponent(title)+'=/res/lib/templates/simpleproblem.problem';
+	escape(title)+'=/res/lib/templates/simpleproblem.problem';
     this.document.forms.newsmpproblem.submit();
    }
 }
@@ -6975,7 +7053,7 @@ function makedropbox() {
    var title=prompt('$js_lt{"p_mdb"}');
    if (title) {
     this.document.forms.newdropbox.importdetail.value=
-        encodeURIComponent(title)+'=/res/lib/templates/DropBox.problem';
+        escape(title)+'=/res/lib/templates/DropBox.problem';
     this.document.forms.newdropbox.submit();
    }
 }
@@ -6984,7 +7062,7 @@ function makebulboard() {
    var title=prompt('$js_lt{"p_mbb"}');
    if (title) {
     this.document.forms.newbul.importdetail.value=
-	encodeURIComponent(title)+'=/adm/$udom/$uname/new/bulletinboard';
+	escape(title)+'=/adm/$udom/$uname/new/bulletinboard';
     this.document.forms.newbul.submit();
    }
 }
@@ -7106,14 +7184,14 @@ function toggleUpload(caller) {
         document.getElementById('upload'+blocks[i]+'form').style.display=disp;
         if ((caller == 'tool') || (caller == 'supptool')) {
             if (disp == 'block') {
-                if (document.getElementById('LC_exttoolid')) {
-                    var toolselector = document.getElementById('LC_exttoolid');
+                if (document.getElementById('LC_exttoolid')) { 
+                    var toolselector = document.getElementById('LC_exttoolid'); 
                     var suppflag = 0;
                     if (caller == 'supptool') {
                         suppflag = 1;
                     }
                     currForm = document.getElementById('new'+caller);
-                    updateExttool(toolselector,currForm,suppflag);
+                    updateExttool(toolselector,currForm,suppflag); 
                 }
             }
         }
@@ -7137,6 +7215,186 @@ function toggleMap(caller) {
     return;
 }
 
+function toggleCrsRes(caller,numauthorrole,numcrsdirs) {
+    var disp = 'none';
+    if (document.getElementById('crsresform')) {
+        if (caller == 'res') {
+            var curr = document.getElementById('crsresform').style.display;
+            if (curr == 'none') {
+                disp='block';
+                numauthor = parseInt(numauthorrole);
+                if (numauthor > 0) {
+                    document.courseresform.authorrole.selectedIndex = 0;
+                    select1priv_changed();
+                    document.courseresform.authorpath.selectedIndex = 0;
+                    document.courseresform.newresourceadd.selectedIndex = 0;
+                    toggleNewInCourse(document.courseresform);
+                    if (document.getElementById('newresource')) {
+                        document.getElementById('newresource').style.display = 'none';
+                    }
+                } else {
+                    if (numcrsdirs) {
+                        document.courseresform.authorpath.selectedIndex = 0;
+                    }
+                }
+                if (document.courseresform.newresusetemp.length) {
+                    document.courseresform.newresusetemp[0].checked = true;
+                    toggleWithTemplate(document.courseresform);
+                }
+                document.courseresform.newresourcename.value = ''; 
+            }
+        }
+        if (document.courseresform.newsubdir.length) {
+            for (var j=0; j<document.courseresform.newsubdir.length; j++) {
+                if (document.courseresform.newsubdir[j].value == 0) {
+                    document.courseresform.newsubdir[j].checked = true;
+                }
+                break;
+            }
+            if (document.getElementById('newsubdirname')) {
+                document.getElementById('newsubdirname').type = "hidden";
+                document.getElementById('newsubdirname').value = "";
+            }
+            if (document.getElementById('newsubdir')) {
+                document.getElementById('newsubdir').innerHTML = "";
+            }
+        }
+        document.getElementById('crsresform').style.display=disp;
+        resize_scrollbox('contentscroll','1','0');
+    }
+    return;
+}
+
+function toggleNewsubdir(form) {
+    if (form.newsubdir.length) {
+        for (var j=0; j<form.newsubdir.length; j++) {
+            if (form.newsubdir[j].checked) {
+                if (document.getElementById('newsubdirname')) {
+                    if (form.newsubdir[j].value == '1') {
+                        document.getElementById('newsubdirname').type = "text"; 
+                        if (document.getElementById('newsubdir')) {
+                            document.getElementById('newsubdir').innerHTML = '<br />$js_lt{'sunm'}';
+                        }
+                    } else {
+                        document.getElementById('newsubdirname').type = "hidden";
+                        document.getElementById('newsubdirname').value = "";
+                        document.getElementById('newsubdir').innerHTML = "";
+                    }
+                }
+                break;
+            }
+        }
+    }
+}
+
+function toggleCrsResTitle() {
+    if (document.getElementById('newresource')) {
+        if (document.courseresform.authorrole.options[document.courseresform.authorrole.selectedIndex].value == 'course') {
+            document.getElementById('newresource').style.display = 'inline';
+            document.courseresform.newresourceadd[0].checked = true;
+            toggleNewInCourse(document.courseresform);
+        } else {
+            document.getElementById('newresource').style.display = 'none';
+        }
+    } 
+}
+
+function toggleNewInCourse(form) {
+    if (form.newresourceadd.length) {
+        for (var i=0; i<form.newresourceadd.length; i++) {
+            if (form.newresourceadd[i].checked) {
+                if (document.getElementById('newresourcetitle')) {
+                    if (form.newresourceadd[i].value == '1') {
+                        document.getElementById('newresourcetitle').type = 'text';
+                        if (document.getElementById('newrestitle')) {
+                            document.getElementById('newrestitle').innerHTML = "<br />$js_lt{'tinc'}";
+                        }
+                    } else {
+                        document.getElementById('newresourcetitle').type = 'hidden';
+                        document.getElementById('newresourcetitle').value = '';
+                        if (document.getElementById('newrestitle')) { 
+                            document.getElementById('newrestitle').innerHTML = '';
+                        }
+                    }
+                }
+                break;
+            }
+        }
+    }
+}
+
+function toggleWithTemplate(form) {
+    if (form.newresusetemp.length) {
+        for (var i=0; i<form.newresusetemp.length; i++) {
+            if (form.newresusetemp[i].checked) {
+                if (document.getElementById('newrestemplate')) { 
+                    if (form.newresusetemp[i].value == '1') {
+                        document.getElementById('newrestemplate').style.display = 'inline';
+                        toggleExampleText();
+                    } else {
+                        form.tempcategory.selectedIndex = 0;
+                        select1template_changed();
+                        document.getElementById('newrestemplate').style.display = 'none';
+                    }
+                }
+            }
+        }
+    }
+}
+
+function toggleExampleText() {
+    if (document.getElementById('newresexample')) {
+        var url = document.courseresform.template.options[document.courseresform.template.selectedIndex].value;
+        if (url == '') {
+            document.getElementById('newresexample').style.fontWeight = 'normal';
+        } else {
+            document.getElementById('newresexample').style.fontWeight = 'bold';
+        }
+    }
+}
+
+function getExample(width,height,scrolling,transparency) {
+    var url;
+    if (document.courseresform.newresusetemp.length) {
+        for (var i=0; i<document.courseresform.newresusetemp.length; i++) {
+            if (document.courseresform.newresusetemp[i].checked) {
+                if (document.courseresform.newresusetemp[i].value == '1') {
+                    var url = document.courseresform.template.options[document.courseresform.template.selectedIndex].value;
+                    if (url == '') {
+                        alert('Pick a category and template');
+                    } else {
+                        url = url.replace("$londocroot",""); 
+                        url += '?inhibitmenu=yes';
+                    }
+                }
+                break;
+            }
+        }
+    }
+    if (url != '') {
+        openMyModal(url,width,height,scrolling,transparency,'');
+    }
+}
+
+function toggleImportCrsres(caller,dircount) {
+    var disp = 'none';
+    if (document.getElementById('importcrsresform')) {
+        if (caller == 'res') {
+            var numdirs = parseInt(dircount);
+            var curr = document.getElementById('importcrsresform').style.display;
+            if (curr == 'none') {
+                disp='block';
+                if (numdirs > 1) {
+                    select1res_changed();
+                }
+            }
+        }
+        document.getElementById('importcrsresform').style.display=disp;
+        resize_scrollbox('contentscroll','1','0');
+    }
+    return;
+}
+
 function makeims(imsform) {
     if ((imsform.uploaddoc.value == '')  || (!imsform.uploaddoc.value)) {
         alert("$js_lt{'imsfile'}");
@@ -7443,11 +7701,11 @@ for (i = 0; i < currentLis.length; i++)
 
 function hideAll(current, nav, data) {
 unselectInactive(nav);
-if (current) {
+if (current) { 
     if (current.className == 'right'){
-	current.className = 'right active'
+        current.className = 'right active'
     } else {
-	current.className = 'active';
+        current.className = 'active';
     }
 }
 currentData = document.getElementById(data);
@@ -7485,13 +7743,15 @@ function showPage(current, pageId, nav,
 	unselectInactive(nav);
         if ((currstate == 'active') || (currstate == 'right active')) {
             if (currstate == 'active') {
-                current.className = '';
+	        current.className = '';
             } else {
                 current.className = 'right';
             }
-            activeTab = '';
+            activeTab = ''; 
             toggleUpload();
             toggleMap();
+            toggleCrsRes();
+            toggleImportCrsres();
             resize_scrollbox('contentscroll','1','0');
             return;
         } else {
@@ -7502,6 +7762,8 @@ function showPage(current, pageId, nav,
         activeTab = pageId;
         toggleUpload();
         toggleMap();
+        toggleCrsRes();
+        toggleImportCrsres();
         if (nav == 'mainnav') {
             var storedpath = "$docs_folderpath";
             var storedpage = "$main_container_page";
@@ -7865,6 +8127,33 @@ function setBoxes(value) {
     return;
 }
 
+function validImportCrsRes() {
+    var path =  document.crsresimportform.coursepath.options[document.crsresimportform.coursepath.selectedIndex].value;
+    var fname = document.crsresimportform.coursefile.options[document.crsresimportform.coursefile.selectedIndex].value;
+    if ((fname == '') || (fname == null)) {
+        alert("$js_lt{'nofi'}");
+        return false;
+    }
+    var url = '/res/$coursedom/$coursenum/';
+    if (path && path != '/') {
+        url += path+'/';
+    }
+    if (fname != '') {
+        url += fname;
+    }
+    var title = document.crsresimportform.crsrestitle.value;
+    document.crsresimportform.importdetail.value=escape(title)+'='+escape(url);
+    return true;
+}
+
+function validateNewRes(caller) {
+    if (caller == 'single') {
+        var role = document.courseresform.authorrole.options[document.courseresform.authorrole.selectedIndex].value; 
+        var authorpath = document.courseresform.authorpath.options[document.courseresform.authorpath.selectedIndex].value;
+        var resname = document.courseresform.newresourcename.value;
+    }
+}
+
 ENDSCRIPT
 }
 
@@ -7995,6 +8284,262 @@ sub makesimpleeditform {
 SIMPFORM
 }
 
+sub makenewproblem {
+    my ($r,$coursedom,$coursenum) = @_;
+# Creating a new problem
+    my ($redirect,$error);
+    if ($env{'form.authorrole'}) {
+        my ($newsubdir,$filename);
+        if ($env{'form.newsubdir'}) {
+            if ($env{'form.newsubdirname'} ne '') {
+                $newsubdir = $env{'form.newsubdirname'};
+            }    
+        }
+        if ($env{'form.newresourcename'}) {
+            $filename = $env{'form.newresourcename'};
+            $filename =~ s/\.(\d+)(\.\w+)$/$2/;
+            $filename =~ s/`//g;
+            $filename =~ s{/\.\./}{_}g;
+            $filename =~ s/\.+/./g;
+            $filename =~ s{/+}{_}g;
+            if ($filename ne '') {
+                my ($name,$ext) = ($filename =~ /(.+)\.([^.]+)$/);
+                if (($ext) && ($ext ne '.problem')) {
+                    $filename = $name.'.problem';
+                } elsif ($ext eq '') {
+                    $filename .= '.problem';
+                }
+                my $docroot = $r->dir_config('lonDocRoot');
+                my @ids=&Apache::lonnet::current_machine_ids();
+                if ($env{'form.authorrole'} eq 'author') {
+                    if ($env{'user.author'}) {
+                        if ($env{'user.home'} && grep(/^\Q$env{'user.home'}\E$/,@ids)) {
+                            my $url = "/priv/$env{'user.domain'}/$env{'user.name'}";
+                            my $path = $docroot.$url;
+                            my $subdir = $env{'form.authorpath'};
+                            $redirect = &finishnewprob($url,$path,$subdir,$newsubdir,$filename);
+                        }
+                    }
+                } elsif ($env{'form.authorrole'} eq 'course') {
+                    my $chome = $env{'course.'.$env{'request.course.id'}.'.home'};
+                    if ($chome && grep(/^\Q$chome\E$/,@ids)) {
+                        my $url = "/priv/$coursedom/$coursenum";
+                        my $path=$docroot.$url;
+                        my $subdir = $env{'form.authorpath'};
+                        $redirect = &finishnewprob($url,$path,$subdir,$newsubdir,$filename);
+                        if ($redirect) {
+                            my $rightsfile = 'default.rights';
+                            my $sourcerights = "$path/$rightsfile";
+                            my $targetrights = $docroot."/res/$coursedom/$coursenum/$rightsfile";
+                            my $now = time;
+                            if (!-e $sourcerights) {
+                                my $cid = $coursedom.'_'.$coursenum;
+                                if (open(my $fh,">$sourcerights")) {
+                                    print $fh <<END;
+<accessrule effect="deny" realm="" type="course" role="" />
+<accessrule effect="allow" realm="$cid" type="course" role="" />
+END
+                                    close($fh);
+                                }
+                            }
+                            if (!-e "$sourcerights.meta") {
+                                if (open(my $fh,">$sourcerights.meta")) {
+                                    my $author=$env{'environment.firstname'}.' '.
+                                               $env{'environment.middlename'}.' '.
+                                               $env{'environment.lastname'}.' '.
+                                               $env{'environment.generation'};
+                                    $author =~ s/\s+$//;
+                                    print $fh <<"END";
+
+<abstract></abstract>
+<author>$author</author>
+<authorspace>$coursenum:$coursedom</authorspace>
+<copyright>private</copyright>
+<creationdate>$now</creationdate>
+<customdistributionfile></customdistributionfile>
+<dependencies></dependencies>
+<domain>$coursedom</domain>
+<highestgradelevel>0</highestgradelevel>
+<keywords></keywords>
+<language>notset </language>
+<lastrevisiondate>$now</lastrevisiondate>
+<lowestgradelevel>0</lowestgradelevel>
+<mime>rights</mime>
+<modifyinguser>$env{'user.name'}:$env{'user.domain'}</modifyinguser>
+<notes></notes>
+<obsolete></obsolete>
+<obsoletereplacement></obsoletereplacement>
+<owner>$coursenum:$coursedom</owner>
+<rule>deny:::course,allow:$cid::course</rule>
+<sourceavail></sourceavail>
+<standards></standards>
+<subject></subject>
+<title></title>
+END
+                                        close($fh);
+                                }
+                                if ((-e $sourcerights) && (-e "$sourcerights.meta")) {
+                                    if (!-e "$docroot/res/$coursedom") {
+                                        mkdir("$docroot/res/$coursedom",0755);
+                                    }
+                                    if (!-e "$docroot/res/$coursedom/$coursenum") {
+                                        mkdir("$docroot/res/$coursedom/$coursenum",0755);
+                                    }
+                                    if ((-e "$docroot/res/$coursedom/$coursenum") && (!-e $targetrights)) {
+                                        my $nokeyref = &Apache::lonpublisher::getnokey($r->dir_config('lonIncludes'));
+                                        my $output = &Apache::lonpublisher::batchpublish($r,$sourcerights,$targetrights,$nokeyref,1);
+                                    }
+                                }
+                            }
+                            if ($env{'form.newresourceadd'}) {
+                                my $template = $env{'form.template'};
+                                my $source = $docroot.$redirect;
+                                my $target = $redirect;
+                                $target =~ s{^/priv/}{/res/};
+                                $target = $docroot.$target;
+                                if (!-e $source) {
+                                    my $copyfrom; 
+                                    if ($template) {
+                                        my %templates;
+                                        my @files = &Apache::lonhomework::get_template_list('problem');
+                                        foreach my $poss (@files) {
+                                            if (ref($poss) eq 'ARRAY') {
+                                                if ($template eq $poss->[0]) {
+                                                    $templates{$template} = 1;
+                                                    last;
+                                                }
+                                            }
+                                        }
+                                        if ($templates{$template}) {
+                                            $copyfrom = $template;
+                                        }
+                                    }
+                                    unless ($copyfrom) {
+                                        $copyfrom = $r->dir_config('lonIncludes').'/templates/blank.problem';
+                                    }
+                                    &File::Copy::copy($copyfrom,$source);
+                                }
+                                if (!-e "$source.meta") {
+                                    my $cid = $coursedom.'_'.$coursenum;
+                                    my $now = time;
+                                    if (open(my $fh,">$source.meta")) {
+                                        my $author=$env{'environment.firstname'}.' '.
+                                                   $env{'environment.middlename'}.' '.
+                                                   $env{'environment.lastname'}.' '.
+                                                   $env{'environment.generation'};
+                                        $author =~ s/\s+$//;
+                                        my $title = $env{'form.newresourcetitle'};
+                                        $title =~ s/^\s+|\s+$//g;
+                                        print $fh <<END;
+
+<abstract></abstract>
+<author>$author</author>
+<authorspace>$coursenum:$coursedom</authorspace>
+<copyright>custom</copyright>
+<creationdate>$now</creationdate>
+<customdistributionfile>/res/$coursedom/$coursenum/default.rights</customdistributionfile>
+<dependencies></dependencies>
+<domain>$coursedom</domain>
+<highestgradelevel>0</highestgradelevel>
+<keywords></keywords>
+<language>notset </language>
+<lastrevisiondate>$now</lastrevisiondate>
+<lowestgradelevel>0</lowestgradelevel>
+<mime>problem</mime>
+<modifyinguser>$coursenum:$coursedom</modifyinguser>
+<notes></notes>
+<obsolete></obsolete>
+<obsoletereplacement></obsoletereplacement>
+<owner>$coursenum:$coursedom</owner>
+<sourceavail></sourceavail>
+<standards></standards>
+<subject></subject>
+<title>$title</title>
+END
+                                        close($fh);
+                                    }
+                                }
+                            }
+                        }
+                    }
+                } else {
+                    my ($auname,$audom,$role) = split('___',$env{'form.authorrole'});
+                    my $rolehome = &Apache::lonnet::homeserver($auname,$audom);
+                    if (grep(/^\Q$rolehome\E$/,@ids)) {
+                        my $now = time;
+                        if (exists($env{'user.role.'.$role.'./'.$audom.'/'.$auname})) {
+                            my ($start,$end) = split(/\./,$env{'user.role.'.$role.'./'.$audom.'/'.$auname});
+                            if (($start <= $now) && (($end == 0) || ($end >= $now))) { 
+                                my $url = "/priv/$audom/$auname";  
+                                my $path = $r->dir_config('lonDocRoot').$url;
+                                my $subdir = $env{'form.authorpath'};
+                                $redirect = &finishnewprob($url,$path,$subdir,$newsubdir,$filename);
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+    return ($redirect,$error);
+}
+
+sub finishnewprob {
+    my ($url,$path,$subdir,$newsubdir,$filename) = @_;
+    unless (-d $path) {
+        unless (mkdir($path,02770)) {
+            return;
+        }
+    }
+    my $redirect;
+    if ($subdir ne '/') {
+        $subdir = &cleandir($subdir);
+        if (($subdir ne '') && (-d "$path/$subdir")) {
+            $path .= "/$subdir";
+            $url .= "/$subdir";
+        }
+    }
+    my $dest;
+    if ($newsubdir ne '') {
+        $newsubdir = &cleandir($newsubdir);
+    }
+    if ($newsubdir ne '') {
+        if (-d "$path/$newsubdir") {
+            $dest = "$path/$newsubdir/$filename";
+        } else {
+            my $dirok;
+            unless (-e "$path/$newsubdir") {
+                if (mkdir("$path/$newsubdir",02770)) {
+                    if (chmod(02770,"$path/$newsubdir")) {
+                        $dirok = 1;
+                    }
+                }
+            }
+            if ($dirok) {
+                $dest = "$path/$newsubdir/$filename";
+            }
+        }
+        if (($dest ne '') && (!-e $dest)) {
+            $redirect = "$url/$newsubdir/$filename";
+        }
+    } else {
+        $dest = "$path/$filename";
+        if (($dest ne '') && (!-e $dest)) {
+            $redirect = "$url/$filename";
+        }
+    }
+    return $redirect;
+}
+
+sub cleandir {
+    my ($dir) = @_;
+    $dir =~ s/^\s+//;
+    $dir =~ s/\s+$//;
+    $dir =~ s/\.+//g;
+    $dir =~ s/[\#\?&%\":]//g;
+    return $dir;
+}
+
 1;
 __END__
 
@@ -8094,9 +8639,7 @@ check on this
 
 Verify Content
 
-=item devalidateversioncache()
-
-=item checkversions()
+=item devalidateversioncache() & checkversions()
 
 Check Versions