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

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.10      banghart   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
1.8       albertel   38:     my ($fileName, $currentPath) = @_;
                     39:     my $anchor = '<a href="/adm/portfolio?selectfile='.$fileName.'&currentpath='.$currentPath.'">'.$fileName.'</a>';
                     40:     return $anchor;
1.6       banghart   41: }
                     42: sub displayDirectory {
1.10      banghart   43:     # returns html with <br /> separated contents of the directory
                     44:     # returns a <strong>currentFile</strong> (bolds the selected file/dir)
1.8       albertel   45:     my ($currentPath, $currentFile, $isDir, @dirList,) = @_;
1.10      banghart   46:     my $displayOut='';  
1.8       albertel   47:     my $fileName;
                     48:     my $upPath;
1.10      banghart   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 />';
1.8       albertel   61:     } else {
1.10      banghart   62:         $displayOut = $displayOut.'at root '.$currentPath.'<br />';
1.8       albertel   63:     }
1.10      banghart   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:     	
1.8       albertel   85:     }
                     86:     return $displayOut;
1.6       banghart   87: }
                     88: sub displayActions {
1.10      banghart   89:     # returns html to offer user appropriate actions depending on selected file/directory
1.8       albertel   90:     my $displayOut;
1.10      banghart   91:     my ($currentPath, $currentFile, $isDir, $isEmpty) = @_;
1.8       albertel   92: #   $displayOut = 'here are actions for '.$currentFile;
1.10      banghart   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 />';
1.8       albertel  143:     }
1.10      banghart  144:     $displayOut = $displayOut.'<hr />Add a file to '.$currentPath;
1.8       albertel  145:     # file upload form 
                    146:     $displayOut = $displayOut.'<form method="post" enctype="multipart/form-data">';
1.9       albertel  147:     $displayOut = $displayOut.'<input name="uploaddoc" type="file" />'.
1.10      banghart  148:         '<input type="hidden" name="currentpath" value="'.$currentPath.'" />'.
                    149:         '<input type="submit" name="storeupl" value="Upload" />'.
                    150:         '</form><hr />';
1.8       albertel  151:     $displayOut = $displayOut.'<form method="POST">
1.10      banghart  152:         <input name="subdir" type="text" />
                    153:         <input type="submit" value="Create Subdirectory" />
                    154:         </form>
                    155:             ';
1.8       albertel  156:     return $displayOut;
1.6       banghart  157: }
1.1       banghart  158: sub handler {
1.8       albertel  159:     # this handles file management
1.10      banghart  160:     my $r = shift;
1.8       albertel  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;
1.10      banghart  169:     my $isEmpty;
1.8       albertel  170:     # send header
1.9       albertel  171:     # FIXME need to start using
1.10      banghart  172:     &Apache::loncommon::no_cache($r);
                    173:     &Apache::loncommon::content_type($r, 'text/html');
                    174:     &Apache::loncommon::bodytag('Portfolio Managment', 'bgcolor="dogfood"');
1.8       albertel  175:     $r->send_http_header;
                    176:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.10      banghart  177:                                             ['selectfile','currentpath', 'currentfile']);
1.8       albertel  178:     # currentPath and currentFile need to be set for the rest of things to happen
                    179:     # sometimes, currentFile will be passed by a form field, selectedfile
                    180:     # if there is no 'form.selectedfile' then the current directory is 
                    181:     # considered as selected 
