File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.22: download - view: text, annotated - select for diffs
Tue Aug 24 04:55:49 2004 UTC (19 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- adding in some starts at file management
- make downloading possible
- fill in some fields

    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)=split(/\&/,$line,16); 
   67:         if (($fileName ne '.') && ($fileName ne '..')){
   68:             if ($testdir =~ m:^1:){
   69:                 # handle directories different from files
   70:                 if ($fileName eq $currentFile){ #checks to bold the selected file
   71:                     $displayOut.= '<strong>'.(makeAnchor($fileName.'/', $fileName.'/').'</strong><br />'."\n");
   72:                 }else{
   73:                     $displayOut.= (makeAnchor($fileName.'/', $fileName.'/').'<br />'."\n");
   74:                 }
   75:             }else{
   76:                 if ($fileName eq $currentFile){ #checks to bold the selected file
   77:                     $displayOut.='<strong>'.(makeAnchor($fileName, $currentPath).'</strong><br />'."\n");
   78:                 }else{
   79:                     $displayOut.=(makeAnchor($fileName, $currentPath).'<br />'."\n");
   80:                 }
   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, $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 $iconpath= $r->dir_config('lonIconsURL') . "/";
  162:     my @dirList; # will hold directory listing as array
  163:     my $udir; # returned from home server
  164:     my $currentPath; # path assuming /userfiles/portfolio/ as root
  165:     my $currentFile; # directory or file contained in $pathToRoot.$currentPath
  166:     my $action; # delete, rename, makedirectory, removedirectory,
  167:     my $filenewname; # for rename action (guess what we do with it!)
  168:     my $isFile;
  169:     my $isEmpty;
  170:     my $dirptr=16384;
  171:     &Apache::loncommon::no_cache($r);
  172:     &Apache::loncommon::content_type($r,'text/html');
  173:     $r->send_http_header;
  174:     # Give the LON-CAPA page header
  175:     $r->print('<html><head><title>'.
  176:               &mt('Portfolio Manager').
  177:               "</title></head>\n".
  178:               &Apache::loncommon::bodytag('Portfolio Manager'));
  179:     $r->rflush();
  180:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  181:                                             ['selectfile','currentpath', 'currentfile']);
  182:     # currentPath and currentFile need to be set for the rest of things to happen
  183:     # sometimes, currentFile will be passed by a form field, selectedfile
  184:     # if there is no 'form.selectedfile' then the current directory is 
  185:     # considered as selected 
  186:     if ($ENV{'form.currentpath'}) {
  187:         $currentPath = $ENV{'form.currentpath'};
  188:     } else {
  189:         $currentPath = '/';
  190:     }
  191:     if ($ENV{'form.selectfile'}) {
  192:         # have to check if the selected file is a subdirectory
  193:         if ($ENV{'form.selectfile'} =~ /-\(Dir\)/){
  194:             # $currentPath =~ /\-\(Dir\)/;
  195:             $currentPath = $`.'/';
  196:             $r->print('<br />'.$currentPath.'<br />');
  197:         }
  198:         $currentFile = $ENV{'form.selectfile'};
  199:     } else {
  200:         $currentFile = '';
  201:     }
  202:     # if we're uploading a file, we need to do it early so it will show in the directory list
  203:     if ($ENV{'form.uploaddoc.filename'}) {
  204:         $r->print($ENV{'form.storeupl'}.'<br />');
  205:         $r->print(&Apache::lonnet::userfileupload('uploaddoc','','portfolio'.$currentPath).'<br />');  
  206:     }
  207:     # similarly, we need to delete or rename files before getting directory list
  208:     if ($ENV{'form.selectfile'}){
  209:         if ($ENV{'form.fileaction'} eq 'delete') {
  210:             $r->print('<br />trying to delete '.$currentPath.$ENV{'form.selectfile'}.'<br />');
  211:             $r->print(&Apache::lonnet::removeuserfile($ENV{'user.name'}, $ENV{'user.domain'},'portfolio'.$currentPath.$ENV{'form.selectfile'}));
  212:             $currentFile = '';
  213:         } elsif ($ENV{'form.fileaction'} eq 'rename') {
  214:             &Apache::lonnet::portfoliomanage($currentPath.$ENV{'form.selectfile'}, 'rename', $currentPath.$ENV{'form.filenewname'} );
  215:         }
  216:     }
  217:     # we always need $dirList, plus this will return information about the current file
  218:     # as well as information about the home server directory structure, specifically
  219:     # the path to the users userfiles directory.    
  220:     # 
  221:     my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
  222: 						     $ENV{'user.name'}).
  223: 						       '/userfiles/portfolio';
  224:     @dirList = &Apache::lonnet::dirlist($currentPath,  $ENV{'user.domain'}, $ENV{'user.name'}, $portfolio_root);
  225:     
  226:     if (@dirList == 2) { # need to know if directory is empty so it can be removed if desired
  227:         $isEmpty = 1;
  228:     } else {
  229:         $isEmpty = 0;
  230:     }
  231:    
  232:     if ($ENV{'form.selectfile'}) {
  233:         if ($ENV{'form.fileaction'} eq 'delete') {
  234:             &Apache::lonnet::portfoliomanage($ENV{'form.selectfile'}, 'delete', undef );
  235:             $ENV{'portfolio.file'} = 'Selected File Deleted';
  236:         } elsif ($ENV{'form.fileaction'} eq 'rename') {
  237:             &Apache::lonnet::portfoliomanage($ENV{'form.selectfile'}, 'rename', $ENV{'form.filenewname'});
  238:         } else {
  239:         # Remember user's file selection for later
  240:         $ENV{'portfolio.file'} = $ENV{'form.selectfile'};
  241:         # offer things user can do with selected file
  242:         }
  243:     }else{
  244:         unless ($ENV{'portfolio.file'}){
  245:             $ENV{'portfolio.file'} = 'No File Selected';
  246:         }
  247:     }
  248:     ##############################
  249:     #
  250:     # Display begins here
  251:     #
  252:     ##############################
  253:     $r->print('<table border="0" cellspacing="2" cellpadding="2"><tr valign="middle">');
  254:     $r->print('<td bgcolor="#ccddaa" align="center">');
  255:     my $displayOut = '<form method="post" enctype="multipart/form-data">';
  256:     $displayOut = $displayOut.'<input name="uploaddoc" type="file" />'.
  257:         '<input type="hidden" name="currentpath" value="'.$currentPath.'" />'.
  258:         '<input type="submit" name="storeupl" value="Upload" />'.
  259:         '</form>';
  260:     $r->print($displayOut);
  261:     $r->print('</td></tr><tr><td bgcolor="#ccddaa" align="center">');
  262:     my $displayOut = '<form method="post">';
  263:     $displayOut = $displayOut.'<input name="newdir" type="input" />'.
  264:         '<input type="hidden" name="currentpath" value="'.$currentPath.'" />'.
  265:         '<input type="submit" name="createdir" value="'.&mt("Create Directory").'" />'.
  266:         '</form>';
  267:     $r->print($displayOut);
  268:     $r->print('</td></tr></table>');
  269:     my @tree = split (/\//,$currentPath);
  270:     $r->print('<font size="+2">'.makeAnchor('/','/'));
  271:     if (@tree > 1){
  272:         my $newCurrentPath = '';
  273:         for (my $i = 1; $i< @tree; $i++){
  274:             $newCurrentPath .= $tree[$i].'/';
  275:             $r->print(makeAnchor($tree[$i],'/'.$newCurrentPath).'/');
  276:         }
  277:     }
  278:     $r->print('</font>');
  279:     &Apache::lonhtmlcommon::store_recent('portfolio',$currentPath,$currentPath);
  280:     $r->print('<br /><form method=post action="/adm/portfolio">'.
  281: 	      &Apache::lonhtmlcommon::select_recent('portfolio','currentpath',
  282: 						    'this.form.submit();'));
  283:     $r->print("</form>");
  284:     $r->print('<table border="0" cellspacing="2" cellpadding="2">'.
  285:             '<tr><th>Actions</th><th>&nbsp;</th><th>Name</th><th>Size</th><th>Last Modified</th></tr>');
  286:     my $href_location="/uploaded/$ENV{'user.domain'}/$ENV{'user.name'}/portfolio/$currentPath/";
  287:     foreach my $line (@dirList) {
  288:     	#$strip holds directory/file name
  289:     	#$dom 
  290:     	my ($fileName,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef)=split(/\&/,$line,16); 
  291:     	if (($fileName ne '.') && ($fileName ne '..')){
  292:             if ($dirptr&$testdir){
  293:                 $r->print('<tr bgcolor="#FFAA99"><td><img src="'.$iconpath.'folder_closed.gif"></td>');
  294:                 $r->print('<td>Go to ...</td>');
  295:                 $r->print('<td>'.makeAnchor($fileName.'/',$currentPath.$fileName.'/').'</td>'); 
  296:                 $r->print('</tr>'); 
  297:             }else{
  298:                 $r->print('<tr bgcolor="#CCCCFF">');
  299:                 $r->print('<td>
  300: <select name="action">
  301:   <option value=""></option>
  302:   <option value="delete">'.&mt("Delete").'</option>
  303:   <option value="rename">'.&mt("Rename").'</option>
  304: </select>
  305: <input type="submit" name="doit" value="Go" />
  306: </td>');
  307:                 $r->print('<td><img src="'.$iconpath.'unknown.gif"></td>');
  308:                 $r->print('<td><a href="'.$href_location.$fileName.'">'.
  309: 			  $fileName.'</a></td>'); 
  310:                 $r->print('<td>'.$size.'</td>');
  311:                 $r->print('<td>'.&Apache::lonlocal::locallocaltime($mtime).'</td>');
  312:                 $r->print('</tr>'); 
  313:             }
  314:         }
  315:     }
  316: #   <tr bgcolor="#FFAA99"> pink bg 
  317: #   <tr bgcolor="#CCCCFF"> blue bg            
  318: #   $r->print(displayDirectory($currentPath, $currentFile, @dirList));
  319: #    $r->print('</td>><td>');
  320: #   $r->print(displayActions($currentPath, $currentFile, $isEmpty));
  321:     $r->print('</table></form>');
  322:     $r->print('</blockquote>');
  323:     $r->print("</body>\n</html>\n");
  324:     $r->rflush();
  325:     return OK;
  326: }
  327: 
  328: 1;
  329: __END__

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