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.'¤tpath='.$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.'¤tpath='.$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: # send header
171: # FIXME need to start using
172: &Apache::loncommon::no_cache($r);
173: &Apache::loncommon::content_type($r, 'text/html');
174: &Apache::loncommon::bodytag('Portfolio Managment', 'bgcolor="dogfood"');
175: #
176: # $r->content_type('text/html');
177: $r->send_http_header;
178: # $r->print('<html><head><title>'.
179: # 'Portfolio Management'.
180: # "</title></head>\n");
181: #
182: # FIXME need to start using
183: # $r->print('
184: # <body bgcolor="dogfood">
185: # <blockquote>');
186:
187: # $r->content_type('text/html');
188: # $r->send_http_header;
189: # $r->print('<html><head><title>'.
190: # 'Portfolio Management'.
191: # "</title></head>\n");
192: #
193: # $r->print ('
194: # <body bgcolor="dogfood">
195: # <blockquote>');
196: #grab stuff that was sent
197: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
198: ['selectfile','currentpath', 'currentfile']);
199: # $r->print ('<br />CP= '.$ENV{'form.currentpath'}.'<br />');
200: # currentPath and currentFile need to be set for the rest of things to happen
201: # sometimes, currentFile will be passed by a form field, selectedfile
202: # if there is no 'form.selectedfile' then the current directory is
203: # considered as selected
204: if ($ENV{'form.currentpath'}){
205: $currentPath = $ENV{'form.currentpath'};
206: }else{
207: $currentPath = '/';
208: }
209: if ($ENV{'form.selectfile'}){
210: $r->print('<br />found selectfile'.$ENV{'form.selectfile'} .'<br />');
211: # have to check if the selected file is a subdirectory
212: if ($ENV{'form.selectfile'} =~ /-\(Dir\)/){
213: # $currentPath =~ /\-\(Dir\)/;
214: $currentPath = $`.'/';
215: $r->print('<br />'.$currentPath.'<br />');
216: }
217: $currentFile = $ENV{'form.selectfile'};
218: }else{
219: $currentFile = '';
220: }
221: # if we're uploading a file, we need to do it early so it will show in the directory list
222: if ($ENV{'form.uploaddoc.filename'}){
223: $r->print ($ENV{'form.storeupl'}.'<br />');
224: $r->print (&Apache::lonnet::userfileupload('uploaddoc','','portfolio'.$currentPath).'<br />');
225: }
226: # similarly, we need to delete or rename files before getting directory list
227: if ($ENV{'form.selectfile'}){
228: if ($ENV{'form.fileaction'} eq 'delete'){
229: $r->print('<br />trying to delete '.$currentPath.$ENV{'form.selectfile'}.'<br />');
230: $r->print(&Apache::lonnet::removeuserfile($ENV{'user.name'}, $ENV{'user.domain'},'portfolio'.$currentPath.$ENV{'form.selectfile'}));
231: $currentFile = '';
232: }elsif($ENV{'form.fileaction'} eq 'rename') {
233: &Apache::lonnet::portfoliomanage($currentPath.$ENV{'form.selectfile'}, 'rename', $currentPath.$ENV{'form.filenewname'} );
234: # $r->print ('We will rename your file');
235: }
236: }
237: # we always need $dirList, plus this will return information about the current file
238: # as well as information about he home server directory structure, specifically
239: # the path to the users userfiles directory.
240: # $r->print('dir list follows<br />'.&Apache::lonnet::portfoliolist($currentPath, $currentFile).'<br />');
241: my $list = &Apache::lonnet::portfoliolist($currentPath, $currentFile);
242: @dirList = split(/:/,$list);
243: # foreach my $line (@dirlist) {
244: #
245: # #$strip holds directory/file name
246: # #$dom
247: # my ($strip,$dom,undef,$testdir,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,$obs,undef)=split(/\&/,$line,16);
248: # $r->print ($strip.' '.$testdir.'<br />');
249: # }
250: # @dirList = split /<br\s\/>/, (&Apache::lonnet::portfoliolist($currentPath, $currentFile));
251:
252: # portfoliolist returns isdir, isfile and udir as the final array elements
253: # we'll pop them off the bottom of the array, and put them where they belong
254:
255: # $londcall added to help debug, contains the command sent to lond
256: # my $londcall = pop @dirList;
257: # $r->print ('<br />udir '.$londcall.'<br />');
258: # $udir = pop @dirList;
259: # $r->print ('<br />path returned '.$udir.'<br />');
260: # $isFile = pop @dirList;
261: # $r->print ('<br />isfile '.$isFile.'<br />');
262: # $isDir = pop @dirList;
263: if (@dirList == 2){ # need to know if directory is empty to it can be removed if desired
264: $isEmpty = 1;
265: }else{
266: $isEmpty = 0;
267: }
268: # $r->print ('<br />lines left ind dirlist '.@dirList.'<br />');
269: # return OK if $r->header_only;
270: # Stuff to maintain proper setting for selected file
271: if ($ENV{'form.selectfile'}){
272: if ($ENV{'form.fileaction'} eq 'delete'){
273: &Apache::lonnet::portfoliomanage($ENV{'form.selectfile'}, 'delete', undef );
274: $ENV{'portfolio.file'} = 'Selected File Deleted';
275: }elsif($ENV{'form.fileaction'} eq 'rename') {
276: &Apache::lonnet::portfoliomanage($ENV{'form.selectfile'}, 'rename', $ENV{'form.filenewname'} );
277: # $r->print ('We will rename your file');
278: }else{
279:
280: # Remember user's file selection for later
281: $ENV{'portfolio.file'} = $ENV{'form.selectfile'};
282: # offer things user can do with selected file
283: }
284: }else{
285: unless ($ENV{'portfolio.file'}){
286: $ENV{'portfolio.file'} = 'No File Selected';
287: }
288: }
289: ##############################
290: #
291: # Display begins here
292: #
293: ##############################
294: $r->print ('<hr /> start ');
295: $r->print ($udir);
296: $r->print ('<table border=1><tr><td>');
297: $r->print (displayDirectory ($currentPath, $currentFile, $isDir, @dirList));
298: $r->print ('</td>><td>');
299: $r->print (displayActions ($currentPath, $currentFile, $isDir, $isEmpty));
300: $r->print ('</td>></tr></table>');
301: $r->print ('<br />end display<br /><hr />');
302: $r->print ('</blockquote></body>');
303: return OK;
304: }
305:
306: 1;
307: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>