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

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

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