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 makeAnchor{
36: my ($fileName, $currentPath) = @_;
37: my $anchor = '<a href="/adm/portfolio?selectfile='.$fileName.'¤tpath='.$currentPath.'">'.$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 displayDirectory {
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){ #checks to bold the selected file
71: $displayOut.= '<strong>'.(makeAnchor($fileName.'/', $fileName.'/').'</strong><br />'."\n");
72: }else{
73: $displayOut.= (makeAnchor($fileName.'/', $fileName.'/').'<br />'."\n");
74: }
75: }else{
76: if ($fileName eq $currentFile){ #checks to bold the selected file
77: $displayOut.='<strong>'.(makeAnchor($fileName, $currentPath).'</strong><br />'."\n");
78: }else{
79: $displayOut.=(makeAnchor($fileName, $currentPath).'<br />'."\n");
80: }
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, $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 $iconpath= $r->dir_config('lonIconsURL') . "/";
162: my @dirList; # will hold directory listing as array
163: my $udir; # returned from home server
164: my $currentPath; # path assuming /userfiles/portfolio/ as root
165: my $currentFile; # directory or file contained in $pathToRoot.$currentPath
166: my $action; # delete, rename, makedirectory, removedirectory,
167: my $filenewname; # for rename action (guess what we do with it!)
168: my $isFile;
169: my $isEmpty;
170: my $dirptr=16384;
171: &Apache::loncommon::no_cache($r);
172: &Apache::loncommon::content_type($r,'text/html');
173: $r->send_http_header;
174: # Give the LON-CAPA page header
175: $r->print('<html><head><title>'.
176: &mt('Portfolio Manager').
177: "</title></head>\n".
178: &Apache::loncommon::bodytag('Portfolio Manager'));
179: $r->rflush();
180: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
181: ['selectfile','currentpath', 'currentfile']);
182: # currentPath and currentFile need to be set for the rest of things to happen
183: # sometimes, currentFile will be passed by a form field, selectedfile
184: # if there is no 'form.selectedfile' then the current directory is
185: # considered as selected
186: if ($ENV{'form.currentpath'}) {
187: $currentPath = $ENV{'form.currentpath'};
188: } else {
189: $currentPath = '/';
190: }
191: if ($ENV{'form.selectfile'}) {
192: # have to check if the selected file is a subdirectory
193: if ($ENV{'form.selectfile'} =~ /-\(Dir\)/){
194: # $currentPath =~ /\-\(Dir\)/;
195: $currentPath = $`.'/';
196: $r->print('<br />'.$currentPath.'<br />');
197: }
198: $currentFile = $ENV{'form.selectfile'};
199: } else {
200: $currentFile = '';
201: }
202: # if we're uploading a file, we need to do it early so it will show in the directory list
203: if ($ENV{'form.uploaddoc.filename'}) {
204: $r->print($ENV{'form.storeupl'}.'<br />');
205: $r->print(&Apache::lonnet::userfileupload('uploaddoc','','portfolio'.$currentPath).'<br />');
206: }
207: # similarly, we need to delete or rename files before getting directory list
208: if ($ENV{'form.selectfile'}){
209: if ($ENV{'form.fileaction'} eq 'delete') {
210: $r->print('<br />trying to delete '.$currentPath.$ENV{'form.selectfile'}.'<br />');
211: $r->print(&Apache::lonnet::removeuserfile($ENV{'user.name'}, $ENV{'user.domain'},'portfolio'.$currentPath.$ENV{'form.selectfile'}));
212: $currentFile = '';
213: } elsif ($ENV{'form.fileaction'} eq 'rename') {
214: &Apache::lonnet::portfoliomanage($currentPath.$ENV{'form.selectfile'}, 'rename', $currentPath.$ENV{'form.filenewname'} );
215: }
216: }
217: # we always need $dirList, plus this will return information about the current file
218: # as well as information about the home server directory structure, specifically
219: # the path to the users userfiles directory.
220: #
221: my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
222: $ENV{'user.name'}).
223: '/userfiles/portfolio';
224: @dirList = &Apache::lonnet::dirlist($currentPath, $ENV{'user.domain'}, $ENV{'user.name'}, $portfolio_root);
225:
226: if (@dirList == 2) { # need to know if directory is empty so it can be removed if desired
227: $isEmpty = 1;
228: } else {
229: $isEmpty = 0;
230: }
231:
232: if ($ENV{'form.selectfile'}) {
233: if ($ENV{'form.fileaction'} eq 'delete') {
234: &Apache::lonnet::portfoliomanage($ENV{'form.selectfile'}, 'delete', undef );
235: $ENV{'portfolio.file'} = 'Selected File Deleted';
236: } elsif ($ENV{'form.fileaction'} eq 'rename') {
237: &Apache::lonnet::portfoliomanage($ENV{'form.selectfile'}, 'rename', $ENV{'form.filenewname'});
238: } else {
239: # Remember user's file selection for later
240: $ENV{'portfolio.file'} = $ENV{'form.selectfile'};
241: # offer things user can do with selected file
242: }
243: }else{
244: unless ($ENV{'portfolio.file'}){
245: $ENV{'portfolio.file'} = 'No File Selected';
246: }
247: }
248: ##############################
249: #
250: # Display begins here
251: #
252: ##############################
253: $r->print('<table border="0" cellspacing="2" cellpadding="2"><tr valign="middle">');
254: $r->print('<td bgcolor="#ccddaa" align="center">');
255: my $displayOut = '<form method="post" enctype="multipart/form-data">';
256: $displayOut = $displayOut.'<input name="uploaddoc" type="file" />'.
257: '<input type="hidden" name="currentpath" value="'.$currentPath.'" />'.
258: '<input type="submit" name="storeupl" value="Upload" />'.
259: '</form>';
260: $r->print($displayOut);
261: $r->print('</td></tr></table>');
262: my @tree = split (/\//,$currentPath);
263: $r->print('<font size="+2">'.makeAnchor('portfolio','/').'/');
264: if (@tree > 1){
265: my $newCurrentPath = '';
266: for (my $i = 1; $i< @tree; $i++){
267: $newCurrentPath .= $tree[$i].'/';
268: $r->print(makeAnchor($tree[$i],'/'.$newCurrentPath).'/');
269: }
270: }
271: $r->print('</font>');
272: $r->print('<table border="0" cellspacing="2" cellpadding="2">'.
273: '<tr><th> </th><th>Actions</th><th>Name</th><th>Title</th><th>Status</th><th>Last Modified</th></tr>');
274: foreach my $line (@dirList) {
275: #$strip holds directory/file name
276: #$dom
277: my ($fileName,$dom,undef,$testdir,undef,undef,undef,undef,undef,undef,undef,undef,undef,undef,$obs,undef)=split(/\&/,$line,16);
278: if (($fileName ne '.') && ($fileName ne '..')){
279: if ($dirptr&$testdir){
280: $r->print('<tr bgcolor="#FFAA99"><td><img src="'.$iconpath.'folder_closed.gif"></td>');
281: $r->print('<td>Go to ...</td>');
282: $r->print('<td>'.makeAnchor($fileName.'/',$currentPath.$fileName.'/').'</td>');
283: $r->print('</tr>');
284: }else{
285: $r->print('<tr bgcolor="#CCCCFF"><td><img src="'.$iconpath.'unknown.gif"></td>');
286: $r->print('<td>Edit</td>');
287: $r->print('<td>'.$fileName.'</td>');
288: $r->print('<td>Title Here</td>');
289: $r->print('<td>Status Here</td>');
290: $r->print('<td>Modified Here</td>');
291: $r->print('</tr>');
292: }
293: }
294: }
295: # <tr bgcolor="#FFAA99"> pink bg
296: # <tr bgcolor="#CCCCFF"> blue bg
297: # $r->print(displayDirectory($currentPath, $currentFile, @dirList));
298: # $r->print('</td>><td>');
299: # $r->print(displayActions($currentPath, $currentFile, $isEmpty));
300: $r->print('</table>');
301: $r->print('</blockquote>');
302: $r->print("</body>\n</html>\n");
303: $r->rflush();
304: return OK;
305: }
306:
307: 1;
308: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>