Annotation of loncom/publisher/lonpubdir.pm, revision 1.183

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

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