File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.73: download - view: text, annotated - select for diffs
Fri Feb 11 00:18:14 2005 UTC (19 years, 4 months ago) by banghart
Branches: MAIN
CVS tags: HEAD
	Address bug 3917. Suppress menu when portfolio in select mode

    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:     return ('<h3><a href="/adm/portfolio?currentpath='.
  257: 	    $ENV{'form.currentpath'}.
  258: 	    '&fieldname='.$ENV{'form.fieldname'}.
  259: 	    '&mode='.$ENV{'form.mode'}.
  260: 	    '">'.&mt('Done').'</a></h3>');
  261: }
  262: 
  263: sub delete {
  264:     my ($r)=@_;
  265:     my @check;
  266:     my $file_name = $ENV{'form.currentpath'}.$ENV{'form.selectfile'};
  267:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
  268: 
  269:     if (&Apache::lonnet::is_locked($file_name,$ENV{'user.domain'},$ENV{'user.name'} ) eq 'true') {
  270:         $r->print ("The file is locked and cannot be deleted.<br />");
  271:         $r->print(&done());
  272:     } else {
  273:         if (scalar(@files)) {
  274:             &open_form($r);
  275:             $r->print('<p>'.&mt('Delete').' '.&display_file(undef,\@files).'?</p>');
  276:             &close_form($r);
  277:         } else {
  278:             $r->print("No file was checked to delete.<br />");
  279:             $r->print(&done());
  280:         }
  281:     }
  282: } 
  283: 
  284: sub delete_confirmed {
  285:     my ($r)=@_;
  286:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
  287:     my $result;
  288:     foreach my $delete_file (@files) {
  289:         $result=&Apache::lonnet::removeuserfile($ENV{'user.name'},
  290: 					       $ENV{'user.domain'},'portfolio'.
  291: 					       $ENV{'form.currentpath'}.
  292: 					       $delete_file);
  293:         if ($result ne 'ok') {
  294: 	$r->print('<font color="red"> An error occured ('.$result.
  295: 		  ') while trying to delete '.&display_file(undef, $delete_file).'</font><br />');
  296:         }
  297:     }
  298:     $r->print(&done());
  299: }
  300: 
  301: sub delete_dir {
  302:     my ($r)=@_;
  303:     &open_form($r);
  304:     $r->print('<p>'.&mt('Delete').' '.&display_file().'?</p>');
  305:     &close_form($r);
  306: } 
  307: 
  308: sub delete_dir_confirmed {
  309:     my ($r)=@_;
  310:     my $directory_name = $ENV{'form.currentpath'};
  311:     $directory_name =~ m/\/$/;
  312:     $directory_name = $`;
  313:     my $result=&Apache::lonnet::removeuserfile($ENV{'user.name'},
  314: 					       $ENV{'user.domain'},'portfolio'.
  315: 					       $directory_name);
  316: 					       
  317:     if ($result ne 'ok') {
  318: 	$r->print('<font color="red"> An error occured (dir) ('.$result.
  319: 		  ') while trying to delete '.$directory_name.'</font><br />');
  320:     } else {
  321:         # now remove from recent
  322: #        $r->print('<br /> removing '.$directory_name.'<br /');
  323:         &Apache::lonhtmlcommon::remove_recent('portfolio',[$directory_name.'/']);
  324:         my @dirs = split m!/!, $directory_name;
  325:         
  326: #        $directory_name =~ m/^(\/*\/)(\/*.)$/;
  327:         $directory_name='/';
  328:         for (my $i=1; $i < (@dirs - 1); $i ++){
  329:             $directory_name .= $dirs[$i].'/';
  330:         }
  331:         $ENV{'form.currentpath'} = $directory_name;
  332:     }
  333:     $r->print(&done());
  334: }
  335: 
  336: sub rename {
  337:     my ($r)=@_;
  338:     my $file_name = $ENV{'form.currentpath'}.$ENV{'form.rename'};
  339:     if (&Apache::lonnet::is_locked($file_name,$ENV{'user.domain'},$ENV{'user.name'}) eq 'true') {
  340:         $r->print ("The file is locked and cannot be renamed.<br />");
  341:         $r->print(&done());
  342:     } else {
  343:         &open_form($r);
  344:         $r->print('<p>'.&mt('Rename').' '.&display_file().' to 
  345:                    <input name="filenewname" type="input" size="50" />?</p>');
  346:         &close_form($r);
  347:     }
  348: }
  349: 
  350: sub rename_confirmed {
  351:     my ($r)=@_;
  352:     my $filenewname=&Apache::lonnet::clean_filename($ENV{'form.filenewname'});
  353:     if ($filenewname eq '') {
  354: 	$r->print('<font color="red">'.
  355: 		  &mt("Error: no valid filename was provided to rename to.").
  356: 		  '</font><br />');
  357: 	$r->print(&done());
  358: 	return;
  359:     } 
  360:     my $result=
  361: 	&Apache::lonnet::renameuserfile($ENV{'user.name'},$ENV{'user.domain'},
  362:             'portfolio'.$ENV{'form.currentpath'}.$ENV{'form.selectfile'},
  363:             'portfolio'.$ENV{'form.currentpath'}.$filenewname);
  364:     if ($result ne 'ok') {
  365: 	$r->print('<font color="red"> An errror occured ('.$result.
  366: 		  ') while trying to rename '.&display_file().' to '.
  367: 		  &display_file(undef,$filenewname).'</font><br />');
  368:     }
  369:     if ($filenewname ne $ENV{'form.filenewname'}) {
  370:         $r->print("The new file name was changed from:<br /><strong>".$ENV{'form.filenewname'}."</strong> to <strong>$filenewname </strong>");
  371:     }
  372:     $r->print(&done());
  373: }
  374: sub select_files {
  375:     my ($r)=@_;
  376:     if ($ENV{'form.continue'} eq 'true') {
  377:         # here we update the selections for the currentpath
  378:         # eventually, have to handle removing those not checked, but . . . 
  379:         my @items=&Apache::loncommon::get_env_multiple('form.selectfile');
  380:         &Apache::lonnet::save_selected_files($ENV{'user.name'}, $ENV{'form.currentpath'}, @items);
  381:     } else {
  382:         if ($ENV{'form.currentpath'} eq '/') {
  383:             #empty the file for a fresh start
  384:             # &Apache::lonnet::clear_selected_files($ENV{'user.name'});
  385:         }
  386:     }
  387:     my @files = &Apache::lonnet::files_not_in_path($ENV{'user.name'}, $ENV{'form.currentpath'});
  388:     my $java_files = join ",", @files;
  389:     if ($java_files) {
  390:         $java_files.=',';
  391:     }
  392:     my $javascript =(<<ENDSMP);
  393:         <script language='javascript'>
  394:         function finishSelect() {
  395: ENDSMP
  396:     $javascript .= 'fileList = "'.$java_files.'";';
  397:     $javascript .= (<<ENDSMP);
  398:             for (i=0;i<document.forms.checkselect.length;i++) { 
  399:                 if (document.forms.checkselect[i].checked){
  400:                     fileList = fileList + document.forms.checkselect.currentpath.value + document.forms.checkselect[i].value + "," ;
  401:                 }
  402:             }
  403:             opener.document.forms.lonhomework.
  404: ENDSMP
  405:     $javascript .= $ENV{'form.fieldname'};
  406:     $javascript .= (<<ENDSMP);
  407:         .value=fileList;
  408:             self.close();
  409:         }
  410:         </script>
  411: ENDSMP
  412:     $r->print($javascript);
  413:     $r->print("<h1>Select portfolio files</h1>
  414:                 Check as many as you wish in response to the essay problem.<br />");
  415:     $r->print("<strong>Files selected from other directories:</strong><br />");
  416:     foreach (&Apache::lonnet::files_not_in_path($ENV{'user.name'}, $ENV{'form.currentpath'})) {
  417:         $r->print($_."<br />");
  418:     }
  419: }
  420: sub upload {
  421:     my ($r)=@_;
  422:     my $fname=$ENV{'form.uploaddoc.filename'};
  423:     my $filesize = (length($ENV{'form.uploaddoc'})) / 1000; #express in k (1024?)
  424:     my $disk_quota = 20000; # expressed in k
  425:     $fname=&Apache::lonnet::clean_filename($fname);
  426:     my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
  427: 						 $ENV{'user.name'}).
  428: 						'/userfiles/portfolio';
  429:     # Fixme --- Move the checking for existing file to LOND error return
  430:     my @dir_list=&Apache::lonnet::dirlist($ENV{'form.currentpath'},
  431: 					  $ENV{'user.domain'},
  432: 					  $ENV{'user.name'},$portfolio_root);
  433:     my $found_file = 0;
  434:     foreach my $line (@dir_list) {
  435:         my ($filename)=split(/\&/,$line,2);
  436:         if ($filename eq $fname){
  437:             $found_file = 1;
  438:         }
  439:     }
  440:     my $current_disk_usage = &Apache::lonnet::diskusage($ENV{'user.domain'}, $ENV{'user.name'},$portfolio_root);
  441:     if ((($current_disk_usage/1000) + $filesize) > $disk_quota){
  442:         $r->print('<font color="red">Unable to upload <strong>'.$fname.' (size = '.$filesize.' kilobytes</strong>. Disk quota will be exceeded.'.
  443:                   '<br />Disk quota is '.$disk_quota.' kilobytes. Your current disk usage is '.$current_disk_usage.' kilobytes.');
  444:     } 
  445:     elsif ($found_file){   
  446:         $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>'.
  447:                   '<br />To upload, rename or delete existing '.$fname.' in '.$ENV{'form.currentpath'});
  448:     } else {
  449:         my $result=&Apache::lonnet::userfileupload('uploaddoc','',
  450: 	        	 'portfolio'.$ENV{'form.currentpath'});
  451:         if ($result !~ m|^/uploaded/|) {
  452:             $r->print('<font color="red"> An errror occured ('.$result.
  453: 	              ') while trying to upload '.&display_file().'</font><br />');
  454:         }
  455:     }
  456:     $r->print(&done());
  457: }
  458: 
  459: sub createdir {
  460:     my ($r)=@_;
  461:     my $newdir=&Apache::lonnet::clean_filename($ENV{'form.newdir'});
  462:     if ($newdir eq '') {
  463:     	$r->print('<font color="red">'.
  464: 	    	  &mt("Error: no directory name was provided.").
  465: 		      '</font><br />');
  466: 	    $r->print(&done());
  467: 	    return;
  468:     } 
  469:     my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
  470: 						 $ENV{'user.name'}).
  471: 						'/userfiles/portfolio';
  472:     my @dir_list=&Apache::lonnet::dirlist($ENV{'form.currentpath'},
  473: 					  $ENV{'user.domain'},
  474: 					  $ENV{'user.name'},$portfolio_root);
  475:     my $found_file = 0;
  476:     foreach my $line (@dir_list) {
  477:         my ($filename)=split(/\&/,$line,2);
  478:         if ($filename eq $newdir){
  479:             $found_file = 1;
  480:         }
  481:     }
  482:     if ($found_file){
  483:     	    $r->print('<font color="red"> Unable to create a directory named <strong>'.$newdir.
  484:     	            ' </strong>a file or directory by that name already exists.</font><br />');
  485:     } else {
  486:         my $result=&Apache::lonnet::mkdiruserfile($ENV{'user.name'},
  487: 	         $ENV{'user.domain'},'portfolio'.$ENV{'form.currentpath'}.$newdir);
  488:         if ($result ne 'ok') {
  489:     	    $r->print('<font color="red"> An errror occured ('.$result.
  490: 	    	      ') while trying to create a new directory '.&display_file().'</font><br />');
  491:         }
  492:     }
  493:     if ($newdir ne $ENV{'form.newdir'}) {
  494:         $r->print("The new directory name was changed from:<br /><strong>".$ENV{'form.newdir'}."</strong> to <strong>$newdir </strong>");  
  495:     }
  496:     $r->print(&done());
  497: }
  498: 
  499: sub handler {
  500:     # this handles file management
  501:     my $r = shift;
  502:     my $bodytag;
  503:     my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
  504: 							 $ENV{'user.name'}).
  505: 							'/userfiles/portfolio';
  506:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  507:                                             ['selectfile','currentpath','meta',
  508: 					     'currentfile','action','fieldname','mode','rename']);
  509:     if ($ENV{"form.mode"} eq 'selectfile'){
  510:         $bodytag = &Apache::loncommon::bodytag('Portfolio Manager',undef,undef,1);
  511:     } else {
  512:         $bodytag = &Apache::loncommon::bodytag('Portfolio Manager');
  513:     }
  514: 
  515:     &Apache::loncommon::no_cache($r);
  516:     &Apache::loncommon::content_type($r,'text/html');
  517:     $r->send_http_header;
  518:     # Give the LON-CAPA page header
  519:     $r->print('<html><head><title>'.
  520:               &mt('Portfolio Manager').
  521:               "</title></head>\n".
  522:               $bodytag);
  523:     $r->rflush();
  524: 	if (($ENV{'form.storeupl'} eq 'Upload') & (!$ENV{'form.uploaddoc.filename'})){
  525:    	    $r->print('<font color="red"> No file was selected to upload.'.
  526:    	            'To upload a file, click <strong>Browse...</strong>'.
  527:    	            ', select a file, then click <strong>Upload</strong>,</font>');
  528: 	}
  529:     if ($ENV{'form.meta'}) {
  530:         &open_form($r);
  531: #        $r->print(&edit_meta_data($r, $ENV{'form.currentpath'}.$ENV{'form.selectfile'}));
  532:         $r->print('Edit the meta data<br />');
  533:         &close_form($r);
  534:     }
  535:     if ($ENV{'form.store'}) {
  536:     }
  537: 
  538:     if ($ENV{'form.uploaddoc.filename'}) {
  539: 	&upload($r);
  540:     } elsif ($ENV{'form.action'} eq 'delete' && $ENV{'form.confirmed'}) {
  541: 	&delete_confirmed($r);
  542:     } elsif ($ENV{'form.action'} eq 'delete') {
  543: 	&delete($r);
  544:     } elsif ($ENV{'form.action'} eq 'deletedir' && $ENV{'form.confirmed'}) {
  545: 	&delete_dir_confirmed($r);
  546:     } elsif ($ENV{'form.action'} eq 'deletedir'){
  547: 	&delete_dir($r);
  548:     } elsif ($ENV{'form.action'} eq 'rename' && $ENV{'form.confirmed'}) {
  549: 	&rename_confirmed($r);
  550:     } elsif ($ENV{'form.rename'}) {
  551:         $ENV{'form.selectfile'} = $ENV{'form.rename'};
  552:         $ENV{'form.action'} = 'rename';
  553: 	&rename($r);
  554:     } elsif ($ENV{'form.createdir'}) {
  555: 	&createdir($r);
  556:     } else {
  557: 	my $current_path='/';
  558: 	if ($ENV{'form.currentpath'}) {
  559: 	    $current_path = $ENV{'form.currentpath'};
  560: 	}
  561: 	my @dir_list=&Apache::lonnet::dirlist($current_path,
  562: 					    $ENV{'user.domain'},
  563: 					    $ENV{'user.name'},$portfolio_root);
  564: 	if ($dir_list[0] eq 'no_such_dir'){
  565: 	    # two main reasons for this:
  566:             #    1) never been here, so directory structure not created
  567: 	    #    2) back-button navigation after deleting a directory
  568: 	    if ($current_path eq '/'){
  569: 	        &Apache::lonnet::mkdiruserfile($ENV{'user.name'},
  570: 					       $ENV{'user.domain'},'portfolio');
  571: 	    } else {
  572:                 # some directory that snuck in get rid of the directory
  573:                 # from the recent pulldown, just in case
  574: 		&Apache::lonhtmlcommon::remove_recent('portfolio',
  575: 						      [$current_path]);
  576: 		$current_path = '/'; # force it back to the root        
  577: 	    }
  578: 	    # now grab the directory list again, for the first time
  579: 	    @dir_list=&Apache::lonnet::dirlist($current_path,
  580: 					    $ENV{'user.domain'},
  581: 					    $ENV{'user.name'},$portfolio_root);
  582:         }
  583: 	# need to know if directory is empty so it can be removed if desired
  584: 	my $is_empty=(@dir_list == 2);
  585: 	&display_common($r,$current_path,$is_empty,\@dir_list);
  586: 	if ($ENV{"form.mode"} eq 'selectfile'){
  587: 	    &select_files($r);
  588:             &display_file_select($r,$current_path,$is_empty,\@dir_list);
  589: 	} else {
  590: 	    &display_directory($r,$current_path,$is_empty,\@dir_list);
  591: 	}
  592: 	$r->print("</body>\n</html>\n");
  593: 	return OK;
  594:     }
  595: }
  596: 1;
  597: __END__

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