File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.79: download - view: text, annotated - select for diffs
Tue May 17 00:46:36 2005 UTC (19 years ago) by www
Branches: MAIN
CVS tags: HEAD
Work on force publication using different copyright, etc.

    1: # The LearningOnline Network with CAPA
    2: # Construction Space Directory Lister
    3: #
    4: # $Id: lonpubdir.pm,v 1.79 2005/05/17 00:46:36 www Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: ###
   29: 
   30: package Apache::lonpubdir;
   31: 
   32: use strict;
   33: use Apache::File;
   34: use File::Copy;
   35: use Apache::Constants qw(:common :http :methods);
   36: use Apache::loncacc;
   37: use Apache::loncommon();
   38: use Apache::lonhtmlcommon();
   39: use Apache::lonlocal;
   40: use Apache::lonmsg;
   41: use Apache::lonmenu;
   42: use Apache::lonnet;
   43: 
   44: sub handler {
   45: 
   46:   my $r=shift;
   47: 
   48:   my $fn;
   49: 
   50: 
   51: 
   52:   $fn = getEffectiveUrl($r);
   53: 
   54:   # Validate access to the construction space and get username@domain.
   55: 
   56:   my $uname;
   57:   my $udom;
   58: 
   59:   ($uname,$udom)=
   60:     &Apache::loncacc::constructaccess(
   61:              $fn,$r->dir_config('lonDefDomain')); 
   62:   unless (($uname) && ($udom)) {
   63:      $r->log_reason($uname.' at '.$udom.
   64:          ' trying to list directory '.$env{'form.filename'}.
   65:          ' ('.$fn.') - not authorized', 
   66:          $r->filename); 
   67:      return HTTP_NOT_ACCEPTABLE;
   68:   }
   69: 
   70:   # Remove trailing / from directory name.
   71: 
   72:   $fn=~s/\/$//;
   73: 
   74:   unless ($fn) { 
   75:      $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
   76:          ' trying to list empty directory', $r->filename); 
   77:      return HTTP_NOT_FOUND;
   78:   } 
   79: 
   80: # ----------------------------------------------------------- Start page output
   81: 
   82:   my $thisdisfn=$fn;
   83:   $thisdisfn=~s/^\/home\/$uname\/public_html//;	# subdirectory part of
   84:                                                 # construction space. 
   85:   my $docroot=$r->dir_config('lonDocRoot');     # Apache  londocument root.
   86: 
   87:   my $resdir=$docroot.'/res/'.$udom.'/'.$uname.$thisdisfn; # Resource directory
   88:   my $targetdir=$udom.'/'.$uname.$thisdisfn; # Publiction target directory.
   89:   my $linkdir='/priv/'.$uname.$thisdisfn;      # Full URL name of constr space.
   90: 
   91:   my %bombs=&Apache::lonmsg::all_url_author_res_msg($uname,$udom);
   92: 
   93:   &startpage($r, $uname, $udom, $thisdisfn);   # Put out the start of page.
   94:   if ($env{'environment.remote'} eq 'off') {
   95:       &dircontrols($r,$uname,$udom,$thisdisfn);    # Put out actions for directory, 
   96:                                                # browse/upload + new file page.
   97:   } else {
   98:       &pubbuttons($r,$uname,$thisdisfn);
   99:   }
  100:   &resourceactions($r,$uname,$udom,$thisdisfn); #Put out form used for printing/deletion etc.
  101: 
  102:   my $numdir = 0;
  103:   my $numres = 0;
  104:   
  105:   # Start off the directory table.
  106:   $r->print('<h3>Directory Contents:</h3>');
  107:   $r->print('<table border="0" cellspacing="2" cellpadding="2"><tr>'.
  108:             '<th bgcolor="#DDDDDD">'.&mt('Type').'</th>'.
  109:             '<th bgcolor="#DDDDDD">'.&mt('Actions').'</th>'.
  110:             '<th bgcolor="#DDDDDD">'.&mt('Name').'</th>'.
  111:             '<th bgcolor="#DDDDDD">'.&mt('Title').'</th>'.
  112: 	    '<th bgcolor="#DDDDDD">'.&mt('Status').'</th>'.
  113:             '<th bgcolor="#DDDDDD">'.&mt('Last Modified').
  114: 	    '</th></tr>');
  115: 
  116:   my $filename;
  117:   my $dirptr=16384;		# Mask indicating a directory in stat.cmode.
  118: 
  119:   opendir(DIR,$fn);
  120:   my @files=sort {uc($a) cmp uc($b)} (readdir(DIR));
  121:   foreach my $filename (@files) {
  122:      my ($cdev,$cino,$cmode,$cnlink,
  123:          $cuid,$cgid,$crdev,$csize,
  124:          $catime,$cmtime,$cctime,
  125:          $cblksize,$cblocks)=stat($fn.'/'.$filename);
  126: 
  127:      my $extension='';
  128:      if ($filename=~/\.(\w+)$/) { $extension=$1; }
  129:      if ($cmode&$dirptr) {
  130: 	 putdirectory($r, $thisdisfn, $linkdir, $filename, $cmtime,$targetdir,\%bombs,\$numdir);
  131:      } elsif (&Apache::loncommon::fileembstyle($extension) ne 'hdn') {
  132: 	 putresource($r, $udom, $uname, $filename, $thisdisfn, $resdir, 
  133: 		     $targetdir, $linkdir, $cmtime,\%bombs,\$numres);
  134:      } else {
  135: 	# "hidden" extension and not a directory, so hide it away.
  136:      }
  137:   }
  138:   closedir(DIR);
  139: 
  140:   $r->print('</table></body></html>');
  141:   return OK;  
  142: }
  143: #
  144: #  Gets the effective URL of the request and returns it:
  145: #    $effn = getEffectiveUrl($r);
  146: #       $r  - The Apache Request object.
  147: sub getEffectiveUrl {
  148:     my $r = shift;
  149:     my $fn;
  150:     
  151:     if ($env{'form.filename'}) {	# If a form filename is defined.
  152: 	$fn=$env{'form.filename'};
  153: 	#
  154: 	#   Replace the ~username of the URL with /home/username/public_html
  155: 	#   so that we don't have to worry about ~ expansion internally.
  156: 	#
  157: 	$fn=~s/^http\:\/\/[^\/]+\///;
  158:         $fn=~s/^\///;
  159:         $fn=~s/\~(\w+)/\/home\/$1\/public_html/;
  160: 	
  161: 	#  Remove trailing / strings (?) 
  162: 	
  163: 	$fn=~s/\/[^\/]+$//;
  164:     } else {
  165: 	#   If no form is defined, use request filename.
  166: 	$fn = $r->filename();
  167: 	my $lonDocRoot=$r->dir_config('lonDocRoot');
  168: 	if ( $fn =~ /$lonDocRoot/ ) {
  169: 	    #internal authentication, needs fixup.
  170: 	    $fn = $r->uri(); # non users do not get the full path request
  171:                              # through SCRIPT_FILENAME
  172: 	    $fn=~s|^/~(\w+)|/home/$1/public_html|;
  173: 	}
  174:     }
  175:     $fn=~s/\/+/\//g;
  176:     return $fn;
  177: }
  178: #
  179: #   Output the header of the page.  This includes:
  180: #   - The HTML header 
  181: #   - The H1/H3  stuff which includes the directory.
  182: #
  183: #     startpage($r, $uame, $udom, $thisdisfn);
  184: #      $r     - The apache request object.
  185: #      $uname - User name.
  186: #      $udom  - Domain name the user is logged in under.
  187: #      $thisdisfn - Displayable version of the filename.
  188: 
  189: sub startpage {
  190:     my ($r, $uname, $udom, $thisdisfn) = @_;
  191:     my $currdir = '/priv/'.$uname.$thisdisfn;
  192:     &Apache::loncommon::content_type($r,'text/html');
  193:     $r->send_http_header;
  194:     my $html=&Apache::lonxml::xmlbegin();
  195:     $r->print($html.'<head><title>LON-CAPA Construction Space</title></head>');
  196: 
  197:     my $pagetitle;
  198:     my $formaction='/priv/'.$uname.$thisdisfn.'/';
  199:     $formaction=~s/\/+/\//g;
  200:     $pagetitle .= &Apache::loncommon::help_open_menu('','','','',3,'Authoring').
  201:         '<font face="Arial, Helvetica, sans-serif" size="+1"><b>Construction Space</b>:</font>&nbsp;'.
  202:         '<form name="dirs" method="post" action="'.$formaction.
  203:         '" target="_parent"><tt><b>'.
  204:         &Apache::lonhtmlcommon::crumbs($uname.$thisdisfn.'/','_top','/priv','','+1',1)."</b></tt><br />".
  205:         &Apache::lonhtmlcommon::select_recent('construct','recent',
  206:                  'this.form.action=this.form.recent.value;this.form.submit()').
  207:               '</form>';
  208:     &Apache::lonhtmlcommon::store_recent('construct',$formaction,$formaction);
  209:     if ($env{'environment.remote'} eq 'off') {
  210: 	$env{'request.noversionuri'}=$currdir.'/';
  211: 	$r->print(&Apache::loncommon::bodytag('Construction Space',undef,undef,undef,undef,undef,$pagetitle));
  212:     } else {
  213: 	$r->print($pagetitle);
  214:     }
  215:     my $pubdirscript=(<<ENDPUBDIRSCRIPT);
  216: <script type="text/javascript">
  217: top.document.title = '$thisdisfn/ - LON-CAPA Construction Space';
  218: // Store directory location for menu bar to find
  219: 
  220: parent.lastknownpriv='/~$uname$thisdisfn/';
  221: 
  222: // Confirmation dialogues
  223: 
  224:     function currdiract(theform) {
  225:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == 'publish') {
  226:             document.publishdir.filename.value = theform.filename.value;
  227: 	    document.publishdir.submit();
  228:         }
  229:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == 'editcat') {
  230:             top.location=theform.filename.value+'default.meta'
  231:         }
  232:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == 'printdir' ) {
  233:             document.printdir.postdata.value=theform.filename.value
  234:             document.printdir.submit();
  235:         }
  236:     }
  237:   
  238:     function checkUpload(theform) {
  239:         if (theform.file == '') {
  240:             alert("Please use 'Browse..' to choose a file first, before uploading")
  241:             return 
  242:         }
  243:         theform.submit()  
  244:     }
  245: 
  246:     function SetPubDir(theform,printForm) {
  247:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "open") {
  248:             top.location = theform.openname.value
  249:             return
  250:         }
  251:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "publish") {
  252:             theform.submit();
  253:         }
  254:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "editcat") {
  255:             top.location=theform.filename.value+'default.meta'
  256:         }
  257:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "printdir") {
  258:             theform.action = '/adm/printout'
  259:             theform.postdata.value = theform.filename.value
  260:             theform.submit()
  261:         }
  262:         return
  263:     }
  264:     function SetResChoice(theform) {
  265:       var activity = theform.reschoice.options[theform.reschoice.selectedIndex].value
  266:       if ((activity == 'rename') || (activity == 'copy') || (activity == 'move')) {
  267:           changename(theform,activity)
  268:       }
  269:       if (activity == 'publish') {
  270:           var pubform = document.pubresource
  271:           pubform.filename.value = theform.filename.value
  272:           pubform.submit()
  273:       }
  274:       if (activity == 'delete') {
  275:           var delform = document.delresource
  276:           delform.filename.value = theform.filename.value
  277:           delform.submit()
  278:       }
  279:       if (activity == 'obsolete') {
  280:           var pubform = document.pubresource
  281:           pubform.filename.value = theform.filename.value
  282:           alert("You will be taken to the publication page.\\nCheck the 'Obsolete' checkbox at the bottom of the page, and click 'Finalize Publication'.")
  283:           pubform.submit()
  284:       }
  285:       if (activity == 'print') {
  286:           document.printresource.postdata.value = theform.filename.value
  287:           document.printresource.submit()
  288:       }
  289:       if (activity == 'retrieve') {
  290:           document.retrieveres.filename.value = theform.filename.value
  291:           document.retrieveres.submit()
  292:       }
  293:       return
  294:     }
  295:     function changename(theform,activity) {
  296:         var newname=prompt('New Name');
  297:         if (newname == "" || !newname)  {
  298:             return
  299:         }
  300:         document.moveresource.newfilename.value = newname
  301:         document.moveresource.filename.value = theform.filename.value
  302:         document.moveresource.action.value = activity
  303:         document.moveresource.submit();
  304:     }
  305: </script>
  306: ENDPUBDIRSCRIPT
  307:     $r->print($pubdirscript);
  308: 
  309:     if ((($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) &&
  310: 	$env{'environment.remote'} ne 'off') {
  311: 	$r->print('<h3>'.&mt('Co-Author').': '.$uname.' at '.$udom.
  312: 		  '</h3>');
  313:     }
  314: }
  315: 
  316: sub dircontrols {
  317:     my ($r,$uname,$udom,$thisdisfn) = @_;
  318:     $r->print(<<END);
  319:         <table cellspacing="4" cellpadding="4" width="100%">
  320:          <tr>
  321:           <td bgcolor="#DDDDDD" align="middle"><font face="Arial, Helvetica, sans-serif" size="-1"><b>Actions for current directory</b></font></td>
  322:           <td bgcolor="#DDDDDD" align="middle"><font face="Arial, Helvetica, sans-serif" size="-1"><b>Upload a new document</b></font></td>
  323:           <td bgcolor="#DDDDDD" align="middle"><font face="Arial, Helvetica, sans-serif" size="-1"><b>Create a new directory or LON-CAPA document</b></font></td>
  324:         </tr>
  325:         <tr>
  326:          <td bgcolor="#ccddaa" valign="top" align="center">
  327:           <form name="curractions" method="post" action="">
  328:            <select name="dirtask" onChange="currdiract(this.form)">
  329:             <option>Select action</option>
  330:             <option value="publish">Publish directory</option>
  331:             <option value="editcat">Edit catalog information</option>
  332:             <option value="printdir">Print contents of directory</option>
  333:            </select>
  334:            <input type="hidden" name="filename" value="/~$uname$thisdisfn/" />
  335:           </form>
  336:           <form name="publishdir" method="post" action="/adm/publish" target="_parent">
  337:            <input type="hidden" name="pubrec" value="" />
  338:            <input type="hidden" name="filename" value="" />
  339:           </form>
  340:           <form name="printdir" method="post" action="/adm/printout" target="_parent">
  341:            <input type="hidden" name="postdata" value="" />
  342:           </form>
  343:          </td>
  344:          <td bgcolor="#ccddaa" valign="top" align="center">
  345: 	    <form name="upublisher" enctype="multipart/form-data" method="post" action="/adm/upload" target="_parent">
  346: 	      <input type="hidden" name="filename" value="/~$uname$thisdisfn/" />
  347: 	      <input type="file" name="upfile" size="20" />
  348: 	      <input type="button" value="Upload file"  onclick="checkUpload(this.form)" />
  349: 	    </form>
  350: 	 </td>
  351: 	 <td bgcolor="#ccddaa" align="center">
  352: 	    <form name="fileaction" method="post" action="/adm/cfile" target="_parent">
  353: 	      <nobr>
  354: 		<input type="hidden" name="filename" value="/~$uname$thisdisfn/" />
  355: 		  <select name="action">
  356: 		    <option>Select Action</option>
  357: 		    <option value="newfile">New file:</option>
  358: 		    <option value="newhtmlfile">New HTML file:</option>
  359: 		    <option value="newproblemfile">New problem:</option>
  360:                     <option value="newpagefile">New assembled page:</option>
  361:                     <option value="newsequencefile">New assembled sequence:</option>
  362:                     <option value="newrightsfile">New custom rights file:</option>
  363:                     <option value="newstyfile">New style file:</option>
  364: 		    <option value="newdir">New subdirectory:</option>
  365: 		  </select>&nbsp;<input type="text" name="newfilename" value="Type Name Here" onfocus="if (this.value == 'Type Name Here') this.value=''" />&nbsp;<input type="button" value="Go" onclick="document.fileaction.submit()" />
  366: 		 </nobr>
  367: 		</form>
  368: 	  </td>
  369:          </tr>
  370:         </table>
  371: END
  372: }
  373: 
  374: sub pubbuttons {
  375:     my ($r,$uname,$thisdisfn) = @_;
  376:     $r->print('<form method="post" action="/adm/publish" target="_parent">'.
  377:               '<table><tr><td><input type="hidden" name="filename" value="/~'.
  378:                $uname.$thisdisfn.'/" />'.
  379:               '<input type="submit" value="'.&mt('Publish Directory').'" /></td><td>'.
  380: '<input type="button" onClick="window.location='."'/~".
  381:                $uname.$thisdisfn."/default.meta'".'" value="'.
  382: &mt('Edit Directory Catalog Information').'" /></td></tr></table></form>');
  383: }
  384: 
  385: sub resourceactions {
  386:     my ($r,$uname,$udom,$thisdisfn) = @_;
  387:     $r->print(<<END);
  388:        <form name="moveresource" action="/adm/cfile" target="_parent" method="post">
  389:          <input type="hidden" name="filename" value="" />
  390:          <input type="hidden" name="newfilename" value="" />
  391:          <input type="hidden" name="action" value="" />
  392:        </form>
  393:        <form name="delresource" action="/adm/cfile" target="_parent" method="post">
  394:          <input type="hidden" name="filename" value="" />
  395:          <input type="hidden" name="action" value="delete" />
  396:        </form>
  397:        <form name="pubresource" action="/adm/publish" target="_parent" method="post">
  398:          <input type="hidden" name="filename" value="" />
  399:        </form>
  400:        <form name="printresource" action="/adm/printout" target="_parent" method="post">
  401:            <input type="hidden" name="postdata" value="" />
  402:        </form>
  403:        <form name="retrieveres" action="/adm/retrieve" target="_parent" method="post">
  404:            <input type="hidden" name="filename" value="" />
  405:        </form>
  406: END
  407: }
  408: 
  409: #
  410: #   Get the title string or "[untitled]" if the file has no title metadata:
  411: #   Without the latter substitution, it's impossible to examine metadata for
  412: #   untitled resources.  Resources may be legitimately untitled, to prevent
  413: #   searches from locating them.
  414: #
  415: #   $str = getTitleString($fullname);
  416: #       $fullname - Fully qualified filename to check.
  417: #
  418: sub getTitleString {
  419:     my $fullname = shift;
  420:     my $title    = &Apache::lonnet::metadata($fullname, 'title');
  421: 
  422:     unless ($title) {
  423: 	$title = "[".&mt('untitled')."]";
  424:     }
  425:     return $title;
  426: }
  427: 
  428: sub getCopyRightString {
  429:     my $fullname = shift;
  430:     return &Apache::lonnet::metadata($fullname, 'copyright');
  431: }
  432: 
  433: sub getSourceRightString {
  434:     my $fullname = shift;
  435:     return &Apache::lonnet::metadata($fullname, 'sourceavail');
  436: }
  437: #
  438: #  Put out a directory table row:
  439: #    putdirectory(r, base, here, dirname, modtime)
  440: #      r       - Apache request object.
  441: #      reqfile - File in request.
  442: #      here    - Where we are in directory tree.
  443: #      dirname - Name of directory special file.
  444: #      modtime - Encoded modification time.
  445: # 
  446: sub putdirectory {
  447:     my ($r, $reqfile, $here, $dirname, $modtime, $resdir, $bombs, $numdir) = @_;
  448:     # construct the display filename: the directory name unless ..:
  449:     
  450:     my $disfilename = $dirname;
  451:     if ($dirname eq '..') {
  452: 	$disfilename = '<i>'.&mt('Parent Directory').'</i>';
  453:     }
  454:     unless ( (($dirname eq '..') && ($reqfile eq '')) || ($dirname eq '.')) {
  455: 	my $kaputt=0;
  456: 	foreach (keys %{$bombs}) {
  457: 	    if ($_=~m:^\Q$resdir\E/\Q$disfilename\E/:) { $kaputt=1; last; }
  458: 	}
  459: 	%Apache::lonpublisher::metadatafields=();
  460: 	%Apache::lonpublisher::metadatakeys=();
  461: 	my $construct=$here;
  462: 	$construct=~s:^/priv/(\w+)$:/home/$1/public_html:;
  463:         my $dirpath = $here;
  464:         $dirpath=~s:^/priv/:/~:;
  465: 	&Apache::lonpublisher::metaeval(&Apache::lonnet::getfile(
  466:        				 $construct.'/'.$dirname.'/default.meta'
  467: 								 ));
  468:         my $actionitem = '';
  469:         if ($dirname eq '..') {
  470:             $actionitem = 'Go to ...';
  471:         } else {
  472:             $actionitem = 
  473:                     '<form name="dirselect_'.$$numdir.
  474:                     '" action="/adm/publish" target="_parent">'.
  475:                     '<select name="diraction" onChange="SetPubDir(this.form,document)">'.
  476:                       '<option selected="selected">'.&mt('Select action').'</option>'.
  477:                       '<option value="open">'.&mt('Open').'</option>'.
  478:                       '<option value="publish">'.&mt('Publish').'</option>'.
  479:                       '<option value="publishsub">'.&mt('Publish with subdirectories').'</option>'.
  480:                       '<option value="editcat">'.&mt('Edit catalog information').'</option>'.
  481:                       '<option value="printdir">'.&mt('Print directory').'</option>'.
  482:                     '</select>'.
  483:                      '<input type="hidden" name="filename" value="'.$dirpath.'/'.$dirname.'/" />'.
  484:                      '<input type="hidden" name="openname" value="'.$here.'/'.$dirname.'/" />'.
  485:                      '<input type="hidden" name="postdata" value="" />'.
  486:                    '</form>';
  487:             $$numdir ++;
  488:         }
  489: 	$r->print('<tr bgcolor="#CCCCFF">'.
  490: 		  '<td><img src="'.
  491: 		  $Apache::lonnet::perlvar{'lonIconsURL'}.'/folder_closed.gif" /></td>'.
  492: 		  '<td>'.$actionitem.'</td>'.
  493: 		  '<td><font face="arial"><a href="'.$here.'/'.$dirname.'/" target="_parent">'.
  494: 		  $disfilename.'</a></font></td>'.
  495: 		        '<td colspan="2">'.($kaputt?&Apache::lonhtmlcommon::authorbombs($resdir.'/'.$disfilename.'/'):'').$Apache::lonpublisher::metadatafields{'title'}.' <i>'.
  496: 		  $Apache::lonpublisher::metadatafields{'subject'}.'</i> '.
  497: 		  $Apache::lonpublisher::metadatafields{'keywords'}.'</td>'.
  498: 		  '<td>'.&Apache::lonlocal::locallocaltime($modtime).'</td>'.
  499: 		  "</tr>\n");
  500:     }
  501:     return OK;
  502: }
  503: #
  504: #   Put a table row for a file resource.
  505: #
  506: sub putresource {
  507:     my ($r, $udom, $uname, $filename, $thisdisfn, 
  508: 	$resdir, $targetdir, $linkdir,
  509: 	$cmtime,$bombs,$numres) = @_;
  510:     my $pubstatus = 'unpublished';
  511:     my $status=&mt('Unpublished');
  512:     my $bgcolor='#FFAA99';
  513:     my $title='&nbsp;';
  514:     my $publish_button=&mt('Publish');
  515: #    my $action_buttons=
  516: #        '<br /><a target="_parent" href="/adm/cfile?action=delete&filename=/~'.
  517: #	$uname.'/'.$thisdisfn.'/'.$filename.'">'.
  518: #	&mt('Delete').'</a>';
  519:     if (-e $resdir.'/'.$filename) {
  520: 	my ($rdev,$rino,$rmode,$rnlink,
  521: 	    $ruid,$rgid,$rrdev,$rsize,
  522: 	    $ratime,$rmtime,$rctime,
  523: 	    $rblksize,$rblocks)=stat($resdir.'/'.$filename);
  524: 	$publish_button=&mt('Re-publish');
  525: 	if ($rmtime>=$cmtime) {
  526:             $pubstatus = 'published';
  527: 	    $status=&mt('Published').'<br />'.
  528: 		&mt(&getCopyRightString($targetdir.'/'.$filename)).' '.
  529: 		&mt(&getSourceRightString($targetdir.'/'.$filename));
  530: 	    $bgcolor='#CCFF88';
  531: 	    if (&Apache::lonnet::metadata($targetdir.'/'.$filename,'obsolete')) {
  532:                 $pubstatus = 'obsolete';
  533: 		$status=&mt('Obsolete');
  534:                 $bgcolor='#AAAAAA';
  535:             }
  536: #	    } else {
  537: #		$action_buttons='';
  538: #	    }
  539: 	    $title='<a href="/res/'.$targetdir.'/'.$filename.
  540: 		'.meta" target="cat">'.
  541: 		&getTitleString($targetdir.'/'.$filename).'</a>';
  542: 	} else {
  543:             $pubstatus = 'modified';
  544: 	    $status=&mt('Modified').'<br />'.
  545: 		&mt(&getCopyRightString($targetdir.'/'.$filename)).' '.
  546: 		&mt(&getSourceRightString($targetdir.'/'.$filename));
  547:             $bgcolor='#FFFF77';
  548: #	    $action_buttons='';
  549: 	    $title='<a href="/res/'.$targetdir.'/'.$filename.'.meta" target="cat">'.
  550: 		&getTitleString($targetdir.'/'.$filename).'</a>';
  551: 	    if (&Apache::loncommon::fileembstyle(($filename=~/\.(\w+)$/)) eq 'ssi') {
  552: 		$status.='<br /><a href="/adm/diff?filename=/~'.$uname.
  553: 		    $thisdisfn.'/'.$filename.
  554: 		    '&amp;versiontwo=priv" target="cat">'.&mt('Diffs').'</a>';
  555: 	    }
  556: 	}
  557: 	$title.="\n".'<br /><a href="/~'.$uname.$thisdisfn.'/'.$filename.'.meta">'. 
  558: 	    ($$bombs{$targetdir.'/'.$filename}?'<img src="/adm/lonMisc/bomb.gif" border="0" />':'Edit Metadata').'</a>';
  559: 	$status.="\n".'<br /><a href="/adm/retrieve?filename=/~'.$uname.
  560: 	    $thisdisfn.'/'.$filename.'" target="_parent">'.&mt('Retrieve').'</a>';
  561:     }
  562:     my $editlink='';
  563:     my $editlink2='';
  564:     if ($filename=~/\.(xml|html|htm|xhtml|xhtm|sty)$/) {
  565: 	$editlink=' <br />(<a href="'.$linkdir.'/'.$filename.'?forceedit=1" target="_parent">'.&mt('Edit').'</a>)';
  566:     }
  567:     if ($filename=~/\.(problem|exam|quiz|assess|survey|form|library)$/) {
  568: 	$editlink=' (<a href="'.$linkdir.'/'.$filename.'?forceedit=1" target="_parent">'.&mt('EditXML').'</a>)';
  569: 	$editlink2=' <br />(<a href="'.$linkdir.'/'.$filename.'?forceColoredit=1" target="_parent">'.&mt('Edit').'</a>)';
  570:     }
  571:     if ($filename=~/\.(zip|tar|bz2|gz|tar.gz|tar.bz2|tgz)$/) {
  572: 	$editlink=' (<a target="_parent" href="/adm/cfile?decompress=/~'.
  573: 	      $uname.$thisdisfn.'/'.$filename.'">'.&mt('Decompress').'</a>)';
  574:     }
  575:     my $pub_select = '';
  576:     &create_pubselect($r,\$pub_select,$udom,$uname,$thisdisfn,$filename,$resdir,$pubstatus,$publish_button,$numres);
  577:     $r->print('<tr bgcolor="'.$bgcolor.'">'.
  578: 	      '<td>'.($filename=~/[\#\~]$/?'&nbsp;':
  579: 		      '<img src="'.&Apache::loncommon::icon($filename).'" /></td>').
  580:               '<td>'.$pub_select.'</td>'.
  581: 	      '<td><font face="arial">'.
  582: 	      '<a href="'.$linkdir.'/'.$filename.'" target="_parent">'.
  583:                $filename.'</a></font>'.$editlink2.$editlink.
  584: 	      '</td>'.
  585: 	      '<td>'.$title.'</td>'.
  586: 	      '<td>'.$status.'</td>'.
  587: 	      '<td>'.&Apache::lonlocal::locallocaltime($cmtime).'</td>'.
  588: 	      "</tr>\n");
  589:     return OK;
  590: }
  591: 
  592: sub create_pubselect {
  593:     my ($r,$pub_select,$udom,$uname,$thisdisfn,$filename,$resdir,$pubstatus,$publish_button,$numres) = @_;
  594:     $$pub_select = '
  595: <form name="resselect_'.$$numres.'" action="">
  596: <select name="reschoice"  onChange="SetResChoice(this.form)">
  597: <option>'.&mt('Select action').'</option>'.
  598: '<option value="copy">'.&mt('Copy').'</option>';
  599:     if ($pubstatus eq 'obsolete' || $pubstatus eq 'unpublished') {
  600:         $$pub_select .= 
  601: '<option value="rename">'.&mt('Rename').'</option>'.
  602: '<option value="move">'.&mt('Move').'</option>'.
  603: '<option value="delete">'.&mt('Delete').'</option>';
  604:     } else {
  605:         $$pub_select .= '
  606: <option value="obsolete">'.&mt('Mark obsolete').'</option>';
  607:     }
  608: # check for versions
  609:     my $versions = &check_for_versions($r,'/'.$filename,$udom,$uname);
  610:     if ($versions > 0) {
  611:         $$pub_select .='
  612: <option value="retrieve">'.&mt('Retrieve old version').'</option>';
  613:     }
  614:     $$pub_select .= '
  615: <option value="publish">'.$publish_button.'</option>'.
  616: '<option value="print">'.&mt('Print').'</option>'.
  617: '</select>
  618: <input type="hidden" name="filename" value="/~'.
  619:  $uname.$thisdisfn.'/'.$filename.'" /></form>';
  620:     $$numres ++;
  621: }
  622: 
  623: sub check_for_versions {
  624:     my ($r,$fn,$udom,$uname) = @_;
  625:     my $versions = 0;
  626:     my $docroot=$r->dir_config('lonDocRoot');
  627:     my $resfn=$docroot.'/res/'.$udom.'/'.$uname.$fn;
  628:     my $resdir=$resfn;
  629:     $resdir=~s/\/[^\/]+$/\//;
  630:     $fn=~/\/([^\/]+)\.(\w+)$/;
  631:     my $main=$1;
  632:     my $suffix=$2;
  633:     opendir(DIR,$resdir);
  634:     while (my $filename=readdir(DIR)) {
  635:         if ($filename=~/^\Q$main\E\.(\d+)\.\Q$suffix\E$/) {
  636:             $versions ++;        
  637:         }
  638:     }
  639:     return $versions;
  640: }
  641: 
  642: #
  643: #   Categorize files in the directory.
  644: #   For each file in a list of files in a file directory, 
  645: #   the  file categorized as one of:
  646: #    - directory  
  647: #    - sequence
  648: #    - problem 
  649: #    - Other resource.
  650: #
  651: #   For each file the modification date is determined as well.
  652: #   Returned is a list of sublists:
  653: #    (directories, sequences, problems, other)
  654: #   each of the sublists contains entries of the following form (sorted by
  655: #   filename):
  656: #     (filename, typecode, lastmodtime)
  657: #
  658: #   $list = CategorizeFiles($location, $files)
  659: #       $location   - Directory in which the files live (relative to our
  660: #                     execution.
  661: #       $files      - list of files.
  662: #
  663: sub CategorizeFiles {
  664:     my $location = shift;
  665:     my $files    = shift;
  666: }
  667: 
  668: 1;
  669: __END__
  670: 
  671: =head1 NAME
  672: 
  673: Apache::lonpubdir - Construction space directory lister
  674: 
  675: =head1 SYNOPSIS
  676: 
  677: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
  678: 
  679:  <LocationMatch "^/\~.*/$">
  680:  PerlAccessHandler       Apache::loncacc
  681:  SetHandler perl-script
  682:  PerlHandler Apache::lonpubdir
  683:  ErrorDocument     403 /adm/login
  684:  ErrorDocument     404 /adm/notfound.html
  685:  ErrorDocument     406 /adm/unauthorized.html
  686:  ErrorDocument	  500 /adm/errorhandler
  687:  </LocationMatch>
  688: 
  689:  <Location /adm/pubdir>
  690:  PerlAccessHandler       Apache::lonacc
  691:  SetHandler perl-script
  692:  PerlHandler Apache::lonpubdir
  693:  ErrorDocument     403 /adm/login
  694:  ErrorDocument     404 /adm/notfound.html
  695:  ErrorDocument     406 /adm/unauthorized.html
  696:  ErrorDocument	  500 /adm/errorhandler
  697:  </Location>
  698: 
  699: =head1 INTRODUCTION
  700: 
  701: This module publishes a directory of files.
  702: 
  703: This is part of the LearningOnline Network with CAPA project
  704: described at http://www.lon-capa.org.
  705: 
  706: =head1 HANDLER SUBROUTINE
  707: 
  708: This routine is called by Apache and mod_perl.
  709: 
  710: =over 4
  711: 
  712: =item *
  713: 
  714: read in information
  715: 
  716: =item *
  717: 
  718: start page output
  719: 
  720: =item *
  721: 
  722: run through list of files and attempt to publish unhidden files
  723: 
  724: =back
  725: 
  726: =cut

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>