File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.154: download - view: text, annotated - select for diffs
Sat May 31 13:55:17 2014 UTC (10 years ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Early out if top level directory in Authoring Space is empty.
- &handler() returns 'OK'; no need to return 'OK' from other subroutines.

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

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