File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.162: download - view: text, annotated - select for diffs
Fri Oct 17 13:00:39 2014 UTC (9 years, 7 months ago) by goltermann
Branches: MAIN
CVS tags: HEAD
the search function is still work in progress!
- fixed html errors

    1: # The LearningOnline Network with CAPA
    2: # Authoring Space Directory Lister
    3: #
    4: # $Id: lonpubdir.pm,v 1.162 2014/10/17 13:00:39 goltermann 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: 
   92:     if (!-d $fn) {
   93:         if (-e $fn) {
   94:             $r->print('<p class="LC_info">'.&mt('Requested item is a file not a directory.').'</p>');
   95:         } else {
   96:             $r->print('<p class="LC_info">'.&mt('The requested subdirectory does not exist.').'</p>');
   97:         }
   98:         $r->print(&Apache::loncommon::end_page());
   99:         return OK;
  100:     }
  101:     my @files;
  102:     if (opendir(DIR,$fn)) {
  103:         @files = grep(!/^\.+$/,readdir(DIR));
  104:         closedir(DIR);
  105:     } else {
  106:         $r->print('<p class="LC_error">'.&mt('Could not open directory.').'</p>');
  107:         $r->print(&Apache::loncommon::end_page());
  108:         return OK;
  109:     }
  110: 
  111:     &dircontrols($r,$uname,$udom,$thisdisfn);   # Put out actions for directory, 
  112:                                                 # browse/upload + new file page.
  113:     &resourceactions($r,$uname,$udom,$thisdisfn); # Put out form used for printing/deletion etc.
  114: 
  115:     my $numdir = 0;
  116:     my $numres = 0;
  117:   
  118:     if ((@files == 0) && ($thisdisfn =~ m{^/$match_domain/$match_username})) {
  119:         if ($thisdisfn =~ m{^/$match_domain/$match_username$}) {
  120:             $r->print('<p class="LC_info">'.&mt('This Authoring Space is currently empty.').'</p>');
  121:         } else {
  122:             $r->print('<p class="LC_info">'.&mt('This subdirectory is currently empty.').'</p>');
  123:         }
  124:         $r->print(&Apache::loncommon::end_page());
  125:         return OK;
  126:     }
  127: 
  128:     # Retrieving value for "sortby" and "sortorder" from QUERY_STRING
  129:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  130:         ['sortby','sortorder']);
  131: 
  132:     # Sort by name as default, not reversed
  133:     if (! exists($env{'form.sortby'})) { $env{'form.sortby'} = 'filename' }
  134:     if (! exists($env{'form.sortorder'})) { $env{'form.sortorder'} = '' }
  135:     my $sortby = $env{'form.sortby'};
  136:     my $sortorder = $env{'form.sortorder'};
  137: 
  138:     # Order in which columns are displayed from left to right
  139:     my @order = ('filetype','actions','filename','title',
  140:                     'pubstatus','cmtime','size');
  141: 
  142:     # Up and down arrows to indicate sort order
  143:     my @arrows = ('&nbsp;&#9650;','&nbsp;&#9660;','');
  144: 
  145:     # Default sort order and column title
  146:     my %columns = (
  147:         filetype =>     {
  148:                             order => 'ascending',
  149:                             text  => &mt('Type'),
  150:                         },
  151:         actions =>      {
  152:                             # Not sortable
  153:                             text  => &mt('Actions'),
  154:                         },
  155:         filename =>     {
  156:                             order => 'ascending',
  157:                             text  => &mt('Name'),
  158:                         },
  159:         title =>        {
  160:                             order => 'ascending',
  161:                             text  => &mt('Title'),
  162:                         },
  163:         pubstatus =>    {
  164:                             order => 'ascending',
  165:                             text  => &mt('Status'),
  166:                             colspan => '2',
  167:                         },
  168:         cmtime =>       {
  169:                             order => 'descending',
  170:                             text  => &mt('Last Modified'),
  171:                         },
  172:         size =>         {
  173:                             order => 'ascending',
  174:                             text  => &mt('Size').' (kB)',
  175:                         },
  176:     ); 
  177: 
  178:     # Print column headers
  179:     my $output = '';
  180:     foreach my $key (@order) {
  181:         my $idx;
  182:         # Append an up or down arrow to sorted column
  183:         if ($sortby eq $key) {
  184:             $idx = ($columns{$key}{order} eq 'ascending') ? 0:1;
  185:             if ($sortorder eq 'rev') { $idx ++; }
  186:             $idx = $idx%2;
  187:         } else { $idx = 2; } # No arrow if column is not sorted
  188:         $output .= (($columns{$key}{order}) ?
  189:             '<th'.($columns{$key}{colspan} ? ' colspan="'.$columns{$key}{colspan}.'"' : '')
  190:             .'><a href="'.$linkdir.'/?sortby='.$key.'&amp;sortorder='
  191:             .((($sortby eq $key) && ($sortorder ne 'rev')) ? 'rev' : '').'">'
  192:             .$columns{$key}{text}.$arrows[$idx].'</a></th>' :
  193:             '<th>'.$columns{$key}{text}.'</th>');
  194:     }
  195: 
  196: my $result = "<script type=\"text/javascript\">
  197:     sessionStorage.setItem('CSTRcache','".&prepareJsonData($uname,$udom,$thisdisfn)."');
  198:     localStorage.setItem('CSTRtrans', '".&prepareJsonTranslations()."');
  199: </script>";
  200:     $r->print($result);
  201: 
  202:     $r->print('<div id="currentFolder">'.&Apache::loncommon::start_data_table()
  203:         .'<tr><th colspan="8" id="searchtitle" style="display:none"></th></tr>'
  204:         .&Apache::loncommon::start_data_table_header_row() . $output
  205:         .&Apache::loncommon::end_data_table_header_row()
  206:     );
  207: 
  208:     my $dirptr=16384;		# Mask indicating a directory in stat.cmode.
  209:     my $filehash = {};
  210:     foreach my $filename (@files) {
  211:         # Skip .DS_Store, .DAV and hidden files
  212:         my ($extension) = ($filename=~/\.(\w+)$/);
  213:         next if (($filename eq '.DS_Store')
  214:                 || ($filename eq '.DAV')
  215:                 || (&Apache::loncommon::fileembstyle($extension) eq 'hdn')
  216:                 || ($filename =~ /^\._/));
  217: 
  218:         my ($cmode,$csize,$cmtime)=(stat($fn.'/'.$filename))[2,7,9];
  219:         my $linkfilename = &HTML::Entities::encode('/priv'.$thisdisfn.'/'.$filename,'<>&"');
  220:         # Identify type of file according to icon used
  221:         my ($filetype) = (&Apache::loncommon::icon($filename) =~ m{/(\w+).gif$}); 
  222:         my $cstr_dir = $r->dir_config('lonDocRoot').'/priv'.$thisdisfn;
  223:         my $meta_same = &isMetaSame($cstr_dir, $resdir, $filename);
  224:         
  225:         # Store size, title, and status for files but not directories
  226:         my $size = (!($cmode&$dirptr)) ? $csize/1024. : 0;
  227:         my ($status, $pubstatus, $title, $fulltitle);
  228:         if (!($cmode&$dirptr)) {
  229:             ($status, $pubstatus) = &getStatus($resdir, $targetdir, $cstr_dir, 
  230:                 $filename, $linkfilename, $cmtime, $meta_same);
  231:             ($fulltitle, $title) = &getTitle($resdir, $targetdir, $filename, 
  232:                                         $linkfilename, $meta_same, \%bombs);
  233:         } else {
  234:             ($status, $pubstatus) = ('','');
  235:             ($fulltitle, $title) = ('','');
  236:         }
  237: 
  238:         # This hash will allow sorting
  239:         $filehash->{ $filename } = {
  240:             "cmtime"            => $cmtime,
  241:             "size"              => $size,
  242:             "cmode"             => $cmode,
  243:             "filetype"          => $filetype,
  244:             "title"             => $title,
  245:             "fulltitle"         => $fulltitle,
  246:             "status"            => $status,
  247:             "pubstatus"         => $pubstatus,
  248:             "linkfilename"      => $linkfilename,
  249:         }
  250:     }
  251:    
  252:     my @sorted_files;
  253:     # Sorting by something other than "Name".  Name is the secondary key.
  254:     if ($sortby =~ m{cmtime|size}) {    # Numeric fields
  255:         # First check if order should be reversed
  256:         if ($sortorder eq "rev") {
  257:             @sorted_files = sort {
  258:                 $filehash->{$a}->{$sortby} <=> $filehash->{$b}->{$sortby}
  259:                     or
  260:                 uc($a) cmp uc($b)
  261:             } (keys(%{$filehash}));
  262:         } else {
  263:             @sorted_files = sort {
  264:                 $filehash->{$b}->{$sortby} <=> $filehash->{$a}->{$sortby}
  265:                     or
  266:                 uc($a) cmp uc($b)
  267:             } (keys(%{$filehash}));
  268:         }
  269:     } elsif ($sortby =~ m{filetype|title|status}) {     # String fields
  270:         if ($sortorder eq "rev") {
  271:             @sorted_files = sort {
  272:                 $filehash->{$b}->{$sortby} cmp $filehash->{$a}->{$sortby}
  273:                     or
  274:                 uc($a) cmp uc($b)
  275:             } (keys(%{$filehash}));
  276:         } else {
  277:             @sorted_files = sort {
  278:                 $filehash->{$a}->{$sortby} cmp $filehash->{$b}->{$sortby}
  279:                     or
  280:                 uc($a) cmp uc($b)
  281:             } (keys(%{$filehash}));
  282:         }
  283: 
  284:     # Sort by "Name" is the default
  285:     } else { 
  286:         if ($sortorder eq "rev") {
  287:             @sorted_files = sort {uc($b) cmp uc($a)} (keys(%{$filehash}));
  288:         } else {
  289:             @sorted_files = sort {uc($a) cmp uc($b)} (keys(%{$filehash}));
  290:         }
  291:     }
  292: 
  293:     # Print the sorted resources
  294:     foreach my $filename (@sorted_files) {
  295:         if ($filehash->{$filename}->{"cmode"}&$dirptr) {        # Directories
  296:             &putdirectory($r, $thisdisfn, $linkdir, $filename, 
  297:                 $filehash->{$filename}->{"cmtime"}, 
  298:                 $targetdir, \%bombs, \$numdir);
  299:         } else {                                                # Files
  300:             &putresource($r, $udom, $uname, $filename, $thisdisfn, $resdir,
  301:                 $targetdir, $linkdir, $filehash->{$filename}->{"cmtime"}, 
  302:                 $filehash->{$filename}->{"size"}, \$numres, 
  303:                 $filehash->{$filename}->{"linkfilename"},
  304:                 $filehash->{$filename}->{"fulltitle"},
  305:                 $filehash->{$filename}->{"status"},
  306:                 $filehash->{$filename}->{"pubstatus"});
  307:         }
  308:     }
  309: 
  310:   $r->print(&Apache::loncommon::end_data_table()
  311:            .'</div><div id="otherplaces" style="display:none">'
  312:            .&Apache::loncommon::start_data_table()
  313:            .'<tr><th colspan="7">'.&mt('Results in other directories:').'</th></tr>'
  314:            .'<tr class="LC_header_row" id="otherplacestable">'
  315:            .'<th>'.&mt('Type').'</th>'
  316:            .'<th>'.&mt('Directory').'</th>'
  317:            .'<th>'.&mt('Name').'</th>'
  318:            .'<th>'.&mt('Title').'</th>'
  319:            .'<th colspan="2">'.&mt('Status').'</th>'
  320:            .'<th>'.&mt('Last Modified').'</th>'
  321:            .'</tr>'
  322:            .&Apache::loncommon::end_data_table()
  323:            .'</div>'
  324:            .&Apache::loncommon::end_page()
  325:   );
  326:   return OK;  
  327: }
  328: 
  329: 
  330: 
  331: #   Output the header of the page.  This includes:
  332: #   - The HTML header 
  333: #   - The H1/H3  stuff which includes the directory.
  334: #
  335: #     startpage($r, $uame, $udom, $thisdisfn);
  336: #      $r     - The apache request object.
  337: #      $uname - User name.
  338: #      $udom  - Domain name the user is logged in under.
  339: #      $thisdisfn - Displayable version of the filename.
  340: 
  341: sub startpage {
  342:     my ($r, $uname, $udom, $thisdisfn) = @_;
  343:     &Apache::loncommon::content_type($r,'text/html');
  344:     $r->send_http_header;
  345: 
  346:     my $formaction='/priv'.$thisdisfn.'/';
  347:     $formaction=~s|/+|/|g;
  348:     &Apache::lonhtmlcommon::store_recent('construct',$formaction,$formaction);
  349: 
  350:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  351:     &Apache::lonhtmlcommon::add_breadcrumb({
  352:         'text'  => 'Authoring Space',
  353:         'href'  => &Apache::loncommon::authorspace($formaction),
  354:     });
  355:     # breadcrumbs (and tools) will be created 
  356:     # in start_page->bodytag->innerregister
  357: 
  358:     $env{'request.noversionuri'}=$formaction;
  359:     $r->print(&Apache::loncommon::start_page('Authoring Space'));
  360: 
  361:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
  362:     my $current_disk_usage = &Apache::lonnet::diskusage($udom,$uname,"$londocroot/priv/$udom/$uname");
  363:     my $disk_quota = &Apache::loncommon::get_user_quota($uname,$udom,'author'); #expressed in MB
  364:     $disk_quota = 1000 * $disk_quota; # convert from MB to kB
  365: 
  366:     $r->print(&Apache::loncommon::head_subbox(
  367:                      '<div style="float:right;padding-top:0;margin-top;0">'
  368:                     .&Apache::lonhtmlcommon::display_usage($current_disk_usage,$disk_quota)
  369:                     .'</div>'
  370:                     .&Apache::loncommon::CSTR_pageheader()));
  371: 
  372:     my $esc_thisdisfn = &Apache::loncommon::escape_single($thisdisfn);
  373:     my $doctitle = 'LON-CAPA '.&mt('Authoring Space');
  374:     my $newname = &mt('New Name');
  375:     my $pubdirscript=(<<ENDPUBDIRSCRIPT);
  376: <script type="text/javascript">
  377: top.document.title = '$esc_thisdisfn/ - $doctitle';
  378: // Store directory location for menu bar to find
  379: 
  380: parent.lastknownpriv='/priv$esc_thisdisfn/';
  381: 
  382: // Confirmation dialogues
  383: 
  384:     function currdiract(theform) {
  385:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == 'publish') {
  386:             document.publishdir.filename.value = theform.filename.value;
  387: 	    document.publishdir.submit();
  388:         }
  389:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == 'editmeta') {
  390:             top.location=theform.filename.value+'default.meta'
  391:         }
  392:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == 'printdir' ) {
  393:             document.printdir.postdata.value=theform.filename.value
  394:             document.printdir.submit();
  395:         }
  396:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == "delete") {
  397:               var delform = document.delresource
  398:               delform.filename.value = theform.filename.value
  399:               delform.submit()
  400:         }
  401:     }
  402:   
  403:     function checkUpload(theform) {
  404:         if (theform.file == '') {
  405:             alert("Please use 'Browse..' to choose a file first, before uploading")
  406:             return 
  407:         }
  408:         theform.submit()  
  409:     }
  410: 
  411:     function SetPubDir(theform,printForm) {
  412:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "open") {
  413:             top.location = theform.openname.value
  414:             return
  415:         }
  416:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "publish") {
  417:             theform.submit();
  418:         }
  419:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "editmeta") {
  420:             top.location=theform.filename.value+'default.meta'
  421:         }
  422:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "printdir") {
  423:             theform.action = '/adm/printout'
  424:             theform.postdata.value = theform.filename.value
  425:             theform.submit()
  426:         }
  427:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "delete") {
  428:               var delform = document.delresource
  429:               delform.filename.value = theform.filename.value
  430:               delform.submit()
  431:         }
  432:         return
  433:     }
  434:     function SetResChoice(theform) {
  435:       var activity = theform.reschoice.options[theform.reschoice.selectedIndex].value
  436:       if ((activity == 'rename') || (activity == 'copy') || (activity == 'move')) {
  437:           changename(theform,activity)
  438:       }
  439:       if (activity == 'publish') {
  440:           var pubform = document.pubresource
  441:           pubform.filename.value = theform.filename.value
  442:           pubform.submit()
  443:       }
  444:       if (activity == 'delete') {
  445:           var delform = document.delresource
  446:           delform.filename.value = theform.filename.value
  447:           delform.submit()
  448:       }
  449:       if (activity == 'obsolete') {
  450:           var pubform = document.pubresource
  451:           pubform.filename.value = theform.filename.value
  452:           pubform.makeobsolete.value=1;
  453:           pubform.submit()
  454:       }
  455:       if (activity == 'print') {
  456:           document.printresource.postdata.value = theform.filename.value
  457:           document.printresource.submit()
  458:       }
  459:       if (activity == 'retrieve') {
  460:           document.retrieveres.filename.value = theform.filename.value
  461:           document.retrieveres.submit()
  462:       }
  463:       if (activity == 'cleanup') {
  464:           document.cleanup.filename.value = theform.filename.value
  465:           document.cleanup.submit()
  466:       }
  467:       return
  468:     }
  469:     function changename(theform,activity) {
  470:         var oldname=theform.dispfilename.value;
  471:         var newname=prompt('$newname',oldname);
  472:         if (newname == "" || !newname || newname == oldname)  {
  473:             return
  474:         }
  475:         document.moveresource.newfilename.value = newname
  476:         document.moveresource.filename.value = theform.filename.value
  477:         document.moveresource.action.value = activity
  478:         document.moveresource.submit();
  479:     }
  480: </script>
  481: ENDPUBDIRSCRIPT
  482:     $r->print($pubdirscript);
  483: }
  484: 
  485: sub dircontrols {
  486:     my ($r,$uname,$udom,$thisdisfn) = @_;
  487:     my %lt=&Apache::lonlocal::texthash(
  488:                                        cnpd => 'Cannot publish directory',
  489:                                        cnrd => 'Cannot retrieve directory',
  490:                                        mcdi => 'Must create new subdirectory inside a directory',
  491:                                        pubr => 'Publish this Resource',
  492:                                        pubd => 'Publish this Directory',
  493:                                        dedr => 'Delete Directory',
  494:                                        rtrv => 'Retrieve Old Version',
  495:                                        list => 'List Directory',
  496:                                        uplo => 'Upload file',  
  497:                                        dele => 'Delete',
  498:                                        edit => 'Edit Metadata', 
  499:                                        sela => 'Select Action',
  500:                                        nfil => 'New file',
  501:                                        nhtm => 'New HTML file',
  502:                                        nprb => 'New problem',
  503:                                        npag => 'New assembled page',
  504:                                        nseq => 'New assembled sequence',
  505:                                        ncrf => 'New custom rights file',
  506:                                        nsty => 'New style file',
  507:                                        nlib => 'New library file',
  508:                                        nbt  => 'New bridgetask file',
  509:                                        nsub => 'New subdirectory',
  510:                                        renm => 'Rename current file to',
  511:                                        move => 'Move current file to',
  512:                                        copy => 'Copy current file to',
  513:                                        type => 'Type Name Here',
  514:                                        go   => 'Go',
  515:                                        prnt => 'Print contents of directory',
  516:                                        crea => 'Create a new directory or LON-CAPA document',
  517:                                        qs   => 'Quick Search',
  518:                                        cs   => 'Case Sensitive',
  519:                                        re   => 'Regular Expression',
  520: 				       acti => 'Actions for current directory',
  521: 				       updc => 'Upload a new document',
  522: 				       pick => 'Please select an action to perform using the new filename',
  523:                                       );
  524:     my $mytype = $lt{'type'}; # avoid conflict with " and ' in javascript
  525:     $r->printf(<<END,&Apache::loncommon::help_open_topic('Quicksearch'));
  526: <div class="LC_columnSection">
  527:   <div>
  528:     <form name="curractions" method="post" action="">
  529:       <fieldset>
  530:         <legend>$lt{'acti'}</legend>
  531:         <select name="dirtask" onchange="currdiract(this.form)">
  532:             <option>$lt{'sela'}</option>
  533:             <option value="publish">$lt{'pubd'}</option>
  534:             <option value="editmeta">$lt{'edit'}</option>
  535:             <option value="printdir">$lt{'prnt'}</option>
  536:             <option value="delete">$lt{'dedr'}</option>
  537:         </select>
  538:         <input type="hidden" name="filename" value="/priv$thisdisfn/" />
  539:       </fieldset>
  540:     </form>
  541:     <form name="publishdir" method="post" action="/adm/publish" target="_parent">
  542:       <input type="hidden" name="pubrec" value="" />
  543:       <input type="hidden" name="filename" value="" />
  544:     </form>
  545:     <form name="printdir" method="post" action="/adm/printout" target="_parent">
  546:       <input type="hidden" name="postdata" value="" />
  547:     </form>
  548:   </div>
  549: 
  550:   <div>
  551:     <form name="upublisher" enctype="multipart/form-data" method="post" action="/adm/upload" target="_parent">
  552:       <fieldset>
  553:         <legend>$lt{'updc'}</legend>
  554:         <input type="hidden" name="filename" value="/priv$thisdisfn/" />
  555:         <input type="file" name="upfile" size="20" />
  556:         <input type="button" value="$lt{'uplo'}"  onclick="checkUpload(this.form)" />
  557:       </fieldset>
  558:     </form>
  559:   </div>
  560: 
  561:   <div>
  562:     <form name="fileaction" method="post" action="/adm/cfile" target="_parent">
  563:       <fieldset>
  564:               <legend>$lt{'crea'}</legend>
  565: 	      <span class="LC_nobreak">
  566: 		<input type="hidden" name="filename" value="/priv$thisdisfn/" />
  567:                   <script type="text/javascript">
  568:                     function validate_go() {
  569:                         var selected = document.fileaction.action.selectedIndex;
  570:                         if (selected == 0) {
  571:                             alert('$lt{'pick'}');
  572:                         } else {
  573:                             document.fileaction.submit();
  574:                         }
  575:                     }
  576:                   </script>
  577: 		  <select name="action">
  578: 		    <option value="none">$lt{'sela'}</option>
  579: 		    <option value="newfile">$lt{'nfil'}:</option>
  580: 		    <option value="newhtmlfile">$lt{'nhtm'}:</option>
  581: 		    <option value="newproblemfile">$lt{'nprb'}:</option>
  582:                     <option value="newpagefile">$lt{'npag'}:</option>
  583:                     <option value="newsequencefile">$lt{'nseq'}:</option>
  584:                     <option value="newrightsfile">$lt{'ncrf'}:</option>
  585:                     <option value="newstyfile">$lt{'nsty'}:</option>
  586:                     <option value="newtaskfile">$lt{'nbt'}:</option>
  587:                     <option value="newlibraryfile">$lt{'nlib'}:</option>
  588: 	            <option value="newdir">$lt{'nsub'}:</option>
  589: 		  </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();" />
  590: 		 </span>
  591:       </fieldset>
  592:     </form>
  593:     </div>
  594:     <div>
  595:       <fieldset style="display:inline">
  596:             <legend>$lt{'qs'}</legend>
  597:                 <script type="text/javascript" src="/adm/quicksearch/quicksearch.js"></script>
  598:                 <input type="text" id="quickfilter" placeholder="Enter search term" onkeyup="applyFilter()"/>
  599:                 <input type="button" value="Clear" onclick="document.getElementById(\'quickfilter\').value=\'\'; applyFilter()" />
  600:                 %s
  601:                 <br />
  602:                 <label><input type="checkbox" id="casesens" onchange="applyFilter()"/>$lt{'cs'}&nbsp;&nbsp;</label>
  603:                 <label><input type="checkbox" id="regex" onchange="applyFilter()"/>$lt{'re'}&nbsp;&nbsp;</label>
  604:         </fieldset>
  605:   </div>
  606: </div>
  607: END
  608: }
  609: 
  610: sub resourceactions {
  611:     my ($r,$uname,$udom,$thisdisfn) = @_;
  612:     $r->print(<<END);
  613:        <form name="moveresource" action="/adm/cfile" target="_parent" method="post">
  614:          <input type="hidden" name="filename" value="" />
  615:          <input type="hidden" name="newfilename" value="" />
  616:          <input type="hidden" name="action" value="" />
  617:        </form>
  618:        <form name="delresource" action="/adm/cfile" target="_parent" method="post">
  619:          <input type="hidden" name="filename" value="" />
  620:          <input type="hidden" name="action" value="delete" />
  621:        </form>
  622:        <form name="pubresource" action="/adm/publish" target="_parent" method="post">
  623:          <input type="hidden" name="filename" value="" />
  624:          <input type="hidden" name="makeobsolete" value="0" />
  625:        </form>
  626:        <form name="printresource" action="/adm/printout" target="_parent" method="post">
  627:            <input type="hidden" name="postdata" value="" />
  628:        </form>
  629:        <form name="retrieveres" action="/adm/retrieve" target="_parent" method="post">
  630:            <input type="hidden" name="filename" value="" />
  631:        </form>
  632:        <form name="cleanup" action="/adm/cleanup" target="_parent" method="post">
  633:            <input type="hidden" name="filename" value="" />
  634:        </form>
  635: END
  636: }
  637: 
  638: #
  639: #   Get the title string or "[untitled]" if the file has no title metadata:
  640: #   Without the latter substitution, it's impossible to examine metadata for
  641: #   untitled resources.  Resources may be legitimately untitled, to prevent
  642: #   searches from locating them.
  643: #
  644: #   $str = getTitleString($fullname);
  645: #       $fullname - Fully qualified filename to check.
  646: #
  647: sub getTitleString {
  648:     my $fullname = shift;
  649:     my $title    = &Apache::lonnet::metadata($fullname, 'title');
  650: 
  651:     unless ($title) {
  652: 	$title = "[".&mt('untitled')."]";
  653:     }
  654:     return $title;
  655: }
  656: 
  657: sub getCopyRightString {
  658:     my $fullname = shift;
  659:     return &Apache::lonnet::metadata($fullname, 'copyright');
  660: }
  661: 
  662: sub getSourceRightString {
  663:     my $fullname = shift;
  664:     return &Apache::lonnet::metadata($fullname, 'sourceavail');
  665: }
  666: #
  667: #  Put out a directory table row:
  668: #    putdirectory(r, base, here, dirname, modtime, targetdir, bombs, numdir)
  669: #      r         - Apache request object.
  670: #      reqfile   - File in request.
  671: #      here      - Where we are in directory tree.
  672: #      dirname   - Name of directory special file.
  673: #      modtime   - Encoded modification time.
  674: #      targetdir - Publication target directory.
  675: #      bombs     - Reference to hash of URLs with runtime error messages.
  676: #      numdir    - Reference to scalar used to track number of sub-directories
  677: #                  in directory (used in form name for each "actions" dropdown).
  678: #
  679: sub putdirectory {
  680:     my ($r, $reqfile, $here, $dirname, $modtime, $targetdir, $bombs, $numdir) = @_;
  681: 
  682: # construct the display filename: the directory name unless ..:
  683:    
  684:     my $actionitem;
  685:  
  686:     my $disfilename = $dirname;
  687: # Don't display directory itself, and there is no way up from root directory
  688:     unless ((($dirname eq '..') && ($reqfile=~/^\/[^\/]+\/[^\/]+$/)) || ($dirname eq '.')) {
  689:         my $kaputt=0;
  690:         if (ref($bombs) eq 'HASH') {
  691:             foreach my $key (keys(%{$bombs})) {
  692:                 my $currentdir = &Apache::lonnet::declutter("$targetdir/$disfilename");
  693:                 if (($key) =~ m{^\Q$currentdir\E/}) { $kaputt=1; last; }
  694:             }
  695:         }
  696: #
  697: # Get the metadata from that directory's default.meta to display titles
  698: #
  699: 	%Apache::lonpublisher::metadatafields=();
  700: 	%Apache::lonpublisher::metadatakeys=();
  701: 	&Apache::lonpublisher::metaeval(
  702:                  &Apache::lonnet::getfile($r->dir_config('lonDocRoot').$here.'/'.$dirname.'/default.meta')
  703:                                        );
  704:         if ($dirname eq '..') {
  705:             $actionitem = &mt('Go to ...');
  706:             $disfilename = '<i>'.&mt('Parent Directory').'</i>';
  707:         } else {
  708:             $actionitem = 
  709:                     '<form name="dirselect_'.$$numdir.
  710:                     '" action="/adm/publish" target="_parent">'.
  711:                     '<select name="diraction" onchange="SetPubDir(this.form,document)">'.
  712:                       '<option selected="selected">'.&mt('Select action').'</option>'.
  713:                       '<option value="open">'.&mt('Open').'</option>'.
  714:                       '<option value="publish">'.&mt('Publish').'</option>'.
  715:                       '<option value="editmeta">'.&mt('Edit Metadata').'</option>'.
  716:                       '<option value="printdir">'.&mt('Print directory').'</option>'.
  717:                       '<option value="delete">'.&mt('Delete directory').'</option>'.
  718:                     '</select>'.
  719:                      '<input type="hidden" name="filename" value="'.&HTML::Entities::encode($here.'/'.$dirname,'<>&"').'/" />'.
  720:                      '<input type="hidden" name="openname" value="'.$here.'/'.$dirname.'/" />'.
  721:                      '<input type="hidden" name="postdata" value="" />'.
  722:                    '</form>';
  723:             $$numdir ++;
  724:         }
  725: 	$r->print('<tr class="LC_browser_folder">'.
  726: 		  '<td><img src="'.
  727: 		  $Apache::lonnet::perlvar{'lonIconsURL'}.'/navmap.folder.closed.gif" alt="folder" /></td>'.
  728: 		  '<td>'.$actionitem.'</td>'.
  729: 		  '<td><span class="LC_filename"><a href="'.&HTML::Entities::encode($here.'/'.$dirname,'<>&"').'/" target="_parent">'.
  730: 		  $disfilename.'</a></span></td>'.
  731: 		        '<td colspan="3">'.($kaputt?&Apache::lonhtmlcommon::authorbombs($targetdir.'/'.$disfilename.'/'):'').$Apache::lonpublisher::metadatafields{'title'});
  732: 	if ($Apache::lonpublisher::metadatafields{'subject'} ne '') {
  733: 	    $r->print(' <i>'.
  734: 		      $Apache::lonpublisher::metadatafields{'subject'}.
  735: 		      '</i> ');
  736: 	}
  737: 	$r->print($Apache::lonpublisher::metadatafields{'keywords'}.'</td>'.
  738: 		  '<td>'.&Apache::lonlocal::locallocaltime($modtime).'</td>'.
  739: 	          '<td></td>'.
  740: 		  "</tr>\n");
  741:     }
  742:     return;
  743: }
  744: 
  745: sub getTitle {
  746:     my ($resdir, $targetdir, $filename, $linkfilename, $meta_same, $bombs) = @_;
  747:     my $title='';
  748:     my $titleString = &getTitleString($targetdir.'/'.$filename);
  749:     if (-e $resdir.'/'.$filename) {
  750: 	$title = '<a href="'.$targetdir.'/'.$filename.
  751: 	    '.meta" target="cat">'.$titleString.'</a>';
  752:         if (!$meta_same) {
  753: 	    $title = &mt('Metadata Modified').'<br />'.$title.
  754: 		'<br />'.
  755:                 &Apache::loncommon::modal_link(
  756:                     '/adm/diff?filename='.$linkfilename.'.meta'.'&amp;versiontwo=priv',
  757:                     &mt('Metadata Diffs'),600,500);
  758: 	    $title.="\n".'<br />'.
  759:                 &Apache::loncommon::modal_link(
  760:                     '/adm/retrieve?filename='.$linkfilename.'.meta&amp;inhibitmenu=yes&amp;add_modal=yes',
  761:                     &mt('Retrieve Metadata'),600,500);
  762:         } 
  763:     }
  764:     # Allow editing metadata of published and unpublished resources
  765:     $title .= "\n".'<br />' if ($title);
  766:     $title .= '<a href="'.$linkfilename.'.meta">'.
  767:               ($$bombs{&Apache::lonnet::declutter($targetdir.'/'.$filename)}?
  768:                   '<img src="/adm/lonMisc/bomb.gif" border="0" alt="'.&mt('bomb').'" />':
  769:                   &mt('Edit Metadata')).
  770:               '</a>';
  771: 
  772:     return ($title, $titleString);
  773: }
  774: 
  775: 
  776: sub isMetaSame {
  777:     my ($cstr_dir, $resdir, $filename) = @_;
  778:     my $meta_cmtime = (stat($cstr_dir.'/'.$filename.'.meta'))[9];
  779:     my $meta_rmtime = (stat($resdir.'/'.$filename.'.meta'))[9];
  780:     return (&Apache::londiff::are_different_files($resdir.'/'.$filename.'.meta',
  781:             $cstr_dir.'/'.$filename.'.meta') && $meta_rmtime < $meta_cmtime) 
  782:         ? 0 : 1;
  783: }
  784:     
  785: 
  786: sub getStatus {    
  787:     my ($resdir, $targetdir, $cstr_dir, $filename,  
  788:             $linkfilename, $cmtime, $meta_same) = @_;
  789:     my $pubstatus = 'unpublished';
  790:     my $status = &mt('Unpublished');
  791: 
  792:     if (-e $resdir.'/'.$filename) {
  793:         my $same = 0;
  794:         if ((stat($resdir.'/'.$filename))[9] >= $cmtime) {
  795:             $same = 1;
  796:         } else {
  797:            if (&Apache::londiff::are_different_files($resdir.'/'.$filename,
  798: 						     $cstr_dir.'/'.$filename)) {
  799:               $same = 0;
  800:            } else {
  801:               $same = 1;
  802:            }
  803:         }
  804: 
  805:         my $rights_status =
  806:             &mt(&getCopyRightString($targetdir.'/'.$filename)).', ';
  807: 
  808:         my %lt_SourceRight = &Apache::lonlocal::texthash(
  809:                'open'   => 'Source: open',
  810:                'closed' => 'Source: closed',
  811:         );
  812:         $rights_status .=
  813:             $lt_SourceRight{&getSourceRightString($targetdir.'/'.$filename)};
  814: 
  815: 	if ($same) {
  816: 	    if (&Apache::lonnet::metadata($targetdir.'/'.$filename,'obsolete')) {
  817:                 $pubstatus = 'obsolete';
  818: 		$status=&mt('Obsolete');
  819:             } else {
  820: 		if (!$meta_same) {
  821: 		    $pubstatus = 'metamodified';
  822: 		} else {
  823: 		    $pubstatus = 'published';
  824: 		}
  825: 		$status=&mt('Published').
  826: 		    '<br />'. $rights_status;
  827: 	    }
  828: 	} else {
  829:             $pubstatus = 'modified';
  830: 	    $status=&mt('Modified').
  831: 		'<br />'. $rights_status;
  832: 	    if (&Apache::loncommon::fileembstyle(($filename=~/\.(\w+)$/)) eq 'ssi') {
  833: 		$status.='<br />'.
  834:                          &Apache::loncommon::modal_link(
  835:                              '/adm/diff?filename='.$linkfilename.'&amp;versiontwo=priv',
  836:                              &mt('Diffs'),600,500);
  837: 	    }
  838: 	} 
  839: 
  840: 	$status.="\n".'<br />'.
  841:              &Apache::loncommon::modal_link(
  842:                  '/adm/retrieve?filename='.$linkfilename.'&amp;inhibitmenu=yes&amp;add_modal=yes',&mt('Retrieve'),600,500);
  843:     }
  844: 
  845:     return ($status, $pubstatus);
  846: }
  847: 
  848: 
  849: #
  850: #   Put a table row for a file resource.
  851: #
  852: sub putresource {
  853:     my ($r, $udom, $uname, $filename, $thisdisfn, $resdir, $targetdir, 
  854:             $linkdir, $cmtime, $size, $numres, $linkfilename, $title, 
  855:             $status, $pubstatus) = @_;
  856:     &Apache::lonnet::devalidate_cache_new('meta',$targetdir.'/'.$filename);
  857: 
  858:     my $editlink='';
  859:     my $editlink2='';
  860:     if ($filename=~/\.(xml|html|htm|xhtml|xhtm|sty)$/) {
  861: 	$editlink=' <br />(<a href="'.$linkdir.'/'.$filename.'?editmode=Edit&amp;problemmode=edit">'.&mt('Edit').'</a>)';
  862:     }
  863:     if ($filename=~/$LONCAPA::assess_re/) {
  864: 	$editlink=' (<a href="'.$linkdir.'/'.$filename.'?editmode=Edit&amp;problemmode=editxml">'.&mt('EditXML').'</a>)';
  865: 	$editlink2=' <br />(<a href="'.$linkdir.'/'.$filename.'?editmode=Edit&amp;problemmode=edit">'.&mt('Edit').'</a>)';
  866:     }
  867:     if ($filename=~/\.(problem|exam|quiz|assess|survey|form|library|xml|html|htm|xhtml|xhtm|sty)$/) {
  868: 	$editlink.=' (<a href="/adm/cleanup?filename='.$linkfilename.'" target="_parent">'.&mt('Clean Up').')</a>';
  869:     }
  870:     if ($filename=~/\.(zip|tar|bz2|gz|tar.gz|tar.bz2|tgz)$/) {
  871: 	$editlink=' (<a target="_parent" href="/adm/cfile?decompress='.$linkfilename.'">'.&mt('Decompress').'</a>)';
  872:     }
  873:     my $publish_button = (-e $resdir.'/'.$filename) ? &mt('Re-publish') : &mt('Publish');
  874:     my $pub_select = '';
  875:     &create_pubselect($r,\$pub_select,$udom,$uname,$thisdisfn,$filename,$resdir,$pubstatus,$publish_button,$numres);
  876:     $r->print(&Apache::loncommon::start_data_table_row().
  877: 	      '<td>'.($filename=~/[\#\~]$/?'&nbsp;':
  878: 		      '<img src="'.&Apache::loncommon::icon($filename).'" alt="" />').'</td>'.
  879:               '<td>'.$pub_select.'</td>'.
  880: 	      '<td><span class="LC_filename">'.
  881: 	      '<a href="'.$linkdir.'/'.$filename.'" target="_parent">'.
  882:                $filename.'</a></span>'.$editlink2.$editlink.
  883: 	      '</td>'.
  884: 	      '<td>'.$title.'</td>'.
  885:               '<td class="LC_browser_file_'.$pubstatus.'">&nbsp;&nbsp;</td>'. # Display publication status
  886:               '<td>'.$status.'</td>'.
  887: 	      '<td>'.&Apache::lonlocal::locallocaltime($cmtime).'</td>'.
  888: 	      '<td>'.sprintf("%.1f",$size).'</td>'.
  889: 	      &Apache::loncommon::end_data_table_row()
  890:     );
  891:     return;
  892: }
  893: 
  894: sub create_pubselect {
  895:     my ($r,$pub_select,$udom,$uname,$thisdisfn,$filename,$resdir,$pubstatus,$publish_button,$numres) = @_;
  896:     $$pub_select = '
  897: <form name="resselect_'.$$numres.'" action="">
  898: <select name="reschoice"  onchange="SetResChoice(this.form)">
  899: <option>'.&mt('Select action').'</option>'.
  900: '<option value="copy">'.&mt('Copy').'</option>';
  901:     if ($pubstatus eq 'obsolete' || $pubstatus eq 'unpublished') {
  902:         $$pub_select .= 
  903: '<option value="rename">'.&mt('Rename').'</option>'.
  904: '<option value="move">'.&mt('Move').'</option>'.
  905: '<option value="delete">'.&mt('Delete').'</option>';
  906:     } else {
  907:         $$pub_select .= '
  908: <option value="obsolete">'.&mt('Mark obsolete').'</option>';
  909:     }
  910: # check for versions
  911:     my $versions = &check_for_versions($r,'/'.$filename,$udom,$uname);
  912:     if ($versions > 0) {
  913:         $$pub_select .='
  914: <option value="retrieve">'.&mt('Retrieve old version').'</option>';
  915:     }
  916:     $$pub_select .= '
  917: <option value="publish">'.$publish_button.'</option>'.
  918: '<option value="cleanup">'.&mt('Clean up').'</option>'.
  919: '<option value="print">'.&mt('Print').'</option>'.
  920: '</select>
  921: <input type="hidden" name="filename" value="/priv'.
  922:  &HTML::Entities::encode($thisdisfn.'/'.$filename,'<>&"').'" />
  923:  <input type="hidden" name="dispfilename" value="'.
  924:  &HTML::Entities::encode($filename).'" /></form>';
  925:     $$numres ++;
  926: }
  927: 
  928: sub check_for_versions {
  929:     my ($r,$fn,$udom,$uname) = @_;
  930:     my $versions = 0;
  931:     my $docroot=$r->dir_config('lonDocRoot');
  932:     my $resfn=$docroot.'/res/'.$udom.'/'.$uname.$fn;
  933:     my $resdir=$resfn;
  934:     $resdir=~s/\/[^\/]+$/\//;
  935:     $fn=~/\/([^\/]+)\.(\w+)$/;
  936:     my $main=$1;
  937:     my $suffix=$2;
  938:     opendir(DIR,$resdir);
  939:     while (my $filename=readdir(DIR)) {
  940:         if ($filename=~/^\Q$main\E\.(\d+)\.\Q$suffix\E$/) {
  941:             $versions ++;        
  942:         }
  943:     }
  944:     closedir(DIR);
  945:     return $versions;
  946: }
  947: 
  948: sub prepareJsonTranslations {
  949:     my $json = 
  950:         '{"translations":{'.
  951:             '"edit":"'.&mt('Edit').'",'.
  952:             '"editxml":"'.&mt('EditXML').'",'.
  953:             '"editmeta":"'.&mt('Edit Metadata').'",'.
  954:             '"obsolete":"'.&mt('Obsolete').'",'.
  955:             '"modified":"'.&mt('Modified').'",'.
  956:             '"published":"'.&mt('Published').'",'.
  957:             '"unpublished":"'.&mt('Unpublished').'",'.
  958:             '"diff":"'.&mt('Diff').'",'.
  959:             '"retrieve":"'.&mt('Retrieve').'",'.
  960:             '"directory":"'.&mt('Directory').'",'.
  961:             '"results":"'.&mt('Show results for keyword:').'"'.
  962:         '}}';
  963: }
  964: 
  965: # gathers all files in the working directory except the ones that are already on screen
  966: sub prepareJsonData {
  967:     my ($uname, $udom, $pathToSkip) = @_;
  968:     my $path = "/home/httpd/html/priv/$udom/$uname/";
  969: 
  970:     # maximum number of entries, to limit workload and required storage space
  971:     my $entries = 100;
  972:     my $firstfile = 1;
  973:     my $firstdir = 1;
  974: 
  975:     my $json = '{"resources":[';
  976:     $json .= &prepareJsonData_rec($path, \$entries, \$firstfile, \$firstdir, $pathToSkip);
  977:     $json .= ']}';
  978: 
  979:     # if the json string is invalid the whole search breaks.
  980:     # so we want to make sure that the string is valid in any case.
  981:     $json =~ s/,\s*,/,/g;
  982:     $json =~ s/\}\s*\{/\},\{/g;
  983:     $json =~ s/\}\s*,\s*\]/\}\]/g;
  984:     return $json;
  985: }
  986: 
  987: # recursive part of json file gathering
  988: sub prepareJsonData_rec {
  989:     my ($path, $entries, $firstfile, $firstdir, $pathToSkip) = @_;
  990:     my $json;
  991:     my $skipThisFolder = $path =~ m/$pathToSkip\/$/?1:0;
  992: 
  993:     my @dirs;
  994:     my @resources;
  995:     my @ignored = qw(bak log meta save . ..);
  996: 
  997: # Phase 1: Gathering
  998:     opendir(DIR,$path);
  999:     my @files=sort {uc($a) cmp uc($b)} (readdir(DIR));
 1000:     foreach my $filename (@files) {
 1001:         next if ($filename eq '.DS_Store');
 1002: 
 1003:         # gather all resources
 1004:         if ($filename !~ /\./) {
 1005:             # its a folder
 1006:             push(@dirs, $filename);
 1007:         } else {
 1008:             # only push files we dont want to ignore
 1009:             next if ($skipThisFolder);
 1010: 
 1011:             $filename =~ /\.(\w+?)$/;
 1012:             unless (grep /$1/, @ignored) {
 1013:                 push(@resources, $filename);
 1014:             }
 1015:         }
 1016:     }
 1017:     closedir(DIR);
 1018:     # nothing to do here if both lists are empty
 1019:     return unless ( @dirs || @resources );
 1020:     
 1021: # Phase 2: Working
 1022:     $$firstfile = 1;
 1023: 
 1024:     foreach (@dirs) {
 1025:         $json .= '{"name":"'.$_.'",'.
 1026:                   '"path":"'.$path.$_.'",'.
 1027:                   '"title":"",'.
 1028:                   '"status":"",'.
 1029:                   '"cmtime":""},';
 1030:     }
 1031: 
 1032:     foreach (@resources) {
 1033:         last if ($$entries < 1);
 1034:         my $title = &getTitleString($path.$_);
 1035: 
 1036:         my $privpath = $path.$_;
 1037:         my $respath = $privpath;
 1038:         $respath =~ s/httpd\/html\/priv\//httpd\/html\/res\//;
 1039: 
 1040:         my $cmtime = (stat($privpath))[9];
 1041:         my $rmtime = (stat($respath))[9];
 1042: 
 1043:         unless ($$firstfile) { $json .= ','; } else { $$firstfile = 0; }
 1044: 
 1045:         my $status = 'unpublished';
 1046: 
 1047:         # if a resource is published, the published version (/html/res/filepath) gets its own modification time
 1048:         # this is newer or equal then the version in your authoring space (/html/priv/filepath)
 1049:         if ($rmtime >= $cmtime) {
 1050:             # obsolete
 1051:             if (&Apache::lonnet::metadata($respath, 'obsolete')) {
 1052:                 $status = 'obsolete';
 1053:             }else{
 1054:                 $status = 'published';
 1055:             }
 1056:         } else {
 1057:             $status = 'modified';
 1058:         }
 1059: 
 1060:         $json .= '{"name":"'.$_.'",'.
 1061:                   '"path":"'.$path.'",'.
 1062:                   '"title":"'.$title.'",'.
 1063:                   '"status":"'.$status.'",'.
 1064:                   '"cmtime":"'.&Apache::lonlocal::locallocaltime($cmtime).'"}';
 1065:         $$entries--;
 1066:     }
 1067: 
 1068:     foreach(@dirs) {
 1069:         next if ($$entries < 1);
 1070:         $json .= ',';
 1071:         $json .= &prepareJsonData_rec
 1072:                     ($path.$_.'/', $entries, $firstfile, $firstdir, $pathToSkip);
 1073:     }
 1074:     return $json;
 1075: }
 1076: 1;
 1077: __END__
 1078: 
 1079: 
 1080: =head1 NAME
 1081: 
 1082: Apache::lonpubdir - Authoring space directory lister
 1083: 
 1084: =head1 SYNOPSIS
 1085: 
 1086: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
 1087: 
 1088:  <LocationMatch "^/+priv.*/$">
 1089:  PerlAccessHandler       Apache::loncacc
 1090:  SetHandler perl-script
 1091:  PerlHandler Apache::lonpubdir
 1092:  ErrorDocument     403 /adm/login
 1093:  ErrorDocument     404 /adm/notfound.html
 1094:  ErrorDocument     406 /adm/unauthorized.html
 1095:  ErrorDocument	  500 /adm/errorhandler
 1096:  </LocationMatch>
 1097: 
 1098:  <Location /adm/pubdir>
 1099:  PerlAccessHandler       Apache::lonacc
 1100:  SetHandler perl-script
 1101:  PerlHandler Apache::lonpubdir
 1102:  ErrorDocument     403 /adm/login
 1103:  ErrorDocument     404 /adm/notfound.html
 1104:  ErrorDocument     406 /adm/unauthorized.html
 1105:  ErrorDocument	  500 /adm/errorhandler
 1106:  </Location>
 1107: 
 1108: =head1 INTRODUCTION
 1109: 
 1110: This module publishes a directory of files.
 1111: 
 1112: This is part of the LearningOnline Network with CAPA project
 1113: described at http://www.lon-capa.org.
 1114: 
 1115: =head1 HANDLER SUBROUTINE
 1116: 
 1117: This routine is called by Apache and mod_perl.
 1118: 
 1119: =over 4
 1120: 
 1121: =item *
 1122: 
 1123: read in information
 1124: 
 1125: =item *
 1126: 
 1127: start page output
 1128: 
 1129: =item *
 1130: 
 1131: run through list of files and attempt to publish unhidden files
 1132: 
 1133: =back
 1134: 
 1135: =head1 SUBROUTINES:
 1136: 
 1137: =over
 1138: 
 1139: =item startpage($r, $uame, $udom, $thisdisfn)
 1140: 
 1141: Output the header of the page.  This includes:
 1142:  - The HTML header 
 1143:  - The H1/H3  stuff which includes the directory.
 1144:  
 1145:     startpage($r, $uame, $udom, $thisdisfn);
 1146:         $r     - The apache request object.
 1147:         $uname - User name.
 1148:         $udom  - Domain name the user is logged in under.
 1149:         $thisdisfn - Displayable version of the filename.
 1150: 
 1151: =item getTitleString($fullname)
 1152: 
 1153:     Get the title string or "[untitled]" if the file has no title metadata:
 1154:     Without the latter substitution, it's impossible to examine metadata for
 1155:     untitled resources.  Resources may be legitimately untitled, to prevent
 1156:     searches from locating them.
 1157:     
 1158:     $str = getTitleString($fullname);
 1159:         $fullname - Fully qualified filename to check.
 1160: 
 1161: =item putdirectory($r, $base, $here, $dirname, $modtime, $targetdir, $bombs,
 1162:                    $numdir)
 1163: 
 1164:     Put out a directory table row:
 1165:     
 1166:         $r        - Apache request object.
 1167:         $reqfile  - File in request.
 1168:         $here     - Where we are in directory tree.
 1169:         $dirname  - Name of directory special file.
 1170:         $modtime  - Encoded modification time.
 1171:         targetdir - Publication target directory.
 1172:         bombs     - Reference to hash of URLs with runtime error messages.
 1173:         numdir    - Reference to scalar used to track number of sub-directories
 1174:                     in directory (used in form name for each "actions" dropdown).
 1175: 
 1176: =back
 1177: 
 1178: =cut

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