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

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 {
                     36:     my ($filename, $current_path) = @_;
                     37:     my $anchor = '<a href="/adm/portfolio?selectfile='.$filename.'&currentpath='.$current_path.'">'.$filename.'</a>';
1.8       albertel   38:     return $anchor;
1.6       banghart   39: }
1.16      banghart   40: 
1.24      albertel   41: my $dirptr=16384;
                     42: sub display_directory {
1.30      banghart   43:     my ($r,$current_path,$is_empty,$dir_list)=@_;
1.18      banghart   44:     my $iconpath= $r->dir_config('lonIconsURL') . "/";
1.20      banghart   45:     $r->print('<table border="0" cellspacing="2" cellpadding="2"><tr valign="middle">');
                     46:     $r->print('<td bgcolor="#ccddaa" align="center">');
                     47:     my $displayOut = '<form method="post" enctype="multipart/form-data">';
                     48:     $displayOut = $displayOut.'<input name="uploaddoc" type="file" />'.
1.24      albertel   49:         '<input type="hidden" name="currentpath" value="'.$current_path.'" />'.
1.20      banghart   50:         '<input type="submit" name="storeupl" value="Upload" />'.
                     51:         '</form>';
                     52:     $r->print($displayOut);
1.22      albertel   53:     $r->print('</td></tr><tr><td bgcolor="#ccddaa" align="center">');
1.24      albertel   54:     $displayOut = '<form method="post">';
                     55:     $displayOut .= '<input name="newdir" type="input" />'.
                     56:         '<input type="hidden" name="currentpath" value="'.$current_path.'" />'.
1.22      albertel   57:         '<input type="submit" name="createdir" value="'.&mt("Create Directory").'" />'.
                     58:         '</form>';
                     59:     $r->print($displayOut);
                     60:     $r->print('</td></tr></table>');
1.24      albertel   61:     my @tree = split (/\//,$current_path);
1.29      albertel   62:     $r->print('<font size="+2">'.&make_anchor('portfolio','/').'/');
1.19      banghart   63:     if (@tree > 1){
                     64:         my $newCurrentPath = '';
                     65:         for (my $i = 1; $i< @tree; $i++){
                     66:             $newCurrentPath .= $tree[$i].'/';
1.23      albertel   67:             $r->print(&make_anchor($tree[$i],'/'.$newCurrentPath).'/');
1.19      banghart   68:         }
                     69:     }
                     70:     $r->print('</font>');
1.24      albertel   71:     &Apache::lonhtmlcommon::store_recent('portfolio',$current_path,$current_path);
1.39      banghart   72:     #Fixme -- remove from recent those paths that are deleted
1.22      albertel   73:     $r->print('<br /><form method=post action="/adm/portfolio">'.
                     74: 	      &Apache::lonhtmlcommon::select_recent('portfolio','currentpath',
                     75: 						    'this.form.submit();'));
1.21      banghart   76:     $r->print("</form>");
1.31      albertel   77:     if ($is_empty) {
1.30      banghart   78:         $displayOut = '<form method="post" action="/adm/portfolio">'.
                     79:         '<input type="hidden" name="action" value="deletedir" />'.
                     80:         '<input type="submit" name="deletedir" value="'.&mt("Delete Directory").'" />'.
                     81:         '<input type="hidden" name="selectfile" value="" />'.
                     82:         '<input type="hidden" name="currentpath" value="'.$current_path.'" />'.
                     83:         '</form>';
                     84:         
                     85:         $r->print($displayOut);
1.31      albertel   86: 	return;
                     87:     }
1.18      banghart   88:     $r->print('<table border="0" cellspacing="2" cellpadding="2">'.
1.22      albertel   89:             '<tr><th>Actions</th><th>&nbsp;</th><th>Name</th><th>Size</th><th>Last Modified</th></tr>');
1.39      banghart   90:     my $href_location="/uploaded/$ENV{'user.domain'}/$ENV{'user.name'}/portfolio$current_path";
1.26      albertel   91:     foreach my $line (sort 
                     92: 		      { 
                     93: 			  my ($afile)=split('&',$a,2);
                     94: 			  my ($bfile)=split('&',$b,2);
                     95: 			  return (lc($afile) cmp lc($bfile));
                     96: 		      } (@$dir_list)) {
1.18      banghart   97:     	#$strip holds directory/file name
                     98:     	#$dom 
1.23      albertel   99:     	my ($filename,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef)=split(/\&/,$line,16); 
                    100:     	if (($filename ne '.') && ($filename ne '..')) {
                    101:             if ($dirptr&$testdir) {
1.19      banghart  102:                 $r->print('<tr bgcolor="#FFAA99"><td><img src="'.$iconpath.'folder_closed.gif"></td>');
                    103:                 $r->print('<td>Go to ...</td>');
1.24      albertel  104:                 $r->print('<td>'.&make_anchor($filename.'/',$current_path.$filename.'/').'</td>'); 
1.19      banghart  105:                 $r->print('</tr>'); 
1.23      albertel  106:             } else {
1.22      albertel  107:                 $r->print('<tr bgcolor="#CCCCFF">');
                    108:                 $r->print('<td>
1.39      banghart  109:                     <form method="post" action="/adm/portfolio">
                    110:                     <select name="action">
                    111:                         <option value=""></option>
                    112:                         <option value="delete">'.&mt("Delete").'</option>
                    113:                         <option value="rename">'.&mt("Rename").'</option>
                    114:                     </select>
                    115:                     <input type="submit" name="doit" value="Go" />
                    116:                     <input type="hidden" name="selectfile" value="'.$filename.'" />
                    117:                     <input type="hidden" name="currentpath" value="'.$current_path.'" />
                    118:                     </form>
                    119:                     </td>');
1.22      albertel  120:                 $r->print('<td><img src="'.$iconpath.'unknown.gif"></td>');
1.23      albertel  121:                 $r->print('<td><a href="'.$href_location.$filename.'">'.
1.39      banghart  122: 			    $filename.'</a></td>'); 
1.22      albertel  123:                 $r->print('<td>'.$size.'</td>');
                    124:                 $r->print('<td>'.&Apache::lonlocal::locallocaltime($mtime).'</td>');
1.19      banghart  125:                 $r->print('</tr>'); 
                    126:             }
1.18      banghart  127:         }
                    128:     }
                    129: #   <tr bgcolor="#FFAA99"> pink bg 
                    130: #   <tr bgcolor="#CCCCFF"> blue bg            
1.24      albertel  131: #   $r->print(&display_directory($current_path, $currentFile, @dir_list));
1.18      banghart  132: #    $r->print('</td>><td>');
1.24      albertel  133: #   $r->print(&display_actions($current_path, $currentFile, $isEmpty));
1.22      albertel  134:     $r->print('</table></form>');
1.24      albertel  135: }
                    136: 
                    137: sub open_form {
                    138:     my ($r)=@_;
                    139:     $r->print('<form method="post" action="/adm/portfolio">');
                    140:     $r->print('<input type="hidden" name="action" value="'.
                    141: 	      $ENV{'form.action'}.'" />');
                    142:     $r->print('<input type="hidden" name="confirmed" value="1" />');
                    143:     $r->print('<input type="hidden" name="selectfile" value="'.
                    144: 	      $ENV{'form.selectfile'}.'" />');
                    145:     $r->print('<input type="hidden" name="currentpath" value="'.
                    146: 	      $ENV{'form.currentpath'}.'" />');
                    147: }
                    148: 
                    149: sub close_form {
                    150:     my ($r)=@_;
                    151:     $r->print('<p><input type="submit" value="'.&mt('Continue').
                    152: 	      '" /></p></form>');
                    153:     $r->print('<form action="/adm/portfolio" method="POST">
                    154:                <p>
                    155:               <input type="hidden" name="currentpath" value="'.
                    156: 	      $ENV{'form.currentpath'}.'" />
                    157:                  <input type="submit" value="'.&mt('Cancel').'" />
                    158:                </p></form>');
                    159: 
                    160: }
                    161: 
                    162: sub display_file {
1.27      albertel  163:     my ($path,$filename)=@_;
                    164:     if (!defined($path)) { $path=$ENV{'form.currentpath'}; }
                    165:     if (!defined($filename)) { $filename=$ENV{'form.selectfile'}; }
                    166:     return '<tt>'.$path.$filename.'</tt>';
1.24      albertel  167: }
                    168: 
                    169: sub done {
1.25      albertel  170:     return ('<h3><a href="/adm/portfolio?currentpath='.
1.24      albertel  171: 	    $ENV{'form.currentpath'}.'">'.&mt('Done').'</a></h3>');
                    172: }
                    173: 
                    174: sub delete {
                    175:     my ($r)=@_;
                    176:     &open_form($r);
                    177:     $r->print('<p>'.&mt('Delete').' '.&display_file().'?</p>');
                    178:     &close_form($r);
                    179: } 
                    180: 
                    181: sub delete_confirmed {
                    182:     my ($r)=@_;
                    183:     my $result=&Apache::lonnet::removeuserfile($ENV{'user.name'},
                    184: 					       $ENV{'user.domain'},'portfolio'.
                    185: 					       $ENV{'form.currentpath'}.
                    186: 					       $ENV{'form.selectfile'});
                    187:     if ($result ne 'ok') {
1.30      banghart  188: 	$r->print('<font color="red"> An error occured ('.$result.
1.25      albertel  189: 		  ') while trying to delete '.&display_file().'</font><br />');
1.24      albertel  190:     }
                    191:     $r->print(&done());
                    192: }
                    193: 
1.30      banghart  194: sub delete_dir {
                    195:     my ($r)=@_;
                    196:     &open_form($r);
                    197:     $r->print('<p>'.&mt('Delete').' '.&display_file().'?</p>');
                    198:     &close_form($r);
                    199: } 
                    200: 
                    201: sub delete_dir_confirmed {
                    202:     my ($r)=@_;
                    203:     my $directory_name = $ENV{'form.currentpath'};
                    204:     $directory_name =~ m/\/$/;
                    205:     $directory_name = $`;
                    206:     my $result=&Apache::lonnet::removeuserfile($ENV{'user.name'},
                    207: 					       $ENV{'user.domain'},'portfolio'.
                    208: 					       $directory_name);
1.32      banghart  209: 					       
1.30      banghart  210:     if ($result ne 'ok') {
                    211: 	$r->print('<font color="red"> An error occured (dir) ('.$result.
                    212: 		  ') while trying to delete '.$directory_name.'</font><br />');
1.32      banghart  213:     } else {
1.41    ! banghart  214:         # now remove from recent
        !           215: #        $r->print('<br /> removing '.$directory_name.'<br /');
        !           216:         my @rem_dirs = ($directory_name.'/');
        !           217:         &Apache::lonhtmlcommon::remove_recent('portfolio',\@rem_dirs);
1.32      banghart  218:         my @dirs = split m!/!, $directory_name;
                    219:         
                    220: #        $directory_name =~ m/^(\/*\/)(\/*.)$/;
                    221:         $directory_name='/';
                    222:         for (my $i=1; $i < (@dirs - 1); $i ++){
                    223:             $directory_name .= $dirs[$i].'/';
                    224:         }
                    225:         $ENV{'form.currentpath'} = $directory_name;
1.30      banghart  226:     }
                    227:     $r->print(&done());
                    228: }
                    229: 
1.24      albertel  230: sub rename {
                    231:     my ($r)=@_;
                    232:     &open_form($r);
1.27      albertel  233:     $r->print('<p>'.&mt('Rename').' '.&display_file().' to 
                    234:                <input name="filenewname" type="input" size="50" />?</p>');
1.24      albertel  235:     &close_form($r);
                    236: }
                    237: 
                    238: sub rename_confirmed {
                    239:     my ($r)=@_;
1.27      albertel  240:     my $filenewname=&Apache::lonnet::clean_filename($ENV{'form.filenewname'});
                    241:     if ($filenewname eq '') {
                    242: 	$r->print('<font color="red">'.
                    243: 		  &mt("Error: no valid filename was provided to rename to.").
                    244: 		  '</font><br />');
                    245: 	$r->print(&done());
                    246: 	return;
                    247:     } 
                    248:     my $result=
                    249: 	&Apache::lonnet::renameuserfile($ENV{'user.name'},$ENV{'user.domain'},
                    250:             'portfolio'.$ENV{'form.currentpath'}.$ENV{'form.selectfile'},
                    251:             'portfolio'.$ENV{'form.currentpath'}.$ENV{'form.filenewname'});
                    252:     if ($result ne 'ok') {
                    253: 	$r->print('<font color="red"> An errror occured ('.$result.
                    254: 		  ') while trying to rename '.&display_file().' to '.
                    255: 		  &display_file(undef,$filenewname).'</font><br />');
                    256:     }
                    257:     $r->print(&done());
                    258: }
1.24      albertel  259: 
                    260: sub upload {
                    261:     my ($r)=@_;
1.33      banghart  262:     my $fname=$ENV{'form.uploaddoc.filename'};
1.38      banghart  263:     my $filesize = (length($ENV{'form.uploaddoc'})) / 1000; #express in k (1024?)
                    264:     my $disk_quota = 20000; # expressed in k
1.34      banghart  265:     $fname=&Apache::lonnet::clean_filename($fname);
                    266:     my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
1.33      banghart  267: 						 $ENV{'user.name'}).
                    268: 						'/userfiles/portfolio';
1.38      banghart  269:     # Fixme --- Move the checking for existing file to LOND error return
1.34      banghart  270:     my @dir_list=&Apache::lonnet::dirlist($ENV{'form.currentpath'},
                    271: 					  $ENV{'user.domain'},
                    272: 					  $ENV{'user.name'},$portfolio_root);
                    273:     my $found_file = 0;
1.33      banghart  274:     foreach my $line (@dir_list) {
1.34      banghart  275:         my ($filename)=split(/\&/,$line,2);
1.33      banghart  276:         if ($filename eq $fname){
                    277:             $found_file = 1;
                    278:         }
                    279:     }
1.38      banghart  280:     my $current_disk_usage = &Apache::lonnet::diskusage($ENV{'user.domain'}, $ENV{'user.name'},$portfolio_root);
                    281:     if (($current_disk_usage + $filesize) > $disk_quota){
                    282:         $r->print('<font color="red">Unable to upload <strong>'.$fname.' (size = '.$filesize.' kilobytes</strong>. Disk quota will be exceeded.'.
                    283:                   '<br />Disk quota is '.$disk_quota.' kilobytes. Your current disk usage is '.$current_disk_usage.' kilobytes.');
                    284:     } 
                    285:     elsif ($found_file){   
1.33      banghart  286:         $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>'.
                    287:                   '<br />To upload, rename or delete existing '.$fname.' in '.$ENV{'form.currentpath'});
                    288:     } else {
                    289:         my $result=&Apache::lonnet::userfileupload('uploaddoc','',
                    290: 	        	 'portfolio'.$ENV{'form.currentpath'});
                    291:         if ($result !~ m|^/uploaded/|) {
1.34      banghart  292:             $r->print('<font color="red"> An errror occured ('.$result.
                    293: 	              ') while trying to upload '.&display_file().'</font><br />');
1.33      banghart  294:         }
1.25      albertel  295:     }
                    296:     $r->print(&done());
                    297: }
                    298: 
                    299: sub createdir {
                    300:     my ($r)=@_;
1.28      albertel  301:     my $newdir=&Apache::lonnet::clean_filename($ENV{'form.newdir'});
                    302:     if ($newdir eq '') {
1.37      banghart  303:     	$r->print('<font color="red">'.
                    304: 	    	  &mt("Error: no directory name was provided.").
                    305: 		      '</font><br />');
                    306: 	    $r->print(&done());
                    307: 	    return;
1.28      albertel  308:     } 
1.37      banghart  309:     my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
                    310: 						 $ENV{'user.name'}).
                    311: 						'/userfiles/portfolio';
                    312:     my @dir_list=&Apache::lonnet::dirlist($ENV{'form.currentpath'},
                    313: 					  $ENV{'user.domain'},
                    314: 					  $ENV{'user.name'},$portfolio_root);
                    315:     my $found_file = 0;
                    316:     foreach my $line (@dir_list) {
                    317:         my ($filename)=split(/\&/,$line,2);
                    318:         if ($filename eq $newdir){
                    319:             $found_file = 1;
                    320:         }
                    321:     }
                    322:     if ($found_file){
                    323:     	    $r->print('<font color="red"> Unable to create a directory named <strong>'.$newdir.
                    324:     	            ' </strong>a file or directory by that name already exists.</font><br />');
                    325:     } else {
                    326:         my $result=&Apache::lonnet::mkdiruserfile($ENV{'user.name'},
                    327: 	         $ENV{'user.domain'},'portfolio'.$ENV{'form.currentpath'}.$newdir);
                    328:         if ($result ne 'ok') {
                    329:     	    $r->print('<font color="red"> An errror occured ('.$result.
                    330: 	    	      ') while trying to create a new directory '.&display_file().'</font><br />');
                    331:         }
1.24      albertel  332:     }
                    333:     $r->print(&done());
                    334: }
                    335: 
                    336: sub handler {
                    337:     # this handles file management
                    338:     my $r = shift;
1.33      banghart  339: 	my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
                    340: 							 $ENV{'user.name'}).
                    341: 							'/userfiles/portfolio';
1.24      albertel  342:     &Apache::loncommon::no_cache($r);
                    343:     &Apache::loncommon::content_type($r,'text/html');
                    344:     $r->send_http_header;
                    345:     # Give the LON-CAPA page header
                    346:     $r->print('<html><head><title>'.
                    347:               &mt('Portfolio Manager').
                    348:               "</title></head>\n".
                    349:               &Apache::loncommon::bodytag('Portfolio Manager'));
                    350:     $r->rflush();
                    351:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                    352:                                             ['selectfile','currentpath',
                    353: 					     'currentfile']);
1.36      banghart  354: 	# make sure there is a portfolio directory off /userfiles
1.38      banghart  355: 	# Fixme -- Do this in a better way -- offload it to the LOND routine
1.36      banghart  356: 	my @dir_list=&Apache::lonnet::dirlist('/',
                    357: 					    $ENV{'user.domain'},
                    358: 					    $ENV{'user.name'},$portfolio_root);
                    359: 	if ($dir_list[0] eq 'no_such_dir'){
                    360: 	     &Apache::lonnet::mkdiruserfile($ENV{'user.name'},
                    361: 	     $ENV{'user.domain'},'portfolio');
                    362: 	}
1.40      banghart  363: 	if (($ENV{'form.storeupl'} eq 'Upload') & (!$ENV{'form.uploaddoc.filename'})){
                    364:    	    $r->print('<font color="red"> No file was selected to upload.'.
                    365:    	            'To upload a file, click <strong>Browse...</strong>'.
                    366:    	            ', select a file, then click <strong>Upload</strong>,</font>');
                    367: 	}
1.24      albertel  368:     if ($ENV{'form.uploaddoc.filename'}) {
                    369: 	&upload($r);
1.27      albertel  370:     } elsif ($ENV{'form.action'} eq 'delete' && $ENV{'form.confirmed'}) {
                    371: 	&delete_confirmed($r);
1.24      albertel  372:     } elsif ($ENV{'form.action'} eq 'delete') {
1.27      albertel  373: 	&delete($r);
1.30      banghart  374:     } elsif ($ENV{'form.action'} eq 'deletedir' && $ENV{'form.confirmed'}) {
1.31      albertel  375: 	&delete_dir_confirmed($r);
1.30      banghart  376:     } elsif ($ENV{'form.action'} eq 'deletedir'){
1.31      albertel  377: 	&delete_dir($r);
1.27      albertel  378:     } elsif ($ENV{'form.action'} eq 'rename' && $ENV{'form.confirmed'}) {
                    379: 	&rename_confirmed($r);
1.24      albertel  380:     } elsif ($ENV{'form.action'} eq 'rename') {
                    381: 	&rename($r);
1.25      albertel  382:     } elsif ($ENV{'form.createdir'}) {
                    383: 	&createdir($r);
1.24      albertel  384:     } else {
                    385: 	my $current_path='/';
                    386: 	if ($ENV{'form.currentpath'}) {
                    387: 	    $current_path = $ENV{'form.currentpath'};
                    388: 	}
1.38      banghart  389: 	@dir_list=&Apache::lonnet::dirlist($current_path,
1.27      albertel  390: 					    $ENV{'user.domain'},
                    391: 					    $ENV{'user.name'},$portfolio_root);
1.24      albertel  392:     
                    393: 	# need to know if directory is empty so it can be removed if desired
                    394: 	my $is_empty=(@dir_list == 2);
1.31      albertel  395: 	&display_directory($r,$current_path,$is_empty,\@dir_list);
                    396: 	$r->print("</body>\n</html>\n");
                    397: 	return OK;
1.30      banghart  398:     }
1.2       banghart  399: }
1.1       banghart  400: 1;
                    401: __END__

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