File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.155: download - view: text, annotated - select for diffs
Fri Jun 20 18:00:51 2014 UTC (9 years, 11 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Detect if Authoring Spaces have not been moved following upgrade from 2.10
  (and older) to 2.11, and display an appropriate message.

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

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