File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.46: download - view: text, annotated - select for diffs
Fri Sep 10 21:33:48 2004 UTC (19 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- really fix the style

    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 && ($current_path ne '/')) {
   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:         # now remove from recent
  215: #        $r->print('<br /> removing '.$directory_name.'<br /');
  216:         &Apache::lonhtmlcommon::remove_recent('portfolio',[$directory_name.'/']);
  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;
  225:     }
  226:     $r->print(&done());
  227: }
  228: 
  229: sub rename {
  230:     my ($r)=@_;
  231:     &open_form($r);
  232:     $r->print('<p>'.&mt('Rename').' '.&display_file().' to 
  233:                <input name="filenewname" type="input" size="50" />?</p>');
  234:     &close_form($r);
  235: }
  236: 
  237: sub rename_confirmed {
  238:     my ($r)=@_;
  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: }
  258: 
  259: sub upload {
  260:     my ($r)=@_;
  261:     my $fname=$ENV{'form.uploaddoc.filename'};
  262:     my $filesize = (length($ENV{'form.uploaddoc'})) / 1000; #express in k (1024?)
  263:     my $disk_quota = 20000; # expressed in k
  264:     $fname=&Apache::lonnet::clean_filename($fname);
  265:     my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
  266: 						 $ENV{'user.name'}).
  267: 						'/userfiles/portfolio';
  268:     # Fixme --- Move the checking for existing file to LOND error return
  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;
  273:     foreach my $line (@dir_list) {
  274:         my ($filename)=split(/\&/,$line,2);
  275:         if ($filename eq $fname){
  276:             $found_file = 1;
  277:         }
  278:     }
  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){   
  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/|) {
  291:             $r->print('<font color="red"> An errror occured ('.$result.
  292: 	              ') while trying to upload '.&display_file().'</font><br />');
  293:         }
  294:     }
  295:     $r->print(&done());
  296: }
  297: 
  298: sub createdir {
  299:     my ($r)=@_;
  300:     my $newdir=&Apache::lonnet::clean_filename($ENV{'form.newdir'});
  301:     if ($newdir eq '') {
  302:     	$r->print('<font color="red">'.
  303: 	    	  &mt("Error: no directory name was provided.").
  304: 		      '</font><br />');
  305: 	    $r->print(&done());
  306: 	    return;
  307:     } 
  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:         }
  331:     }
  332:     $r->print(&done());
  333: }
  334: 
  335: sub handler {
  336:     # this handles file management
  337:     my $r = shift;
  338: 	my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
  339: 							 $ENV{'user.name'}).
  340: 							'/userfiles/portfolio';
  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']);
  353: 	if (($ENV{'form.storeupl'} eq 'Upload') & (!$ENV{'form.uploaddoc.filename'})){
  354:    	    $r->print('<font color="red"> No file was selected to upload.'.
  355:    	            'To upload a file, click <strong>Browse...</strong>'.
  356:    	            ', select a file, then click <strong>Upload</strong>,</font>');
  357: 	}
  358:     if ($ENV{'form.uploaddoc.filename'}) {
  359: 	&upload($r);
  360:     } elsif ($ENV{'form.action'} eq 'delete' && $ENV{'form.confirmed'}) {
  361: 	&delete_confirmed($r);
  362:     } elsif ($ENV{'form.action'} eq 'delete') {
  363: 	&delete($r);
  364:     } elsif ($ENV{'form.action'} eq 'deletedir' && $ENV{'form.confirmed'}) {
  365: 	&delete_dir_confirmed($r);
  366:     } elsif ($ENV{'form.action'} eq 'deletedir'){
  367: 	&delete_dir($r);
  368:     } elsif ($ENV{'form.action'} eq 'rename' && $ENV{'form.confirmed'}) {
  369: 	&rename_confirmed($r);
  370:     } elsif ($ENV{'form.action'} eq 'rename') {
  371: 	&rename($r);
  372:     } elsif ($ENV{'form.createdir'}) {
  373: 	&createdir($r);
  374:     } else {
  375: 	my $current_path='/';
  376: 	if ($ENV{'form.currentpath'}) {
  377: 	    $current_path = $ENV{'form.currentpath'};
  378: 	}
  379: 	my @dir_list=&Apache::lonnet::dirlist($current_path,
  380: 					    $ENV{'user.domain'},
  381: 					    $ENV{'user.name'},$portfolio_root);
  382: 	if ($dir_list[0] eq 'no_such_dir'){
  383: 	    # two main reasons for this:
  384:             #    1) never been here, so directory structure not created
  385: 	    #    2) back-button navigation after deleting a directory
  386: 	    if ($current_path eq '/'){
  387: 	        &Apache::lonnet::mkdiruserfile($ENV{'user.name'},
  388: 					       $ENV{'user.domain'},'portfolio');
  389: 	    } else {
  390:                 # some directory that snuck in get rid of the directory
  391:                 # from the recent pulldown, just in case
  392: 		&Apache::lonhtmlcommon::remove_recent('portfolio',
  393: 						      [$current_path]);
  394: 		$current_path = '/'; # force it back to the root        
  395: 	    }
  396: 	    # now grab the directory list again, for the first time
  397: 	    @dir_list=&Apache::lonnet::dirlist($current_path,
  398: 					    $ENV{'user.domain'},
  399: 					    $ENV{'user.name'},$portfolio_root);
  400:         }
  401: 	# need to know if directory is empty so it can be removed if desired
  402: 	my $is_empty=(@dir_list == 2);
  403: 	&display_directory($r,$current_path,$is_empty,\@dir_list);
  404: 	$r->print("</body>\n</html>\n");
  405: 	return OK;
  406:     }
  407: }
  408: 1;
  409: __END__

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