1.11      banghart  182:     if ($ENV{'form.currentpath'}) {
1.10      banghart  183:         $currentPath = $ENV{'form.currentpath'};
1.11      banghart  184:     } else {
1.10      banghart  185:         $currentPath = '/';
1.8       albertel  186:     }
1.11      banghart  187:     if ($ENV{'form.selectfile'}) {
1.10      banghart  188:         # have to check if the selected file is a subdirectory
                    189:         if ($ENV{'form.selectfile'} =~ /-\(Dir\)/){
                    190:             # $currentPath =~ /\-\(Dir\)/;
                    191:             $currentPath = $`.'/';
                    192:             $r->print('<br />'.$currentPath.'<br />');
                    193:         }
                    194:         $currentFile = $ENV{'form.selectfile'};
1.11      banghart  195:     } else {
1.10      banghart  196:         $currentFile = '';
1.8       albertel  197:     }
                    198:     # if we're uploading a file, we need to do it early so it will show in the directory list
1.11      banghart  199:     if ($ENV{'form.uploaddoc.filename'}) {
                    200:         $r->print($ENV{'form.storeupl'}.'<br />');
                    201:         $r->print(&Apache::lonnet::userfileupload('uploaddoc','','portfolio'.$currentPath).'<br />');  
1.8       albertel  202:     }
                    203:     # similarly, we need to delete or rename files before getting directory list
1.10      banghart  204:     if ($ENV{'form.selectfile'}){
1.11      banghart  205:         if ($ENV{'form.fileaction'} eq 'delete') {
1.10      banghart  206:             $r->print('<br />trying to delete '.$currentPath.$ENV{'form.selectfile'}.'<br />');
                    207:             $r->print(&Apache::lonnet::removeuserfile($ENV{'user.name'}, $ENV{'user.domain'},'portfolio'.$currentPath.$ENV{'form.selectfile'}));
                    208:             $currentFile = '';
1.11      banghart  209:         } elsif ($ENV{'form.fileaction'} eq 'rename') {
1.10      banghart  210:             &Apache::lonnet::portfoliomanage($currentPath.$ENV{'form.selectfile'}, 'rename', $currentPath.$ENV{'form.filenewname'} );
                    211:         }
1.8       albertel  212:     }
                    213:     # we always need $dirList, plus this will return information about the current file
                    214:     # as well as information about he home server directory structure, specifically
1.10      banghart  215:     # the path to the users userfiles directory.    
1.12    ! banghart  216:     my $portfolioRoot = '/home/httpd/lonUsers/'.$ENV{'user.domain'}.'/';
        !           217:     $ENV{'user.name'} =~ /^(.?)(.?)(.?)/;
        !           218:     $portfolioRoot = $portfolioRoot.$1.'/'.$2.'/'.$3.'/'.$ENV{'user.name'}.'/userfiles/portfolio';
        !           219:     my @list = &Apache::lonnet::dirlist($currentPath,  $ENV{'user.domain'}, $ENV{'user.name'}, $portfolioRoot);
        !           220:     @dirList = @list;
        !           221:     if (@dirList == 2) { # need to know if directory is empty so it can be removed if desired
1.10      banghart  222:         $isEmpty = 1;
                    223:     }else{
                    224:         $isEmpty = 0;
                    225:     }
1.8       albertel  226:     # Stuff to maintain proper setting for selected file
1.11      banghart  227:     if ($ENV{'form.selectfile'}) {
                    228:         if ($ENV{'form.fileaction'} eq 'delete') {
1.10      banghart  229:             &Apache::lonnet::portfoliomanage($ENV{'form.selectfile'}, 'delete', undef );
                    230:             $ENV{'portfolio.file'} = 'Selected File Deleted';
1.11      banghart  231:         } elsif ($ENV{'form.fileaction'} eq 'rename') {
                    232:             &Apache::lonnet::portfoliomanage($ENV{'form.selectfile'}, 'rename', $ENV{'form.filenewname'});
                    233:         } else {
1.10      banghart  234:         # Remember user's file selection for later
                    235:         $ENV{'portfolio.file'} = $ENV{'form.selectfile'};
                    236:         # offer things user can do with selected file
                    237:         }
                    238:     }else{
                    239:         unless ($ENV{'portfolio.file'}){
                    240:             $ENV{'portfolio.file'} = 'No File Selected';
                    241:         }
1.8       albertel  242:     }
                    243:     ##############################
                    244:     #
                    245:     # Display begins here
                    246:     #
                    247:     ##############################
1.11      banghart  248:     $r->print('<table border=1><tr><td>');
                    249:     $r->print(displayDirectory($currentPath, $currentFile, $isDir, @dirList));
                    250:     $r->print('</td>><td>');
                    251:     $r->print(displayActions($currentPath, $currentFile, $isDir, $isEmpty));
                    252:     $r->print('</td>></tr></table>');
                    253:     $r->print('</blockquote></body>');
1.8       albertel  254:     return OK;
1.2       banghart  255: }
1.1       banghart  256: 
                    257: 1;
                    258: __END__

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