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

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); 
                    115:     	if (($filename ne '.') && ($filename ne '..')) {
                    116:             if ($dirptr&$testdir) {
1.19      banghart  117:                 $r->print('<tr bgcolor="#FFAA99"><td><img src="'.$iconpath.'folder_closed.gif"></td>');
                    118:                 $r->print('<td>Go to ...</td>');
1.57      banghart  119:                 $r->print('<td>'.&make_anchor($filename.'/',$current_path.$filename.'/',$ENV{'form.mode'},$ENV{"form.fieldname"}).'</td>'); 
1.19      banghart  120:                 $r->print('</tr>'); 
1.23      albertel  121:             } else {
1.22      albertel  122:                 $r->print('<tr bgcolor="#CCCCFF">');
                    123:                 $r->print('<td>
1.39      banghart  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>');
1.22      albertel  135:                 $r->print('<td><img src="'.$iconpath.'unknown.gif"></td>');
1.23      albertel  136:                 $r->print('<td><a href="'.$href_location.$filename.'">'.
1.39      banghart  137: 			    $filename.'</a></td>'); 
1.22      albertel  138:                 $r->print('<td>'.$size.'</td>');
                    139:                 $r->print('<td>'.&Apache::lonlocal::locallocaltime($mtime).'</td>');
1.19      banghart  140:                 $r->print('</tr>'); 
                    141:             }
1.18      banghart  142:         }
                    143:     }
                    144: #   <tr bgcolor="#FFAA99"> pink bg 
                    145: #   <tr bgcolor="#CCCCFF"> blue bg            
1.24      albertel  146: #   $r->print(&display_directory($current_path, $currentFile, @dir_list));
1.18      banghart  147: #    $r->print('</td>><td>');
1.24      albertel  148: #   $r->print(&display_actions($current_path, $currentFile, $isEmpty));
1.22      albertel  149:     $r->print('</table></form>');
1.24      albertel  150: }
1.47      banghart  151: sub display_file_select {
                    152:     my ($r,$current_path,$is_empty,$dir_list)=@_;
                    153:     my $iconpath= $r->dir_config('lonIconsURL') . "/";
1.48      banghart  154:     my $display_out;
1.61      banghart  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:     }
1.47      banghart  159:     if ($is_empty && ($current_path ne '/')) {
1.48      banghart  160:         $display_out = '<form method="post" action="/adm/portfolio">'.
1.47      banghart  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.'" />'.
1.48      banghart  165: 
1.47      banghart  166:         '</form>';
                    167:         
1.48      banghart  168:         $r->print($display_out);
1.47      banghart  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";
1.48      banghart  174:     $r->print('<form method="post" name="checkselect" action="/adm/portfolio">');
1.47      banghart  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); 
1.61      banghart  184:     	$filename =~ s/\s+$//;
1.47      banghart  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>');
1.57      banghart  189:                 $r->print('<td>'.&make_anchor($filename.'/',$current_path.$filename.'/',$ENV{'form.mode'},$ENV{"form.fieldname"}).'</td>'); 
1.47      banghart  190:                 $r->print('</tr>'); 
                    191:             } else {
                    192:                 $r->print('<tr bgcolor="#CCCCFF">');
1.61      banghart  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>');
1.47      banghart  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:     }
1.48      banghart  210:     $r->print('</table>
1.60      banghart  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 />
1.48      banghart  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>');
1.47      banghart  218: }
1.24      albertel  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 {
1.27      albertel  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>';
1.24      albertel  250: }
                    251: 
                    252: sub done {
1.25      albertel  253:     return ('<h3><a href="/adm/portfolio?currentpath='.
1.50      banghart  254: 	    $ENV{'form.currentpath'}.
                    255: 	    '&fieldname='.$ENV{'form.fieldname'}.
1.57      banghart  256: 	    '&mode='.$ENV{'form.mode'}.
1.50      banghart  257: 	    '">'.&mt('Done').'</a></h3>');
1.24      albertel  258: }
                    259: 
                    260: sub delete {
                    261:     my ($r)=@_;
1.55      banghart  262:     my @check;
                    263:     my $file_name = $ENV{'form.currentpath'}.$ENV{'form.selectfile'};
1.56      banghart  264:     if (&Apache::lonnet::is_locked($file_name,$ENV{'user.domain'},$ENV{'user.name'} ) eq 'true') {
1.55      banghart  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:     }
1.24      albertel  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') {
1.30      banghart  281: 	$r->print('<font color="red"> An error occured ('.$result.
1.25      albertel  282: 		  ') while trying to delete '.&display_file().'</font><br />');
1.24      albertel  283:     }
                    284:     $r->print(&done());
                    285: }
                    286: 
