Annotation of loncom/interface/portfolio.pm, revision 1.9

1.3       banghart    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: 
1.1       banghart   27: package Apache::portfolio;
                     28: use strict;
                     29: use Apache::Constants qw(:common :http);
1.2       banghart   30: use Apache::loncommon;
1.1       banghart   31: use Apache::lonnet;
1.2       banghart   32: use Apache::lontexconvert;
                     33: use Apache::lonfeedback;
                     34: use Apache::lonlocal;
1.8       albertel   35: 
                     36: # receives a file name assumed to reside in username/userfiles/portfolio/
                     37: # returns a form consisting of a single submit button labeled with the filename
                     38: sub makeAnchor {
                     39:     my ($fileName, $currentPath) = @_;
                     40:     my $anchor = '<a href="/adm/portfolio?selectfile='.$fileName.'&currentpath='.$currentPath.'">'.$fileName.'</a>';
                     41: #    my $button = '
1.6       banghart   42: #		<form method="POST" action="/adm/portfolio">
1.9     ! albertel   43: #		<input type="hidden" name="selectfile" value="'.$fileName.'" />
        !            44: #		<input type="submit" value="'.$fileName.'" name="storeupl" />
1.6       banghart   45: #		</form>
                     46: #		';
1.8       albertel   47:     return $anchor;
1.6       banghart   48: }
1.8       albertel   49: 
                     50: # returns html with <br /> separated contents of the directory
                     51: # returns a <strong>currentFile</strong> (bolds the selected file/dir)
1.6       banghart   52: sub displayDirectory {
1.8       albertel   53:     my ($currentPath, $currentFile, $isDir, @dirList,) = @_;
                     54:     my $displayOut='';	
                     55:     my $fileName;
                     56:     my $upPath;
                     57:     if ($currentPath ne '/') {
                     58: 	$displayOut = 'Listing of '.$currentPath.'<br /><hr />'.
                     59: 	    # provides the "up one directory level" function
                     60: 	    # it means shortening the currentpath to the parent directory
                     61: 	    $currentPath =~ m:(^/.*)(/.*/$ ):;
                     62: 	if ($1 ne '/') {
                     63: 	    $upPath = $1.'/';
1.6       banghart   64: 	} else {
1.8       albertel   65: 	    $upPath = $1;
1.6       banghart   66: 	}
1.8       albertel   67: 		
                     68: 	$displayOut = $displayOut.'<a href="/adm/portfolio?selectfile=updir&currentpath='.$upPath.'">..</a><br />';
                     69:     } else {
                     70: 	$displayOut = $displayOut.'at root '.$currentPath.'<br />';
                     71:     }
                     72:     while ($fileName = shift @dirList) {
                     73: 	if (($fileName ne './') && ($fileName ne '../')) {
                     74: 	    if ($fileName =~ m:/$:) {
                     75: 		# handle directories different from files
                     76: 		if ($fileName eq $currentFile) {
                     77: 		    #checks to bold the selected file
                     78: 		    $displayOut = $displayOut.'<strong>'.(&makeAnchor($fileName, $currentPath.$fileName).'</strong><br />');
                     79: 		} else {
                     80: 		    $displayOut = $displayOut.(&makeAnchor($fileName, $currentPath.$fileName).'<br />');
                     81: 		}
                     82: 	    } else {
                     83: 		if ($fileName eq $currentFile) {
                     84: 		    #checks to bold the selected file
                     85: 		    $displayOut = $displayOut.'<strong>'.(&makeAnchor($fileName, $currentPath).'</strong><br />');
                     86: 		} else {
                     87: 		    $displayOut = $displayOut.(&makeAnchor($fileName, $currentPath).'<br />');
                     88: 		}
                     89: 	    }
                     90: 	}
                     91:     }
                     92:     #$displayOut = $displayOut.
                     93:     return $displayOut;
1.6       banghart   94: }
1.8       albertel   95: 
                     96: # returns html to offer user appropriate actions depending on selected
                     97: # file/directory
