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: my $dirptr=16384;
41: sub display_common {
42: my ($r,$current_path,$is_empty,$dir_list)=@_;
43: my $iconpath= $r->dir_config('lonIconsURL') . "/";
44: $r->print('<table border="0" cellspacing="2" cellpadding="2"><tr valign="middle">');
45: $r->print('<td bgcolor="#ccddaa" align="center">');
46: my $displayOut = '<form method="post" enctype="multipart/form-data">';
47: $displayOut .= '<input name="uploaddoc" type="file" />'.
48: '<input type="hidden" name="currentpath" value="'.$current_path.'" />'.
49: '<input type="hidden" name="action" value="'.$ENV{"form.action"}.'" />'.
50: '<input type="hidden" name="fieldname" value="'.$ENV{"form.fieldname"}.'" />'.
51: '<input type="submit" name="storeupl" value="Upload" />'.
52: '</form>';
53: $r->print($displayOut);
54: $r->print('</td></tr><tr><td bgcolor="#ccddaa" align="center">');
55: $displayOut = '<form method="post">';
56: $displayOut .= '<input name="newdir" type="input" />'.
57: '<input type="hidden" name="currentpath" value="'.$current_path.'" />'.
58: '<input type="hidden" name="action" value="'.$ENV{"form.action"}.'" />'.
59: '<input type="hidden" name="fieldname" value="'.$ENV{"form.fieldname"}.'" />'.
60: '<input type="submit" name="createdir" value="'.&mt("Create Directory").'" />'.
61: '</form>';
62: $r->print($displayOut);
63: $r->print('</td></tr></table>');
64: my @tree = split (/\//,$current_path);
65: $r->print('<font size="+2">'.&make_anchor('portfolio','/').'/');
66: if (@tree > 1){
67: my $newCurrentPath = '';
68: for (my $i = 1; $i< @tree; $i++){
69: $newCurrentPath .= $tree[$i].'/';
70: $r->print(&make_anchor($tree[$i],'/'.$newCurrentPath).'/');
71: }
72: }
73: $r->print('</font>');
74: &Apache::lonhtmlcommon::store_recent('portfolio',$current_path,$current_path);
75: $r->print('<br /><form method=post action="/adm/portfolio">'.
76: &Apache::lonhtmlcommon::select_recent('portfolio','currentpath',
77: 'this.form.submit();'));
78: $r->print("</form>");
79: }
80: sub display_directory {
81: my ($r,$current_path,$is_empty,$dir_list)=@_;
82: my $iconpath= $r->dir_config('lonIconsURL') . "/";
83: my $display_out;
84: my %locked_files=&Apache::lonnet::dump('file_permissions',
85: $ENV{'user.domain'},$ENV{'user.name'});
86: my $locked_file;
87: foreach my $key (keys %locked_files) {
88: $locked_file .= $locked_files{$key}.':';
89: }
90: if ($is_empty && ($current_path ne '/')) {
91: $display_out = '<form method="post" action="/adm/portfolio">'.
92: '<input type="hidden" name="action" value="deletedir" />'.
93: '<input type="submit" name="deletedir" value="'.&mt("Delete Directory").'" />'.
94: '<input type="hidden" name="selectfile" value="" />'.
95: '<input type="hidden" name="currentpath" value="'.$current_path.'" />'.
96: '</form>';
97:
98: $r->print($display_out);
99: return;
100: }
101: $r->print('<table border="0" cellspacing="2" cellpadding="2">'.
102: '<tr><th>Actions</th><th> </th><th>Name</th><th>Size</th><th>Last Modified</th></tr>');
103: my $href_location="/uploaded/$ENV{'user.domain'}/$ENV{'user.name'}/portfolio$current_path";
104: foreach my $line (sort
105: {
106: my ($afile)=split('&',$a,2);
107: my ($bfile)=split('&',$b,2);
108: return (lc($afile) cmp lc($bfile));
109: } (@$dir_list)) {
110: #$strip holds directory/file name
111: #$dom
112: my ($filename,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef)=split(/\&/,$line,16);
113: if ($Apache::lonhomework::results{"resource.$ENV{'user.domain'}.$ENV{'user.name'}.$filename"}eq'true'){
114: &Apache::lonnet::logthis("found submitted file");
115: &Apache::lonnet::logthis("resource.$ENV{'user.domain'}.$ENV{'user.name'}.$filename");
116: } else {
117: &Apache::lonnet::logthis("$filename not found submitted file");
118: &Apache::lonnet::logthis("resource.$ENV{'user.domain'}.$ENV{'user.name'}.$filename");
119: }
120: if (($filename ne '.') && ($filename ne '..')) {
121: if ($dirptr&$testdir) {
122: $r->print('<tr bgcolor="#FFAA99"><td><img src="'.$iconpath.'folder_closed.gif"></td>');
123: $r->print('<td>Go to ...</td>');
124: $r->print('<td>'.&make_anchor($filename.'/',$current_path.$filename.'/').'</td>');
125: $r->print('</tr>');
126: } else {
127: $r->print('<tr bgcolor="#CCCCFF">');
128: $r->print('<td>
129: <form method="post" action="/adm/portfolio">
130: <select name="action">
131: <option value=""></option>
132: <option value="delete">'.&mt("Delete").'</option>
133: <option value="rename">'.&mt("Rename").'</option>
134: </select>
135: <input type="submit" name="doit" value="Go" />
136: <input type="hidden" name="selectfile" value="'.$filename.'" />
137: <input type="hidden" name="currentpath" value="'.$current_path.'" />
138: </form>
139: </td>');
140: $r->print('<td><img src="'.$iconpath.'unknown.gif"></td>');
141: $r->print('<td><a href="'.$href_location.$filename.'">'.
142: $filename.'</a></td>');
143: $r->print('<td>'.$size.'</td>');
144: $r->print('<td>'.&Apache::lonlocal::locallocaltime($mtime).'</td>');
145: $r->print('</tr>');
146: }
147: }
148: }
149: # <tr bgcolor="#FFAA99"> pink bg
150: # <tr bgcolor="#CCCCFF"> blue bg
151: # $r->print(&display_directory($current_path, $currentFile, @dir_list));
152: # $r->print('</td>><td>');
153: # $r->print(&display_actions($current_path, $currentFile, $isEmpty));
154: $r->print('</table></form>');
155: }
156: sub display_file_select {
157: my ($r,$current_path,$is_empty,$dir_list)=@_;
158: my $iconpath= $r->dir_config('lonIconsURL') . "/";
159: my $display_out;
160: if ($is_empty && ($current_path ne '/')) {
161: $display_out = '<form method="post" action="/adm/portfolio">'.
162: '<input type="hidden" name="action" value="deletedir" />'.
163: '<input type="submit" name="deletedir" value="'.&mt("Delete Directory").'" />'.
164: '<input type="hidden" name="selectfile" value="" />'.
165: '<input type="hidden" name="currentpath" value="'.$current_path.'" />'.
166:
167: '</form>';
168:
169: $r->print($display_out);
170: return;
171: }
172: $r->print('<table border="0" cellspacing="2" cellpadding="2">'.
173: '<tr><th>Select</th><th> </th><th>Name</th><th>Size</th><th>Last Modified</th></tr>');
174: my $href_location="/uploaded/$ENV{'user.domain'}/$ENV{'user.name'}/portfolio$current_path";
175: $r->print('<form method="post" name="checkselect" action="/adm/portfolio">');
176: foreach my $line (sort
177: {
178: my ($afile)=split('&',$a,2);
179: my ($bfile)=split('&',$b,2);
180: return (lc($afile) cmp lc($bfile));
181: } (@$dir_list)) {
182: #$strip holds directory/file name
183: #$dom
184: my ($filename,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef)=split(/\&/,$line,16);
185: if (($filename ne '.') && ($filename ne '..')) {
186: if ($dirptr&$testdir) {
187: $r->print('<tr bgcolor="#FFAA99"><td><img src="'.$iconpath.'folder_closed.gif"></td>');
188: $r->print('<td>Go to ...</td>');
189: $r->print('<td>'.&make_anchor($filename.'/',$current_path.$filename.'/').'</td>');
190: $r->print('</tr>');
191: } else {
192: $r->print('<tr bgcolor="#CCCCFF">');
193: $r->print('<td>
194: <input type="checkbox" name="selectedfile" value="'.$filename.'" >
195: </td>');
196: $r->print('<td><img src="'.$iconpath.'unknown.gif"></td>');
197: $r->print('<td><a href="'.$href_location.$filename.'">'.
198: $filename.'</a></td>');
199: $r->print('<td>'.$size.'</td>');
200: $r->print('<td>'.&Apache::lonlocal::locallocaltime($mtime).'</td>');
201: $r->print('</tr>');
202: }
203: }
204: }
205: $r->print('</table>
206: <input type="button" name="doit" onClick= "finishSelect();" value="Select checked files, and close window" />
207: <input type="hidden" name="currentpath" value="'.$current_path.'" />
208: </form>');
209: }
210:
211: sub open_form {
212: my ($r)=@_;
213: $r->print('<form method="post" action="/adm/portfolio">');
214: $r->print('<input type="hidden" name="action" value="'.
215: $ENV{'form.action'}.'" />');
216: $r->print('<input type="hidden" name="confirmed" value="1" />');
217: $r->print('<input type="hidden" name="selectfile" value="'.
218: $ENV{'form.selectfile'}.'" />');
219: $r->print('<input type="hidden" name="currentpath" value="'.
220: $ENV{'form.currentpath'}.'" />');
221: }
222:
223: sub close_form {
224: my ($r)=@_;
225: $r->print('<p><input type="submit" value="'.&mt('Continue').
226: '" /></p></form>');
227: $r->print('<form action="/adm/portfolio" method="POST">
228: <p>
229: <input type="hidden" name="currentpath" value="'.
230: $ENV{'form.currentpath'}.'" />
231: <input type="submit" value="'.&mt('Cancel').'" />
232: </p></form>');
233:
234: }
235:
236: sub display_file {
237: my ($path,$filename)=@_;
238: if (!defined($path)) { $path=$ENV{'form.currentpath'}; }
239: if (!defined($filename)) { $filename=$ENV{'form.selectfile'}; }
240: return '<tt>'.$path.$filename.'</tt>';
241: }
242:
243: sub done {
244: return ('<h3><a href="/adm/portfolio?currentpath='.
245: $ENV{'form.currentpath'}.
246: '&fieldname='.$ENV{'form.fieldname'}.
247: '">'.&mt('Done').'</a></h3>');
248: }
249:
250: sub delete {
251: my ($r)=@_;
252: my @check;
253: my $file_name = $ENV{'form.currentpath'}.$ENV{'form.selectfile'};
254: if (&Apache::lonnet::is_locked($file_name,$ENV{'user.domain'},$ENV{'user.name'} ) eq 'true') {
255: $r->print ("The file is locked and cannot be deleted.<br />");
256: $r->print(&done());
257: } else {
258: &open_form($r);
259: $r->print('<p>'.&mt('Delete').' '.&display_file().'?</p>');
260: &close_form($r);
261: }
262: }
263:
264: sub delete_confirmed {
265: my ($r)=@_;
266: my $result=&Apache::lonnet::removeuserfile($ENV{'user.name'},
267: $ENV{'user.domain'},'portfolio'.
268: $ENV{'form.currentpath'}.
269: $ENV{'form.selectfile'});
270: if ($result ne 'ok') {
271: $r->print('<font color="red"> An error occured ('.$result.
272: ') while trying to delete '.&display_file().'</font><br />');
273: }
274: $r->print(&done());
275: }
276:
277: sub delete_dir {
278: my ($r)=@_;
279: &open_form($r);
280: $r->print('<p>'.&mt('Delete').' '.&display_file().'?</p>');
281: &close_form($r);
282: }
283:
284: sub delete_dir_confirmed {
285: my ($r)=@_;
286: my $directory_name = $ENV{'form.currentpath'};
287: $directory_name =~ m/\/$/;
288: $directory_name = $`;
289: my $result=&Apache::lonnet::removeuserfile($ENV{'user.name'},
290: $ENV{'user.domain'},'portfolio'.
291: $directory_name);
292:
293: if ($result ne 'ok') {
294: $r->print('<font color="red"> An error occured (dir) ('.$result.
295: ') while trying to delete '.$directory_name.'</font><br />');
296: } else {
297: # now remove from recent
298: # $r->print('<br /> removing '.$directory_name.'<br /');
299: &Apache::lonhtmlcommon::remove_recent('portfolio',[$directory_name.'/']);
300: my @dirs = split m!/!, $directory_name;
301:
302: # $directory_name =~ m/^(\/*\/)(\/*.)$/;
303: $directory_name='/';
304: for (my $i=1; $i < (@dirs - 1); $i ++){
305: $directory_name .= $dirs[$i].'/';
306: }
307: $ENV{'form.currentpath'} = $directory_name;
308: }
309: $r->print(&done());
310: }
311:
312: sub rename {
313: my ($r)=@_;
314: my $file_name = $ENV{'form.currentpath'}.$ENV{'form.selectfile'};
315: if (&Apache::lonnet::is_locked($file_name,$ENV{'user.domain'},$ENV{'user.name'}) eq 'true') {
316: $r->print ("The file is locked and cannot be renamed.<br />");
317: $r->print(&done());
318: } else {
319: &open_form($r);
320: $r->print('<p>'.&mt('Rename').' '.&display_file().' to
321: <input name="filenewname" type="input" size="50" />?</p>');
322: &close_form($r);
323: }
324: }
325:
326: sub rename_confirmed {
327: my ($r)=@_;
328: my $filenewname=&Apache::lonnet::clean_filename($ENV{'form.filenewname'});
329: if ($filenewname eq '') {
330: $r->print('<font color="red">'.
331: &mt("Error: no valid filename was provided to rename to.").
332: '</font><br />');
333: $r->print(&done());
334: return;
335: }
336: my $result=
337: &Apache::lonnet::renameuserfile($ENV{'user.name'},$ENV{'user.domain'},
338: 'portfolio'.$ENV{'form.currentpath'}.$ENV{'form.selectfile'},
339: 'portfolio'.$ENV{'form.currentpath'}.$ENV{'form.filenewname'});
340: if ($result ne 'ok') {
341: $r->print('<font color="red"> An errror occured ('.$result.
342: ') while trying to rename '.&display_file().' to '.
343: &display_file(undef,$filenewname).'</font><br />');
344: }
345: $r->print(&done());
346: }
347: sub select_files {
348: my ($r)=@_;
349: my $java_script =(<<ENDSMP);
350: <script language='javascript'>
351: function finishSelect() {
352: fileList = "";
353: for (i=0;i<document.forms.checkselect.length;i++) {
354: if (document.forms.checkselect[i].checked){
355: fileList = fileList + document.forms.checkselect.currentpath.value + document.forms.checkselect[i].value + "," ;
356: }
357: }
358: opener.document.forms.lonhomework.
359: ENDSMP
360: $java_script .= $ENV{'form.fieldname'};
361: $java_script .= (<<ENDSMP);
362: .value=fileList;
363: self.close();
364: }
365: </script>
366: ENDSMP
367: $r->print($java_script);
368: $r->print("<h1>Select portfolio files</h1>
369: Check as many as you wish in response to the essay problem.<br />");
370: }
371: sub upload {
372: my ($r)=@_;
373: my $fname=$ENV{'form.uploaddoc.filename'};
374: my $filesize = (length($ENV{'form.uploaddoc'})) / 1000; #express in k (1024?)
375: my $disk_quota = 20000; # expressed in k
376: $fname=&Apache::lonnet::clean_filename($fname);
377: my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
378: $ENV{'user.name'}).
379: '/userfiles/portfolio';
380: # Fixme --- Move the checking for existing file to LOND error return
381: my @dir_list=&Apache::lonnet::dirlist($ENV{'form.currentpath'},
382: $ENV{'user.domain'},
383: $ENV{'user.name'},$portfolio_root);
384: my $found_file = 0;
385: foreach my $line (@dir_list) {
386: my ($filename)=split(/\&/,$line,2);
387: if ($filename eq $fname){
388: $found_file = 1;
389: }
390: }
391: my $current_disk_usage = &Apache::lonnet::diskusage($ENV{'user.domain'}, $ENV{'user.name'},$portfolio_root);
392: if (($current_disk_usage + $filesize) > $disk_quota){
393: $r->print('<font color="red">Unable to upload <strong>'.$fname.' (size = '.$filesize.' kilobytes</strong>. Disk quota will be exceeded.'.
394: '<br />Disk quota is '.$disk_quota.' kilobytes. Your current disk usage is '.$current_disk_usage.' kilobytes.');
395: }
396: elsif ($found_file){
397: $r->print('<font color="red">Unable to upload <strong>'.$fname.'</strong>, a file by that name was found in <strong>'.$ENV{'form.currentpath'}.'</strong></font>'.
398: '<br />To upload, rename or delete existing '.$fname.' in '.$ENV{'form.currentpath'});
399: } else {
400: my $result=&Apache::lonnet::userfileupload('uploaddoc','',
401: 'portfolio'.$ENV{'form.currentpath'});
402: if ($result !~ m|^/uploaded/|) {
403: $r->print('<font color="red"> An errror occured ('.$result.
404: ') while trying to upload '.&display_file().'</font><br />');
405: }
406: }
407: $r->print(&done());
408: }
409:
410: sub createdir {
411: my ($r)=@_;
412: my $newdir=&Apache::lonnet::clean_filename($ENV{'form.newdir'});
413: if ($newdir eq '') {
414: $r->print('<font color="red">'.
415: &mt("Error: no directory name was provided.").
416: '</font><br />');
417: $r->print(&done());
418: return;
419: }
420: my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
421: $ENV{'user.name'}).
422: '/userfiles/portfolio';
423: my @dir_list=&Apache::lonnet::dirlist($ENV{'form.currentpath'},
424: $ENV{'user.domain'},
425: $ENV{'user.name'},$portfolio_root);
426: my $found_file = 0;
427: foreach my $line (@dir_list) {
428: my ($filename)=split(/\&/,$line,2);
429: if ($filename eq $newdir){
430: $found_file = 1;
431: }
432: }
433: if ($found_file){
434: $r->print('<font color="red"> Unable to create a directory named <strong>'.$newdir.
435: ' </strong>a file or directory by that name already exists.</font><br />');
436: } else {
437: my $result=&Apache::lonnet::mkdiruserfile($ENV{'user.name'},
438: $ENV{'user.domain'},'portfolio'.$ENV{'form.currentpath'}.$newdir);
439: if ($result ne 'ok') {
440: $r->print('<font color="red"> An errror occured ('.$result.
441: ') while trying to create a new directory '.&display_file().'</font><br />');
442: }
443: }
444: $r->print(&done());
445: }
446:
447: sub handler {
448: # this handles file management
449: my $r = shift;
450: my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
451: $ENV{'user.name'}).
452: '/userfiles/portfolio';
453: &Apache::loncommon::no_cache($r);
454: &Apache::loncommon::content_type($r,'text/html');
455: $r->send_http_header;
456: # Give the LON-CAPA page header
457: $r->print('<html><head><title>'.
458: &mt('Portfolio Manager').
459: "</title></head>\n".
460: &Apache::loncommon::bodytag('Portfolio Manager'));
461: $r->rflush();
462: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
463: ['selectfile','currentpath',
464: 'currentfile','action','fieldname','mode']);
465: if (($ENV{'form.storeupl'} eq 'Upload') & (!$ENV{'form.uploaddoc.filename'})){
466: $r->print('<font color="red"> No file was selected to upload.'.
467: 'To upload a file, click <strong>Browse...</strong>'.
468: ', select a file, then click <strong>Upload</strong>,</font>');
469: }
470: if ($ENV{'form.uploaddoc.filename'}) {
471: &upload($r);
472: } elsif ($ENV{'form.action'} eq 'delete' && $ENV{'form.confirmed'}) {
473: &delete_confirmed($r);
474: } elsif ($ENV{'form.action'} eq 'delete') {
475: &delete($r);
476: } elsif ($ENV{'form.action'} eq 'deletedir' && $ENV{'form.confirmed'}) {
477: &delete_dir_confirmed($r);
478: } elsif ($ENV{'form.action'} eq 'deletedir'){
479: &delete_dir($r);
480: } elsif ($ENV{'form.action'} eq 'rename' && $ENV{'form.confirmed'}) {
481: &rename_confirmed($r);
482: } elsif ($ENV{'form.action'} eq 'rename') {
483: &rename($r);
484: } elsif ($ENV{'form.createdir'}) {
485: &createdir($r);
486: } else {
487: my $current_path='/';
488: if ($ENV{'form.currentpath'}) {
489: $current_path = $ENV{'form.currentpath'};
490: }
491: my @dir_list=&Apache::lonnet::dirlist($current_path,
492: $ENV{'user.domain'},
493: $ENV{'user.name'},$portfolio_root);
494: if ($dir_list[0] eq 'no_such_dir'){
495: # two main reasons for this:
496: # 1) never been here, so directory structure not created
497: # 2) back-button navigation after deleting a directory
498: if ($current_path eq '/'){
499: &Apache::lonnet::mkdiruserfile($ENV{'user.name'},
500: $ENV{'user.domain'},'portfolio');
501: } else {
502: # some directory that snuck in get rid of the directory
503: # from the recent pulldown, just in case
504: &Apache::lonhtmlcommon::remove_recent('portfolio',
505: [$current_path]);
506: $current_path = '/'; # force it back to the root
507: }
508: # now grab the directory list again, for the first time
509: @dir_list=&Apache::lonnet::dirlist($current_path,
510: $ENV{'user.domain'},
511: $ENV{'user.name'},$portfolio_root);
512: }
513: # need to know if directory is empty so it can be removed if desired
514: my $is_empty=(@dir_list == 2);
515: &display_common($r,$current_path,$is_empty,\@dir_list);
516: if ($ENV{"form.mode"} eq 'selectfile'){
517: &select_files($r);
518: &display_file_select($r,$current_path,$is_empty,\@dir_list);
519: } else {
520: &display_directory($r,$current_path,$is_empty,\@dir_list);
521: }
522: $r->print("</body>\n</html>\n");
523: return OK;
524: }
525: }
526: 1;
527: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>