Annotation of loncom/interface/portfolio.pm, revision 1.14
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.'¤tpath='.$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.'¤tpath='.$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;
170: &Apache::loncommon::no_cache($r);
1.14 ! banghart 171: &Apache::loncommon::content_type($r,'text/html');
1.8 albertel 172: $r->send_http_header;
1.14 ! banghart 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();
1.8 albertel 179: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.10 banghart 180: ['selectfile','currentpath', 'currentfile']);
1.8 albertel 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
1.11 banghart 185: if ($ENV{'form.currentpath'}) {
1.10 banghart 186: $currentPath = $ENV{'form.currentpath'};
1.11 banghart 187: } else {
1.10 banghart 188: $currentPath = '/';
1.8 albertel 189: }
1.11 banghart 190: if ($ENV{'form.selectfile'}) {
1.10 banghart 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'};
1.11 banghart 198: } else {
1.10 banghart 199: $currentFile = '';
1.8 albertel 200: }
201: # if we're uploading a file, we need to do it early so it will show in the directory list
1.11 banghart 202: if ($ENV{'form.uploaddoc.filename'}) {
203: $r->print($ENV{'form.storeupl'}.'<br />');
204: $r->print(&Apache::lonnet::userfileupload('uploaddoc','','portfolio'.$currentPath).'<br />');
1.8 albertel 205: }
206: # similarly, we need to delete or rename files before getting directory list
1.10 banghart 207: if ($ENV{'form.selectfile'}){
1.11 banghart 208: if ($ENV{'form.fileaction'} eq 'delete') {
1.10 banghart 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 = '';
1.11 banghart 212: } elsif ($ENV{'form.fileaction'} eq 'rename') {
1.10 banghart 213: &Apache::lonnet::portfoliomanage($currentPath.$ENV{'form.selectfile'}, 'rename', $currentPath.$ENV{'form.filenewname'} );
214: }
1.8 albertel 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
1.10 banghart 218: # the path to the users userfiles directory.
1.12 banghart 219: my $portfolioRoot = '/home/httpd/lonUsers/'.$ENV{'user.domain'}.'/';
220: $ENV{'user.name'} =~ /^(.?)(.?)(.?)/;
221: $portfolioRoot = $portfolioRoot.$1.'/'.$2.'/'.$3.'/'.$ENV{'user.name'}.'/userfiles/portfolio';
222: my @list = &Apache::lonnet::dirlist($currentPath, $ENV{'user.domain'}, $ENV{'user.name'}, $portfolioRoot);
223: @dirList = @list;
224: if (@dirList == 2) { # need to know if directory is empty so it can be removed if desired
1.10 banghart 225: $isEmpty = 1;
1.13 banghart 226: } else {
1.10 banghart 227: $isEmpty = 0;
228: }
1.8 albertel 229: # Stuff to maintain proper setting for selected file
1.11 banghart 230: if ($ENV{'form.selectfile'}) {
231: if ($ENV{'form.fileaction'} eq 'delete') {
1.10 banghart 232: &Apache::lonnet::portfoliomanage($ENV{'form.selectfile'}, 'delete', undef );
233: $ENV{'portfolio.file'} = 'Selected File Deleted';
1.11 banghart 234: } elsif ($ENV{'form.fileaction'} eq 'rename') {
235: &Apache::lonnet::portfoliomanage($ENV{'form.selectfile'}, 'rename', $ENV{'form.filenewname'});
236: } else {
1.10 banghart 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: }
1.8 albertel 245: }
246: ##############################
247: #
248: # Display begins here
249: #
250: ##############################
1.11 banghart 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>');
1.14 ! banghart 256: $r->print('</blockquote>');
! 257: $r->print("</body>\n</html>\n");
! 258: $r->rflush();
1.8 albertel 259: return OK;
1.2 banghart 260: }
1.1 banghart 261:
262: 1;
263: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>