File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.80: download - view: text, annotated - select for diffs
Tue Mar 15 23:11:08 2005 UTC (19 years, 2 months ago) by banghart
Branches: MAIN
CVS tags: HEAD

	change uri to direct to explict edit uploaded (portfolio) file

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

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