1.30      banghart  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);
1.32      banghart  302: 					       
1.30      banghart  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 />');
1.32      banghart  306:     } else {
1.41      banghart  307:         # now remove from recent
                    308: #        $r->print('<br /> removing '.$directory_name.'<br /');
1.42      banghart  309:         &Apache::lonhtmlcommon::remove_recent('portfolio',[$directory_name.'/']);
1.32      banghart  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;
1.30      banghart  318:     }
                    319:     $r->print(&done());
                    320: }
                    321: 
1.24      albertel  322: sub rename {
                    323:     my ($r)=@_;
1.55      banghart  324:     my $file_name = $ENV{'form.currentpath'}.$ENV{'form.selectfile'};
1.56      banghart  325:     if (&Apache::lonnet::is_locked($file_name,$ENV{'user.domain'},$ENV{'user.name'}) eq 'true') {
1.55      banghart  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:     }
1.24      albertel  334: }
                    335: 
                    336: sub rename_confirmed {
                    337:     my ($r)=@_;
1.27      albertel  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'},
1.59      banghart  349:             'portfolio'.$ENV{'form.currentpath'}.$filenewname);
1.27      albertel  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: }
1.47      banghart  357: sub select_files {
                    358:     my ($r)=@_;
1.60      banghart  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);
1.62    ! banghart  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.=',';
1.60      banghart  374:     }
1.48      banghart  375:     my $java_script =(<<ENDSMP);
                    376:         <script language='javascript'>
                    377:         function finishSelect() {
1.62    ! banghart  378: ENDSMP
        !           379:     $java_script .= 'fileList = "'.$java_files.'";';
        !           380:     $java_script .= (<<ENDSMP);
1.49      banghart  381:             for (i=0;i<document.forms.checkselect.length;i++) { 
                    382:                 if (document.forms.checkselect[i].checked){
1.54      banghart  383:                     fileList = fileList + document.forms.checkselect.currentpath.value + document.forms.checkselect[i].value + "," ;
1.49      banghart  384:                 }
                    385:             }
                    386:             opener.document.forms.lonhomework.
                    387: ENDSMP
                    388:     $java_script .= $ENV{'form.fieldname'};
                    389:     $java_script .= (<<ENDSMP);
                    390:         .value=fileList;
1.48      banghart  391:             self.close();
                    392:         }
                    393:         </script>
                    394: ENDSMP
                    395:     $r->print($java_script);
1.47      banghart  396:     $r->print("<h1>Select portfolio files</h1>
                    397:                 Check as many as you wish in response to the essay problem.<br />");
1.61      banghart  398:     $r->print("<strong>Files selected from other directories:</strong><br />");
1.60      banghart  399:     foreach (&Apache::lonnet::files_not_in_path($ENV{'user.name'}, $ENV{'form.currentpath'})) {
                    400:         $r->print($_."<br />");
                    401:     }
1.47      banghart  402: }
1.24      albertel  403: sub upload {
                    404:     my ($r)=@_;
1.33      banghart  405:     my $fname=$ENV{'form.uploaddoc.filename'};
1.38      banghart  406:     my $filesize = (length($ENV{'form.uploaddoc'})) / 1000; #express in k (1024?)
                    407:     my $disk_quota = 20000; # expressed in k
1.34      banghart  408:     $fname=&Apache::lonnet::clean_filename($fname);
                    409:     my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
1.33      banghart  410: 						 $ENV{'user.name'}).
                    411: 						'/userfiles/portfolio';
