Annotation of loncom/interface/portfolio.pm, revision 1.59

1.3       banghart    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: 
1.1       banghart   24: package Apache::portfolio;
                     25: use strict;
                     26: use Apache::Constants qw(:common :http);
1.2       banghart   27: use Apache::loncommon;
1.1       banghart   28: use Apache::lonnet;
1.2       banghart   29: use Apache::lontexconvert;
                     30: use Apache::lonfeedback;
                     31: use Apache::lonlocal;
1.16      banghart   32: 
                     33: # receives a file name and path stub from username/userfiles/portfolio/
                     34: # returns an anchor tag consisting encoding filename and currentpath
1.23      albertel   35: sub make_anchor {
1.57      banghart   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>';
1.8       albertel   38:     return $anchor;
1.6       banghart   39: }
1.24      albertel   40: my $dirptr=16384;
1.48      banghart   41: sub display_common {
1.30      banghart   42:     my ($r,$current_path,$is_empty,$dir_list)=@_;
1.18      banghart   43:     my $iconpath= $r->dir_config('lonIconsURL') . "/";
1.20      banghart   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">';
1.50      banghart   47:     $displayOut .= '<input name="uploaddoc" type="file" />'.
1.24      albertel   48:         '<input type="hidden" name="currentpath" value="'.$current_path.'" />'.
1.50      banghart   49:         '<input type="hidden" name="action" value="'.$ENV{"form.action"}.'" />'.
                     50:         '<input type="hidden" name="fieldname" value="'.$ENV{"form.fieldname"}.'" />'.
1.57      banghart   51:         '<input type="hidden" name="mode" value="'.$ENV{"form.mode"}.'" />'. 
1.20      banghart   52:         '<input type="submit" name="storeupl" value="Upload" />'.
                     53:         '</form>';
                     54:     $r->print($displayOut);
1.22      albertel   55:     $r->print('</td></tr><tr><td bgcolor="#ccddaa" align="center">');
1.24      albertel   56:     $displayOut = '<form method="post">';
                     57:     $displayOut .= '<input name="newdir" type="input" />'.
                     58:         '<input type="hidden" name="currentpath" value="'.$current_path.'" />'.
1.50      banghart   59:         '<input type="hidden" name="action" value="'.$ENV{"form.action"}.'" />'.
                     60:         '<input type="hidden" name="fieldname" value="'.$ENV{"form.fieldname"}.'" />'.
1.57      banghart   61:         '<input type="hidden" name="mode" value="'.$ENV{"form.mode"}.'" />'. 
1.22      albertel   62:         '<input type="submit" name="createdir" value="'.&mt("Create Directory").'" />'.
                     63:         '</form>';
                     64:     $r->print($displayOut);
                     65:     $r->print('</td></tr></table>');
1.24      albertel   66:     my @tree = split (/\//,$current_path);
1.57      banghart   67:     $r->print('<font size="+2">'.&make_anchor('portfolio','/',$ENV{"form.mode"},$ENV{"form.fieldname"}).'/');
1.19      banghart   68:     if (@tree > 1){
                     69:         my $newCurrentPath = '';
                     70:         for (my $i = 1; $i< @tree; $i++){
                     71:             $newCurrentPath .= $tree[$i].'/';
1.57      banghart   72:             $r->print(&make_anchor($tree[$i],'/'.$newCurrentPath, $ENV{"form.mode"},$ENV{"form.fieldname"}).'/');
1.19      banghart   73:         }
                     74:     }
                     75:     $r->print('</font>');
1.24      albertel   76:     &Apache::lonhtmlcommon::store_recent('portfolio',$current_path,$current_path);
1.58      banghart   77:     $r->print('<br /><form method=post action="/adm/portfolio?mode='.$ENV{"form.mode"}.'&fieldname='.$ENV{"form.fieldname"}.'">'.
1.22      albertel   78: 	      &Apache::lonhtmlcommon::select_recent('portfolio','currentpath',
                     79: 						    'this.form.submit();'));
1.21      banghart   80:     $r->print("</form>");
1.48      banghart   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;
1.53      banghart   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:     }
1.45      banghart   92:     if ($is_empty && ($current_path ne '/')) {
1.48      banghart   93:         $display_out = '<form method="post" action="/adm/portfolio">'.
1.30      banghart   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:         
1.48      banghart  100:         $r->print($display_out);
1.31      albertel  101: 	return;
                    102:     }
1.18      banghart  103:     $r->print('<table border="0" cellspacing="2" cellpadding="2">'.
1.22      albertel  104:             '<tr><th>Actions</th><th>&nbsp;</th><th>Name</th><th>Size</th><th>Last Modified</th></tr>');
1.39      banghart  105:     my $href_location="/uploaded/$ENV{'user.domain'}/$ENV{'user.name'}/portfolio$current_path";
1.26      albertel  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)) {
1.18      banghart  112:     	#$strip holds directory/file name
                    113:     	#$dom 
1.23      albertel  114:     	my ($filename,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef)=split(/\&/,$line,16); 
1.53      banghart  115:     	if ($Apache::lonhomework::results{"resource.$ENV{'user.domain'}.$ENV{'user.name'}.$filename"}eq'true'){
                    116:     	    &Apache::lonnet::logthis("found submitted file");
                    117:     	    &Apache::lonnet::logthis("resource.$ENV{'user.domain'}.$ENV{'user.name'}.$filename");
                    118:     	} else {
                    119:     	    &Apache::lonnet::logthis("$filename not found submitted file");
                    120:     	    &Apache::lonnet::logthis("resource.$ENV{'user.domain'}.$ENV{'user.name'}.$filename");
                    121:     	}
1.23      albertel  122:     	if (($filename ne '.') && ($filename ne '..')) {
                    123:             if ($dirptr&$testdir) {
1.19      banghart  124:                 $r->print('<tr bgcolor="#FFAA99"><td><img src="'.$iconpath.'folder_closed.gif"></td>');
                    125:                 $r->print('<td>Go to ...</td>');
1.57      banghart  126:                 $r->print('<td>'.&make_anchor($filename.'/',$current_path.$filename.'/',$ENV{'form.mode'},$ENV{"form.fieldname"}).'</td>'); 
1.19      banghart  127:                 $r->print('</tr>'); 
1.23      albertel  128:             } else {
1.22      albertel  129:                 $r->print('<tr bgcolor="#CCCCFF">');
                    130:                 $r->print('<td>
1.39      banghart  131:                     <form method="post" action="/adm/portfolio">
                    132:                     <select name="action">
                    133:                         <option value=""></option>
                    134:                         <option value="delete">'.&mt("Delete").'</option>
                    135:                         <option value="rename">'.&mt("Rename").'</option>
                    136:                     </select>
                    137:                     <input type="submit" name="doit" value="Go" />
                    138:                     <input type="hidden" name="selectfile" value="'.$filename.'" />
                    139:                     <input type="hidden" name="currentpath" value="'.$current_path.'" />
                    140:                     </form>
                    141:                     </td>');
1.22      albertel  142:                 $r->print('<td><img src="'.$iconpath.'unknown.gif"></td>');
1.23      albertel  143:                 $r->print('<td><a href="'.$href_location.$filename.'">'.
1.39      banghart  144: 			    $filename.'</a></td>'); 
1.22      albertel  145:                 $r->print('<td>'.$size.'</td>');
                    146:                 $r->print('<td>'.&Apache::lonlocal::locallocaltime($mtime).'</td>');
1.19      banghart  147:                 $r->print('</tr>'); 
                    148:             }
1.18      banghart  149:         }
                    150:     }
                    151: #   <tr bgcolor="#FFAA99"> pink bg 
                    152: #   <tr bgcolor="#CCCCFF"> blue bg            
1.24      albertel  153: #   $r->print(&display_directory($current_path, $currentFile, @dir_list));
1.18      banghart  154: #    $r->print('</td>><td>');
1.24      albertel  155: #   $r->print(&display_actions($current_path, $currentFile, $isEmpty));
1.22      albertel  156:     $r->print('</table></form>');
1.24      albertel  157: }
1.47      banghart  158: sub display_file_select {
                    159:     my ($r,$current_path,$is_empty,$dir_list)=@_;
                    160:     my $iconpath= $r->dir_config('lonIconsURL') . "/";
1.48      banghart  161:     my $display_out;
1.47      banghart  162:     if ($is_empty && ($current_path ne '/')) {
1.48      banghart  163:         $display_out = '<form method="post" action="/adm/portfolio">'.
1.47      banghart  164:         '<input type="hidden" name="action" value="deletedir" />'.
                    165:         '<input type="submit" name="deletedir" value="'.&mt("Delete Directory").'" />'.
                    166:         '<input type="hidden" name="selectfile" value="" />'.
                    167:         '<input type="hidden" name="currentpath" value="'.$current_path.'" />'.
1.48      banghart  168: 
1.47      banghart  169:         '</form>';
                    170:         
1.48      banghart  171:         $r->print($display_out);
1.47      banghart  172: 	return;
                    173:     }
                    174:     $r->print('<table border="0" cellspacing="2" cellpadding="2">'.
                    175:             '<tr><th>Select</th><th>&nbsp;</th><th>Name</th><th>Size</th><th>Last Modified</th></tr>');
                    176:     my $href_location="/uploaded/$ENV{'user.domain'}/$ENV{'user.name'}/portfolio$current_path";
1.48      banghart  177:     $r->print('<form method="post" name="checkselect" action="/adm/portfolio">');
1.47      banghart  178:     foreach my $line (sort 
                    179: 		      { 
                    180: 			  my ($afile)=split('&',$a,2);
                    181: 			  my ($bfile)=split('&',$b,2);
                    182: 			  return (lc($afile) cmp lc($bfile));
                    183: 		      } (@$dir_list)) {
                    184:     	#$strip holds directory/file name
                    185:     	#$dom 
                    186:     	my ($filename,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef)=split(/\&/,$line,16); 
                    187:     	if (($filename ne '.') && ($filename ne '..')) {
                    188:             if ($dirptr&$testdir) {
                    189:                 $r->print('<tr bgcolor="#FFAA99"><td><img src="'.$iconpath.'folder_closed.gif"></td>');
                    190:                 $r->print('<td>Go to ...</td>');
1.57      banghart  191:                 $r->print('<td>'.&make_anchor($filename.'/',$current_path.$filename.'/',$ENV{'form.mode'},$ENV{"form.fieldname"}).'</td>'); 
1.47      banghart  192:                 $r->print('</tr>'); 
                    193:             } else {
                    194:                 $r->print('<tr bgcolor="#CCCCFF">');
                    195:                 $r->print('<td>
                    196:                         <input type="checkbox" name="selectedfile" value="'.$filename.'" >
                    197:                     </td>');
                    198:                 $r->print('<td><img src="'.$iconpath.'unknown.gif"></td>');
                    199:                 $r->print('<td><a href="'.$href_location.$filename.'">'.
                    200: 			    $filename.'</a></td>'); 
                    201:                 $r->print('<td>'.$size.'</td>');
                    202:                 $r->print('<td>'.&Apache::lonlocal::locallocaltime($mtime).'</td>');
                    203:                 $r->print('</tr>'); 
                    204:             }
                    205:         }
                    206:     }
1.48      banghart  207:     $r->print('</table>
                    208:             <input type="button" name="doit" onClick= "finishSelect();" value="Select checked files, and close window" />
                    209:             <input type="hidden" name="currentpath" value="'.$current_path.'" />
                    210:     </form>');
1.47      banghart  211: }
1.24      albertel  212: 
                    213: sub open_form {
                    214:     my ($r)=@_;
                    215:     $r->print('<form method="post" action="/adm/portfolio">');
                    216:     $r->print('<input type="hidden" name="action" value="'.
                    217: 	      $ENV{'form.action'}.'" />');
                    218:     $r->print('<input type="hidden" name="confirmed" value="1" />');
                    219:     $r->print('<input type="hidden" name="selectfile" value="'.
                    220: 	      $ENV{'form.selectfile'}.'" />');
                    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 {
1.27      albertel  239:     my ($path,$filename)=@_;
                    240:     if (!defined($path)) { $path=$ENV{'form.currentpath'}; }
                    241:     if (!defined($filename)) { $filename=$ENV{'form.selectfile'}; }
                    242:     return '<tt>'.$path.$filename.'</tt>';
1.24      albertel  243: }
                    244: 
                    245: sub done {
1.25      albertel  246:     return ('<h3><a href="/adm/portfolio?currentpath='.
1.50      banghart  247: 	    $ENV{'form.currentpath'}.
                    248: 	    '&fieldname='.$ENV{'form.fieldname'}.
1.57      banghart  249: 	    '&mode='.$ENV{'form.mode'}.
1.50      banghart  250: 	    '">'.&mt('Done').'</a></h3>');
1.24      albertel  251: }
                    252: 
                    253: sub delete {
                    254:     my ($r)=@_;
1.55      banghart  255:     my @check;
                    256:     my $file_name = $ENV{'form.currentpath'}.$ENV{'form.selectfile'};
1.56      banghart  257:     if (&Apache::lonnet::is_locked($file_name,$ENV{'user.domain'},$ENV{'user.name'} ) eq 'true') {
1.55      banghart  258:         $r->print ("The file is locked and cannot be deleted.<br />");
                    259:         $r->print(&done());
                    260:     } else {
                    261:         &open_form($r);
                    262:         $r->print('<p>'.&mt('Delete').' '.&display_file().'?</p>');
                    263:         &close_form($r);
                    264:     }
1.24      albertel  265: } 
                    266: 
                    267: sub delete_confirmed {
                    268:     my ($r)=@_;
                    269:     my $result=&Apache::lonnet::removeuserfile($ENV{'user.name'},
                    270: 					       $ENV{'user.domain'},'portfolio'.
                    271: 					       $ENV{'form.currentpath'}.
                    272: 					       $ENV{'form.selectfile'});
                    273:     if ($result ne 'ok') {
1.30      banghart  274: 	$r->print('<font color="red"> An error occured ('.$result.
1.25      albertel  275: 		  ') while trying to delete '.&display_file().'</font><br />');
1.24      albertel  276:     }
                    277:     $r->print(&done());
                    278: }
                    279: 
1.30      banghart  280: sub delete_dir {
                    281:     my ($r)=@_;
                    282:     &open_form($r);
                    283:     $r->print('<p>'.&mt('Delete').' '.&display_file().'?</p>');
                    284:     &close_form($r);
                    285: } 
                    286: 
                    287: sub delete_dir_confirmed {
                    288:     my ($r)=@_;
                    289:     my $directory_name = $ENV{'form.currentpath'};
                    290:     $directory_name =~ m/\/$/;
                    291:     $directory_name = $`;
                    292:     my $result=&Apache::lonnet::removeuserfile($ENV{'user.name'},
                    293: 					       $ENV{'user.domain'},'portfolio'.
                    294: 					       $directory_name);
1.32      banghart  295: 					       
1.30      banghart  296:     if ($result ne 'ok') {
                    297: 	$r->print('<font color="red"> An error occured (dir) ('.$result.
                    298: 		  ') while trying to delete '.$directory_name.'</font><br />');
1.32      banghart  299:     } else {
1.41      banghart  300:         # now remove from recent
                    301: #        $r->print('<br /> removing '.$directory_name.'<br /');
1.42      banghart  302:         &Apache::lonhtmlcommon::remove_recent('portfolio',[$directory_name.'/']);
1.32      banghart  303:         my @dirs = split m!/!, $directory_name;
                    304:         
                    305: #        $directory_name =~ m/^(\/*\/)(\/*.)$/;
                    306:         $directory_name='/';
                    307:         for (my $i=1; $i < (@dirs - 1); $i ++){
                    308:             $directory_name .= $dirs[$i].'/';
                    309:         }
                    310:         $ENV{'form.currentpath'} = $directory_name;
1.30      banghart  311:     }
                    312:     $r->print(&done());
                    313: }
                    314: 
1.24      albertel  315: sub rename {
                    316:     my ($r)=@_;
1.55      banghart  317:     my $file_name = $ENV{'form.currentpath'}.$ENV{'form.selectfile'};
1.56      banghart  318:     if (&Apache::lonnet::is_locked($file_name,$ENV{'user.domain'},$ENV{'user.name'}) eq 'true') {
1.55      banghart  319:         $r->print ("The file is locked and cannot be renamed.<br />");
                    320:         $r->print(&done());
                    321:     } else {
                    322:         &open_form($r);
                    323:         $r->print('<p>'.&mt('Rename').' '.&display_file().' to 
                    324:                    <input name="filenewname" type="input" size="50" />?</p>');
                    325:         &close_form($r);
                    326:     }
1.24      albertel  327: }
                    328: 
                    329: sub rename_confirmed {
                    330:     my ($r)=@_;
1.27      albertel  331:     my $filenewname=&Apache::lonnet::clean_filename($ENV{'form.filenewname'});
                    332:     if ($filenewname eq '') {
                    333: 	$r->print('<font color="red">'.
                    334: 		  &mt("Error: no valid filename was provided to rename to.").
                    335: 		  '</font><br />');
                    336: 	$r->print(&done());
                    337: 	return;
                    338:     } 
                    339:     my $result=
                    340: 	&Apache::lonnet::renameuserfile($ENV{'user.name'},$ENV{'user.domain'},
                    341:             'portfolio'.$ENV{'form.currentpath'}.$ENV{'form.selectfile'},
1.59    ! banghart  342:             'portfolio'.$ENV{'form.currentpath'}.$filenewname);
1.27      albertel  343:     if ($result ne 'ok') {
                    344: 	$r->print('<font color="red"> An errror occured ('.$result.
                    345: 		  ') while trying to rename '.&display_file().' to '.
                    346: 		  &display_file(undef,$filenewname).'</font><br />');
                    347:     }
                    348:     $r->print(&done());
                    349: }
1.47      banghart  350: sub select_files {
                    351:     my ($r)=@_;
1.48      banghart  352:     my $java_script =(<<ENDSMP);
                    353:         <script language='javascript'>
                    354:         function finishSelect() {
1.49      banghart  355:             fileList = "";
                    356:             for (i=0;i<document.forms.checkselect.length;i++) { 
                    357:                 if (document.forms.checkselect[i].checked){
1.54      banghart  358:                     fileList = fileList + document.forms.checkselect.currentpath.value + document.forms.checkselect[i].value + "," ;
1.49      banghart  359:                 }
                    360:             }
                    361:             opener.document.forms.lonhomework.
                    362: ENDSMP
                    363:     $java_script .= $ENV{'form.fieldname'};
                    364:     $java_script .= (<<ENDSMP);
                    365:         .value=fileList;
1.48      banghart  366:             self.close();
                    367:         }
                    368:         </script>
                    369: ENDSMP
                    370:     $r->print($java_script);
1.47      banghart  371:     $r->print("<h1>Select portfolio files</h1>
                    372:                 Check as many as you wish in response to the essay problem.<br />");
                    373: }
1.24      albertel  374: sub upload {
                    375:     my ($r)=@_;
1.33      banghart  376:     my $fname=$ENV{'form.uploaddoc.filename'};
1.38      banghart  377:     my $filesize = (length($ENV{'form.uploaddoc'})) / 1000; #express in k (1024?)
                    378:     my $disk_quota = 20000; # expressed in k
1.34      banghart  379:     $fname=&Apache::lonnet::clean_filename($fname);
                    380:     my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
1.33      banghart  381: 						 $ENV{'user.name'}).
                    382: 						'/userfiles/portfolio';
1.38      banghart  383:     # Fixme --- Move the checking for existing file to LOND error return
1.34      banghart  384:     my @dir_list=&Apache::lonnet::dirlist($ENV{'form.currentpath'},
                    385: 					  $ENV{'user.domain'},
                    386: 					  $ENV{'user.name'},$portfolio_root);
                    387:     my $found_file = 0;
1.33      banghart  388:     foreach my $line (@dir_list) {
1.34      banghart  389:         my ($filename)=split(/\&/,$line,2);
1.33      banghart  390:         if ($filename eq $fname){
                    391:             $found_file = 1;
                    392:         }
                    393:     }
1.38      banghart  394:     my $current_disk_usage = &Apache::lonnet::diskusage($ENV{'user.domain'}, $ENV{'user.name'},$portfolio_root);
                    395:     if (($current_disk_usage + $filesize) > $disk_quota){
                    396:         $r->print('<font color="red">Unable to upload <strong>'.$fname.' (size = '.$filesize.' kilobytes</strong>. Disk quota will be exceeded.'.
                    397:                   '<br />Disk quota is '.$disk_quota.' kilobytes. Your current disk usage is '.$current_disk_usage.' kilobytes.');
                    398:     } 
                    399:     elsif ($found_file){   
1.33      banghart  400:         $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>'.
                    401:                   '<br />To upload, rename or delete existing '.$fname.' in '.$ENV{'form.currentpath'});
                    402:     } else {
                    403:         my $result=&Apache::lonnet::userfileupload('uploaddoc','',
                    404: 	        	 'portfolio'.$ENV{'form.currentpath'});
                    405:         if ($result !~ m|^/uploaded/|) {
1.34      banghart  406:             $r->print('<font color="red"> An errror occured ('.$result.
                    407: 	              ') while trying to upload '.&display_file().'</font><br />');
1.33      banghart  408:         }
1.25      albertel  409:     }
                    410:     $r->print(&done());
                    411: }
                    412: 
                    413: sub createdir {
                    414:     my ($r)=@_;
1.28      albertel  415:     my $newdir=&Apache::lonnet::clean_filename($ENV{'form.newdir'});
                    416:     if ($newdir eq '') {
1.37      banghart  417:     	$r->print('<font color="red">'.
                    418: 	    	  &mt("Error: no directory name was provided.").
                    419: 		      '</font><br />');
                    420: 	    $r->print(&done());
                    421: 	    return;
1.28      albertel  422:     } 
1.37      banghart  423:     my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
                    424: 						 $ENV{'user.name'}).
                    425: 						'/userfiles/portfolio';
                    426:     my @dir_list=&Apache::lonnet::dirlist($ENV{'form.currentpath'},
                    427: 					  $ENV{'user.domain'},
                    428: 					  $ENV{'user.name'},$portfolio_root);
                    429:     my $found_file = 0;
                    430:     foreach my $line (@dir_list) {
                    431:         my ($filename)=split(/\&/,$line,2);
                    432:         if ($filename eq $newdir){
                    433:             $found_file = 1;
                    434:         }
                    435:     }
                    436:     if ($found_file){
                    437:     	    $r->print('<font color="red"> Unable to create a directory named <strong>'.$newdir.
                    438:     	            ' </strong>a file or directory by that name already exists.</font><br />');
                    439:     } else {
                    440:         my $result=&Apache::lonnet::mkdiruserfile($ENV{'user.name'},
                    441: 	         $ENV{'user.domain'},'portfolio'.$ENV{'form.currentpath'}.$newdir);
                    442:         if ($result ne 'ok') {
                    443:     	    $r->print('<font color="red"> An errror occured ('.$result.
                    444: 	    	      ') while trying to create a new directory '.&display_file().'</font><br />');
                    445:         }
1.24      albertel  446:     }
                    447:     $r->print(&done());
                    448: }
                    449: 
                    450: sub handler {
                    451:     # this handles file management
                    452:     my $r = shift;
1.47      banghart  453:     my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
1.33      banghart  454: 							 $ENV{'user.name'}).
                    455: 							'/userfiles/portfolio';
1.24      albertel  456:     &Apache::loncommon::no_cache($r);
                    457:     &Apache::loncommon::content_type($r,'text/html');
                    458:     $r->send_http_header;
                    459:     # Give the LON-CAPA page header
                    460:     $r->print('<html><head><title>'.
                    461:               &mt('Portfolio Manager').
                    462:               "</title></head>\n".
                    463:               &Apache::loncommon::bodytag('Portfolio Manager'));
                    464:     $r->rflush();
                    465:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                    466:                                             ['selectfile','currentpath',
1.52      banghart  467: 					     'currentfile','action','fieldname','mode']);
1.40      banghart  468: 	if (($ENV{'form.storeupl'} eq 'Upload') & (!$ENV{'form.uploaddoc.filename'})){
                    469:    	    $r->print('<font color="red"> No file was selected to upload.'.
                    470:    	            'To upload a file, click <strong>Browse...</strong>'.
                    471:    	            ', select a file, then click <strong>Upload</strong>,</font>');
                    472: 	}
1.24      albertel  473:     if ($ENV{'form.uploaddoc.filename'}) {
                    474: 	&upload($r);
1.27      albertel  475:     } elsif ($ENV{'form.action'} eq 'delete' && $ENV{'form.confirmed'}) {
                    476: 	&delete_confirmed($r);
1.24      albertel  477:     } elsif ($ENV{'form.action'} eq 'delete') {
1.27      albertel  478: 	&delete($r);
1.30      banghart  479:     } elsif ($ENV{'form.action'} eq 'deletedir' && $ENV{'form.confirmed'}) {
1.31      albertel  480: 	&delete_dir_confirmed($r);
1.30      banghart  481:     } elsif ($ENV{'form.action'} eq 'deletedir'){
1.31      albertel  482: 	&delete_dir($r);
1.27      albertel  483:     } elsif ($ENV{'form.action'} eq 'rename' && $ENV{'form.confirmed'}) {
                    484: 	&rename_confirmed($r);
1.24      albertel  485:     } elsif ($ENV{'form.action'} eq 'rename') {
                    486: 	&rename($r);
1.25      albertel  487:     } elsif ($ENV{'form.createdir'}) {
                    488: 	&createdir($r);
1.24      albertel  489:     } else {
                    490: 	my $current_path='/';
                    491: 	if ($ENV{'form.currentpath'}) {
                    492: 	    $current_path = $ENV{'form.currentpath'};
                    493: 	}
1.43      banghart  494: 	my @dir_list=&Apache::lonnet::dirlist($current_path,
1.27      albertel  495: 					    $ENV{'user.domain'},
                    496: 					    $ENV{'user.name'},$portfolio_root);
1.46      albertel  497: 	if ($dir_list[0] eq 'no_such_dir'){
                    498: 	    # two main reasons for this:
                    499:             #    1) never been here, so directory structure not created
                    500: 	    #    2) back-button navigation after deleting a directory
                    501: 	    if ($current_path eq '/'){
1.43      banghart  502: 	        &Apache::lonnet::mkdiruserfile($ENV{'user.name'},
1.46      albertel  503: 					       $ENV{'user.domain'},'portfolio');
                    504: 	    } else {
                    505:                 # some directory that snuck in get rid of the directory
                    506:                 # from the recent pulldown, just in case
                    507: 		&Apache::lonhtmlcommon::remove_recent('portfolio',
                    508: 						      [$current_path]);
                    509: 		$current_path = '/'; # force it back to the root        
                    510: 	    }
                    511: 	    # now grab the directory list again, for the first time
                    512: 	    @dir_list=&Apache::lonnet::dirlist($current_path,
                    513: 					    $ENV{'user.domain'},
                    514: 					    $ENV{'user.name'},$portfolio_root);
1.43      banghart  515:         }
1.46      albertel  516: 	# need to know if directory is empty so it can be removed if desired
                    517: 	my $is_empty=(@dir_list == 2);
1.48      banghart  518: 	&display_common($r,$current_path,$is_empty,\@dir_list);
1.51      matthew   519: 	if ($ENV{"form.mode"} eq 'selectfile'){
1.50      banghart  520: 	    &select_files($r);
                    521:             &display_file_select($r,$current_path,$is_empty,\@dir_list);
                    522: 	} else {
                    523: 	    &display_directory($r,$current_path,$is_empty,\@dir_list);
                    524: 	}
1.46      albertel  525: 	$r->print("</body>\n</html>\n");
                    526: 	return OK;
1.30      banghart  527:     }
1.2       banghart  528: }
1.1       banghart  529: 1;
                    530: __END__

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