File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.147: download - view: text, annotated - select for diffs
Tue Jul 2 19:04:49 2013 UTC (10 years, 11 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Disk quotas for authoring spaces.
  - Default is 500 MB, unless set at domain level, or individual user level.
  - Domain default set via "Blogs, personal web pages, webDAV/quotas, portfolio" in
    "Set domain configuration" by DC.
  - Quota for individual user's authoring space set via:
    "Create users or modify the roles and privileges of users" by DC
  - &display_usage() moved from portfolio.pm to lonhtmlcommon.pm to
    facilitate re-use. In portfolio.pm &display_portfolio_usage() now calls
    lonhtmlcommon::display_usage().

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

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