1.38      banghart  412:     # Fixme --- Move the checking for existing file to LOND error return
1.34      banghart  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;
1.33      banghart  417:     foreach my $line (@dir_list) {
1.34      banghart  418:         my ($filename)=split(/\&/,$line,2);
1.33      banghart  419:         if ($filename eq $fname){
                    420:             $found_file = 1;
                    421:         }
                    422:     }
1.38      banghart  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){   
1.33      banghart  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/|) {
1.34      banghart  435:             $r->print('<font color="red"> An errror occured ('.$result.
                    436: 	              ') while trying to upload '.&display_file().'</font><br />');
1.33      banghart  437:         }
1.25      albertel  438:     }
                    439:     $r->print(&done());
                    440: }
                    441: 
                    442: sub createdir {
                    443:     my ($r)=@_;
1.28      albertel  444:     my $newdir=&Apache::lonnet::clean_filename($ENV{'form.newdir'});
                    445:     if ($newdir eq '') {
1.37      banghart  446:     	$r->print('<font color="red">'.
                    447: 	    	  &mt("Error: no directory name was provided.").
                    448: 		      '</font><br />');
                    449: 	    $r->print(&done());
                    450: 	    return;
1.28      albertel  451:     } 
1.37      banghart  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:         }
1.24      albertel  475:     }
                    476:     $r->print(&done());
                    477: }
                    478: 
                    479: sub handler {
                    480:     # this handles file management
                    481:     my $r = shift;
1.47      banghart  482:     my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
1.33      banghart  483: 							 $ENV{'user.name'}).
                    484: 							'/userfiles/portfolio';
1.24      albertel  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',
1.52      banghart  496: 					     'currentfile','action','fieldname','mode']);
1.40      banghart  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: 	}
1.24      albertel  502:     if ($ENV{'form.uploaddoc.filename'}) {
                    503: 	&upload($r);
1.27      albertel  504:     } elsif ($ENV{'form.action'} eq 'delete' && $ENV{'form.confirmed'}) {
                    505: 	&delete_confirmed($r);
1.24      albertel  506:     } elsif ($ENV{'form.action'} eq 'delete') {
1.27      albertel  507: 	&delete($r);
1.30      banghart  508:     } elsif ($ENV{'form.action'} eq 'deletedir' && $ENV{'form.confirmed'}) {
1.31      albertel  509: 	&delete_dir_confirmed($r);
1.30      banghart  510:     } elsif ($ENV{'form.action'} eq 'deletedir'){
1.31      albertel  511: 	&delete_dir($r);
1.27      albertel  512:     } elsif ($ENV{'form.action'} eq 'rename' && $ENV{'form.confirmed'}) {
                    513: 	&rename_confirmed($r);
1.24      albertel  514:     } elsif ($ENV{'form.action'} eq 'rename') {
                    515: 	&rename($r);
1.25      albertel  516:     } elsif ($ENV{'form.createdir'}) {
                    517: 	&createdir($r);
1.24      albertel  518:     } else {
                    519: 	my $current_path='/';
                    520: 	if ($ENV{'form.currentpath'}) {
                    521: 	    $current_path = $ENV{'form.currentpath'};
                    522: 	}
1.43      banghart  523: 	my @dir_list=&Apache::lonnet::dirlist($current_path,
1.27      albertel  524: 					    $ENV{'user.domain'},
                    525: 					    $ENV{'user.name'},$portfolio_root);
1.46      albertel  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 '/'){
1.43      banghart  531: 	        &Apache::lonnet::mkdiruserfile($ENV{'user.name'},
1.46      albertel  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);
1.43      banghart  544:         }
1.46      albertel  545: 	# need to know if directory is empty so it can be removed if desired
                    546: 	my $is_empty=(@dir_list == 2);
1.48      banghart  547: 	&display_common($r,$current_path,$is_empty,\@dir_list);
1.51      matthew   548: 	if ($ENV{"form.mode"} eq 'selectfile'){
1.50      banghart  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: 	}
1.46      albertel  554: 	$r->print("</body>\n</html>\n");
                    555: 	return OK;
1.30      banghart  556:     }
1.2       banghart  557: }
1.1       banghart  558: 1;
                    559: __END__

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