1.6       banghart   98: sub displayActions {
1.8       albertel   99:     my $displayOut;
                    100:     my ($currentPath, $currentFile, $isDir, $isFile) = @_;
                    101: #   $displayOut = 'here are actions for '.$currentFile;
                    102:     if ($isDir){
                    103: 	$displayOut = 'Directory';
                    104:     }
                    105:     if ($isFile){
                    106: 	$displayOut = 'File';
                    107:     }
                    108:     
                    109:     $displayOut = $displayOut.'<form method="POST">
1.6       banghart  110: 			<input type="hidden" name="selectfile" 
1.9     ! albertel  111: 			value="'.$currentFile.'" />
        !           112: 			<input type="hidden" name="fileaction" value="delete" /> 
1.6       banghart  113: 			<center>
                    114: 			<input type="submit" 
                    115: 			
1.9     ! albertel  116: 			value="Delete '.$currentFile.'" />
1.6       banghart  117: 			</center>
                    118: 			</form>
                    119: 			<hr />
                    120: 			<form method="POST">
                    121: 			<input type="hidden" name="selectfile" 
1.9     ! albertel  122: 			value="'.$currentFile.'" />
        !           123: 			<input type="hidden" name="fileaction" value="rename" /> 
        !           124: 			<input type="input" name="filenewname" value="Type new name here" /> 
1.6       banghart  125: 			<input type="submit" 
1.9     ! albertel  126: 			value="Rename '.$currentFile.'" />
1.6       banghart  127: 			</form>
1.9     ! albertel  128: 			<hr />';
        !           129:     $displayOut = $displayOut.'<hr />Add a file to your portfolio';
1.8       albertel  130:     # file upload form 
                    131:     $displayOut = $displayOut.'<form method="post" enctype="multipart/form-data">';
1.9     ! albertel  132:     $displayOut = $displayOut.'<input name="uploaddoc" type="file" />'.
        !           133: 	'<input type="hidden" name="currentpath" value="'.$currentPath.'" />'.
        !           134: 	'<input type="submit" name="storeupl" value="Upload" />'.
1.8       albertel  135: 	'</form><hr>';
                    136:     $displayOut = $displayOut.'<form method="POST">
1.9     ! albertel  137: 		<input name="subdir" type="text" />
        !           138: 		<input type="submit" value="Create Subdirectory" />
1.6       banghart  139: 		</form>
                    140: 			';
1.8       albertel  141:     return $displayOut;
1.6       banghart  142: }
1.8       albertel  143: 
1.1       banghart  144: sub handler {
1.8       albertel  145:     # this handles file management
                    146:     my ($r)=@_;
                    147:     my @dirList; # will hold directory listing as array
                    148:     my $udir; # returned from home server
                    149:     my $currentPath; # path assuming /userfiles/portfolio/ as root
                    150:     my $currentFile; # directory or file contained in $pathToRoot.$currentPath
                    151:     my $action; # delete, rename, makedirectory, removedirectory,
                    152:     my $filenewname; # for rename action (guess what we do with it!)
                    153:     my $isFile;
                    154:     my $isDir;
                    155:     # send header
1.9     ! albertel  156:     # FIXME need to start using
        !           157:     # &Apache::loncommon::content_type()
1.8       albertel  158:     $r->content_type('text/html');
                    159:     $r->send_http_header;
                    160:     $r->print('<html><head><title>'.
1.6       banghart  161:               'Portfolio Management'.
                    162:               "</title></head>\n");
                    163: 
1.9     ! albertel  164:     # FIXME need to start using
        !           165:     # &Apache::loncommon::bodytag()
1.8       albertel  166:     $r->print('
1.6       banghart  167: 	<body bgcolor="dogfood">
                    168: 	<blockquote>');
1.8       albertel  169:     #grab stuff that was sent
                    170:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                    171:                                             ['selectfile','currentpath',
                    172: 					     'currentfile']);
                    173:     $r->print ('<br />CP= '.$ENV{'form.currentpath'}.'<br />');
                    174:     # currentPath and currentFile need to be set for the rest of things to happen
                    175:     # sometimes, currentFile will be passed by a form field, selectedfile
                    176:     # if there is no 'form.selectedfile' then the current directory is 
                    177:     # considered as selected 
                    178:     if ($ENV{'form.currentpath'}) {
                    179: 	$currentPath = $ENV{'form.currentpath'};
                    180:     } else {
                    181: 	$currentPath = '/';
                    182:     }
                    183:     if ($ENV{'form.selectfile'}) {
                    184: 	$r->print('<br />found selectfile'.$ENV{'form.selectfile'} .'<br />');
                    185: 	# have to check if the selected file is a subdirectory
                    186: 	if ($ENV{'form.selectfile'} =~ /-\(Dir\)/) {
                    187: 	    # $currentPath =~ /\-\(Dir\)/;
                    188: 	    $currentPath = $`.'/';
                    189: 	    $r->print('<br />'.$currentPath.'<br />');
                    190: 	}
                    191: 	$currentFile = $ENV{'form.selectfile'};
                    192:     } else {
                    193: 	$currentFile = '';
                    194:     }
                    195:     # if we're uploading a file, we need to do it early so it will show in the directory list
                    196:     if ($ENV{'form.uploaddoc.filename'}) {
                    197: 	$r->print($ENV{'form.storeupl'}.'<br />');
                    198: 	$r->print(&Apache::lonnet::userfileupload('uploaddoc','','portfolio'.$currentPath).'<br />');	
                    199:     }
                    200:     # similarly, we need to delete or rename files before getting directory list
                    201:     if ($ENV{'form.selectfile'}) {
                    202: 	if ($ENV{'form.fileaction'} eq 'delete') {
                    203: 	    &Apache::lonnet::portfoliomanage($ENV{'form.selectfile'}, 'delete', undef );
                    204: 	    $currentFile = '';
                    205: 	} elsif($ENV{'form.fileaction'} eq 'rename') {
                    206: 	    &Apache::lonnet::portfoliomanage($ENV{'form.selectfile'}, 'rename', $ENV{'form.filenewname'} );
                    207: 	    # $r->print('We will rename your file');
                    208: 	}
                    209:     }
                    210:     # we always need $dirList, plus this will return information about the current file
                    211:     # as well as information about he home server directory structure, specifically
                    212:     # the path to the users userfiles directory.	
                    213:     @dirList = split /<br\s\/>/, (&Apache::lonnet::portfoliolist($currentPath, $currentFile));
                    214:     # portfoliolist returns isdir, isfile and udir as the final array elements
                    215:     # we'll pop them off the bottom of the array, and put them where they belong
                    216:     
                    217:     # $londcall added to help debug
                    218:     my $londcall = pop(@dirList);
                    219:     $r->print('<br />udir '.$londcall.'<br />');
                    220:     $udir = pop(@dirList);
                    221:     $r->print('<br />path returned '.$udir.'<br />');
                    222:     $isFile = pop(@dirList);
                    223: #   $r->print('<br />isfile '.$isFile.'<br />');
                    224:     $isDir = pop(@dirList);
                    225: #   $r->print('<br />isdir '.$isDir.'<br />');
                    226: #   return OK if $r->header_only;
                    227:     # Stuff to maintain proper setting for selected file
                    228:     if ($ENV{'form.selectfile'}) {
                    229: 	if ($ENV{'form.fileaction'} eq 'delete') {
                    230: 	    &Apache::lonnet::portfoliomanage($ENV{'form.selectfile'}, 'delete', undef );
                    231: 	    $ENV{'portfolio.file'} = 'Selected File Deleted';
                    232: 	} elsif($ENV{'form.fileaction'} eq 'rename') {
                    233: 	    &Apache::lonnet::portfoliomanage($ENV{'form.selectfile'}, 'rename', $ENV{'form.filenewname'} );
                    234: #			$r->print('We will rename your file');
                    235: 	} else {
                    236: 	    
                    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('<hr /> start ');
                    252:     $r->print($udir);
1.9     ! albertel  253:     $r->print('<table border="1"><tr><td>');
1.8       albertel  254:     $r->print(&displayDirectory($currentPath, $currentFile, $isDir, @dirList));
1.9     ! albertel  255:     $r->print('</td><td>');
1.8       albertel  256:     $r->print(&displayActions($currentPath, $currentFile, $isDir, $isFile));
1.9     ! albertel  257:     $r->print('</td></tr></table>');
1.8       albertel  258:     $r->print('<br />end display<br /><hr />');
                    259:     $r->print('</blockquote></body>');
                    260:     return OK;
1.2       banghart  261: }
1.1       banghart  262: 
                    263: 1;
                    264: __END__

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