File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.39: download - view: text, annotated - select for diffs
Wed Sep 8 20:34:28 2004 UTC (19 years, 9 months ago) by banghart
Branches: MAIN
CVS tags: HEAD

	Some formatting changes, hoping style police approve.

	Removed superfluous '/' added to $href_location owing to leading and trailing '/' in
	$current_path

	Added additional Fixme comment to remove dead directories from recent pulldown.

	Removed unused routines.

	Corrected error in earlier submit. Removed " character I had erroneously inserted in
	make_anchor routine.

    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.'&currentpath='.$current_path.'">'.$filename.'</a>';
   38:     return $anchor;
   39: }
   40: 
   41: my $dirptr=16384;
   42: sub display_directory {
   43:     my ($r,$current_path,$is_empty,$dir_list)=@_;
   44:     my $iconpath= $r->dir_config('lonIconsURL') . "/";
   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" />'.
   49:         '<input type="hidden" name="currentpath" value="'.$current_path.'" />'.
   50:         '<input type="submit" name="storeupl" value="Upload" />'.
   51:         '</form>';
   52:     $r->print($displayOut);
   53:     $r->print('</td></tr><tr><td bgcolor="#ccddaa" align="center">');
   54:     $displayOut = '<form method="post">';
   55:     $displayOut .= '<input name="newdir" type="input" />'.
   56:         '<input type="hidden" name="currentpath" value="'.$current_path.'" />'.
   57:         '<input type="submit" name="createdir" value="'.&mt("Create Directory").'" />'.
   58:         '</form>';
   59:     $r->print($displayOut);
   60:     $r->print('</td></tr></table>');
   61:     my @tree = split (/\//,$current_path);
   62:     $r->print('<font size="+2">'.&make_anchor('portfolio','/').'/');
   63:     if (@tree > 1){
   64:         my $newCurrentPath = '';
   65:         for (my $i = 1; $i< @tree; $i++){
   66:             $newCurrentPath .= $tree[$i].'/';
   67:             $r->print(&make_anchor($tree[$i],'/'.$newCurrentPath).'/');
   68:         }
   69:     }
   70:     $r->print('</font>');
   71:     &Apache::lonhtmlcommon::store_recent('portfolio',$current_path,$current_path);
   72:     #Fixme -- remove from recent those paths that are deleted
   73:     $r->print('<br /><form method=post action="/adm/portfolio">'.
   74: 	      &Apache::lonhtmlcommon::select_recent('portfolio','currentpath',
   75: 						    'this.form.submit();'));
   76:     $r->print("</form>");
   77:     if ($is_empty) {
   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);
   86: 	return;
   87:     }
   88:     $r->print('<table border="0" cellspacing="2" cellpadding="2">'.
   89:             '<tr><th>Actions</th><th>&nbsp;</th><th>Name</th><th>Size</th><th>Last Modified</th></tr>');
   90:     my $href_location="/uploaded/$ENV{'user.domain'}/$ENV{'user.name'}/portfolio$current_path";
   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)) {
   97:     	#$strip holds directory/file name
   98:     	#$dom 
   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) {
  102:                 $r->print('<tr bgcolor="#FFAA99"><td><img src="'.$iconpath.'folder_closed.gif"></td>');
  103:                 $r->print('<td>Go to ...</td>');
  104:                 $r->print('<td>'.&make_anchor($filename.'/',$current_path.$filename.'/').'</td>'); 
  105:                 $r->print('</tr>'); 
  106:             } else {
  107:                 $r->print('<tr bgcolor="#CCCCFF">');
  108:                 $r->print('<td>
  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>');
  120:                 $r->print('<td><img src="'.$iconpath.'unknown.gif"></td>');
  121:                 $r->print('<td><a href="'.$href_location.$filename.'">'.
  122: 			    $filename.'</a></td>'); 
  123:                 $r->print('<td>'.$size.'</td>');
  124:                 $r->print('<td>'.&Apache::lonlocal::locallocaltime($mtime).'</td>');
  125:                 $r->print('</tr>'); 
  126:             }
  127:         }
  128:     }
  129: #   <tr bgcolor="#FFAA99"> pink bg 
  130: #   <tr bgcolor="#CCCCFF"> blue bg            
  131: #   $r->print(&display_directory($current_path, $currentFile, @dir_list));
  132: #    $r->print('</td>><td>');
  133: #   $r->print(&display_actions($current_path, $currentFile, $isEmpty));
  134:     $r->print('</table></form>');
  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 {
  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>';
  167: }
  168: 
  169: sub done {
  170:     return ('<h3><a href="/adm/portfolio?currentpath='.
  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') {
  188: 	$r->print('<font color="red"> An error occured ('.$result.
  189: 		  ') while trying to delete '.&display_file().'</font><br />');
  190:     }
  191:     $r->print(&done());
  192: }
  193: 
  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);
  209: 					       
  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 />');
  213:     } else {
  214:         my @dirs = split m!/!, $directory_name;
  215:         
  216: #        $directory_name =~ m/^(\/*\/)(\/*.)$/;
  217:         $directory_name='/';
  218:         for (my $i=1; $i < (@dirs - 1); $i ++){
  219:             $directory_name .= $dirs[$i].'/';
  220:         }
  221:         $ENV{'form.currentpath'} = $directory_name;
  222:     }
  223:     $r->print(&done());
  224: }
  225: 
  226: sub rename {
  227:     my ($r)=@_;
  228:     &open_form($r);
  229:     $r->print('<p>'.&mt('Rename').' '.&display_file().' to 
  230:                <input name="filenewname" type="input" size="50" />?</p>');
  231:     &close_form($r);
  232: }
  233: 
  234: sub rename_confirmed {
  235:     my ($r)=@_;
  236:     my $filenewname=&Apache::lonnet::clean_filename($ENV{'form.filenewname'});
  237:     if ($filenewname eq '') {
  238: 	$r->print('<font color="red">'.
  239: 		  &mt("Error: no valid filename was provided to rename to.").
  240: 		  '</font><br />');
  241: 	$r->print(&done());
  242: 	return;
  243:     } 
  244:     my $result=
  245: 	&Apache::lonnet::renameuserfile($ENV{'user.name'},$ENV{'user.domain'},
  246:             'portfolio'.$ENV{'form.currentpath'}.$ENV{'form.selectfile'},
  247:             'portfolio'.$ENV{'form.currentpath'}.$ENV{'form.filenewname'});
  248:     if ($result ne 'ok') {
  249: 	$r->print('<font color="red"> An errror occured ('.$result.
  250: 		  ') while trying to rename '.&display_file().' to '.
  251: 		  &display_file(undef,$filenewname).'</font><br />');
  252:     }
  253:     $r->print(&done());
  254: }
  255: 
  256: sub upload {
  257:     my ($r)=@_;
  258:     my $fname=$ENV{'form.uploaddoc.filename'};
  259:     my $filesize = (length($ENV{'form.uploaddoc'})) / 1000; #express in k (1024?)
  260:     my $disk_quota = 20000; # expressed in k
  261:     $fname=&Apache::lonnet::clean_filename($fname);
  262:     my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
  263: 						 $ENV{'user.name'}).
  264: 						'/userfiles/portfolio';
  265:     # Fixme --- Move the checking for existing file to LOND error return
  266:     my @dir_list=&Apache::lonnet::dirlist($ENV{'form.currentpath'},
  267: 					  $ENV{'user.domain'},
  268: 					  $ENV{'user.name'},$portfolio_root);
  269:     my $found_file = 0;
  270:     foreach my $line (@dir_list) {
  271:         my ($filename)=split(/\&/,$line,2);
  272:         if ($filename eq $fname){
  273:             $found_file = 1;
  274:         }
  275:     }
  276:     my $current_disk_usage = &Apache::lonnet::diskusage($ENV{'user.domain'}, $ENV{'user.name'},$portfolio_root);
  277:     if (($current_disk_usage + $filesize) > $disk_quota){
  278:         $r->print('<font color="red">Unable to upload <strong>'.$fname.' (size = '.$filesize.' kilobytes</strong>. Disk quota will be exceeded.'.
  279:                   '<br />Disk quota is '.$disk_quota.' kilobytes. Your current disk usage is '.$current_disk_usage.' kilobytes.');
  280:     } 
  281:     elsif ($found_file){   
  282:         $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>'.
  283:                   '<br />To upload, rename or delete existing '.$fname.' in '.$ENV{'form.currentpath'});
  284:     } else {
  285:         my $result=&Apache::lonnet::userfileupload('uploaddoc','',
  286: 	        	 'portfolio'.$ENV{'form.currentpath'});
  287:         if ($result !~ m|^/uploaded/|) {
  288:             $r->print('<font color="red"> An errror occured ('.$result.
  289: 	              ') while trying to upload '.&display_file().'</font><br />');
  290:         }
  291:     }
  292:     $r->print(&done());
  293: }
  294: 
  295: sub createdir {
  296:     my ($r)=@_;
  297:     my $newdir=&Apache::lonnet::clean_filename($ENV{'form.newdir'});
  298:     if ($newdir eq '') {
  299:     	$r->print('<font color="red">'.
  300: 	    	  &mt("Error: no directory name was provided.").
  301: 		      '</font><br />');
  302: 	    $r->print(&done());
  303: 	    return;
  304:     } 
  305:     my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
  306: 						 $ENV{'user.name'}).
  307: 						'/userfiles/portfolio';
  308:     my @dir_list=&Apache::lonnet::dirlist($ENV{'form.currentpath'},
  309: 					  $ENV{'user.domain'},
  310: 					  $ENV{'user.name'},$portfolio_root);
  311:     my $found_file = 0;
  312:     foreach my $line (@dir_list) {
  313:         my ($filename)=split(/\&/,$line,2);
  314:         if ($filename eq $newdir){
  315:             $found_file = 1;
  316:         }
  317:     }
  318:     if ($found_file){
  319:     	    $r->print('<font color="red"> Unable to create a directory named <strong>'.$newdir.
  320:     	            ' </strong>a file or directory by that name already exists.</font><br />');
  321:     } else {
  322:         my $result=&Apache::lonnet::mkdiruserfile($ENV{'user.name'},
  323: 	         $ENV{'user.domain'},'portfolio'.$ENV{'form.currentpath'}.$newdir);
  324:         if ($result ne 'ok') {
  325:     	    $r->print('<font color="red"> An errror occured ('.$result.
  326: 	    	      ') while trying to create a new directory '.&display_file().'</font><br />');
  327:         }
  328:     }
  329:     $r->print(&done());
  330: }
  331: 
  332: sub handler {
  333:     # this handles file management
  334:     my $r = shift;
  335: 	my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
  336: 							 $ENV{'user.name'}).
  337: 							'/userfiles/portfolio';
  338:     &Apache::loncommon::no_cache($r);
  339:     &Apache::loncommon::content_type($r,'text/html');
  340:     $r->send_http_header;
  341:     # Give the LON-CAPA page header
  342:     $r->print('<html><head><title>'.
  343:               &mt('Portfolio Manager').
  344:               "</title></head>\n".
  345:               &Apache::loncommon::bodytag('Portfolio Manager'));
  346:     $r->rflush();
  347:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  348:                                             ['selectfile','currentpath',
  349: 					     'currentfile']);
  350: 	# make sure there is a portfolio directory off /userfiles
  351: 	# Fixme -- Do this in a better way -- offload it to the LOND routine
  352: 	my @dir_list=&Apache::lonnet::dirlist('/',
  353: 					    $ENV{'user.domain'},
  354: 					    $ENV{'user.name'},$portfolio_root);
  355: 	if ($dir_list[0] eq 'no_such_dir'){
  356: 	     &Apache::lonnet::mkdiruserfile($ENV{'user.name'},
  357: 	     $ENV{'user.domain'},'portfolio');
  358: 	}
  359:     if ($ENV{'form.uploaddoc.filename'}) {
  360: 	&upload($r);
  361:     } elsif ($ENV{'form.action'} eq 'delete' && $ENV{'form.confirmed'}) {
  362: 	&delete_confirmed($r);
  363:     } elsif ($ENV{'form.action'} eq 'delete') {
  364: 	&delete($r);
  365:     } elsif ($ENV{'form.action'} eq 'deletedir' && $ENV{'form.confirmed'}) {
  366: 	&delete_dir_confirmed($r);
  367:     } elsif ($ENV{'form.action'} eq 'deletedir'){
  368: 	&delete_dir($r);
  369:     } elsif ($ENV{'form.action'} eq 'rename' && $ENV{'form.confirmed'}) {
  370: 	&rename_confirmed($r);
  371:     } elsif ($ENV{'form.action'} eq 'rename') {
  372: 	&rename($r);
  373:     } elsif ($ENV{'form.createdir'}) {
  374: 	&createdir($r);
  375:     } else {
  376: 	my $current_path='/';
  377: 	if ($ENV{'form.currentpath'}) {
  378: 	    $current_path = $ENV{'form.currentpath'};
  379: 	}
  380: 	@dir_list=&Apache::lonnet::dirlist($current_path,
  381: 					    $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: 	&display_directory($r,$current_path,$is_empty,\@dir_list);
  387: 	$r->print("</body>\n</html>\n");
  388: 	return OK;
  389:     }
  390: }
  391: 1;
  392: __END__

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