File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.34: download - view: text, annotated - select for diffs
Fri Aug 27 15:45:33 2004 UTC (19 years, 9 months ago) by banghart
Branches: MAIN
CVS tags: HEAD

	Cleaned up formatting, removed redundant sub clean_filename,
	removed Fixme comment

    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: # 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.'&currentpath='.$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,$is_empty,$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('portfolio','/').'/');
  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:     if ($is_empty) {
  198:         $displayOut = '<form method="post" action="/adm/portfolio">'.
  199:         '<input type="hidden" name="action" value="deletedir" />'.
  200:         '<input type="submit" name="deletedir" value="'.&mt("Delete Directory").'" />'.
  201:         '<input type="hidden" name="selectfile" value="" />'.
  202:         '<input type="hidden" name="currentpath" value="'.$current_path.'" />'.
  203:         '</form>';
  204:         
  205:         $r->print($displayOut);
  206: 	return;
  207:     }
  208:     $r->print('<table border="0" cellspacing="2" cellpadding="2">'.
  209:             '<tr><th>Actions</th><th>&nbsp;</th><th>Name</th><th>Size</th><th>Last Modified</th></tr>');
  210:     my $href_location="/uploaded/$ENV{'user.domain'}/$ENV{'user.name'}/portfolio/$current_path/";
  211:     foreach my $line (sort 
  212: 		      { 
  213: 			  my ($afile)=split('&',$a,2);
  214: 			  my ($bfile)=split('&',$b,2);
  215: 			  return (lc($afile) cmp lc($bfile));
  216: 		      } (@$dir_list)) {
  217:     	#$strip holds directory/file name
  218:     	#$dom 
  219:     	my ($filename,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef)=split(/\&/,$line,16); 
  220:     	if (($filename ne '.') && ($filename ne '..')) {
  221:             if ($dirptr&$testdir) {
  222:                 $r->print('<tr bgcolor="#FFAA99"><td><img src="'.$iconpath.'folder_closed.gif"></td>');
  223:                 $r->print('<td>Go to ...</td>');
  224:                 $r->print('<td>'.&make_anchor($filename.'/',$current_path.$filename.'/').'</td>'); 
  225:                 $r->print('</tr>'); 
  226:             } else {
  227:                 $r->print('<tr bgcolor="#CCCCFF">');
  228:                 $r->print('<td>
  229: <form method="post" action="/adm/portfolio">
  230: <select name="action">
  231:   <option value=""></option>
  232:   <option value="delete">'.&mt("Delete").'</option>
  233:   <option value="rename">'.&mt("Rename").'</option>
  234: </select>
  235: <input type="submit" name="doit" value="Go" />
  236: <input type="hidden" name="selectfile" value="'.$filename.'" />
  237: <input type="hidden" name="currentpath" value="'.$current_path.'" />
  238: </form>
  239: </td>');
  240:                 $r->print('<td><img src="'.$iconpath.'unknown.gif"></td>');
  241:                 $r->print('<td><a href="'.$href_location.$filename.'">'.
  242: 			  $filename.'</a></td>'); 
  243:                 $r->print('<td>'.$size.'</td>');
  244:                 $r->print('<td>'.&Apache::lonlocal::locallocaltime($mtime).'</td>');
  245:                 $r->print('</tr>'); 
  246:             }
  247:         }
  248:     }
  249: #   <tr bgcolor="#FFAA99"> pink bg 
  250: #   <tr bgcolor="#CCCCFF"> blue bg            
  251: #   $r->print(&display_directory($current_path, $currentFile, @dir_list));
  252: #    $r->print('</td>><td>');
  253: #   $r->print(&display_actions($current_path, $currentFile, $isEmpty));
  254:     $r->print('</table></form>');
  255: }
  256: 
  257: sub open_form {
  258:     my ($r)=@_;
  259:     $r->print('<form method="post" action="/adm/portfolio">');
  260:     $r->print('<input type="hidden" name="action" value="'.
  261: 	      $ENV{'form.action'}.'" />');
  262:     $r->print('<input type="hidden" name="confirmed" value="1" />');
  263:     $r->print('<input type="hidden" name="selectfile" value="'.
  264: 	      $ENV{'form.selectfile'}.'" />');
  265:     $r->print('<input type="hidden" name="currentpath" value="'.
  266: 	      $ENV{'form.currentpath'}.'" />');
  267: }
  268: 
  269: sub close_form {
  270:     my ($r)=@_;
  271:     $r->print('<p><input type="submit" value="'.&mt('Continue').
  272: 	      '" /></p></form>');
  273:     $r->print('<form action="/adm/portfolio" method="POST">
  274:                <p>
  275:               <input type="hidden" name="currentpath" value="'.
  276: 	      $ENV{'form.currentpath'}.'" />
  277:                  <input type="submit" value="'.&mt('Cancel').'" />
  278:                </p></form>');
  279: 
  280: }
  281: 
  282: sub display_file {
  283:     my ($path,$filename)=@_;
  284:     if (!defined($path)) { $path=$ENV{'form.currentpath'}; }
  285:     if (!defined($filename)) { $filename=$ENV{'form.selectfile'}; }
  286:     return '<tt>'.$path.$filename.'</tt>';
  287: }
  288: 
  289: sub done {
  290:     return ('<h3><a href="/adm/portfolio?currentpath='.
  291: 	    $ENV{'form.currentpath'}.'">'.&mt('Done').'</a></h3>');
  292: }
  293: 
  294: sub delete {
  295:     my ($r)=@_;
  296:     &open_form($r);
  297:     $r->print('<p>'.&mt('Delete').' '.&display_file().'?</p>');
  298:     &close_form($r);
  299: } 
  300: 
  301: sub delete_confirmed {
  302:     my ($r)=@_;
  303:     my $result=&Apache::lonnet::removeuserfile($ENV{'user.name'},
  304: 					       $ENV{'user.domain'},'portfolio'.
  305: 					       $ENV{'form.currentpath'}.
  306: 					       $ENV{'form.selectfile'});
  307:     if ($result ne 'ok') {
  308: 	$r->print('<font color="red"> An error occured ('.$result.
  309: 		  ') while trying to delete '.&display_file().'</font><br />');
  310:     }
  311:     $r->print(&done());
  312: }
  313: 
  314: sub delete_dir {
  315:     my ($r)=@_;
  316:     &open_form($r);
  317:     $r->print('<p>'.&mt('Delete').' '.&display_file().'?</p>');
  318:     &close_form($r);
  319: } 
  320: 
  321: sub delete_dir_confirmed {
  322:     my ($r)=@_;
  323:     my $directory_name = $ENV{'form.currentpath'};
  324:     $directory_name =~ m/\/$/;
  325:     $directory_name = $`;
  326:     my $result=&Apache::lonnet::removeuserfile($ENV{'user.name'},
  327: 					       $ENV{'user.domain'},'portfolio'.
  328: 					       $directory_name);
  329: 					       
  330:     if ($result ne 'ok') {
  331: 	$r->print('<font color="red"> An error occured (dir) ('.$result.
  332: 		  ') while trying to delete '.$directory_name.'</font><br />');
  333:     } else {
  334:         my @dirs = split m!/!, $directory_name;
  335:         
  336: #        $directory_name =~ m/^(\/*\/)(\/*.)$/;
  337:         $directory_name='/';
  338:         for (my $i=1; $i < (@dirs - 1); $i ++){
  339:             $directory_name .= $dirs[$i].'/';
  340:         }
  341:         $ENV{'form.currentpath'} = $directory_name;
  342:     }
  343:     $r->print(&done());
  344: }
  345: 
  346: sub rename {
  347:     my ($r)=@_;
  348:     &open_form($r);
  349:     $r->print('<p>'.&mt('Rename').' '.&display_file().' to 
  350:                <input name="filenewname" type="input" size="50" />?</p>');
  351:     &close_form($r);
  352: }
  353: 
  354: sub rename_confirmed {
  355:     my ($r)=@_;
  356:     my $filenewname=&Apache::lonnet::clean_filename($ENV{'form.filenewname'});
  357:     if ($filenewname eq '') {
  358: 	$r->print('<font color="red">'.
  359: 		  &mt("Error: no valid filename was provided to rename to.").
  360: 		  '</font><br />');
  361: 	$r->print(&done());
  362: 	return;
  363:     } 
  364:     my $result=
  365: 	&Apache::lonnet::renameuserfile($ENV{'user.name'},$ENV{'user.domain'},
  366:             'portfolio'.$ENV{'form.currentpath'}.$ENV{'form.selectfile'},
  367:             'portfolio'.$ENV{'form.currentpath'}.$ENV{'form.filenewname'});
  368:     if ($result ne 'ok') {
  369: 	$r->print('<font color="red"> An errror occured ('.$result.
  370: 		  ') while trying to rename '.&display_file().' to '.
  371: 		  &display_file(undef,$filenewname).'</font><br />');
  372:     }
  373:     $r->print(&done());
  374: }
  375: 
  376: sub upload {
  377:     my ($r)=@_;
  378:     my $fname=$ENV{'form.uploaddoc.filename'};
  379:     $fname=&Apache::lonnet::clean_filename($fname);
  380:     my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
  381: 						 $ENV{'user.name'}).
  382: 						'/userfiles/portfolio';
  383:     my @dir_list=&Apache::lonnet::dirlist($ENV{'form.currentpath'},
  384: 					  $ENV{'user.domain'},
  385: 					  $ENV{'user.name'},$portfolio_root);
  386:     my $found_file = 0;
  387:     foreach my $line (@dir_list) {
  388:         my ($filename)=split(/\&/,$line,2);
  389:         if ($filename eq $fname){
  390:             $found_file = 1;
  391:         }
  392:     }
  393:     if ($found_file){   
  394:         $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>'.
  395:                   '<br />To upload, rename or delete existing '.$fname.' in '.$ENV{'form.currentpath'});
  396:     } else {
  397:         my $result=&Apache::lonnet::userfileupload('uploaddoc','',
  398: 	        	 'portfolio'.$ENV{'form.currentpath'});
  399:         if ($result !~ m|^/uploaded/|) {
  400:             $r->print('<font color="red"> An errror occured ('.$result.
  401: 	              ') while trying to upload '.&display_file().'</font><br />');
  402:         }
  403:     }
  404:     $r->print(&done());
  405: }
  406: 
  407: sub createdir {
  408:     my ($r)=@_;
  409:     #FIXME 1) file exists in place of dir (errormessage needs improvement)
  410:     my $newdir=&Apache::lonnet::clean_filename($ENV{'form.newdir'});
  411:     if ($newdir eq '') {
  412: 	$r->print('<font color="red">'.
  413: 		  &mt("Error: no valid directory name was provided.").
  414: 		  '</font><br />');
  415: 	$r->print(&done());
  416: 	return;
  417:     } 
  418:     my $result=&Apache::lonnet::mkdiruserfile($ENV{'user.name'},
  419: 	     $ENV{'user.domain'},'portfolio'.$ENV{'form.currentpath'}.$newdir);
  420:     if ($result ne 'ok') {
  421: 	$r->print('<font color="red"> An errror occured ('.$result.
  422: 		  ') while trying to create a new directory '.&display_file().'</font><br />');
  423:     }
  424:     $r->print(&done());
  425: }
  426: 
  427: sub handler {
  428:     # this handles file management
  429:     my $r = shift;
  430: 	my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
  431: 							 $ENV{'user.name'}).
  432: 							'/userfiles/portfolio';
  433:     &Apache::loncommon::no_cache($r);
  434:     &Apache::loncommon::content_type($r,'text/html');
  435:     $r->send_http_header;
  436:     # Give the LON-CAPA page header
  437:     $r->print('<html><head><title>'.
  438:               &mt('Portfolio Manager').
  439:               "</title></head>\n".
  440:               &Apache::loncommon::bodytag('Portfolio Manager'));
  441:     $r->rflush();
  442:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  443:                                             ['selectfile','currentpath',
  444: 					     'currentfile']);
  445: 
  446:     if ($ENV{'form.uploaddoc.filename'}) {
  447: 	&upload($r);
  448:     } elsif ($ENV{'form.action'} eq 'delete' && $ENV{'form.confirmed'}) {
  449: 	&delete_confirmed($r);
  450:     } elsif ($ENV{'form.action'} eq 'delete') {
  451: 	&delete($r);
  452:     } elsif ($ENV{'form.action'} eq 'deletedir' && $ENV{'form.confirmed'}) {
  453: 	&delete_dir_confirmed($r);
  454:     } elsif ($ENV{'form.action'} eq 'deletedir'){
  455: 	&delete_dir($r);
  456:     } elsif ($ENV{'form.action'} eq 'rename' && $ENV{'form.confirmed'}) {
  457: 	&rename_confirmed($r);
  458:     } elsif ($ENV{'form.action'} eq 'rename') {
  459: 	&rename($r);
  460:     } elsif ($ENV{'form.createdir'}) {
  461: 	&createdir($r);
  462:     } else {
  463: 	my $current_path='/';
  464: 	if ($ENV{'form.currentpath'}) {
  465: 	    $current_path = $ENV{'form.currentpath'};
  466: 	}
  467: 	my @dir_list=&Apache::lonnet::dirlist($current_path,
  468: 					    $ENV{'user.domain'},
  469: 					    $ENV{'user.name'},$portfolio_root);
  470:     
  471: 	# need to know if directory is empty so it can be removed if desired
  472: 	my $is_empty=(@dir_list == 2);
  473: 	&display_directory($r,$current_path,$is_empty,\@dir_list);
  474: 	$r->print("</body>\n</html>\n");
  475: 	return OK;
  476:     }
  477: }
  478: 1;
  479: __END__

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