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 make_anchor {
36: my ($filename, $current_path) = @_;
37: my $anchor = '<a href="/adm/portfolio?selectfile='.$filename.'¤tpath='.$current_path.'">'.$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 display_directory {
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.'¤tpath='.$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) {
71: #checks to bold the selected file
72: $displayOut.= '<strong>'.(&make_anchor($filename.'/', $filename.'/').'</strong><br />'."\n");
73: } else {
74: $displayOut.= (&make_anchor($filename.'/', $filename.'/').'<br />'."\n");
75: }
76: } else {
77: if ($filename eq $currentFile) {
78: #checks to bold the selected file
79: $displayOut.='<strong>'.(&make_anchor($filename, $currentPath).'</strong><br />'."\n");
80: } else {
81: $displayOut.=(&make_anchor($filename, $currentPath).'<br />'."\n");
82: }
83: }
84:
85: }
86:
87: }
88: return $displayOut;
89: }
90:
91: sub displayActions {
92: # returns html to offer user appropriate actions depending on selected file/directory
93: my $displayOut;
94: my ($currentPath, $currentFile, $isEmpty) = @_;
95: # $displayOut = 'here are actions for '.$currentFile;
96: if ($currentFile =~ m:/$:) {
97: # if the selected file is a directory, these are the options
98: # offer the chance to delete the directory only if it is empty
99: if ($isEmpty && ($currentPath ne '/')) {
100: $displayOut = $displayOut.'<form method="POST">
101: <input type="hidden" name="selectfile"
102: value="'.$currentFile.'" />
103: <input type="hidden" name="fileaction" value="delete" />
104: <input type="hidden" name="currentpath" value="'.$currentPath.'" />
105: <center>
106: <input type="submit"
107: value="Delete '.$currentFile.'" />
108: </center>
109: </form>';
110: }
111: if ($currentPath ne '/') {
112: $displayOut = $displayOut.'<hr />
113: <form method="POST">
114: <input type="hidden" name="selectfile"
115: value="'.$currentFile.'" />
116: <input type="hidden" name="fileaction" value="rename" />
117: <input type="hidden" name="currentpath" value="'.$currentPath.'" />
118: <input type="input" name="filenewname" value="Type new name here" />
119: <input type="submit"
120: value="Rename '.$currentFile.'" />
121: </form>';
122: }
123: } else { #action options offered for files
124: $displayOut = $displayOut.'<form method="POST">';
125: $displayOut = $displayOut.'<input type="hidden" name="selectfile"';
126: $displayOut = $displayOut.'value="'.$currentFile;
127: $displayOut = $displayOut.'" /><input type="hidden" name="fileaction" value="delete" />
128: <input type="hidden" name="currentpath" value="'.$currentPath.'" />
129: <center>
130: <input type="submit"
131: value="Delete '.$currentFile.'" />
132: </center>
133: </form>';
134:
135: $displayOut = $displayOut.'<hr />
136: <form method="POST">
137: <input type="hidden" name="selectfile"
138: value="'.$currentFile.'" />
139: <input type="hidden" name="fileaction" value="rename" />
140: <input type="hidden" name="currentpath" value="'.$currentPath.'" />
141: <input type="input" name="filenewname" value="Type new name here" />
142: <input type="submit"
143: value="Rename '.$currentFile.'" />
144: </form>
145: <hr />';
146: }
147: $displayOut = $displayOut.'<hr />Add a file to '.$currentPath;
148: # file upload form
149: $displayOut = $displayOut.'<form method="post" enctype="multipart/form-data">';
150: $displayOut = $displayOut.'<input name="uploaddoc" type="file" />'.
151: '<input type="hidden" name="currentpath" value="'.$currentPath.'" />'.
152: '<input type="submit" name="storeupl" value="Upload" />'.
153: '</form><hr />';
154: $displayOut = $displayOut.'<form method="POST">
155: <input name="subdir" type="text" />
156: <input type="submit" value="Create Subdirectory" />
157: </form>
158: ';
159: return $displayOut;
160: }
161:
162: sub handler {
163: # this handles file management
164: my $r = shift;
165: my $iconpath= $r->dir_config('lonIconsURL') . "/";
166: my @dirList; # will hold directory listing as array
167: my $udir; # returned from home server
168: my $currentPath; # path assuming /userfiles/portfolio/ as root
169: my $currentFile; # directory or file contained in $pathToRoot.$currentPath
170: my $action; # delete, rename, makedirectory, removedirectory,
171: my $filenewname; # for rename action (guess what we do with it!)
172: my $isFile;
173: my $isEmpty;
174: my $dirptr=16384;
175: &Apache::loncommon::no_cache($r);
176: &Apache::loncommon::content_type($r,'text/html');
177: $r->send_http_header;
178: # Give the LON-CAPA page header
179: $r->print('<html><head><title>'.
180: &mt('Portfolio Manager').
181: "</title></head>\n".
182: &Apache::loncommon::bodytag('Portfolio Manager'));
183: $r->rflush();
184: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
185: ['selectfile','currentpath', 'currentfile']);
186: # currentPath and currentFile need to be set for the rest of things to happen
187: # sometimes, currentFile will be passed by a form field, selectedfile
188: # if there is no 'form.selectedfile' then the current directory is
189: # considered as selected
190: if ($ENV{'form.currentpath'}) {
191: $currentPath = $ENV{'form.currentpath'};
192: } else {
193: $currentPath = '/';
194: }
195: if ($ENV{'form.selectfile'}) {
196: # have to check if the selected file is a subdirectory
197: if ($ENV{'form.selectfile'} =~ /-\(Dir\)/) {
198: # $currentPath =~ /\-\(Dir\)/;
199: $currentPath = $`.'/';
200: $r->print('<br />'.$currentPath.'<br />');
201: }
202: $currentFile = $ENV{'form.selectfile'};
203: } else {
204: $currentFile = '';
205: }
206: # if we're uploading a file, we need to do it early so it will show in the directory list
207: if ($ENV{'form.uploaddoc.filename'}) {
208: $r->print($ENV{'form.storeupl'}.'<br />');
209: $r->print(&Apache::lonnet::userfileupload('uploaddoc','','portfolio'.$currentPath).'<br />');
210: }
211: # similarly, we need to delete or rename files before getting directory list
212: if ($ENV{'form.selectfile'}) {
213: if ($ENV{'form.fileaction'} eq 'delete') {
214: $r->print('<br />trying to delete '.$currentPath.$ENV{'form.selectfile'}.'<br />');
215: $r->print(&Apache::lonnet::removeuserfile($ENV{'user.name'}, $ENV{'user.domain'},'portfolio'.$currentPath.$ENV{'form.selectfile'}));
216: $currentFile = '';
217: } elsif ($ENV{'form.fileaction'} eq 'rename') {
218: &Apache::lonnet::portfoliomanage($currentPath.$ENV{'form.selectfile'}, 'rename', $currentPath.$ENV{'form.filenewname'} );
219: }
220: }
221: # we always need $dirList, plus this will return information about the current file
222: # as well as information about the home server directory structure, specifically
223: # the path to the users userfiles directory.
224: #
225: my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
226: $ENV{'user.name'}).
227: '/userfiles/portfolio';
228: @dirList = &Apache::lonnet::dirlist($currentPath, $ENV{'user.domain'}, $ENV{'user.name'}, $portfolio_root);
229:
230: if (@dirList == 2) { # need to know if directory is empty so it can be removed if desired
231: $isEmpty = 1;
232: } else {
233: $isEmpty = 0;
234: }
235:
236: if ($ENV{'form.selectfile'}) {
237: if ($ENV{'form.fileaction'} eq 'delete') {
238: &Apache::lonnet::portfoliomanage($ENV{'form.selectfile'}, 'delete', undef );
239: $ENV{'portfolio.file'} = 'Selected File Deleted';
240: } elsif ($ENV{'form.fileaction'} eq 'rename') {
241: &Apache::lonnet::portfoliomanage($ENV{'form.selectfile'}, 'rename', $ENV{'form.filenewname'});
242: } else {
243: # Remember user's file selection for later
244: $ENV{'portfolio.file'} = $ENV{'form.selectfile'};
245: # offer things user can do with selected file
246: }
247: } else {
248: unless ($ENV{'portfolio.file'}){
249: $ENV{'portfolio.file'} = 'No File Selected';
250: }
251: }
252: ##############################
253: #
254: # Display begins here
255: #
256: ##############################
257: $r->print('<table border="0" cellspacing="2" cellpadding="2"><tr valign="middle">');
258: $r->print('<td bgcolor="#ccddaa" align="center">');
259: my $displayOut = '<form method="post" enctype="multipart/form-data">';
260: $displayOut = $displayOut.'<input name="uploaddoc" type="file" />'.
261: '<input type="hidden" name="currentpath" value="'.$currentPath.'" />'.
262: '<input type="submit" name="storeupl" value="Upload" />'.
263: '</form>';
264: $r->print($displayOut);
265: $r->print('</td></tr><tr><td bgcolor="#ccddaa" align="center">');
266: my $displayOut = '<form method="post">';
267: $displayOut = $displayOut.'<input name="newdir" type="input" />'.
268: '<input type="hidden" name="currentpath" value="'.$currentPath.'" />'.
269: '<input type="submit" name="createdir" value="'.&mt("Create Directory").'" />'.
270: '</form>';
271: $r->print($displayOut);
272: $r->print('</td></tr></table>');
273: my @tree = split (/\//,$currentPath);
274: $r->print('<font size="+2">'.&make_anchor('/','/'));
275: if (@tree > 1){
276: my $newCurrentPath = '';
277: for (my $i = 1; $i< @tree; $i++){
278: $newCurrentPath .= $tree[$i].'/';
279: $r->print(&make_anchor($tree[$i],'/'.$newCurrentPath).'/');
280: }
281: }
282: $r->print('</font>');
283: &Apache::lonhtmlcommon::store_recent('portfolio',$currentPath,$currentPath);
284: $r->print('<br /><form method=post action="/adm/portfolio">'.
285: &Apache::lonhtmlcommon::select_recent('portfolio','currentpath',
286: 'this.form.submit();'));
287: $r->print("</form>");
288: $r->print('<table border="0" cellspacing="2" cellpadding="2">'.
289: '<tr><th>Actions</th><th> </th><th>Name</th><th>Size</th><th>Last Modified</th></tr>');
290: my $href_location="/uploaded/$ENV{'user.domain'}/$ENV{'user.name'}/portfolio/$currentPath/";
291: foreach my $line (@dirList) {
292: #$strip holds directory/file name
293: #$dom
294: my ($filename,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef)=split(/\&/,$line,16);
295: if (($filename ne '.') && ($filename ne '..')) {
296: if ($dirptr&$testdir) {
297: $r->print('<tr bgcolor="#FFAA99"><td><img src="'.$iconpath.'folder_closed.gif"></td>');
298: $r->print('<td>Go to ...</td>');
299: $r->print('<td>'.&make_anchor($filename.'/',$currentPath.$filename.'/').'</td>');
300: $r->print('</tr>');
301: } else {
302: $r->print('<tr bgcolor="#CCCCFF">');
303: $r->print('<td>
304: <select name="action">
305: <option value=""></option>
306: <option value="delete">'.&mt("Delete").'</option>
307: <option value="rename">'.&mt("Rename").'</option>
308: </select>
309: <input type="submit" name="doit" value="Go" />
310: </td>');
311: $r->print('<td><img src="'.$iconpath.'unknown.gif"></td>');
312: $r->print('<td><a href="'.$href_location.$filename.'">'.
313: $filename.'</a></td>');
314: $r->print('<td>'.$size.'</td>');
315: $r->print('<td>'.&Apache::lonlocal::locallocaltime($mtime).'</td>');
316: $r->print('</tr>');
317: }
318: }
319: }
320: # <tr bgcolor="#FFAA99"> pink bg
321: # <tr bgcolor="#CCCCFF"> blue bg
322: # $r->print(&display_directory($currentPath, $currentFile, @dirList));
323: # $r->print('</td>><td>');
324: # $r->print(&display_actions($currentPath, $currentFile, $isEmpty));
325: $r->print('</table></form>');
326: $r->print('</blockquote>');
327: $r->print("</body>\n</html>\n");
328: $r->rflush();
329: return OK;
330: }
331:
332: 1;
333: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>