File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.115: download - view: text, annotated - select for diffs
Fri Nov 28 16:10:26 2008 UTC (15 years, 6 months ago) by bisitz
Branches: MAIN
CVS tags: HEAD
- Use LON-CAPA standard data tables in Construction Space and Portofolio Tables
- Moved color indicator from whole background to new and only small column.
  This should prevent a "color shock" but keeps the "show status by different colors" functionality.

loncommon.pm:
- Adjusted the related styles ("LC_browser...") correspondingly to the changes described above.

Additional changes/optimizations/corrections in portfolio.pm:
- Added missing &mt() calls to headline in selection mode
- Replaced hardcoded nobreak with CSS
- Fill empty table cells in directory rows with blanks to have a proper table structure
- Optimized and extended colspan precalculation
- XHTML conform "checked" usage in checkboxes

    1: # The LearningOnline Network with CAPA
    2: # Construction Space Directory Lister
    3: #
    4: # $Id: lonpubdir.pm,v 1.115 2008/11/28 16:10:26 bisitz 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::londiff();
   40: use Apache::lonlocal;
   41: use Apache::lonmsg;
   42: use Apache::lonmenu;
   43: use Apache::lonnet;
   44: use LONCAPA;
   45: 
   46: sub handler {
   47: 
   48:   my $r=shift;
   49: 
   50:   my $fn;
   51: 
   52: 
   53: 
   54:   $fn = getEffectiveUrl($r);
   55: 
   56:   # Validate access to the construction space and get username@domain.
   57: 
   58:   my $uname;
   59:   my $udom;
   60: 
   61:   ($uname,$udom)=
   62:     &Apache::loncacc::constructaccess(
   63:              $fn,$r->dir_config('lonDefDomain')); 
   64:   unless (($uname) && ($udom)) {
   65:      $r->log_reason($uname.' at '.$udom.
   66:          ' trying to list directory '.$env{'form.filename'}.
   67:          ' ('.$fn.') - not authorized', 
   68:          $r->filename); 
   69:      return HTTP_NOT_ACCEPTABLE;
   70:   }
   71: 
   72:   # Remove trailing / from directory name.
   73: 
   74:   $fn=~s/\/$//;
   75: 
   76:   unless ($fn) { 
   77:      $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
   78:          ' trying to list empty directory', $r->filename); 
   79:      return HTTP_NOT_FOUND;
   80:   } 
   81: 
   82: # ----------------------------------------------------------- Start page output
   83: 
   84:   my $thisdisfn=$fn;
   85:   $thisdisfn=~s/^\/home\/$uname\/public_html//;	# subdirectory part of
   86:                                                 # construction space. 
   87:   my $docroot=$r->dir_config('lonDocRoot');     # Apache  londocument root.
   88: 
   89:   my $resdir=$docroot.'/res/'.$udom.'/'.$uname.$thisdisfn; # Resource directory
   90:   my $targetdir=$udom.'/'.$uname.$thisdisfn; # Publiction target directory.
   91:   my $linkdir='/priv/'.$uname.$thisdisfn;      # Full URL name of constr space.
   92: 
   93:   my %bombs=&Apache::lonmsg::all_url_author_res_msg($uname,$udom);
   94: 
   95:   &startpage($r, $uname, $udom, $thisdisfn);   # Put out the start of page.
   96:   if ($env{'environment.remote'} eq 'off') {
   97:       &dircontrols($r,$uname,$udom,$thisdisfn);    # Put out actions for directory, 
   98:                                                # browse/upload + new file page.
   99:   } else {
  100:       &pubbuttons($r,$uname,$thisdisfn);
  101:   }
  102:   &resourceactions($r,$uname,$udom,$thisdisfn); #Put out form used for printing/deletion etc.
  103: 
  104:   my $numdir = 0;
  105:   my $numres = 0;
  106:   
  107:   # Start off the directory table.
  108:   $r->print('<h3>'.&mt('Directory Contents:').'</h3>');
  109:   $r->print(&Apache::loncommon::start_data_table()
  110:            .&Apache::loncommon::start_data_table_header_row()
  111:            .'<th>'.&mt('Type').'</th>'
  112:            .'<th>'.&mt('Actions').'</th>'
  113:            .'<th>'.&mt('Name').'</th>'
  114:            .'<th>'.&mt('Title').'</th>'
  115:            .'<th colspan="2">'.&mt('Status').'</th>'
  116:            .'<th>'.&mt('Last Modified').'</th>'
  117:            .&Apache::loncommon::end_data_table_header_row()
  118:   );
  119: 
  120:   my $filename;
  121:   my $dirptr=16384;		# Mask indicating a directory in stat.cmode.
  122: 
  123:   opendir(DIR,$fn);
  124:   my @files=sort {uc($a) cmp uc($b)} (readdir(DIR));
  125:   foreach my $filename (@files) {
  126:      my ($cdev,$cino,$cmode,$cnlink,
  127:          $cuid,$cgid,$crdev,$csize,
  128:          $catime,$cmtime,$cctime,
  129:          $cblksize,$cblocks)=stat($fn.'/'.$filename);
  130: 
  131:      my $extension='';
  132:      if ($filename=~/\.(\w+)$/) { $extension=$1; }
  133:      if ($cmode&$dirptr) {
  134: 	 putdirectory($r, $thisdisfn, $linkdir, $filename, $cmtime,$targetdir,\%bombs,\$numdir);
  135:      } elsif (&Apache::loncommon::fileembstyle($extension) ne 'hdn') {
  136: 	 putresource($r, $udom, $uname, $filename, $thisdisfn, $resdir, 
  137: 		     $targetdir, $linkdir, $cmtime,\%bombs,\$numres);
  138:      } else {
  139: 	# "hidden" extension and not a directory, so hide it away.
  140:      }
  141:   }
  142:   closedir(DIR);
  143: 
  144:   $r->print(&Apache::loncommon::end_data_table()
  145:            .&Apache::loncommon::end_page()
  146:   );
  147:   return OK;  
  148: }
  149: #
  150: #  Gets the effective URL of the request and returns it:
  151: #    $effn = getEffectiveUrl($r);
  152: #       $r  - The Apache Request object.
  153: sub getEffectiveUrl {
  154:     my $r = shift;
  155:     my $fn;
  156:     
  157:     if ($env{'form.filename'}) {	# If a form filename is defined.
  158: 	$fn=$env{'form.filename'};
  159: 	#
  160: 	#   Replace the ~username of the URL with /home/username/public_html
  161: 	#   so that we don't have to worry about ~ expansion internally.
  162: 	#
  163: 	$fn=~s/^http\:\/\/[^\/]+\///;
  164:         $fn=~s/^\///;
  165:         $fn=~s{~($LONCAPA::username_re)}{/home/$1/public_html};
  166: 	
  167: 	#  Remove trailing / strings (?) 
  168: 	
  169: 	$fn=~s/\/[^\/]+$//;
  170:     } else {
  171: 	#   If no form is defined, use request filename.
  172: 	$fn = $r->filename();
  173: 	my $lonDocRoot=$r->dir_config('lonDocRoot');
  174: 	if ( $fn =~ /$lonDocRoot/ ) {
  175: 	    #internal authentication, needs fixup.
  176: 	    $fn = $r->uri(); # non users do not get the full path request
  177:                              # through SCRIPT_FILENAME
  178: 	    $fn=~s{^/~($LONCAPA::username_re)}{/home/$1/public_html};
  179: 	}
  180:     }
  181:     $fn=~s/\/+/\//g;
  182:     return $fn;
  183: }
  184: #
  185: #   Output the header of the page.  This includes:
  186: #   - The HTML header 
  187: #   - The H1/H3  stuff which includes the directory.
  188: #
  189: #     startpage($r, $uame, $udom, $thisdisfn);
  190: #      $r     - The apache request object.
  191: #      $uname - User name.
  192: #      $udom  - Domain name the user is logged in under.
  193: #      $thisdisfn - Displayable version of the filename.
  194: 
  195: sub startpage {
  196:     my ($r, $uname, $udom, $thisdisfn) = @_;
  197:     my $currdir = '/priv/'.$uname.$thisdisfn;
  198:     &Apache::loncommon::content_type($r,'text/html');
  199:     $r->send_http_header;
  200: 
  201:     my $formaction='/priv/'.$uname.$thisdisfn.'/';
  202:     $formaction=~s|/+|/|g;
  203:     my $pagetitle .= &Apache::loncommon::help_open_menu('','',3,'Authoring').
  204:         '<font face="Arial, Helvetica, sans-serif" size="+1"><b>'.&mt('Construction Space').'</b>:</font>&nbsp;'.
  205:         '<form name="dirs" method="post" action="'.$formaction.
  206:         '" target="_parent"><tt><b>'.
  207:         &Apache::lonhtmlcommon::crumbs($uname.$thisdisfn.'/','_top','/priv','','+1',1)."</b></tt><br />".
  208:         &Apache::lonhtmlcommon::select_recent('construct','recent',
  209:                  'this.form.action=this.form.recent.value;this.form.submit()').
  210:               '</form>';
  211:     &Apache::lonhtmlcommon::store_recent('construct',$formaction,$formaction);
  212:     if ($env{'environment.remote'} eq 'off') {
  213: 	$env{'request.noversionuri'}=$currdir.'/';
  214: 	$r->print(&Apache::loncommon::start_page('Construction Space',undef,
  215: 						 {'body_title' =>
  216: 						      $pagetitle,}));
  217:     } else {
  218: 	$r->print(&Apache::loncommon::start_page('Construction Space',undef,
  219: 						 { 'only_body' => 1,}));
  220: 	$r->print($pagetitle);
  221:     }
  222: 
  223:     my $esc_thisdisfn = &Apache::loncommon::escape_single($thisdisfn);
  224:     my $doctitle = 'LON-CAPA '.&mt('Construction Space');
  225:     my $newname = &mt('New Name');
  226:     my $pubdirscript=(<<ENDPUBDIRSCRIPT);
  227: <script type="text/javascript">
  228: top.document.title = '$esc_thisdisfn/ - $doctitle';
  229: // Store directory location for menu bar to find
  230: 
  231: parent.lastknownpriv='/~$uname$esc_thisdisfn/';
  232: 
  233: // Confirmation dialogues
  234: 
  235:     function currdiract(theform) {
  236:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == 'publish') {
  237:             document.publishdir.filename.value = theform.filename.value;
  238: 	    document.publishdir.submit();
  239:         }
  240:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == 'editmeta') {
  241:             top.location=theform.filename.value+'default.meta'
  242:         }
  243:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == 'printdir' ) {
  244:             document.printdir.postdata.value=theform.filename.value
  245:             document.printdir.submit();
  246:         }
  247:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == "delete") {
  248:               var delform = document.delresource
  249:               delform.filename.value = theform.filename.value
  250:               delform.submit()
  251:         }
  252:     }
  253:   
  254:     function checkUpload(theform) {
  255:         if (theform.file == '') {
  256:             alert("Please use 'Browse..' to choose a file first, before uploading")
  257:             return 
  258:         }
  259:         theform.submit()  
  260:     }
  261: 
  262:     function SetPubDir(theform,printForm) {
  263:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "open") {
  264:             top.location = theform.openname.value
  265:             return
  266:         }
  267:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "publish") {
  268:             theform.submit();
  269:         }
  270:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "editmeta") {
  271:             top.location=theform.filename.value+'default.meta'
  272:         }
  273:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "printdir") {
  274:             theform.action = '/adm/printout'
  275:             theform.postdata.value = theform.filename.value
  276:             theform.submit()
  277:         }
  278:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "delete") {
  279:               var delform = document.delresource
  280:               delform.filename.value = theform.filename.value
  281:               delform.submit()
  282:         }
  283:         return
  284:     }
  285:     function SetResChoice(theform) {
  286:       var activity = theform.reschoice.options[theform.reschoice.selectedIndex].value
  287:       if ((activity == 'rename') || (activity == 'copy') || (activity == 'move')) {
  288:           changename(theform,activity)
  289:       }
  290:       if (activity == 'publish') {
  291:           var pubform = document.pubresource
  292:           pubform.filename.value = theform.filename.value
  293:           pubform.submit()
  294:       }
  295:       if (activity == 'delete') {
  296:           var delform = document.delresource
  297:           delform.filename.value = theform.filename.value
  298:           delform.submit()
  299:       }
  300:       if (activity == 'obsolete') {
  301:           var pubform = document.pubresource
  302:           pubform.filename.value = theform.filename.value
  303:           pubform.makeobsolete.value=1;
  304:           pubform.submit()
  305:       }
  306:       if (activity == 'print') {
  307:           document.printresource.postdata.value = theform.filename.value
  308:           document.printresource.submit()
  309:       }
  310:       if (activity == 'retrieve') {
  311:           document.retrieveres.filename.value = theform.filename.value
  312:           document.retrieveres.submit()
  313:       }
  314:       if (activity == 'cleanup') {
  315:           document.cleanup.filename.value = theform.filename.value
  316:           document.cleanup.submit()
  317:       }
  318:       return
  319:     }
  320:     function changename(theform,activity) {
  321:         var oldname=theform.dispfilename.value;
  322:         var newname=prompt('$newname',oldname);
  323:         if (newname == "" || !newname || newname == oldname)  {
  324:             return
  325:         }
  326:         document.moveresource.newfilename.value = newname
  327:         document.moveresource.filename.value = theform.filename.value
  328:         document.moveresource.action.value = activity
  329:         document.moveresource.submit();
  330:     }
  331: </script>
  332: ENDPUBDIRSCRIPT
  333:     $r->print($pubdirscript);
  334: 
  335:     if ((($uname ne $env{'user.name'}) || ($udom ne $env{'user.domain'})) &&
  336: 	$env{'environment.remote'} ne 'off') {
  337: 	$r->print('<h3>'.&mt('Co-Author').': '.$uname.' at '.$udom.
  338: 		  '</h3>');
  339:     }
  340: }
  341: 
  342: sub dircontrols {
  343:     my ($r,$uname,$udom,$thisdisfn) = @_;
  344:     my %lt=&Apache::lonlocal::texthash(
  345:                                        cnpd => 'Cannot publish directory',
  346:                                        cnrd => 'Cannot retrieve directory',
  347:                                        mcdi => 'Must create new subdirectory inside a directory',
  348:                                        pubr => 'Publish this Resource',
  349:                                        pubd => 'Publish this Directory',
  350:                                        dedr => 'Delete Directory',
  351:                                        rtrv => 'Retrieve Old Version',
  352:                                        list => 'List Directory',
  353:                                        uplo => 'Upload file',  
  354:                                        dele => 'Delete',
  355:                                        edit => 'Edit Metadata', 
  356:                                        sela => 'Select Action',
  357:                                        nfil => 'New file',
  358:                                        nhtm => 'New HTML file',
  359:                                        nprb => 'New problem',
  360:                                        npag => 'New assembled page',
  361:                                        nseq => 'New assembled sequence',
  362:                                        ncrf => 'New custom rights file',
  363:                                        nsty => 'New style file',
  364:                                        nlib => 'New library file',
  365:                                        nbt  => 'New bridgetask file',
  366:                                        nsub => 'New subdirectory',
  367:                                        renm => 'Rename current file to',
  368:                                        move => 'Move current file to',
  369:                                        copy => 'Copy current file to',
  370:                                        type => 'Type Name Here',
  371:                                        go   => 'Go',
  372:                                        prnt => 'Print contents of directory',
  373:                                        crea => 'Create a new directory or LON-CAPA document',
  374: 				       acti => 'Actions for current directory',
  375: 				       updc => 'Upload a new document',
  376: 				       pick => 'Please select an action to perform using the new filename',
  377:                                       );
  378:     my $mytype = $lt{'type'}; # avoid conflict with " and ' in javascript
  379:     $r->print(<<END);
  380:         <table id="LC_cstr_controls">
  381:          <tr>
  382:           <th>$lt{'acti'}</th>
  383:           <th>$lt{'updc'}</th>
  384:           <th>$lt{'crea'}</th>
  385:         </tr>
  386:         <tr>
  387:          <td>
  388:           <form name="curractions" method="post" action="">
  389:            <select name="dirtask" onchange="currdiract(this.form)">
  390:             <option>$lt{'sela'}</option>
  391:             <option value="publish">$lt{'pubd'}</option>
  392:             <option value="editmeta">$lt{'edit'}</option>
  393:             <option value="printdir">$lt{'prnt'}</option>
  394:             <option value="delete">$lt{'dedr'}</option>
  395:            </select>
  396:            <input type="hidden" name="filename" value="/~$uname$thisdisfn/" />
  397:           </form>
  398:           <form name="publishdir" method="post" action="/adm/publish" target="_parent">
  399:            <input type="hidden" name="pubrec" value="" />
  400:            <input type="hidden" name="filename" value="" />
  401:           </form>
  402:           <form name="printdir" method="post" action="/adm/printout" target="_parent">
  403:            <input type="hidden" name="postdata" value="" />
  404:           </form>
  405:          </td>
  406:          <td>
  407: 	    <form name="upublisher" enctype="multipart/form-data" method="post" action="/adm/upload" target="_parent">
  408: 	      <input type="hidden" name="filename" value="/~$uname$thisdisfn/" />
  409: 	      <input type="file" name="upfile" size="20" />
  410: 	      <input type="button" value="$lt{'uplo'}"  onclick="checkUpload(this.form)" />
  411: 	    </form>
  412: 	 </td>
  413: 	 <td>
  414: 	    <form name="fileaction" method="post" action="/adm/cfile" target="_parent">
  415: 	      <span style="white-space: nowrap">
  416: 		<input type="hidden" name="filename" value="/~$uname$thisdisfn/" />
  417:                   <script type="text/javascript">
  418:                     function validate_go() {
  419:                         var selected = document.fileaction.action.selectedIndex;
  420:                         if (selected == 0) {
  421:                             alert('$lt{'pick'}');
  422:                         } else {
  423:                             document.fileaction.submit();
  424:                         }
  425:                     }
  426:                   </script>
  427: 		  <select name="action">
  428: 		    <option value="none">$lt{'sela'}</option>
  429: 		    <option value="newfile">$lt{'nfil'}:</option>
  430: 		    <option value="newhtmlfile">$lt{'nhtm'}:</option>
  431: 		    <option value="newproblemfile">$lt{'nprb'}:</option>
  432:                     <option value="newpagefile">$lt{'npag'}:</option>
  433:                     <option value="newsequencefile">$lt{'nseq'}:</option>
  434:                     <option value="newrightsfile">$lt{'ncrf'}:</option>
  435:                     <option value="newstyfile">$lt{'nsty'}:</option>
  436:                     <option value="newtaskfile">$lt{'nbt'}:</option>
  437:                     <option value="newlibraryfile">$lt{'nlib'}:</option>
  438: 	            <option value="newdir">$lt{'nsub'}:</option>
  439: 		  </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();" />
  440: 		 </span>
  441: 		</form>
  442: 	  </td>
  443:          </tr>
  444:         </table>
  445: END
  446: }
  447: 
  448: sub pubbuttons {
  449:     my ($r,$uname,$thisdisfn) = @_;
  450:     $r->print('<form method="post" action="/adm/publish" target="_parent">'.
  451:               '<table><tr><td><input type="hidden" name="filename" value="/~'.
  452:                $uname.$thisdisfn.'/" />'.
  453:               '<input type="submit" value="'.&mt('Publish Directory').'" /></td><td>'.
  454: '<input type="button" onclick="window.location='."'/~".
  455:                $uname.$thisdisfn."/default.meta'".'" value="'.
  456: &mt('Edit Directory Catalog Information').'" /></td></tr></table></form>');
  457: }
  458: 
  459: sub resourceactions {
  460:     my ($r,$uname,$udom,$thisdisfn) = @_;
  461:     $r->print(<<END);
  462:        <form name="moveresource" action="/adm/cfile" target="_parent" method="post">
  463:          <input type="hidden" name="filename" value="" />
  464:          <input type="hidden" name="newfilename" value="" />
  465:          <input type="hidden" name="action" value="" />
  466:        </form>
  467:        <form name="delresource" action="/adm/cfile" target="_parent" method="post">
  468:          <input type="hidden" name="filename" value="" />
  469:          <input type="hidden" name="action" value="delete" />
  470:        </form>
  471:        <form name="pubresource" action="/adm/publish" target="_parent" method="post">
  472:          <input type="hidden" name="filename" value="" />
  473:          <input type="hidden" name="makeobsolete" value="0" />
  474:        </form>
  475:        <form name="printresource" action="/adm/printout" target="_parent" method="post">
  476:            <input type="hidden" name="postdata" value="" />
  477:        </form>
  478:        <form name="retrieveres" action="/adm/retrieve" target="_parent" method="post">
  479:            <input type="hidden" name="filename" value="" />
  480:        </form>
  481:        <form name="cleanup" action="/adm/cleanup" target="_parent" method="post">
  482:            <input type="hidden" name="filename" value="" />
  483:        </form>
  484: END
  485: }
  486: 
  487: #
  488: #   Get the title string or "[untitled]" if the file has no title metadata:
  489: #   Without the latter substitution, it's impossible to examine metadata for
  490: #   untitled resources.  Resources may be legitimately untitled, to prevent
  491: #   searches from locating them.
  492: #
  493: #   $str = getTitleString($fullname);
  494: #       $fullname - Fully qualified filename to check.
  495: #
  496: sub getTitleString {
  497:     my $fullname = shift;
  498:     my $title    = &Apache::lonnet::metadata($fullname, 'title');
  499: 
  500:     unless ($title) {
  501: 	$title = "[".&mt('untitled')."]";
  502:     }
  503:     return $title;
  504: }
  505: 
  506: sub getCopyRightString {
  507:     my $fullname = shift;
  508:     return &Apache::lonnet::metadata($fullname, 'copyright');
  509: }
  510: 
  511: sub getSourceRightString {
  512:     my $fullname = shift;
  513:     return &Apache::lonnet::metadata($fullname, 'sourceavail');
  514: }
  515: #
  516: #  Put out a directory table row:
  517: #    putdirectory(r, base, here, dirname, modtime)
  518: #      r       - Apache request object.
  519: #      reqfile - File in request.
  520: #      here    - Where we are in directory tree.
  521: #      dirname - Name of directory special file.
  522: #      modtime - Encoded modification time.
  523: # 
  524: sub putdirectory {
  525:     my ($r, $reqfile, $here, $dirname, $modtime, $resdir, $bombs, $numdir) = @_;
  526:     # construct the display filename: the directory name unless ..:
  527:     
  528:     my $disfilename = $dirname;
  529:     if ($dirname eq '..') {
  530: 	$disfilename = '<i>'.&mt('Parent Directory').'</i>';
  531:     }
  532:     unless ( (($dirname eq '..') && ($reqfile eq '')) || ($dirname eq '.')) {
  533: 	my $kaputt=0;
  534: 	foreach (keys %{$bombs}) {
  535: 	    if ($_=~m:^\Q$resdir\E/\Q$disfilename\E/:) { $kaputt=1; last; }
  536: 	}
  537: 	%Apache::lonpublisher::metadatafields=();
  538: 	%Apache::lonpublisher::metadatakeys=();
  539: 	my $construct=$here;
  540: 	$construct=~s{^/priv/($LONCAPA::username_re)$}{/home/$1/public_html};
  541:         my $dirpath = $here;
  542:         $dirpath=~s{^/priv/}{/~};
  543: 	&Apache::lonpublisher::metaeval(&Apache::lonnet::getfile(
  544:        				 $construct.'/'.$dirname.'/default.meta'
  545: 								 ));
  546:         my $actionitem = '';
  547:         if ($dirname eq '..') {
  548:             $actionitem = &mt('Go to ...');
  549:         } else {
  550:             $actionitem = 
  551:                     '<form name="dirselect_'.$$numdir.
  552:                     '" action="/adm/publish" target="_parent">'.
  553:                     '<select name="diraction" onchange="SetPubDir(this.form,document)">'.
  554:                       '<option selected="selected">'.&mt('Select action').'</option>'.
  555:                       '<option value="open">'.&mt('Open').'</option>'.
  556:                       '<option value="publish">'.&mt('Publish').'</option>'.
  557:                       '<option value="editmeta">'.&mt('Edit Metadata').'</option>'.
  558:                       '<option value="printdir">'.&mt('Print directory').'</option>'.
  559:                       '<option value="delete">'.&mt('Delete directory').'</option>'.
  560:                     '</select>'.
  561:                      '<input type="hidden" name="filename" value="'.&HTML::Entities::encode($dirpath.'/'.$dirname,'<>&"').'/" />'.
  562:                      '<input type="hidden" name="openname" value="'.$here.'/'.$dirname.'/" />'.
  563:                      '<input type="hidden" name="postdata" value="" />'.
  564:                    '</form>';
  565:             $$numdir ++;
  566:         }
  567: 	$r->print('<tr class="LC_browser_folder">'.
  568: 		  '<td><img src="'.
  569: 		  $Apache::lonnet::perlvar{'lonIconsURL'}.'/folder_closed.gif" alt="folder" /></td>'.
  570: 		  '<td>'.$actionitem.'</td>'.
  571: 		  '<td><span class="LC_filename"><a href="'.&HTML::Entities::encode($here.'/'.$dirname,'<>&"').'/" target="_parent">'.
  572: 		  $disfilename.'</a></span></td>'.
  573: 		        '<td colspan="3">'.($kaputt?&Apache::lonhtmlcommon::authorbombs($resdir.'/'.$disfilename.'/'):'').$Apache::lonpublisher::metadatafields{'title'});
  574: 	if ($Apache::lonpublisher::metadatafields{'subject'} ne '') {
  575: 	    $r->print(' <i>'.
  576: 		      $Apache::lonpublisher::metadatafields{'subject'}.
  577: 		      '</i> ');
  578: 	}
  579: 	$r->print($Apache::lonpublisher::metadatafields{'keywords'}.'</td>'.
  580: 		  '<td>'.&Apache::lonlocal::locallocaltime($modtime).'</td>'.
  581: 		  "</tr>\n");
  582:     }
  583:     return OK;
  584: }
  585: #
  586: #   Put a table row for a file resource.
  587: #
  588: sub putresource {
  589:     my ($r, $udom, $uname, $filename, $thisdisfn, 
  590: 	$resdir, $targetdir, $linkdir,
  591: 	$cmtime,$bombs,$numres) = @_;
  592:     &Apache::lonnet::devalidate_cache_new('meta',$targetdir.'/'.$filename);
  593:     my $pubstatus = 'unpublished';
  594:     my $status=&mt('Unpublished');
  595:     my $css_class='LC_browser_file';
  596:     my $title='&nbsp;';
  597:     my $publish_button=&mt('Publish');
  598:     my $cstr_dir = '/home/'.$uname.'/public_html/'.$thisdisfn.'/';
  599: #    my $action_buttons=
  600: #        '<br /><a target="_parent" href="/adm/cfile?action=delete&filename=/~'.
  601: #	$uname.'/'.$thisdisfn.'/'.$filename.'">'.
  602: #	&mt('Delete').'</a>';
  603:     if (-e $resdir.'/'.$filename) {
  604:         my $same=0;
  605: 	my ($rdev,$rino,$rmode,$rnlink,
  606: 	    $ruid,$rgid,$rrdev,$rsize,
  607: 	    $ratime,$rmtime,$rctime,
  608: 	    $rblksize,$rblocks)=stat($resdir.'/'.$filename);
  609:         if ($rmtime>=$cmtime) {
  610:            $same=1;
  611:         } else {
  612:            if (&Apache::londiff::are_different_files($resdir.'/'.$filename,
  613: 						     $cstr_dir.'/'.$filename)) {
  614:               $same=0;
  615:            } else {
  616:               $same=1;
  617:            }
  618:         }
  619: 	my $meta_cmtime = (stat($cstr_dir.'/'.$filename.'.meta'))[9];
  620: 	my $meta_rmtime = (stat($resdir.'/'.$filename.'.meta'))[9];
  621: 	my $meta_same = 1;
  622: 	if ($meta_rmtime < $meta_cmtime
  623: 	    && &Apache::londiff::are_different_files($resdir.'/'.$filename.'.meta',
  624: 						     $cstr_dir.'/'.$filename.'.meta')) {
  625: 	    $meta_same = 0;
  626: 	}
  627: 	$publish_button=&mt('Re-publish');
  628: 	my $rights_status =
  629: 	    &mt(&getCopyRightString($targetdir.'/'.$filename)).' '.
  630: 	    &mt(&getSourceRightString($targetdir.'/'.$filename));
  631: 	$title = '<a href="/res/'.$targetdir.'/'.$filename.
  632: 	    '.meta" target="cat">'.
  633: 	    &getTitleString($targetdir.'/'.$filename).'</a>';
  634: 	if ($same) {
  635: 	    if (&Apache::lonnet::metadata($targetdir.'/'.$filename,'obsolete')) {
  636:                 $pubstatus = 'obsolete';
  637: 		$status=&mt('Obsolete');
  638:             } else {
  639: 		if (!$meta_same) {
  640: 		    $pubstatus = 'metamodified';
  641: 		} else {
  642: 		    $pubstatus = 'published';
  643: 		}
  644: 		$status=&mt('Published').
  645: 		    '<br />'. $rights_status;
  646: 	    }
  647: #	    } else {
  648: #		$action_buttons='';
  649: #	    }
  650: 	} else {
  651:             $pubstatus = 'modified';
  652: 	    $status=&mt('Modified').
  653: 		'<br />'. $rights_status;
  654: #	    $action_buttons='';
  655: 	    if (&Apache::loncommon::fileembstyle(($filename=~/\.(\w+)$/)) eq 'ssi') {
  656: 		$status.='<br /><a href="/adm/diff?filename=/~'.$uname.
  657: 		    $thisdisfn.'/'.$filename.
  658: 		    '&amp;versiontwo=priv" target="cat">'.&mt('Diffs').'</a>';
  659: 	    }
  660: 	} 
  661: 
  662: 	$title.="\n".'<br /><a href="/~'.$uname.$thisdisfn.'/'.$filename.'.meta">'. 
  663: 	    ($$bombs{$targetdir.'/'.$filename}?'<img src="/adm/lonMisc/bomb.gif" border="0" alt="'.&mt('bomb').'" />':&mt('Edit Metadata')).'</a>';
  664: 
  665: 	if (!$meta_same) {
  666: 	    $title = &mt('Metadata Modified').'<br />'.$title.
  667: 		'<br /><a href="/adm/diff?filename=/~'.$uname.
  668: 		$thisdisfn.'/'.$filename.'.meta'.
  669: 		'&amp;versiontwo=priv" target="cat">'.&mt('Metadata Diffs').'</a>';
  670: 	    $title.="\n".'<br /><a href="/adm/retrieve?filename=/~'.$uname.
  671: 		$thisdisfn.'/'.$filename.'.meta" target="_parent">'.&mt('Retrieve Metadata').'</a>';
  672: 	}
  673: 	$status.="\n".'<br /><a href="/adm/retrieve?filename=/~'.$uname.
  674: 	    $thisdisfn.'/'.$filename.'" target="_parent">'.&mt('Retrieve').'</a>';
  675:     }
  676:     my $editlink='';
  677:     my $editlink2='';
  678:     if ($filename=~/\.(xml|html|htm|xhtml|xhtm|sty)$/) {
  679: 	$editlink=' <br />(<a href="'.$linkdir.'/'.$filename.'?forceedit=1" target="_parent">'.&mt('Edit').'</a>)';
  680:     }
  681:     if ($filename=~/\.(problem|exam|quiz|assess|survey|form|library)$/) {
  682: 	$editlink=' (<a href="'.$linkdir.'/'.$filename.'?forceedit=1" target="_parent">'.&mt('EditXML').'</a>)';
  683: 	$editlink2=' <br />(<a href="'.$linkdir.'/'.$filename.'?forceColoredit=1" target="_parent">'.&mt('Edit').'</a>)';
  684:     }
  685:     if ($filename=~/\.(problem|exam|quiz|assess|survey|form|library|xml|html|htm|xhtml|xhtm|sty)$/) {
  686: 	$editlink.=' (<a href="/adm/cleanup?filename=/~'.$uname.
  687: 	    $thisdisfn.'/'.$filename.'" target="_parent">'.&mt('Clean Up').')</a>';
  688:     }
  689:     if ($filename=~/\.(zip|tar|bz2|gz|tar.gz|tar.bz2|tgz)$/) {
  690: 	$editlink=' (<a target="_parent" href="/adm/cfile?decompress=/~'.
  691: 	      $uname.$thisdisfn.'/'.$filename.'">'.&mt('Decompress').'</a>)';
  692:     }
  693:     my $pub_select = '';
  694:     &create_pubselect($r,\$pub_select,$udom,$uname,$thisdisfn,$filename,$resdir,$pubstatus,$publish_button,$numres);
  695:     $r->print(&Apache::loncommon::start_data_table_row().
  696: 	      '<td>'.($filename=~/[\#\~]$/?'&nbsp;':
  697: 		      '<img src="'.&Apache::loncommon::icon($filename).'" alt="" />').'</td>'.
  698:               '<td>'.$pub_select.'</td>'.
  699: 	      '<td><span class="LC_filename">'.
  700: 	      '<a href="'.$linkdir.'/'.$filename.'" target="_parent">'.
  701:                $filename.'</a></span>'.$editlink2.$editlink.
  702: 	      '</td>'.
  703: 	      '<td>'.$title.'</td>'.
  704:               '<td class="LC_browser_file_'.$pubstatus.'">&nbsp;&nbsp;</td>'. # Display publication status
  705:               '<td>'.$status.'</td>'.
  706: 	      '<td>'.&Apache::lonlocal::locallocaltime($cmtime).'</td>'.
  707: 	      &Apache::loncommon::end_data_table_row()
  708:     );
  709:     return OK;
  710: }
  711: 
  712: sub create_pubselect {
  713:     my ($r,$pub_select,$udom,$uname,$thisdisfn,$filename,$resdir,$pubstatus,$publish_button,$numres) = @_;
  714:     $$pub_select = '
  715: <form name="resselect_'.$$numres.'" action="">
  716: <select name="reschoice"  onchange="SetResChoice(this.form)">
  717: <option>'.&mt('Select action').'</option>'.
  718: '<option value="copy">'.&mt('Copy').'</option>';
  719:     if ($pubstatus eq 'obsolete' || $pubstatus eq 'unpublished') {
  720:         $$pub_select .= 
  721: '<option value="rename">'.&mt('Rename').'</option>'.
  722: '<option value="move">'.&mt('Move').'</option>'.
  723: '<option value="delete">'.&mt('Delete').'</option>';
  724:     } else {
  725:         $$pub_select .= '
  726: <option value="obsolete">'.&mt('Mark obsolete').'</option>';
  727:     }
  728: # check for versions
  729:     my $versions = &check_for_versions($r,'/'.$filename,$udom,$uname);
  730:     if ($versions > 0) {
  731:         $$pub_select .='
  732: <option value="retrieve">'.&mt('Retrieve old version').'</option>';
  733:     }
  734:     $$pub_select .= '
  735: <option value="publish">'.$publish_button.'</option>'.
  736: '<option value="cleanup">'.&mt('Clean up').'</option>'.
  737: '<option value="print">'.&mt('Print').'</option>'.
  738: '</select>
  739: <input type="hidden" name="filename" value="/~'.
  740:  &HTML::Entities::encode($uname.$thisdisfn.'/'.$filename,'<>&"').'" />
  741:  <input type="hidden" name="dispfilename" value="'.
  742:  &HTML::Entities::encode($filename).'" /></form>';
  743:     $$numres ++;
  744: }
  745: 
  746: sub check_for_versions {
  747:     my ($r,$fn,$udom,$uname) = @_;
  748:     my $versions = 0;
  749:     my $docroot=$r->dir_config('lonDocRoot');
  750:     my $resfn=$docroot.'/res/'.$udom.'/'.$uname.$fn;
  751:     my $resdir=$resfn;
  752:     $resdir=~s/\/[^\/]+$/\//;
  753:     $fn=~/\/([^\/]+)\.(\w+)$/;
  754:     my $main=$1;
  755:     my $suffix=$2;
  756:     opendir(DIR,$resdir);
  757:     while (my $filename=readdir(DIR)) {
  758:         if ($filename=~/^\Q$main\E\.(\d+)\.\Q$suffix\E$/) {
  759:             $versions ++;        
  760:         }
  761:     }
  762:     return $versions;
  763: }
  764: 
  765: #
  766: #   Categorize files in the directory.
  767: #   For each file in a list of files in a file directory, 
  768: #   the  file categorized as one of:
  769: #    - directory  
  770: #    - sequence
  771: #    - problem 
  772: #    - Other resource.
  773: #
  774: #   For each file the modification date is determined as well.
  775: #   Returned is a list of sublists:
  776: #    (directories, sequences, problems, other)
  777: #   each of the sublists contains entries of the following form (sorted by
  778: #   filename):
  779: #     (filename, typecode, lastmodtime)
  780: #
  781: #   $list = CategorizeFiles($location, $files)
  782: #       $location   - Directory in which the files live (relative to our
  783: #                     execution.
  784: #       $files      - list of files.
  785: #
  786: sub CategorizeFiles {
  787:     my $location = shift;
  788:     my $files    = shift;
  789: }
  790: 
  791: 1;
  792: __END__
  793: 
  794: 
  795: =head1 NAME
  796: 
  797: Apache::lonpubdir - Construction space directory lister
  798: 
  799: =head1 SYNOPSIS
  800: 
  801: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
  802: 
  803:  <LocationMatch "^/\~.*/$">
  804:  PerlAccessHandler       Apache::loncacc
  805:  SetHandler perl-script
  806:  PerlHandler Apache::lonpubdir
  807:  ErrorDocument     403 /adm/login
  808:  ErrorDocument     404 /adm/notfound.html
  809:  ErrorDocument     406 /adm/unauthorized.html
  810:  ErrorDocument	  500 /adm/errorhandler
  811:  </LocationMatch>
  812: 
  813:  <Location /adm/pubdir>
  814:  PerlAccessHandler       Apache::lonacc
  815:  SetHandler perl-script
  816:  PerlHandler Apache::lonpubdir
  817:  ErrorDocument     403 /adm/login
  818:  ErrorDocument     404 /adm/notfound.html
  819:  ErrorDocument     406 /adm/unauthorized.html
  820:  ErrorDocument	  500 /adm/errorhandler
  821:  </Location>
  822: 
  823: =head1 INTRODUCTION
  824: 
  825: This module publishes a directory of files.
  826: 
  827: This is part of the LearningOnline Network with CAPA project
  828: described at http://www.lon-capa.org.
  829: 
  830: =head1 HANDLER SUBROUTINE
  831: 
  832: This routine is called by Apache and mod_perl.
  833: 
  834: =over 4
  835: 
  836: =item *
  837: 
  838: read in information
  839: 
  840: =item *
  841: 
  842: start page output
  843: 
  844: =item *
  845: 
  846: run through list of files and attempt to publish unhidden files
  847: 
  848: =back
  849: 
  850: =head1 SUBROUTINES:
  851: 
  852: =over
  853: 
  854: =item startpage($r, $uame, $udom, $thisdisfn)
  855: 
  856: Output the header of the page.  This includes:
  857:  - The HTML header 
  858:  - The H1/H3  stuff which includes the directory.
  859:  
  860:     startpage($r, $uame, $udom, $thisdisfn);
  861:         $r     - The apache request object.
  862:         $uname - User name.
  863:         $udom  - Domain name the user is logged in under.
  864:         $thisdisfn - Displayable version of the filename.
  865: 
  866: =item getTitleString($fullname)
  867: 
  868:     Get the title string or "[untitled]" if the file has no title metadata:
  869:     Without the latter substitution, it's impossible to examine metadata for
  870:     untitled resources.  Resources may be legitimately untitled, to prevent
  871:     searches from locating them.
  872:     
  873:     $str = getTitleString($fullname);
  874:         $fullname - Fully qualified filename to check.
  875: 
  876: =item putdirectory(r, base, here, dirname, modtime)
  877: 
  878:     Put out a directory table row:
  879:     
  880:     putdirectory($r, $base, $here, $dirname, $modtime)
  881:         $r       - Apache request object.
  882:         $reqfile - File in request.
  883:         $here    - Where we are in directory tree.
  884:         $dirname - Name of directory special file.
  885:         $modtime - Encoded modification time.
  886: 
  887: =item CategorizeFiles($location, $files)
  888:     
  889:     Categorize files in the directory.
  890:     For each file in a list of files in a file directory, 
  891:     the  file categorized as one of:
  892:         - directory  
  893:         - sequence
  894:         - problem 
  895:         - Other resource.
  896: 
  897:     For each file the modification date is determined as well.
  898:     Returned is a list of sublists:
  899:         (directories, sequences, problems, other)
  900:     each of the sublists contains entries of the following form (sorted by filename):
  901:     (filename, typecode, lastmodtime)
  902:     
  903:         $list = CategorizeFiles($location, $files)
  904:         $location   - Directory in which the files live (relative to our execution)
  905:         $files      - list of files.
  906: 
  907: =back
  908: 
  909: =cut
  910: 

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