File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.62: download - view: text, annotated - select for diffs
Sat Dec 4 19:08:42 2004 UTC (19 years, 6 months ago) by banghart
Branches: MAIN
CVS tags: HEAD
	Actually works once. Need to work out logic for clearing the
	file storing selected files, so files are "preselected" on
	subsequent visits.

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

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