File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.76: download - view: text, annotated - select for diffs
Sun Feb 20 20:39:30 2005 UTC (19 years, 4 months ago) by banghart
Branches: MAIN
CVS tags: HEAD
	Bug 3919: Better error messages for uploading files.

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

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