File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.16: download - view: text, annotated - select for diffs
Fri Jul 23 01:25:56 2004 UTC (19 years, 10 months ago) by banghart
Branches: MAIN
CVS tags: HEAD


	Completely broken at this point, but approaching a better way of
	seeing/managing directory contents.

    1: # Copyright Michigan State University Board of Trustees
    2: #
    3: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    4: #
    5: # LON-CAPA is free software; you can redistribute it and/or modify
    6: # it under the terms of the GNU General Public License as published by
    7: # the Free Software Foundation; either version 2 of the License, or 
    8: # (at your option) any later version.
    9: #
   10: # LON-CAPA is distributed in the hope that it will be useful,
   11: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   12: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13: # GNU General Public License for more details.
   14: #
   15: # You should have received a copy of the GNU General Public License
   16: # along with LON-CAPA; if not, write to the Free Software
   17: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   18: #
   19: # /home/httpd/html/adm/gpl.txt
   20: #
   21: # http://www.lon-capa.org/
   22: #
   23: 
   24: package Apache::portfolio;
   25: use strict;
   26: use Apache::Constants qw(:common :http);
   27: use Apache::loncommon;
   28: use Apache::lonnet;
   29: use Apache::lontexconvert;
   30: use Apache::lonfeedback;
   31: use Apache::lonlocal;
   32: 
   33: # receives a file name and path stub from username/userfiles/portfolio/
   34: # returns an anchor tag consisting encoding filename and currentpath
   35: sub makeAnchor{
   36:     my ($fileName, $currentPath) = @_;
   37:     my $anchor = '<a href="/adm/portfolio?selectfile='.$fileName.'&currentpath='.$currentPath.'">'.$fileName.'</a>';
   38:     return $anchor;
   39: }
   40: 
   41: # returns html with <br /> separated contents of the directory
   42: # returns a <strong>currentFile</strong> (bolds the selected file/dir)
   43: sub displayDirectory {
   44:     my ($currentPath, $currentFile, @dirList,) = @_;
   45:     my $displayOut='';  
   46:     my $fileName;
   47:     my $upPath;
   48:     if ($currentPath ne '/'){
   49:         $displayOut = 'Listing of '.$currentPath.'<br /><hr />'.
   50:         # provides the "up one directory level" function
   51:         # it means shortening the currentpath to the parent directory
   52:         $currentPath =~ m:(^/.*)(/.*/$):;
   53:         if ($1 ne '/'){
   54:             $upPath = $1.'/';
   55:         }else{
   56:             $upPath = $1;
   57:         }
   58:         
   59:         $displayOut = $displayOut.'<a href="/adm/portfolio?selectfile='.$upPath.'&currentpath='.$upPath.'">..</a><br />';
   60:     } else {
   61:         $displayOut = $displayOut.'at root '.$currentPath.'<br />';
   62:     }
   63:     foreach my $line (@dirList) {
   64:     	#$strip holds directory/file name
   65:     	#$dom 
   66:     	my ($fileName,$dom,undef,$testdir,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,$obs,undef,$path)=split(/\&/,$line,17); 
   67:     	$path =~ m:/:;
   68:     	my $dirDepth = @-;
   69:         if (($fileName ne '.') && ($fileName ne '..')){
   70:           	for (my $i = 0; $i <= $dirDepth; $i += 1){
   71:     	        $displayOut.='<blockquote>';
   72:     	    }
   73:             if ($testdir =~ m:^1:){
   74:                 # handle directories different from files
   75:                 if ($fileName eq $currentFile){ #checks to bold the selected file
   76:                     $displayOut.= '<strong>'.(makeAnchor($fileName.'/', $path.$fileName.'/').'</strong><br />'."\n");
   77:                 }else{
   78:                     $displayOut.= (makeAnchor($fileName.'/', $path.$fileName.'/').'<br />'."\n");
   79:                 }
   80:             }else{
   81:                 if ($fileName eq $currentFile){ #checks to bold the selected file
   82:                     $displayOut.='<strong>'.(makeAnchor($fileName, $currentPath).'</strong><br />'."\n");
   83:                 }else{
   84:                     $displayOut.=(makeAnchor($fileName, $currentPath).'<br />'."\n");
   85:                 }
   86:             }
   87:         	for (my $i = 0; $i <= $dirDepth; $i += 1){
   88:         	    $displayOut.='</blockquote>';
   89:     	    }
   90:             
   91:         }
   92:     	
   93:     }
   94:     return $displayOut;
   95: }
   96: sub displayActions {
   97:     # returns html to offer user appropriate actions depending on selected file/directory
   98:     my $displayOut;
   99:     my ($currentPath, $currentFile, $isEmpty) = @_;
  100: #   $displayOut = 'here are actions for '.$currentFile;
  101:     if ($currentFile =~ m:/$:){
  102:         # if the selected file is a directory, these are the options
  103:         # offer the chance to delete the directory only if it is empty
  104:         if ($isEmpty && ($currentPath ne '/')) {
  105:             $displayOut =   $displayOut.'<form method="POST">
  106:             <input type="hidden" name="selectfile" 
  107:             value="'.$currentFile.'" />
  108:             <input type="hidden" name="fileaction" value="delete" /> 
  109:             <input type="hidden" name="currentpath" value="'.$currentPath.'" /> 
  110:             <center>
  111:             <input type="submit" 
  112:             value="Delete '.$currentFile.'" />
  113:             </center>
  114:             </form>';
  115:         } 
  116:         if ($currentPath ne '/') {
  117:             $displayOut = $displayOut.'<hr />
  118:             <form method="POST">
  119:             <input type="hidden" name="selectfile" 
  120:             value="'.$currentFile.'" />
  121:             <input type="hidden" name="fileaction" value="rename" /> 
  122:             <input type="hidden" name="currentpath" value="'.$currentPath.'" /> 
  123:             <input type="input" name="filenewname" value="Type new name here" /> 
  124:             <input type="submit" 
  125:             value="Rename '.$currentFile.'" />
  126:             </form>';
  127:         }
  128:     }else{  #action options offered for files
  129:         $displayOut = $displayOut.'<form method="POST">';
  130:         $displayOut = $displayOut.'<input type="hidden" name="selectfile"';
  131:         $displayOut = $displayOut.'value="'.$currentFile;
  132:         $displayOut = $displayOut.'" /><input type="hidden" name="fileaction" value="delete" /> 
  133:         <input type="hidden" name="currentpath" value="'.$currentPath.'" /> 
  134:         <center>
  135:         <input type="submit"
  136:         value="Delete '.$currentFile.'" />
  137:         </center>
  138:         </form>';
  139:     
  140:         $displayOut = $displayOut.'<hr />
  141:         <form method="POST">
  142:         <input type="hidden" name="selectfile" 
  143:         value="'.$currentFile.'" />
  144:         <input type="hidden" name="fileaction" value="rename" /> 
  145:         <input type="hidden" name="currentpath" value="'.$currentPath.'" /> 
  146:         <input type="input" name="filenewname" value="Type new name here" /> 
  147:         <input type="submit" 
  148:         value="Rename '.$currentFile.'" />
  149:         </form>
  150:         <hr />';
  151:     }
  152:     $displayOut = $displayOut.'<hr />Add a file to '.$currentPath;
  153:     # file upload form 
  154:     $displayOut = $displayOut.'<form method="post" enctype="multipart/form-data">';
  155:     $displayOut = $displayOut.'<input name="uploaddoc" type="file" />'.
  156:         '<input type="hidden" name="currentpath" value="'.$currentPath.'" />'.
  157:         '<input type="submit" name="storeupl" value="Upload" />'.
  158:         '</form><hr />';
  159:     $displayOut = $displayOut.'<form method="POST">
  160:         <input name="subdir" type="text" />
  161:         <input type="submit" value="Create Subdirectory" />
  162:         </form>
  163:             ';
  164:     return $displayOut;
  165: }
  166: sub handler {
  167:     # this handles file management
  168:     my $r = shift;
  169:     my @dirList; # will hold directory listing as array
  170:     my $udir; # returned from home server
  171:     my $currentPath; # path assuming /userfiles/portfolio/ as root
  172:     my $currentFile; # directory or file contained in $pathToRoot.$currentPath
  173:     my $action; # delete, rename, makedirectory, removedirectory,
  174:     my $filenewname; # for rename action (guess what we do with it!)
  175:     my $isFile;
  176:     my $isEmpty;
  177:     &Apache::loncommon::no_cache($r);
  178:     &Apache::loncommon::content_type($r,'text/html');
  179:     $r->send_http_header;
  180:     # Give the LON-CAPA page header
  181:     $r->print('<html><head><title>'.
  182:               &mt('Portfolio Manager').
  183:               "</title></head>\n".
  184:               &Apache::loncommon::bodytag('Portfolio Manager'));
  185:     $r->rflush();
  186:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  187:                                             ['selectfile','currentpath', 'currentfile']);
  188:     # currentPath and currentFile need to be set for the rest of things to happen
  189:     # sometimes, currentFile will be passed by a form field, selectedfile
  190:     # if there is no 'form.selectedfile' then the current directory is 
  191:     # considered as selected 
  192:     if ($ENV{'form.currentpath'}) {
  193:         $currentPath = $ENV{'form.currentpath'};
  194:     } else {
  195:         $currentPath = '/';
  196:     }
  197:     if ($ENV{'form.selectfile'}) {
  198:         # have to check if the selected file is a subdirectory
  199:         if ($ENV{'form.selectfile'} =~ /-\(Dir\)/){
  200:             # $currentPath =~ /\-\(Dir\)/;
  201:             $currentPath = $`.'/';
  202:             $r->print('<br />'.$currentPath.'<br />');
  203:         }
  204:         $currentFile = $ENV{'form.selectfile'};
  205:     } else {
  206:         $currentFile = '';
  207:     }
  208:     # if we're uploading a file, we need to do it early so it will show in the directory list
  209:     if ($ENV{'form.uploaddoc.filename'}) {
  210:         $r->print($ENV{'form.storeupl'}.'<br />');
  211:         $r->print(&Apache::lonnet::userfileupload('uploaddoc','','portfolio'.$currentPath).'<br />');  
  212:     }
  213:     # similarly, we need to delete or rename files before getting directory list
  214:     if ($ENV{'form.selectfile'}){
  215:         if ($ENV{'form.fileaction'} eq 'delete') {
  216:             $r->print('<br />trying to delete '.$currentPath.$ENV{'form.selectfile'}.'<br />');
  217:             $r->print(&Apache::lonnet::removeuserfile($ENV{'user.name'}, $ENV{'user.domain'},'portfolio'.$currentPath.$ENV{'form.selectfile'}));
  218:             $currentFile = '';
  219:         } elsif ($ENV{'form.fileaction'} eq 'rename') {
  220:             &Apache::lonnet::portfoliomanage($currentPath.$ENV{'form.selectfile'}, 'rename', $currentPath.$ENV{'form.filenewname'} );
  221:         }
  222:     }
  223:     # we always need $dirList, plus this will return information about the current file
  224:     # as well as information about he home server directory structure, specifically
  225:     # the path to the users userfiles directory.    
  226:     # 
  227:     my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
  228: 						     $ENV{'user.name'}).
  229: 						       '/userfiles/portfolio';
  230:     my $done = 0;
  231:     my $subdir = '';
  232:     my @workinglist; # intermediate array, holds directory listing lines (dirlist), and path information
  233:     my $readDirectory = 1; # flag for directory
  234:     my $loopCounter = 0; # needed only for development to prevent run away program (or maybe more?)
  235:     while (!$done){
  236:         # Needed while developing. Later, too?
  237:         $loopCounter += 1;
  238:         if ($loopCounter > 50) {
  239:             $r->print('<br />stop runaway');
  240:             return OK;
  241:         }
  242:         # ---
  243:         if ($readDirectory){ # is true the first time through, then true if dirlist line is a subdir
  244:             # $r->print('<br />reading '.$portfolio_root.$subdir);
  245:             my @list = &Apache::lonnet::dirlist($currentPath,  $ENV{'user.domain'}, $ENV{'user.name'}, $portfolio_root.$subdir);
  246:             foreach my $line(@list){
  247:                 $line = $line.'&'.$subdir; # append the subdirectory information
  248:                 my ($fileName,$dom,undef,$testdir,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,$obs,undef,$subpath)=split(/\&/,$line,17); 
  249:                 if (($fileName ne '.') && ($fileName ne '..')){ # we throw away the current and parent directories
  250:                     $r->print('<br />'.$line);
  251:                     # should this be shift?
  252:                     push @workinglist, $line; # add the line to the working list array
  253:                 }
  254:             }
  255:         }
  256:         my $line = shift @workinglist; #take one off the working list
  257:         if ($line eq '') { # if the working list is empty
  258:             $done = 1;
  259:         }else{
  260:             push @dirList, $line; # and put it in the display list
  261:             my ($fileName,$dom,undef,$testdir,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,$obs,undef,$subpath)=split(/\&/,$line,17); 
  262:             if ($testdir =~ m:^1:) { # true if this is a directory
  263:                 # $r->print('<br />added subdir '.$fileName);
  264:                 $subdir = $subpath.'/'.$fileName; 
  265:                 $readDirectory = 1;
  266:             }else{
  267:                 $readDirectory = 0;
  268:             }
  269:         }
  270:     }
  271:     #	if item is directory {  get the next level down
  272:     #		my @list = &Apache::lonnet::dirlist($currentPath,  $ENV{'user.domain'}, $ENV{'user.name'}, $portfolio_root.$);
  273:     
  274:     if (@dirList == 2) { # need to know if directory is empty so it can be removed if desired
  275:         $isEmpty = 1;
  276:     } else {
  277:         $isEmpty = 0;
  278:     }
  279:    
  280:     if ($ENV{'form.selectfile'}) {
  281:         if ($ENV{'form.fileaction'} eq 'delete') {
  282:             &Apache::lonnet::portfoliomanage($ENV{'form.selectfile'}, 'delete', undef );
  283:             $ENV{'portfolio.file'} = 'Selected File Deleted';
  284:         } elsif ($ENV{'form.fileaction'} eq 'rename') {
  285:             &Apache::lonnet::portfoliomanage($ENV{'form.selectfile'}, 'rename', $ENV{'form.filenewname'});
  286:         } else {
  287:         # Remember user's file selection for later
  288:         $ENV{'portfolio.file'} = $ENV{'form.selectfile'};
  289:         # offer things user can do with selected file
  290:         }
  291:     }else{
  292:         unless ($ENV{'portfolio.file'}){
  293:             $ENV{'portfolio.file'} = 'No File Selected';
  294:         }
  295:     }
  296:     ##############################
  297:     #
  298:     # Display begins here
  299:     #
  300:     ##############################
  301:     $r->print('<table border=1><tr><td>');
  302:     $r->print(displayDirectory($currentPath, $currentFile, @dirList));
  303:     $r->print('</td>><td>');
  304:     $r->print(displayActions($currentPath, $currentFile, $isEmpty));
  305:     $r->print('</td>></tr></table>');
  306:     $r->print('</blockquote>');
  307:     $r->print("</body>\n</html>\n");
  308:     $r->rflush();
  309:     return OK;
  310: }
  311: 
  312: 1;
  313: __END__

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