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

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 {
                    214:         my @dirs = split m!/!, $directory_name;
                    215:         
                    216: #        $directory_name =~ m/^(\/*\/)(\/*.)$/;
                    217:         $directory_name='/';
                    218:         for (my $i=1; $i < (@dirs - 1); $i ++){
                    219:             $directory_name .= $dirs[$i].'/';
                    220:         }
                    221:         $ENV{'form.currentpath'} = $directory_name;
1.30      banghart  222:     }
                    223:     $r->print(&done());
                    224: }
                    225: 
1.24      albertel  226: sub rename {
                    227:     my ($r)=@_;
                    228:     &open_form($r);
1.27      albertel  229:     $r->print('<p>'.&mt('Rename').' '.&display_file().' to 
                    230:                <input name="filenewname" type="input" size="50" />?</p>');
1.24      albertel  231:     &close_form($r);
                    232: }
                    233: 
                    234: sub rename_confirmed {
                    235:     my ($r)=@_;
1.27      albertel  236:     my $filenewname=&Apache::lonnet::clean_filename($ENV{'form.filenewname'});
                    237:     if ($filenewname eq '') {
                    238: 	$r->print('<font color="red">'.
                    239: 		  &mt("Error: no valid filename was provided to rename to.").
                    240: 		  '</font><br />');
                    241: 	$r->print(&done());
                    242: 	return;
                    243:     } 
                    244:     my $result=
                    245: 	&Apache::lonnet::renameuserfile($ENV{'user.name'},$ENV{'user.domain'},
                    246:             'portfolio'.$ENV{'form.currentpath'}.$ENV{'form.selectfile'},
                    247:             'portfolio'.$ENV{'form.currentpath'}.$ENV{'form.filenewname'});
                    248:     if ($result ne 'ok') {
                    249: 	$r->print('<font color="red"> An errror occured ('.$result.
                    250: 		  ') while trying to rename '.&display_file().' to '.
                    251: 		  &display_file(undef,$filenewname).'</font><br />');
                    252:     }
                    253:     $r->print(&done());
                    254: }
1.24      albertel  255: 
                    256: sub upload {
                    257:     my ($r)=@_;
1.33      banghart  258:     my $fname=$ENV{'form.uploaddoc.filename'};
1.38      banghart  259:     my $filesize = (length($ENV{'form.uploaddoc'})) / 1000; #express in k (1024?)
                    260:     my $disk_quota = 20000; # expressed in k
1.34      banghart  261:     $fname=&Apache::lonnet::clean_filename($fname);
                    262:     my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
1.33      banghart  263: 						 $ENV{'user.name'}).
                    264: 						'/userfiles/portfolio';
1.38      banghart  265:     # Fixme --- Move the checking for existing file to LOND error return
1.34      banghart  266:     my @dir_list=&Apache::lonnet::dirlist($ENV{'form.currentpath'},
                    267: 					  $ENV{'user.domain'},
                    268: 					  $ENV{'user.name'},$portfolio_root);
                    269:     my $found_file = 0;
1.33      banghart  270:     foreach my $line (@dir_list) {
1.34      banghart  271:         my ($filename)=split(/\&/,$line,2);
1.33      banghart  272:         if ($filename eq $fname){
                    273:             $found_file = 1;
                    274:         }
                    275:     }
1.38      banghart  276:     my $current_disk_usage = &Apache::lonnet::diskusage($ENV{'user.domain'}, $ENV{'user.name'},$portfolio_root);
                    277:     if (($current_disk_usage + $filesize) > $disk_quota){
                    278:         $r->print('<font color="red">Unable to upload <strong>'.$fname.' (size = '.$filesize.' kilobytes</strong>. Disk quota will be exceeded.'.
                    279:                   '<br />Disk quota is '.$disk_quota.' kilobytes. Your current disk usage is '.$current_disk_usage.' kilobytes.');
                    280:     } 
                    281:     elsif ($found_file){   
1.33      banghart  282:         $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>'.
                    283:                   '<br />To upload, rename or delete existing '.$fname.' in '.$ENV{'form.currentpath'});
                    284:     } else {
                    285:         my $result=&Apache::lonnet::userfileupload('uploaddoc','',
                    286: 	        	 'portfolio'.$ENV{'form.currentpath'});
                    287:         if ($result !~ m|^/uploaded/|) {
1.34      banghart  288:             $r->print('<font color="red"> An errror occured ('.$result.
                    289: 	              ') while trying to upload '.&display_file().'</font><br />');
1.33      banghart  290:         }
1.25      albertel  291:     }
                    292:     $r->print(&done());
                    293: }
                    294: 
                    295: sub createdir {
                    296:     my ($r)=@_;
1.28      albertel  297:     my $newdir=&Apache::lonnet::clean_filename($ENV{'form.newdir'});
                    298:     if ($newdir eq '') {
1.37      banghart  299:     	$r->print('<font color="red">'.
                    300: 	    	  &mt("Error: no directory name was provided.").
                    301: 		      '</font><br />');
                    302: 	    $r->print(&done());
                    303: 	    return;
1.28      albertel  304:     } 
1.37      banghart  305:     my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
                    306: 						 $ENV{'user.name'}).
                    307: 						'/userfiles/portfolio';
                    308:     my @dir_list=&Apache::lonnet::dirlist($ENV{'form.currentpath'},
                    309: 					  $ENV{'user.domain'},
                    310: 					  $ENV{'user.name'},$portfolio_root);
                    311:     my $found_file = 0;
                    312:     foreach my $line (@dir_list) {
                    313:         my ($filename)=split(/\&/,$line,2);
                    314:         if ($filename eq $newdir){
                    315:             $found_file = 1;
                    316:         }
                    317:     }
                    318:     if ($found_file){
                    319:     	    $r->print('<font color="red"> Unable to create a directory named <strong>'.$newdir.
                    320:     	            ' </strong>a file or directory by that name already exists.</font><br />');
                    321:     } else {
                    322:         my $result=&Apache::lonnet::mkdiruserfile($ENV{'user.name'},
                    323: 	         $ENV{'user.domain'},'portfolio'.$ENV{'form.currentpath'}.$newdir);
                    324:         if ($result ne 'ok') {
                    325:     	    $r->print('<font color="red"> An errror occured ('.$result.
                    326: 	    	      ') while trying to create a new directory '.&display_file().'</font><br />');
                    327:         }
1.24      albertel  328:     }
                    329:     $r->print(&done());
                    330: }
                    331: 
                    332: sub handler {
                    333:     # this handles file management
                    334:     my $r = shift;
1.33      banghart  335: 	my $portfolio_root = &Apache::loncommon::propath($ENV{'user.domain'},
                    336: 							 $ENV{'user.name'}).
                    337: 							'/userfiles/portfolio';
1.24      albertel  338:     &Apache::loncommon::no_cache($r);
                    339:     &Apache::loncommon::content_type($r,'text/html');
                    340:     $r->send_http_header;
                    341:     # Give the LON-CAPA page header
                    342:     $r->print('<html><head><title>'.
                    343:               &mt('Portfolio Manager').
                    344:               "</title></head>\n".
                    345:               &Apache::loncommon::bodytag('Portfolio Manager'));
                    346:     $r->rflush();
                    347:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                    348:                                             ['selectfile','currentpath',
                    349: 					     'currentfile']);
