File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.15: download - view: text, annotated - select for diffs
Fri Jul 9 18:43:40 2004 UTC (19 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: version_1_2_X, version_1_2_1, version_1_2_0, version_1_1_99_5, version_1_1_99_4, version_1_1_99_3, version_1_1_99_2, HEAD
- correcting to use propath

    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: 
   22: 
   23: # http://www.lon-capa.org/
   24: #
   25: 
   26: 
   27: package Apache::portfolio;
   28: use strict;
   29: use Apache::Constants qw(:common :http);
   30: use Apache::loncommon;
   31: use Apache::lonnet;
   32: use Apache::lontexconvert;
   33: use Apache::lonfeedback;
   34: use Apache::lonlocal;
   35: sub makeAnchor{
   36:     # receives a file name and path stub from username/userfiles/portfolio/
   37:     # returns an anchor tag consisting encoding filename and currentpath
   38:     my ($fileName, $currentPath) = @_;
   39:     my $anchor = '<a href="/adm/portfolio?selectfile='.$fileName.'&currentpath='.$currentPath.'">'.$fileName.'</a>';
   40:     return $anchor;
   41: }
   42: sub displayDirectory {
   43:     # returns html with <br /> separated contents of the directory
   44:     # returns a <strong>currentFile</strong> (bolds the selected file/dir)
   45:     my ($currentPath, $currentFile, $isDir, @dirList,) = @_;
   46:     my $displayOut='';  
   47:     my $fileName;
   48:     my $upPath;
   49:     if ($currentPath ne '/'){
   50:         $displayOut = 'Listing of '.$currentPath.'<br /><hr />'.
   51:         # provides the "up one directory level" function
   52:         # it means shortening the currentpath to the parent directory
   53:         $currentPath =~ m:(^/.*)(/.*/$):;
   54:         if ($1 ne '/'){
   55:             $upPath = $1.'/';
   56:         }else{
   57:             $upPath = $1;
   58:         }
   59:         
   60:         $displayOut = $displayOut.'<a href="/adm/portfolio?selectfile='.$upPath.'&currentpath='.$upPath.'">..</a><br />';
   61:     } else {
   62:         $displayOut = $displayOut.'at root '.$currentPath.'<br />';
   63:     }
   64:     foreach my $line (@dirList) {
   65:     	#$strip holds directory/file name
   66:     	#$dom 
   67:     	my ($fileName,$dom,undef,$testdir,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,$obs,undef)=split(/\&/,$line,16); 
   68:         if (($fileName ne '.') && ($fileName ne '..')){
   69:             if ($testdir =~ m:^1:){
   70:                 # handle directories different from files
   71:                 if ($fileName eq $currentFile){ #checks to bold the selected file
   72:                     $displayOut.= '<strong>'.(makeAnchor($fileName.'/', $currentPath.$fileName.'/').'</strong><br />');
   73:                 }else{
   74:                     $displayOut.= (makeAnchor($fileName.'/', $currentPath.$fileName.'/').'<br />');
   75:                 }
   76:             }else{
   77:                 if ($fileName eq $currentFile){ #checks to bold the selected file
   78:                     $displayOut.='<strong>'.(makeAnchor($fileName, $currentPath).'</strong><br />');
   79:                 }else{
   80:                     $displayOut.=(makeAnchor($fileName, $currentPath).'<br />');
   81:                 }
   82:             }
   83:         }
   84:     	
   85:     }
   86:     return $displayOut;
   87: }
   88: sub displayActions {
   89:     # returns html to offer user appropriate actions depending on selected file/directory
   90:     my $displayOut;
   91:     my ($currentPath, $currentFile, $isDir, $isEmpty) = @_;
   92: #   $displayOut = 'here are actions for '.$currentFile;
   93:     if ($currentFile =~ m:/$:){
   94:         # if the selected file is a directory, these are the options
   95:         # offer the chance to delete the directory only if it is empty
   96:         if ($isEmpty && ($currentPath ne '/')) {
   97:             $displayOut =   $displayOut.'<form method="POST">
   98:             <input type="hidden" name="selectfile" 
   99:             value="'.$currentFile.'" />
  100:             <input type="hidden" name="fileaction" value="delete" /> 
  101:             <input type="hidden" name="currentpath" value="'.$currentPath.'" /> 
  102:             <center>
  103:             <input type="submit" 
  104:             value="Delete '.$currentFile.'" />
  105:             </center>
  106:             </form>';
  107:         } 
  108:         if ($currentPath ne '/') {
  109:             $displayOut = $displayOut.'<hr />
  110:             <form method="POST">
  111:             <input type="hidden" name="selectfile" 
  112:             value="'.$currentFile.'" />
  113:             <input type="hidden" name="fileaction" value="rename" /> 
  114:             <input type="hidden" name="currentpath" value="'.$currentPath.'" /> 
  115:             <input type="input" name="filenewname" value="Type new name here" /> 
  116:             <input type="submit" 
  117:             value="Rename '.$currentFile.'" />
  118:             </form>';
  119:         }
  120:     }else{  #action options offered for files
  121:         $displayOut = $displayOut.'<form method="POST">';
  122:         $displayOut = $displayOut.'<input type="hidden" name="selectfile"';
  123:         $displayOut = $displayOut.'value="'.$currentFile;
  124:         $displayOut = $displayOut.'" /><input type="hidden" name="fileaction" value="delete" /> 
  125:         <input type="hidden" name="currentpath" value="'.$currentPath.'" /> 
  126:         <center>
  127:         <input type="submit"
  128:         value="Delete '.$currentFile.'" />
  129:         </center>
  130:         </form>';
  131:     
  132:         $displayOut = $displayOut.'<hr />
  133:         <form method="POST">
  134:         <input type="hidden" name="selectfile" 
  135:         value="'.$currentFile.'" />
  136:         <input type="hidden" name="fileaction" value="rename" /> 
  137:         <input type="hidden" name="currentpath" value="'.$currentPath.'" /> 
  138:         <input type="input" name="filenewname" value="Type new name here" /> 
  139:         <input type="submit" 
  140:         value="Rename '.$currentFile.'" />
  141:         </form>
  142:         <hr />';
  143:     }
  144:     $displayOut = $displayOut.'<hr />Add a file to '.$currentPath;
  145:     # file upload form 
  146:     $displayOut = $displayOut.'<form method="post" enctype="multipart/form-data">';
  147:     $displayOut = $displayOut.'<input name="uploaddoc" type="file" />'.
  148:         '<input type="hidden" name="currentpath" value="'.$currentPath.'" />'.
  149:         '<input type="submit" name="storeupl" value="Upload" />'.
  150:         '</form><hr />';
  151:     $displayOut = $displayOut.'<form method="POST">
  152:         <input name="subdir" type="text" />
  153:         <input type="submit" value="Create Subdirectory" />
  154:         </form>
  155:             ';
  156:     return $displayOut;
  157: }
  158: sub handler {
  159:     # this handles file management
  160:     my $r = shift;
  161:     my @dirList; # will hold directory listing as array
  162:     my $udir; # returned from home server
  163:     my $currentPath; # path assuming /userfiles/portfolio/ as root
  164:     my $currentFile; # directory or file contained in $pathToRoot.$currentPath
  165:     my $action; # delete, rename, makedirectory, removedirectory,
  166:     my $filenewname; # for rename action (guess what we do with it!)
  167:     my $isFile;
  168:     my $isDir;
  169:     my $isEmpty;
  170:     &Apache::loncommon::no_cache($r);
  171:     &Apache::loncommon::content_type($r,'text/html');
  172:     $r->send_http_header;
  173:     # Give the LON-CAPA page header
  174:     $r->print('<html><head><title>'.
  175:               &mt('Portfolio Manager').
  176:               "</title></head>\n".
  177:               &Apache::loncommon::bodytag('Portfolio Manager'));
  178:     $r->rflush();
  179:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  180:                                             ['selectfile','currentpath', 'currentfile']);
  181:     # currentPath and currentFile need to be set for the rest of things to happen
  182:     # sometimes, currentFile will be passed by a form field, selectedfile
  183:     # if there is no 'form.selectedfile' then the current directory is 
  184:     # considered as selected 
  185:     if ($ENV{'form.currentpath'}) {
  186:         $currentPath = $ENV{'form.currentpath'};
  187:     } else {
  188:         $currentPath = '/';
  189:     }
  190:     if ($ENV{'form.selectfile'}) {
  191:         # have to check if the selected file is a subdirectory
  192:         if ($ENV{'form.selectfile'} =~ /-\(Dir\)/){
  193:             # $currentPath =~ /\-\(Dir\)/;
  194:             $currentPath = $`.'/';
  195:             $r->print('<br />'.$currentPath.'<br />');
  196:         }
  197:         $currentFile = $ENV{'form.selectfile'};
  198:     } else {
  199:         $currentFile = '';
  200:     }
  201:     # if we're uploading a file, we need to do it early so it will show in the directory list
  202:     if ($ENV{'form.uploaddoc.filename'}) {
  203:         $r->print($ENV{'form.storeupl'}.'<br />');
  204:         $r->print(&Apache::lonnet::userfileupload('uploaddoc','','portfolio'.$currentPath).'<br />');  
  205:     }
  206:     # similarly, we need to delete or rename files before getting directory list
  207:     if ($ENV{'form.selectfile'}){
  208:         if ($ENV{'form.fileaction'} eq 'delete') {
  209:             $r->print('<br />trying to delete '.$currentPath.$ENV{'form.selectfile'}.'<br />');
  210:             $r->print(&Apache::lonnet::removeuserfile($ENV{'user.name'}, $ENV{'user.domain'},'portfolio'.$currentPath.$ENV{'form.selectfile'}));
  211:             $currentFile = '';
  212:         } elsif ($ENV{'form.fileaction'} eq 'rename') {
  213:             &Apache::lonnet::portfoliomanage($currentPath.$ENV{'form.selectfile'}, 'rename', $currentPath.$ENV{'form.filenewname'} );
  214:         }
  215:     }
  216:     # we always need $dirList, plus this will return information about the current file
  217:     # as well as information about he home server directory structure, specifically
  218:     # the path to the users userfiles directory.    
  219:     my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
  220: 						     $ENV{'user.name'}).
  221: 						       '/userfiles/portfolio';
  222:     my @list = &Apache::lonnet::dirlist($currentPath,  $ENV{'user.domain'}, $ENV{'user.name'}, $portfolio_root);
  223:     @dirList = @list;
  224:     if (@dirList == 2) { # need to know if directory is empty so it can be removed if desired
  225:         $isEmpty = 1;
  226:     } else {
  227:         $isEmpty = 0;
  228:     }
  229:     # Stuff to maintain proper setting for selected file
  230:     if ($ENV{'form.selectfile'}) {
  231:         if ($ENV{'form.fileaction'} eq 'delete') {
  232:             &Apache::lonnet::portfoliomanage($ENV{'form.selectfile'}, 'delete', undef );
  233:             $ENV{'portfolio.file'} = 'Selected File Deleted';
  234:         } elsif ($ENV{'form.fileaction'} eq 'rename') {
  235:             &Apache::lonnet::portfoliomanage($ENV{'form.selectfile'}, 'rename', $ENV{'form.filenewname'});
  236:         } else {
  237:         # Remember user's file selection for later
  238:         $ENV{'portfolio.file'} = $ENV{'form.selectfile'};
  239:         # offer things user can do with selected file
  240:         }
  241:     }else{
  242:         unless ($ENV{'portfolio.file'}){
  243:             $ENV{'portfolio.file'} = 'No File Selected';
  244:         }
  245:     }
  246:     ##############################
  247:     #
  248:     # Display begins here
  249:     #
  250:     ##############################
  251:     $r->print('<table border=1><tr><td>');
  252:     $r->print(displayDirectory($currentPath, $currentFile, $isDir, @dirList));
  253:     $r->print('</td>><td>');
  254:     $r->print(displayActions($currentPath, $currentFile, $isDir, $isEmpty));
  255:     $r->print('</td>></tr></table>');
  256:     $r->print('</blockquote>');
  257:     $r->print("</body>\n</html>\n");
  258:     $r->rflush();
  259:     return OK;
  260: }
  261: 
  262: 1;
  263: __END__

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