Annotation of loncom/interface/portfolio.pm, revision 1.42

1.3       banghart    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: 
1.1       banghart   24: package Apache::portfolio;
                     25: use strict;
                     26: use Apache::Constants qw(:common :http);
1.2       banghart   27: use Apache::loncommon;
1.1       banghart   28: use Apache::lonnet;
1.2       banghart   29: use Apache::lontexconvert;
                     30: use Apache::lonfeedback;
                     31: use Apache::lonlocal;
1.16      banghart   32: 
                     33: # receives a file name and path stub from username/userfiles/portfolio/
                     34: # returns an anchor tag consisting encoding filename and currentpath
1.23      albertel   35: sub make_anchor {
                     36:     my ($filename, $current_path) = @_;
                     37:     my $anchor = '<a href="/adm/portfolio?selectfile='.$filename.'&currentpath='.$current_path.'">'.$filename.'</a>';
1.8       albertel   38:     return $anchor;
1.6       banghart   39: }
1.16      banghart   40: 
1.24      albertel   41: my $dirptr=16384;
                     42: sub display_directory {
1.30      banghart   43:     my ($r,$current_path,$is_empty,$dir_list)=@_;
1.18      banghart   44:     my $iconpath= $r->dir_config('lonIconsURL') . "/";
1.20      banghart   45:     $r->print('<table border="0" cellspacing="2" cellpadding="2"><tr valign="middle">');
                     46:     $r->print('<td bgcolor="#ccddaa" align="center">');
                     47:     my $displayOut = '<form method="post" enctype="multipart/form-data">';
                     48:     $displayOut = $displayOut.'<input name="uploaddoc" type="file" />'.
1.24      albertel   49:         '<input type="hidden" name="currentpath" value="'.$current_path.'" />'.
1.20      banghart   50:         '<input type="submit" name="storeupl" value="Upload" />'.
                     51:         '</form>';
                     52:     $r->print($displayOut);
1.22      albertel   53:     $r->print('</td></tr><tr><td bgcolor="#ccddaa" align="center">');
1.24      albertel   54:     $displayOut = '<form method="post">';
                     55:     $displayOut .= '<input name="newdir" type="input" />'.
                     56:         '<input type="hidden" name="currentpath" value="'.$current_path.'" />'.
1.22      albertel   57:         '<input type="submit" name="createdir" value="'.&mt("Create Directory").'" />'.
                     58:         '</form>';
                     59:     $r->print($displayOut);
                     60:     $r->print('</td></tr></table>');
1.24      albertel   61:     my @tree = split (/\//,$current_path);
1.29      albertel   62:     $r->print('<font size="+2">'.&make_anchor('portfolio','/').'/');
1.19      banghart   63:     if (@tree > 1){
                     64:         my $newCurrentPath = '';
                     65:         for (my $i = 1; $i< @tree; $i++){
                     66:             $newCurrentPath .= $tree[$i].'/';
1.23      albertel   67:             $r->print(&make_anchor($tree[$i],'/'.$newCurrentPath).'/');
1.19      banghart   68:         }
                     69:     }
                     70:     $r->print('</font>');
1.24      albertel   71:     &Apache::lonhtmlcommon::store_recent('portfolio',$current_path,$current_path);
1.39      banghart   72:     #Fixme -- remove from recent those paths that are deleted
1.22      albertel   73:     $r->print('<br /><form method=post action="/adm/portfolio">'.
                     74: 	      &Apache::lonhtmlcommon::select_recent('portfolio','currentpath',
                     75: 						    'this.form.submit();'));
1.21      banghart   76:     $r->print("</form>");
1.31      albertel   77:     if ($is_empty) {
1.30      banghart   78:         $displayOut = '<form method="post" action="/adm/portfolio">'.
                     79:         '<input type="hidden" name="action" value="deletedir" />'.
                     80:         '<input type="submit" name="deletedir" value="'.&mt("Delete Directory").'" />'.
                     81:         '<input type="hidden" name="selectfile" value="" />'.
                     82:         '<input type="hidden" name="currentpath" value="'.$current_path.'" />'.
                     83:         '</form>';
                     84:         
                     85:         $r->print($displayOut);
1.31      albertel   86: 	return;
                     87:     }
1.18      banghart   88:     $r->print('<table border="0" cellspacing="2" cellpadding="2">'.
1.22      albertel   89:             '<tr><th>Actions</th><th>&nbsp;</th><th>Name</th><th>Size</th><th>Last Modified</th></tr>');
1.39      banghart   90:     my $href_location="/uploaded/$ENV{'user.domain'}/$ENV{'user.name'}/portfolio$current_path";
1.26      albertel   91:     foreach my $line (sort 
                     92: 		      { 
                     93: 			  my ($afile)=split('&',$a,2);
                     94: 			  my ($bfile)=split('&',$b,2);
                     95: 			  return (lc($afile) cmp lc($bfile));
                     96: 		      } (@$dir_list)) {
1.18      banghart   97:     	#$strip holds directory/file name
                     98:     	#$dom 
1.23      albertel   99:     	my ($filename,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef)=split(/\&/,$line,16); 
                    100:     	if (($filename ne '.') && ($filename ne '..')) {
                    101:             if ($dirptr&$testdir) {
1.19      banghart  102:                 $r->print('<tr bgcolor="#FFAA99"><td><img src="'.$iconpath.'folder_closed.gif"></td>');
                    103:                 $r->print('<td>Go to ...</td>');
1.24      albertel  104:                 $r->print('<td>'.&make_anchor($filename.'/',$current_path.$filename.'/').'</td>'); 
1.19      banghart  105:                 $r->print('</tr>'); 
1.23      albertel  106:             } else {
1.22      albertel  107:                 $r->print('<tr bgcolor="#CCCCFF">');
                    108:                 $r->print('<td>
1.39      banghart  109:                     <form method="post" action="/adm/portfolio">
                    110:                     <select name="action">
                    111:                         <option value=""></option>
                    112:                         <option value="delete">'.&mt("Delete").'</option>
                    113:                         <option value="rename">'.&mt("Rename").'</option>
                    114:                     </select>
                    115:                     <input type="submit" name="doit" value="Go" />
                    116:                     <input type="hidden" name="selectfile" value="'.$filename.'" />
                    117:                     <input type="hidden" name="currentpath" value="'.$current_path.'" />
                    118:                     </form>
                    119:                     </td>');
1.22      albertel  120:                 $r->print('<td><img src="'.$iconpath.'unknown.gif"></td>');
1.23      albertel  121:                 $r->print('<td><a href="'.$href_location.$filename.'">'.
1.39      banghart  122: 			    $filename.'</a></td>'); 
1.22      albertel  123:                 $r->print('<td>'.$size.'</td>');
                    124:                 $r->print('<td>'.&Apache::lonlocal::locallocaltime($mtime).'</td>');
1.19      banghart  125:                 $r->print('</tr>'); 
                    126:             }
1.18      banghart  127:         }
                    128:     }
                    129: #   <tr bgcolor="#FFAA99"> pink bg 
                    130: #   <tr bgcolor="#CCCCFF"> blue bg            
1.24      albertel  131: #   $r->print(&display_directory($current_path, $currentFile, @dir_list));
1.18      banghart  132: #    $r->print('</td>><td>');
1.24      albertel  133: #   $r->print(&display_actions($current_path, $currentFile, $isEmpty));
1.22      albertel  134:     $r->print('</table></form>');
1.24      albertel  135: }
                    136: 
                    137: sub open_form {
                    138:     my ($r)=@_;
                    139:     $r->print('<form method="post" action="/adm/portfolio">');
                    140:     $r->print('<input type="hidden" name="action" value="'.
                    141: 	      $ENV{'form.action'}.'" />');
                    142:     $r->print('<input type="hidden" name="confirmed" value="1" />');
                    143:     $r->print('<input type="hidden" name="selectfile" value="'.
                    144: 	      $ENV{'form.selectfile'}.'" />');
                    145:     $r->print('<input type="hidden" name="currentpath" value="'.
                    146: 	      $ENV{'form.currentpath'}.'" />');
                    147: }
                    148: 
                    149: sub close_form {
                    150:     my ($r)=@_;
                    151:     $r->print('<p><input type="submit" value="'.&mt('Continue').
                    152: 	      '" /></p></form>');
                    153:     $r->print('<form action="/adm/portfolio" method="POST">
                    154:                <p>
                    155:               <input type="hidden" name="currentpath" value="'.
                    156: 	      $ENV{'form.currentpath'}.'" />
                    157:                  <input type="submit" value="'.&mt('Cancel').'" />
                    158:                </p></form>');
                    159: 
                    160: }
                    161: 
                    162: sub display_file {
1.27      albertel  163:     my ($path,$filename)=@_;
                    164:     if (!defined($path)) { $path=$ENV{'form.currentpath'}; }
                    165:     if (!defined($filename)) { $filename=$ENV{'form.selectfile'}; }
                    166:     return '<tt>'.$path.$filename.'</tt>';
1.24      albertel  167: }
                    168: 
                    169: sub done {
1.25      albertel  170:     return ('<h3><a href="/adm/portfolio?currentpath='.
1.24      albertel  171: 	    $ENV{'form.currentpath'}.'">'.&mt('Done').'</a></h3>');
                    172: }
                    173: 
                    174: sub delete {
                    175:     my ($r)=@_;
                    176:     &open_form($r);
                    177:     $r->print('<p>'.&mt('Delete').' '.&display_file().'?</p>');
                    178:     &close_form($r);
                    179: } 
                    180: 
                    181: sub delete_confirmed {
                    182:     my ($r)=@_;
                    183:     my $result=&Apache::lonnet::removeuserfile($ENV{'user.name'},
                    184: 					       $ENV{'user.domain'},'portfolio'.
                    185: 					       $ENV{'form.currentpath'}.
                    186: 					       $ENV{'form.selectfile'});
                    187:     if ($result ne 'ok') {
1.30      banghart  188: 	$r->print('<font color="red"> An error occured ('.$result.
1.25      albertel  189: 		  ') while trying to delete '.&display_file().'</font><br />');
1.24      albertel  190:     }
                    191:     $r->print(&done());
                    192: }
                    193: 
1.30      banghart  194: sub delete_dir {
                    195:     my ($r)=@_;
                    196:     &open_form($r);
                    197:     $r->print('<p>'.&mt('Delete').' '.&display_file().'?</p>');
                    198:     &close_form($r);
                    199: } 
                    200: 
                    201: sub delete_dir_confirmed {
                    202:     my ($r)=@_;
                    203:     my $directory_name = $ENV{'form.currentpath'};
                    204:     $directory_name =~ m/\/$/;
                    205:     $directory_name = $`;
                    206:     my $result=&Apache::lonnet::removeuserfile($ENV{'user.name'},
                    207: 					       $ENV{'user.domain'},'portfolio'.
                    208: 					       $directory_name);
1.32      banghart  209: 					       
1.30      banghart  210:     if ($result ne 'ok') {
                    211: 	$r->print('<font color="red"> An error occured (dir) ('.$result.
                    212: 		  ') while trying to delete '.$directory_name.'</font><br />');
1.32      banghart  213:     } else {
1.41      banghart  214:         # now remove from recent
                    215: #        $r->print('<br /> removing '.$directory_name.'<br /');
1.42    ! banghart  216:         &Apache::lonhtmlcommon::remove_recent('portfolio',[$directory_name.'/']);
1.32      banghart  217:         my @dirs = split m!/!, $directory_name;
                    218:         
                    219: #        $directory_name =~ m/^(\/*\/)(\/*.)$/;
                    220:         $directory_name='/';
                    221:         for (my $i=1; $i < (@dirs - 1); $i ++){
                    222:             $directory_name .= $dirs[$i].'/';
                    223:         }
                    224:         $ENV{'form.currentpath'} = $directory_name;
1.30      banghart  225:     }
                    226:     $r->print(&done());
                    227: }
                    228: 
1.24      albertel  229: sub rename {
                    230:     my ($r)=@_;
                    231:     &open_form($r);
1.27      albertel  232:     $r->print('<p>'.&mt('Rename').' '.&display_file().' to 
                    233:                <input name="filenewname" type="input" size="50" />?</p>');
1.24      albertel  234:     &close_form($r);
                    235: }
                    236: 
                    237: sub rename_confirmed {
                    238:     my ($r)=@_;
1.27      albertel  239:     my $filenewname=&Apache::lonnet::clean_filename($ENV{'form.filenewname'});
                    240:     if ($filenewname eq '') {
                    241: 	$r->print('<font color="red">'.
                    242: 		  &mt("Error: no valid filename was provided to rename to.").
                    243: 		  '</font><br />');
                    244: 	$r->print(&done());
                    245: 	return;
                    246:     } 
                    247:     my $result=
                    248: 	&Apache::lonnet::renameuserfile($ENV{'user.name'},$ENV{'user.domain'},
                    249:             'portfolio'.$ENV{'form.currentpath'}.$ENV{'form.selectfile'},
                    250:             'portfolio'.$ENV{'form.currentpath'}.$ENV{'form.filenewname'});
                    251:     if ($result ne 'ok') {
                    252: 	$r->print('<font color="red"> An errror occured ('.$result.
                    253: 		  ') while trying to rename '.&display_file().' to '.
                    254: 		  &display_file(undef,$filenewname).'</font><br />');
                    255:     }
                    256:     $r->print(&done());
                    257: }
1.24      albertel  258: 
                    259: sub upload {
                    260:     my ($r)=@_;
1.33      banghart  261:     my $fname=$ENV{'form.uploaddoc.filename'};
1.38      banghart  262:     my $filesize = (length($ENV{'form.uploaddoc'})) / 1000; #express in k (1024?)
                    263:     my $disk_quota = 20000; # expressed in k
1.34      banghart  264:     $fname=&Apache::lonnet::clean_filename($fname);
                    265:     my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
1.33      banghart  266: 						 $ENV{'user.name'}).
                    267: 						'/userfiles/portfolio';
1.38      banghart  268:     # Fixme --- Move the checking for existing file to LOND error return
1.34      banghart  269:     my @dir_list=&Apache::lonnet::dirlist($ENV{'form.currentpath'},
                    270: 					  $ENV{'user.domain'},
                    271: 					  $ENV{'user.name'},$portfolio_root);
                    272:     my $found_file = 0;
1.33      banghart  273:     foreach my $line (@dir_list) {
1.34      banghart  274:         my ($filename)=split(/\&/,$line,2);
1.33      banghart  275:         if ($filename eq $fname){
                    276:             $found_file = 1;
                    277:         }
                    278:     }
1.38      banghart  279:     my $current_disk_usage = &Apache::lonnet::diskusage($ENV{'user.domain'}, $ENV{'user.name'},$portfolio_root);
                    280:     if (($current_disk_usage + $filesize) > $disk_quota){
                    281:         $r->print('<font color="red">Unable to upload <strong>'.$fname.' (size = '.$filesize.' kilobytes</strong>. Disk quota will be exceeded.'.
                    282:                   '<br />Disk quota is '.$disk_quota.' kilobytes. Your current disk usage is '.$current_disk_usage.' kilobytes.');
                    283:     } 
                    284:     elsif ($found_file){   
1.33      banghart  285:         $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>'.
                    286:                   '<br />To upload, rename or delete existing '.$fname.' in '.$ENV{'form.currentpath'});
                    287:     } else {
                    288:         my $result=&Apache::lonnet::userfileupload('uploaddoc','',
                    289: 	        	 'portfolio'.$ENV{'form.currentpath'});
                    290:         if ($result !~ m|^/uploaded/|) {
1.34      banghart  291:             $r->print('<font color="red"> An errror occured ('.$result.
                    292: 	              ') while trying to upload '.&display_file().'</font><br />');
1.33      banghart  293:         }
1.25      albertel  294:     }
                    295:     $r->print(&done());
                    296: }
                    297: 
                    298: sub createdir {
                    299:     my ($r)=@_;
1.28      albertel  300:     my $newdir=&Apache::lonnet::clean_filename($ENV{'form.newdir'});
                    301:     if ($newdir eq '') {
1.37      banghart  302:     	$r->print('<font color="red">'.
                    303: 	    	  &mt("Error: no directory name was provided.").
                    304: 		      '</font><br />');
                    305: 	    $r->print(&done());
                    306: 	    return;
1.28      albertel  307:     } 
1.37      banghart  308:     my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
                    309: 						 $ENV{'user.name'}).
                    310: 						'/userfiles/portfolio';
                    311:     my @dir_list=&Apache::lonnet::dirlist($ENV{'form.currentpath'},
                    312: 					  $ENV{'user.domain'},
                    313: 					  $ENV{'user.name'},$portfolio_root);
                    314:     my $found_file = 0;
                    315:     foreach my $line (@dir_list) {
                    316:         my ($filename)=split(/\&/,$line,2);
                    317:         if ($filename eq $newdir){
                    318:             $found_file = 1;
                    319:         }
                    320:     }
                    321:     if ($found_file){
                    322:     	    $r->print('<font color="red"> Unable to create a directory named <strong>'.$newdir.
                    323:     	            ' </strong>a file or directory by that name already exists.</font><br />');
                    324:     } else {
                    325:         my $result=&Apache::lonnet::mkdiruserfile($ENV{'user.name'},
                    326: 	         $ENV{'user.domain'},'portfolio'.$ENV{'form.currentpath'}.$newdir);
                    327:         if ($result ne 'ok') {
                    328:     	    $r->print('<font color="red"> An errror occured ('.$result.
                    329: 	    	      ') while trying to create a new directory '.&display_file().'</font><br />');
                    330:         }
1.24      albertel  331:     }
                    332:     $r->print(&done());
                    333: }
                    334: 
                    335: sub handler {
                    336:     # this handles file management
                    337:     my $r = shift;
1.33      banghart  338: 	my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
                    339: 							 $ENV{'user.name'}).
                    340: 							'/userfiles/portfolio';
1.24      albertel  341:     &Apache::loncommon::no_cache($r);
                    342:     &Apache::loncommon::content_type($r,'text/html');
                    343:     $r->send_http_header;
                    344:     # Give the LON-CAPA page header
                    345:     $r->print('<html><head><title>'.
                    346:               &mt('Portfolio Manager').
                    347:               "</title></head>\n".
                    348:               &Apache::loncommon::bodytag('Portfolio Manager'));
                    349:     $r->rflush();
                    350:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                    351:                                             ['selectfile','currentpath',
                    352: 					     'currentfile']);
1.36      banghart  353: 	# make sure there is a portfolio directory off /userfiles
1.38      banghart  354: 	# Fixme -- Do this in a better way -- offload it to the LOND routine
1.36      banghart  355: 	my @dir_list=&Apache::lonnet::dirlist('/',
                    356: 					    $ENV{'user.domain'},
                    357: 					    $ENV{'user.name'},$portfolio_root);
                    358: 	if ($dir_list[0] eq 'no_such_dir'){
                    359: 	     &Apache::lonnet::mkdiruserfile($ENV{'user.name'},
                    360: 	     $ENV{'user.domain'},'portfolio');
                    361: 	}
1.40      banghart  362: 	if (($ENV{'form.storeupl'} eq 'Upload') & (!$ENV{'form.uploaddoc.filename'})){
                    363:    	    $r->print('<font color="red"> No file was selected to upload.'.
                    364:    	            'To upload a file, click <strong>Browse...</strong>'.
                    365:    	            ', select a file, then click <strong>Upload</strong>,</font>');
                    366: 	}
1.24      albertel  367:     if ($ENV{'form.uploaddoc.filename'}) {
                    368: 	&upload($r);
1.27      albertel  369:     } elsif ($ENV{'form.action'} eq 'delete' && $ENV{'form.confirmed'}) {
                    370: 	&delete_confirmed($r);
1.24      albertel  371:     } elsif ($ENV{'form.action'} eq 'delete') {
1.27      albertel  372: 	&delete($r);
1.30      banghart  373:     } elsif ($ENV{'form.action'} eq 'deletedir' && $ENV{'form.confirmed'}) {
1.31      albertel  374: 	&delete_dir_confirmed($r);
1.30      banghart  375:     } elsif ($ENV{'form.action'} eq 'deletedir'){
1.31      albertel  376: 	&delete_dir($r);
1.27      albertel  377:     } elsif ($ENV{'form.action'} eq 'rename' && $ENV{'form.confirmed'}) {
                    378: 	&rename_confirmed($r);
1.24      albertel  379:     } elsif ($ENV{'form.action'} eq 'rename') {
                    380: 	&rename($r);
1.25      albertel  381:     } elsif ($ENV{'form.createdir'}) {
                    382: 	&createdir($r);
1.24      albertel  383:     } else {
                    384: 	my $current_path='/';
                    385: 	if ($ENV{'form.currentpath'}) {
                    386: 	    $current_path = $ENV{'form.currentpath'};
                    387: 	}
1.38      banghart  388: 	@dir_list=&Apache::lonnet::dirlist($current_path,
1.27      albertel  389: 					    $ENV{'user.domain'},
                    390: 					    $ENV{'user.name'},$portfolio_root);
1.24      albertel  391:     
                    392: 	# need to know if directory is empty so it can be removed if desired
                    393: 	my $is_empty=(@dir_list == 2);
1.31      albertel  394: 	&display_directory($r,$current_path,$is_empty,\@dir_list);
                    395: 	$r->print("</body>\n</html>\n");
                    396: 	return OK;
1.30      banghart  397:     }
1.2       banghart  398: }
1.1       banghart  399: 1;
                    400: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>