--- loncom/publisher/lonpubdir.pm	2014/10/17 13:00:39	1.162
+++ loncom/publisher/lonpubdir.pm	2023/06/10 23:55:41	1.176
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Authoring Space Directory Lister
 #
-# $Id: lonpubdir.pm,v 1.162 2014/10/17 13:00:39 goltermann Exp $
+# $Id: lonpubdir.pm,v 1.176 2023/06/10 23:55:41 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -80,14 +80,38 @@ sub handler {
         }
     }
     $thisdisfn=~s/^\Q$docroot\E\/priv//;
-    
+
     my $resdir=$docroot.'/res'.$thisdisfn; # Resource directory
     my $targetdir='/res'.$thisdisfn; # Publication target directory.
     my $linkdir='/priv'.$thisdisfn;      # Full URL name of constr space.
 
+    my $cstr = 'author';
+    my ($crsauthor,$crstype);
+    if ($env{'request.course.id'}) {
+        my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
+        my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
+        if ($thisdisfn =~ m{^/\Q$cdom/$cnum\E}) {
+            $crsauthor = 1;
+            $cstr = 'course';
+            $crstype = &Apache::loncommon::course_type();
+        }
+    }
+
     my %bombs=&Apache::lonmsg::all_url_author_res_msg($uname,$udom);
 
-    &startpage($r, $uname, $udom, $thisdisfn);  # Put out the start of page.
+    my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
+    my $current_disk_usage = &Apache::lonnet::diskusage($udom,$uname,
+                                "$londocroot/priv/$udom/$uname"); # expressed in kB
+    my $disk_quota = &Apache::loncommon::get_user_quota($uname,$udom,
+                                                 $cstr,$crstype); # expressed in MB
+    my $diraction;
+    if (-d $fn) {
+        $diraction = &diractions($thisdisfn);
+    }
+
+    # Put out the start of page.
+    
+    &startpage($r, $uname, $udom, $thisdisfn, $current_disk_usage, $disk_quota, $crsauthor, $diraction);
 
     if (!-d $fn) {
         if (-e $fn) {
@@ -108,8 +132,8 @@ sub handler {
         return OK;
     }
 
-    &dircontrols($r,$uname,$udom,$thisdisfn);   # Put out actions for directory, 
-                                                # browse/upload + new file page.
+    # Put out actions for directory, browse/upload + new file page.
+    &dircontrols($r,$uname,$udom,$thisdisfn, $current_disk_usage, $disk_quota);
     &resourceactions($r,$uname,$udom,$thisdisfn); # Put out form used for printing/deletion etc.
 
     my $numdir = 0;
@@ -332,45 +356,56 @@ my $result = "<script type=\"text/javasc
 #   - The HTML header 
 #   - The H1/H3  stuff which includes the directory.
 #
-#     startpage($r, $uame, $udom, $thisdisfn);
+#     startpage($r, $uame, $udom, $thisdisfn, $current_disk_usage, $disk_quota);
 #      $r     - The apache request object.
 #      $uname - User name.
 #      $udom  - Domain name the user is logged in under.
 #      $thisdisfn - Displayable version of the filename.
+#      $current_disk_usage - User's current disk usage (in kB).
+#      $disk_quota - Disk quota for user's authoring space (in MB).
+#      $crstype - Course type, if this is for "course author"
 
 sub startpage {
-    my ($r, $uname, $udom, $thisdisfn) = @_;
+    my ($r, $uname, $udom, $thisdisfn, $current_disk_usage, $disk_quota, $crsauthor, $diraction) = @_;
     &Apache::loncommon::content_type($r,'text/html');
     $r->send_http_header;
 
     my $formaction='/priv'.$thisdisfn.'/';
     $formaction=~s|/+|/|g;
-    &Apache::lonhtmlcommon::store_recent('construct',$formaction,$formaction);
+
+    my $title;
+    if ($crsauthor) {
+        $title = 'Course Authoring';
+    } else {
+        $title = 'Authoring Space';
+        &Apache::lonhtmlcommon::store_recent('construct',$formaction,$formaction);
+    }
 
     &Apache::lonhtmlcommon::clear_breadcrumbs();
     &Apache::lonhtmlcommon::add_breadcrumb({
-        'text'  => 'Authoring Space',
+        'text'  => $title,
         'href'  => &Apache::loncommon::authorspace($formaction),
     });
     # breadcrumbs (and tools) will be created 
     # in start_page->bodytag->innerregister
 
     $env{'request.noversionuri'}=$formaction;
-    $r->print(&Apache::loncommon::start_page('Authoring Space'));
+    my $js = '<script type="text/javascript" 
+                src="/res/adm/includes/file_upload.js"></script>';
+    $r->print(&Apache::loncommon::start_page($title, $js));
 
-    my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
-    my $current_disk_usage = &Apache::lonnet::diskusage($udom,$uname,"$londocroot/priv/$udom/$uname");
-    my $disk_quota = &Apache::loncommon::get_user_quota($uname,$udom,'author'); #expressed in MB
-    $disk_quota = 1000 * $disk_quota; # convert from MB to kB
+    $disk_quota = 1024 * $disk_quota; # convert from MB to kB
 
+    my $heading = &mt('Directory');
     $r->print(&Apache::loncommon::head_subbox(
                      '<div style="float:right;padding-top:0;margin-top;0">'
-                    .&Apache::lonhtmlcommon::display_usage($current_disk_usage,$disk_quota)
+                    .&Apache::lonhtmlcommon::display_usage($current_disk_usage,
+                                                           $disk_quota,'authoring')
                     .'</div>'
-                    .&Apache::loncommon::CSTR_pageheader()));
+                    .&Apache::loncommon::CSTR_pageheader('','',$heading,$diraction)));
 
     my $esc_thisdisfn = &Apache::loncommon::escape_single($thisdisfn);
-    my $doctitle = 'LON-CAPA '.&mt('Authoring Space');
+    my $doctitle = 'LON-CAPA '.&mt($title);
     my $newname = &mt('New Name');
     my $pubdirscript=(<<ENDPUBDIRSCRIPT);
 <script type="text/javascript">
@@ -482,20 +517,52 @@ ENDPUBDIRSCRIPT
     $r->print($pubdirscript);
 }
 