1.36      banghart  350: 	# make sure there is a portfolio directory off /userfiles
1.38      banghart  351: 	# Fixme -- Do this in a better way -- offload it to the LOND routine
1.36      banghart  352: 	my @dir_list=&Apache::lonnet::dirlist('/',
                    353: 					    $ENV{'user.domain'},
                    354: 					    $ENV{'user.name'},$portfolio_root);
                    355: 	if ($dir_list[0] eq 'no_such_dir'){
                    356: 	     &Apache::lonnet::mkdiruserfile($ENV{'user.name'},
                    357: 	     $ENV{'user.domain'},'portfolio');
                    358: 	}
1.40    ! banghart  359: 	if (($ENV{'form.storeupl'} eq 'Upload') & (!$ENV{'form.uploaddoc.filename'})){
        !           360:    	    $r->print('<font color="red"> No file was selected to upload.'.
        !           361:    	            'To upload a file, click <strong>Browse...</strong>'.
        !           362:    	            ', select a file, then click <strong>Upload</strong>,</font>');
        !           363: 	}
1.24      albertel  364:     if ($ENV{'form.uploaddoc.filename'}) {
                    365: 	&upload($r);
1.27      albertel  366:     } elsif ($ENV{'form.action'} eq 'delete' && $ENV{'form.confirmed'}) {
                    367: 	&delete_confirmed($r);
1.24      albertel  368:     } elsif ($ENV{'form.action'} eq 'delete') {
1.27      albertel  369: 	&delete($r);
1.30      banghart  370:     } elsif ($ENV{'form.action'} eq 'deletedir' && $ENV{'form.confirmed'}) {
1.31      albertel  371: 	&delete_dir_confirmed($r);
1.30      banghart  372:     } elsif ($ENV{'form.action'} eq 'deletedir'){
1.31      albertel  373: 	&delete_dir($r);
1.27      albertel  374:     } elsif ($ENV{'form.action'} eq 'rename' && $ENV{'form.confirmed'}) {
                    375: 	&rename_confirmed($r);
1.24      albertel  376:     } elsif ($ENV{'form.action'} eq 'rename') {
                    377: 	&rename($r);
1.25      albertel  378:     } elsif ($ENV{'form.createdir'}) {
                    379: 	&createdir($r);
1.24      albertel  380:     } else {
                    381: 	my $current_path='/';
                    382: 	if ($ENV{'form.currentpath'}) {
                    383: 	    $current_path = $ENV{'form.currentpath'};
                    384: 	}
1.38      banghart  385: 	@dir_list=&Apache::lonnet::dirlist($current_path,
1.27      albertel  386: 					    $ENV{'user.domain'},
                    387: 					    $ENV{'user.name'},$portfolio_root);
1.24      albertel  388:     
                    389: 	# need to know if directory is empty so it can be removed if desired
                    390: 	my $is_empty=(@dir_list == 2);
1.31      albertel  391: 	&display_directory($r,$current_path,$is_empty,\@dir_list);
                    392: 	$r->print("</body>\n</html>\n");
                    393: 	return OK;
1.30      banghart  394:     }
1.2       banghart  395: }
1.1       banghart  396: 1;
                    397: __END__

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