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_old {
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: my $dirptr=16384;
163: sub display_directory {
164: my ($r,$current_path,$dir_list)=@_;
165: my $iconpath= $r->dir_config('lonIconsURL') . "/";
166: $r->print('<table border="0" cellspacing="2" cellpadding="2"><tr valign="middle">');
167: $r->print('<td bgcolor="#ccddaa" align="center">');
168: my $displayOut = '<form method="post" enctype="multipart/form-data">';
169: $displayOut = $displayOut.'<input name="uploaddoc" type="file" />'.
170: '<input type="hidden" name="currentpath" value="'.$current_path.'" />'.
171: '<input type="submit" name="storeupl" value="Upload" />'.
172: '</form>';
173: $r->print($displayOut);
174: $r->print('</td></tr><tr><td bgcolor="#ccddaa" align="center">');
175: $displayOut = '<form method="post">';
176: $displayOut .= '<input name="newdir" type="input" />'.
177: '<input type="hidden" name="currentpath" value="'.$current_path.'" />'.
178: '<input type="submit" name="createdir" value="'.&mt("Create Directory").'" />'.
179: '</form>';
180: $r->print($displayOut);
181: $r->print('</td></tr></table>');
182: my @tree = split (/\//,$current_path);
183: $r->print('<font size="+2">'.&make_anchor('/','/'));
184: if (@tree > 1){
185: my $newCurrentPath = '';
186: for (my $i = 1; $i< @tree; $i++){
187: $newCurrentPath .= $tree[$i].'/';
188: $r->print(&make_anchor($tree[$i],'/'.$newCurrentPath).'/');
189: }
190: }
191: $r->print('</font>');
192: &Apache::lonhtmlcommon::store_recent('portfolio',$current_path,$current_path);
193: $r->print('<br /><form method=post action="/adm/portfolio">'.
194: &Apache::lonhtmlcommon::select_recent('portfolio','currentpath',
195: 'this.form.submit();'));
196: $r->print("</form>");
197: $r->print('<table border="0" cellspacing="2" cellpadding="2">'.
198: '<tr><th>Actions</th><th> </th><th>Name</th><th>Size</th><th>Last Modified</th></tr>');
199: my $href_location="/uploaded/$ENV{'user.domain'}/$ENV{'user.name'}/portfolio/$current_path/";
200: foreach my $line (@$dir_list) {
201: #$strip holds directory/file name
202: #$dom
203: my ($filename,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef)=split(/\&/,$line,16);
204: if (($filename ne '.') && ($filename ne '..')) {
205: if ($dirptr&$testdir) {
206: $r->print('<tr bgcolor="#FFAA99"><td><img src="'.$iconpath.'folder_closed.gif"></td>');
207: $r->print('<td>Go to ...</td>');
208: $r->print('<td>'.&make_anchor($filename.'/',$current_path.$filename.'/').'</td>');
209: $r->print('</tr>');
210: } else {
211: $r->print('<tr bgcolor="#CCCCFF">');
212: $r->print('<td>
213: <form method="post" action="/adm/portfolio">
214: <select name="action">
215: <option value=""></option>
216: <option value="delete">'.&mt("Delete").'</option>
217: <option value="rename">'.&mt("Rename").'</option>
218: </select>
219: <input type="submit" name="doit" value="Go" />
220: <input type="hidden" name="selectfile" value="'.$filename.'" />
221: <input type="hidden" name="currentpath" value="'.$current_path.'" />
222: </form>
223: </td>');
224: $r->print('<td><img src="'.$iconpath.'unknown.gif"></td>');
225: $r->print('<td><a href="'.$href_location.$filename.'">'.
226: $filename.'</a></td>');
227: $r->print('<td>'.$size.'</td>');
228: $r->print('<td>'.&Apache::lonlocal::locallocaltime($mtime).'</td>');
229: $r->print('</tr>');
230: }
231: }
232: }
233: # <tr bgcolor="#FFAA99"> pink bg
234: # <tr bgcolor="#CCCCFF"> blue bg
235: # $r->print(&display_directory($current_path, $currentFile, @dir_list));
236: # $r->print('</td>><td>');
237: # $r->print(&display_actions($current_path, $currentFile, $isEmpty));
238: $r->print('</table></form>');
239: }
240:
241: sub open_form {
242: my ($r)=@_;
243: $r->print('<form method="post" action="/adm/portfolio">');
244: $r->print('<input type="hidden" name="action" value="'.
245: $ENV{'form.action'}.'" />');
246: $r->print('<input type="hidden" name="confirmed" value="1" />');
247: $r->print('<input type="hidden" name="selectfile" value="'.
248: $ENV{'form.selectfile'}.'" />');
249: $r->print('<input type="hidden" name="currentpath" value="'.
250: $ENV{'form.currentpath'}.'" />');
251: }
252:
253: sub close_form {
254: my ($r)=@_;
255: $r->print('<p><input type="submit" value="'.&mt('Continue').
256: '" /></p></form>');
257: $r->print('<form action="/adm/portfolio" method="POST">
258: <p>
259: <input type="hidden" name="currentpath" value="'.
260: $ENV{'form.currentpath'}.'" />
261: <input type="submit" value="'.&mt('Cancel').'" />
262: </p></form>');
263:
264: }
265:
266: sub display_file {
267: return $ENV{'form.currentpath'}.$ENV{'form.selectfile'};
268: }
269:
270: sub done {
271: return ('<h3><a href="/adm/portfolio?currentpath='.
272: $ENV{'form.currentpath'}.'">'.&mt('Done').'</a></h3>');
273: }
274:
275: sub delete {
276: my ($r)=@_;
277: &open_form($r);
278: $r->print('<p>'.&mt('Delete').' '.&display_file().'?</p>');
279: &close_form($r);
280: }
281:
282: sub delete_confirmed {
283: my ($r)=@_;
284: my $result=&Apache::lonnet::removeuserfile($ENV{'user.name'},
285: $ENV{'user.domain'},'portfolio'.
286: $ENV{'form.currentpath'}.
287: $ENV{'form.selectfile'});
288: if ($result ne 'ok') {
289: $r->print('<font color="red"> An errror occured ('.$result.
290: ') while trying to delete '.&display_file().'</font><br />');
291: }
292: $r->print(&done());
293: }
294:
295: sub rename {
296: my ($r)=@_;
297: &open_form($r);
298: $r->print('<p>'.&mt('Rename').' '.&display_file().'?</p>');
299: &close_form($r);
300: }
301:
302: sub rename_confirmed {
303: my ($r)=@_;
304: &Apache::lonnet::renameuserfile($ENV{'form.currentpath'}.$ENV{'form.selectfile'}, 'rename', $ENV{'form.currentpath'}.$ENV{'form.filenewname'} );
305: }
306:
307: sub upload {
308: my ($r)=@_;
309: #FIXME if the file already exists we need to do a confirmation pass
310: #before overwriting
311: my $result=&Apache::lonnet::userfileupload('uploaddoc','',
312: 'portfolio'.$ENV{'form.currentpath'});
313: if ($result !~ m|^/uploaded/|) {
314: $r->print('<font color="red"> An errror occured ('.$result.
315: ') while trying to upload '.&display_file().'</font><br />');
316: }
317: $r->print(&done());
318: }
319:
320: sub createdir {
321: my ($r)=@_;
322: #FIXME 1) bad dirnames
323: # 2) file exists in place of dir
324: my $result=&Apache::lonnet::mkdiruserfile($ENV{'user.name'},
325: $ENV{'user.domain'},'portfolio'.$ENV{'form.currentpath'}.$ENV{'form.newdir'});
326: if ($result ne 'ok') {
327: $r->print('<font color="red"> An errror occured ('.$result.
328: ') while trying to create a new directory '.&display_file().'</font><br />');
329: }
330: $r->print(&done());
331: }
332:
333: sub handler {
334: # this handles file management
335: my $r = shift;
336: my @dir_list; # will hold directory listing as array
337: my $udir; # returned from home server
338: my $currentFile; # directory or file contained in $pathToRoot.$current_path
339: my $action; # delete, rename, makedirectory, removedirectory,
340: my $filenewname; # for rename action (guess what we do with it!)
341: my $isFile;
342: &Apache::loncommon::no_cache($r);
343: &Apache::loncommon::content_type($r,'text/html');
344: $r->send_http_header;
345: # Give the LON-CAPA page header
346: $r->print('<html><head><title>'.
347: &mt('Portfolio Manager').
348: "</title></head>\n".
349: &Apache::loncommon::bodytag('Portfolio Manager'));
350: $r->rflush();
351: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
352: ['selectfile','currentpath',
353: 'currentfile']);
354:
355: # if we're uploading a file, we need to do it early so it will
356: # show in the directory list
357:
358: if ($ENV{'form.uploaddoc.filename'}) {
359: &upload($r);
360: } elsif ($ENV{'form.action'} eq 'delete') {
361: if ($ENV{'form.confirmed'}) {
362: &delete_confirmed($r);
363: } else {
364: &delete($r);
365: }
366: } elsif ($ENV{'form.action'} eq 'rename') {
367: # similarly, we need to delete or rename files before getting
368: # directory list
369: &rename($r);
370: } elsif ($ENV{'form.createdir'}) {
371: &createdir($r);
372: } else {
373: my $current_path='/';
374: if ($ENV{'form.currentpath'}) {
375: $current_path = $ENV{'form.currentpath'};
376: }
377:
378: my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
379: $ENV{'user.name'}).
380: '/userfiles/portfolio';
381: @dir_list=&Apache::lonnet::dirlist($current_path,$ENV{'user.domain'},
382: $ENV{'user.name'},$portfolio_root);
383:
384: # need to know if directory is empty so it can be removed if desired
385: my $is_empty=(@dir_list == 2);
386:
387: &display_directory($r,$current_path,\@dir_list);
388:
389: }
390:
391: $r->print("</body>\n</html>\n");
392: $r->rflush();
393: return OK;
394: }
395:
396: 1;
397: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>