+sub diractions {
+    my ($thisdisfn) = @_;
+    my %lt=&Apache::lonlocal::texthash(
+                                       acti => 'Actions for current directory',
+                                       sela => 'Select Action',
+                                       pubd => 'Publish this Directory',
+                                       prnt => 'Print contents of directory',
+                                       edit => 'Edit Metadata',
+                                       dedr => 'Delete Directory',
+    );
+    return <<END;
+<div style="display:inline-block;padding-left:20px">
+<b>$lt{'acti'}</b><br />
+<form name="curractions" method="post" action="">
+    <select name="dirtask" onchange="currdiract(this.form)">
+        <option>$lt{'sela'}</option>
+        <option value="publish">$lt{'pubd'}</option>
+        <option value="editmeta">$lt{'edit'}</option>
+        <option value="printdir">$lt{'prnt'}</option>
+        <option value="delete">$lt{'dedr'}</option>
+    </select>
+    <input type="hidden" name="filename" value="/priv$thisdisfn/" />
+</form>
+<form name="publishdir" method="post" action="/adm/publish">
+  <input type="hidden" name="pubrec" value="" />
+  <input type="hidden" name="filename" value="" />
+</form>
+<form name="printdir" method="post" action="/adm/printout">
+  <input type="hidden" name="postdata" value="" />
+</form>
+</div>
+END
+
+}
+
 sub dircontrols {
-    my ($r,$uname,$udom,$thisdisfn) = @_;
+    my ($r,$uname,$udom,$thisdisfn, $current_disk_usage, $disk_quota) = @_;
     my %lt=&Apache::lonlocal::texthash(
                                        cnpd => 'Cannot publish directory',
                                        cnrd => 'Cannot retrieve directory',
                                        mcdi => 'Must create new subdirectory inside a directory',
                                        pubr => 'Publish this Resource',
-                                       pubd => 'Publish this Directory',
-                                       dedr => 'Delete Directory',
                                        rtrv => 'Retrieve Old Version',
                                        list => 'List Directory',
                                        uplo => 'Upload file',  
                                        dele => 'Delete',
-                                       edit => 'Edit Metadata', 
                                        sela => 'Select Action',
                                        nfil => 'New file',
                                        nhtm => 'New HTML file',
@@ -512,54 +579,54 @@ sub dircontrols {
                                        copy => 'Copy current file to',
                                        type => 'Type Name Here',
                                        go   => 'Go',
-                                       prnt => 'Print contents of directory',
-                                       crea => 'Create a new directory or LON-CAPA document',
-                                       qs   => 'Quick Search',
+                                       crea => 'Create a new subdirectory or document',
+                                       qs   => 'Quick name search',
                                        cs   => 'Case Sensitive',
                                        re   => 'Regular Expression',
-				       acti => 'Actions for current directory',
 				       updc => 'Upload a new document',
 				       pick => 'Please select an action to perform using the new filename',
+                                       shcu => 'Shortcuts',
                                       );
+    my %js_lt = &Apache::lonlocal::texthash(
+                                       nanf => 'Name of New File',
+                                       nans => 'Name of New Subdirectory',
+                                       psfn => 'Please specify file name',
+    ); 
+    &js_escape(\%js_lt);
     my $mytype = $lt{'type'}; # avoid conflict with " and ' in javascript
+    # Calculate free space in bytes.
+    # $disk_quota is in MB and $current_disk_usage is in kB
+    my $free_space = 1024 * ((1024 * $disk_quota) - $current_disk_usage);
     $r->printf(<<END,&Apache::loncommon::help_open_topic('Quicksearch'));
 <div class="LC_columnSection">
-  <div>
-    <form name="curractions" method="post" action="">
-      <fieldset>
-        <legend>$lt{'acti'}</legend>
-        <select name="dirtask" onchange="currdiract(this.form)">
-            <option>$lt{'sela'}</option>
-            <option value="publish">$lt{'pubd'}</option>
-            <option value="editmeta">$lt{'edit'}</option>
-            <option value="printdir">$lt{'prnt'}</option>
-            <option value="delete">$lt{'dedr'}</option>
-        </select>
-        <input type="hidden" name="filename" value="/priv$thisdisfn/" />
-      </fieldset>
-    </form>
-    <form name="publishdir" method="post" action="/adm/publish" target="_parent">
-      <input type="hidden" name="pubrec" value="" />
-      <input type="hidden" name="filename" value="" />
-    </form>
-    <form name="printdir" method="post" action="/adm/printout" target="_parent">
-      <input type="hidden" name="postdata" value="" />
-    </form>
+    <div>
+      <fieldset style="display:inline">
+            <legend>$lt{'qs'}</legend>
+                <script type="text/javascript" src="/adm/quicksearch/quicksearch.js"></script>
+                <input type="text" id="quickfilter" placeholder="Enter search term" onkeyup="applyFilter()"/>
+                <input type="button" value="Clear" onclick="document.getElementById(\'quickfilter\').value=\'\'; applyFilter()" />
+                %s
+                <br />
+                <label><input type="checkbox" id="casesens" onchange="applyFilter()"/>$lt{'cs'}&nbsp;&nbsp;</label>
+                <label><input type="checkbox" id="regex" onchange="applyFilter()"/>$lt{'re'}&nbsp;&nbsp;</label>
+        </fieldset>
   </div>
-
-  <div>
-    <form name="upublisher" enctype="multipart/form-data" method="post" action="/adm/upload" target="_parent">
+END
+    $r->print(<<END);
+  <div style="padding-bottom: 2px">
+    <form name="upublisher" enctype="multipart/form-data" method="post" action="/adm/upload">
       <fieldset>
         <legend>$lt{'updc'}</legend>
         <input type="hidden" name="filename" value="/priv$thisdisfn/" />
-        <input type="file" name="upfile" size="20" />
+        <input type="file" name="upfile" class="LC_flUpload" />
+        <input type="hidden" id="LC_free_space" value="$free_space" />
         <input type="button" value="$lt{'uplo'}"  onclick="checkUpload(this.form)" />
       </fieldset>
     </form>
   </div>
 
   <div>
-    <form name="fileaction" method="post" action="/adm/cfile" target="_parent">
+    <form name="fileaction" method="post" action="/adm/cfile">
       <fieldset>
               <legend>$lt{'crea'}</legend>
 	      <span class="LC_nobreak">
@@ -586,23 +653,58 @@ sub dircontrols {
                     <option value="newtaskfile">$lt{'nbt'}:</option>
                     <option value="newlibraryfile">$lt{'nlib'}:</option>
 	            <option value="newdir">$lt{'nsub'}:</option>
-		  </select>&nbsp;<input type="text" name="newfilename" value="$lt{'type'}" onfocus="if (this.value == '$mytype') this.value=''" />&nbsp;<input type="button" value="Go" onclick="validate_go();" />
+		  </select>&nbsp;<input type="text" id="newnameid" name="newfilename" placeholder="$lt{'type'}" value="" onfocus="if (this.value == is.empty()) this.value=''" />&nbsp;<input type="button" value="Go" onclick="validate_go();" />
+		<br />
+                <span>$lt{'shcu'}:
+                 <input type="hidden" name="mode"/>
+                 <a href="javascript:void(0)" onclick="javascript:validate_action('blank')">
+		    <img src="/adm/lonIcons/unknown.gif" title="Create blank problem file"></a>
+                 <a href="javascript:void(0)" onclick="javascript:validate_action('problemtempl')"> 
+                    <img src="/adm/lonIcons/problem.gif" title="Create new problem from template"></a>
+                 <a href="javascript:void(0)" onclick="javascript:validate_action('blankhtml')"> 
+                    <img src="/adm/lonIcons/html.gif" title="Create new blank HTML file"></a>
+                 <a href="javascript:void(0)" onclick="javascript:validate_action('folder')"> 
+		    <img src="/adm/lonIcons/navmap.folder.closed.gif" title="Create new subdirectory"></a>
+                </span>
+                 <script type="text/javascript">
+                     function validate_action(action){
+                         if (document.getElementById('newnameid')) {
+                             if (document.getElementById('newnameid').value == '') {
+                                 var newname;
+                                 var prompttext = "$js_lt{'nanf'}";
+                                 if (action == 'folder') {
+                                     prompttext = "$js_lt{'nans'}";
+                                 }
+                                 newname=prompt(prompttext);
+                                 if (newname != '') {
+                                     document.getElementById('newnameid').value = newname;
+                                 }
+                             }
+                             if (document.getElementById('newnameid').value != '') {
+                                 if (action == 'blank') {
+				     document.fileaction.action.value='newproblemfile';
+				     document.fileaction.mode.value='blank';
+			         } else if (action == 'problemtempl') {
+				     document.fileaction.action.value='newproblemfile';
+                                     validate_go();
+                                 } else if (action == 'blankhtml') {
+                                     document.fileaction.action.value='newhtmlfile';
+                                     validate_go();
+                                 } else if (action == 'folder') {
+                                     document.fileaction.action.value='newdir';
+                                     document.fileaction.mode.value='folder';
+                                 }
+                                 fileaction.submit();
+                             } else {
+                                 alert("$js_lt{'psfn'}");
+                             }
+                         }
+                     }
+                 </script>
 		 </span>
       </fieldset>
     </form>
     </div>
-    <div>
-      <fieldset style="display:inline">
-            <legend>$lt{'qs'}</legend>
-                <script type="text/javascript" src="/adm/quicksearch/quicksearch.js"></script>
-                <input type="text" id="quickfilter" placeholder="Enter search term" onkeyup="applyFilter()"/>
-                <input type="button" value="Clear" onclick="document.getElementById(\'quickfilter\').value=\'\'; applyFilter()" />
-                %s
-                <br />
-                <label><input type="checkbox" id="casesens" onchange="applyFilter()"/>$lt{'cs'}&nbsp;&nbsp;</label>
-                <label><input type="checkbox" id="regex" onchange="applyFilter()"/>$lt{'re'}&nbsp;&nbsp;</label>
-        </fieldset>
-  </div>
 </div>
 END
 }
@@ -610,26 +712,26 @@ END
 sub resourceactions {
     my ($r,$uname,$udom,$thisdisfn) = @_;
     $r->print(<<END);
-       <form name="moveresource" action="/adm/cfile" target="_parent" method="post">
+       <form name="moveresource" action="/adm/cfile" method="post">
          <input type="hidden" name="filename" value="" />
          <input type="hidden" name="newfilename" value="" />
          <input type="hidden" name="action" value="" />
        </form>
-       <form name="delresource" action="/adm/cfile" target="_parent" method="post">
+       <form name="delresource" action="/adm/cfile" method="post">
          <input type="hidden" name="filename" value="" />
          <input type="hidden" name="action" value="delete" />
        </form>
-       <form name="pubresource" action="/adm/publish" target="_parent" method="post">
+       <form name="pubresource" action="/adm/publish" method="post">
          <input type="hidden" name="filename" value="" />
          <input type="hidden" name="makeobsolete" value="0" />
        </form>
-       <form name="printresource" action="/adm/printout" target="_parent" method="post">
+       <form name="printresource" action="/adm/printout" method="post">
            <input type="hidden" name="postdata" value="" />
        </form>
-       <form name="retrieveres" action="/adm/retrieve" target="_parent" method="post">
+       <form name="retrieveres" action="/adm/retrieve" method="post">
            <input type="hidden" name="filename" value="" />
        </form>
-       <form name="cleanup" action="/adm/cleanup" target="_parent" method="post">
+       <form name="cleanup" action="/adm/cleanup" method="post">
            <input type="hidden" name="filename" value="" />
        </form>
 END
@@ -707,7 +809,7 @@ sub putdirectory {
         } else {
             $actionitem = 
                     '<form name="dirselect_'.$$numdir.
-                    '" action="/adm/publish" target="_parent">'.
+                    '" action="/adm/publish">'.
                     '<select name="diraction" onchange="SetPubDir(this.form,document)">'.
                       '<option selected="selected">'.&mt('Select action').'</option>'.
                       '<option value="open">'.&mt('Open').'</option>'.
@@ -726,7 +828,7 @@ sub putdirectory {
 		  '<td><img src="'.
 		  $Apache::lonnet::perlvar{'lonIconsURL'}.'/navmap.folder.closed.gif" alt="folder" /></td>'.
 		  '<td>'.$actionitem.'</td>'.
-		  '<td><span class="LC_filename"><a href="'.&HTML::Entities::encode($here.'/'.$dirname,'<>&"').'/" target="_parent">'.
+		  '<td><span class="LC_filename"><a href="'.&HTML::Entities::encode($here.'/'.$dirname,'<>&"').'/">'.
 		  $disfilename.'</a></span></td>'.
 		        '<td colspan="3">'.($kaputt?&Apache::lonhtmlcommon::authorbombs($targetdir.'/'.$disfilename.'/'):'').$Apache::lonpublisher::metadatafields{'title'});
 	if ($Apache::lonpublisher::metadatafields{'subject'} ne '') {
@@ -864,11 +966,17 @@ sub putresource {
 	$editlink=' (<a href="'.$linkdir.'/'.$filename.'?editmode=Edit&amp;problemmode=editxml">'.&mt('EditXML').'</a>)';
 	$editlink2=' <br />(<a href="'.$linkdir.'/'.$filename.'?editmode=Edit&amp;problemmode=edit">'.&mt('Edit').'</a>)';
     }
+    if ($filename=~/\.(xml|html|htm|xhtml|xhtm)$/ || $filename=~/$LONCAPA::assess_re/) {
+        if ($env{'browser.type'} ne 'explorer' || $env{'browser.version'} > 9) {
+            my $daxeurl = '/daxepage'.$linkdir.'/'.$filename;
+            $editlink .= ' (<a href="'.$daxeurl.'" target="_blank">Daxe</a>)';
+        }
+    }
     if ($filename=~/\.(problem|exam|quiz|assess|survey|form|library|xml|html|htm|xhtml|xhtm|sty)$/) {
-	$editlink.=' (<a href="/adm/cleanup?filename='.$linkfilename.'" target="_parent">'.&mt('Clean Up').')</a>';
+	$editlink.=' (<a href="/adm/cleanup?filename='.$linkfilename.'">'.&mt('Clean Up').')</a>';
     }
     if ($filename=~/\.(zip|tar|bz2|gz|tar.gz|tar.bz2|tgz)$/) {
-	$editlink=' (<a target="_parent" href="/adm/cfile?decompress='.$linkfilename.'">'.&mt('Decompress').'</a>)';
+	$editlink=' (<a href="/adm/cfile?decompress='.$linkfilename.'">'.&mt('Decompress').'</a>)';
     }
     my $publish_button = (-e $resdir.'/'.$filename) ? &mt('Re-publish') : &mt('Publish');
     my $pub_select = '';
@@ -878,7 +986,7 @@ sub putresource {
 		      '<img src="'.&Apache::loncommon::icon($filename).'" alt="" />').'</td>'.
               '<td>'.$pub_select.'</td>'.
 	      '<td><span class="LC_filename">'.
-	      '<a href="'.$linkdir.'/'.$filename.'" target="_parent">'.
+	      '<a href="'.$linkdir.'/'.$filename.'">'.
                $filename.'</a></span>'.$editlink2.$editlink.
 	      '</td>'.
 	      '<td>'.$title.'</td>'.