File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.102: download - view: text, annotated - select for diffs
Wed Jun 7 18:41:48 2006 UTC (18 years ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Work in progress.  Adding access control entry to file_permissions.db.  Current implementation is as additional arrays in array of arrays for each key (key =f file name - including path). New arrays contain either 'access' or 'accesscount' as first element.  These will become hashes in the next iteration. Added column to portfolio directory displays to show access status.  Some fixes to paths to handle coursegroup portfolios - e.g., new &prepend_group() function.

    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: 
   24: package Apache::portfolio;
   25: use strict;
   26: use Apache::Constants qw(:common :http);
   27: use Apache::loncommon;
   28: use Apache::lonnet;
   29: use Apache::lontexconvert;
   30: use Apache::lonfeedback;
   31: use Apache::lonlocal;
   32: use Apache::lonnet;
   33: use Apache::longroup;
   34: use lib '/home/httpd/lib/perl';
   35: use LONCAPA;
   36: 
   37: # receives a file name and path stub from username/userfiles/portfolio/
   38: # returns an anchor tag consisting encoding filename and currentpath
   39: sub make_anchor {
   40:     my ($url, $filename, $current_path, $current_mode, $field_name,
   41:         $continue_select,$group) = @_;
   42:     if ($continue_select ne 'true') {$continue_select = 'false'};
   43:     my $anchor = '<a href="'.$url.'?selectfile='.$filename.'&currentpath='.$current_path.'&mode='.$current_mode.'&continue='.$continue_select.'&fieldname='.$field_name;
   44:     if (defined($group)) {
   45:         $anchor .= '&group='.$group;
   46:     }
   47:     $anchor .= '">'.$filename.'</a>';
   48:     return $anchor;
   49: }
   50: my $dirptr=16384;
   51: sub display_common {
   52:     my ($r,$url,$current_path,$is_empty,$dir_list,$group)=@_;
   53:     my $groupitem;
   54:     my $namespace = &get_namespace($group);
   55:     my $port_path = &get_port_path($group);
   56:     if (defined($group)) {
   57:         $groupitem = '<input type="hidden" name="group" value="'.$group.'" />';
   58:     } 
   59:     my $iconpath= $r->dir_config('lonIconsURL') . "/";
   60:     my %text=&Apache::lonlocal::texthash('upload' => 'Upload',
   61: 					 'upload_label' =>  
   62: 					 'Upload file to current directory:',
   63: 					 'createdir' => 'Create Subdirectory',
   64: 					 'createdir_label' => 
   65: 					 'Create subdirectory in current directory:');
   66:     $r->print(<<"TABLE"); 
   67: <table border="0" cellspacing="2" cellpadding="2">
   68:   <form method="post" enctype="multipart/form-data">
   69:     <tr valign="middle">
   70:       <td bgcolor="#ccddaa" align="right">
   71:         $text{'upload_label'}
   72:       </td>
   73:       <td bgcolor="#ccddaa" align="left">$groupitem
   74:         <input name="uploaddoc" type="file" />
   75: 	<input type="hidden" name="currentpath" value="$current_path" />
   76: 	<input type="hidden" name="action" value="$env{"form.action"}" />
   77: 	<input type="hidden" name="fieldname" value="$env{"form.fieldname"}" />
   78: 	<input type="hidden" name="mode" value="$env{"form.mode"}" />
   79: 	<input type="submit" name="storeupl" value="$text{'upload'}" />
   80:       </td>
   81:     </tr>
   82:   </form>
   83:   <form method="post">
   84:     <tr>
   85:       <td bgcolor="#ccddaa" align="right">
   86:         $text{'createdir_label'}
   87:       </td>
   88:       <td bgcolor="#ccddaa" align="left">
   89:         <input name="newdir" type="input" />$groupitem
   90:         <input type="hidden" name="currentpath" value="$current_path" />
   91:         <input type="hidden" name="action" value="$env{"form.action"}" />
   92:         <input type="hidden" name="fieldname" value="$env{"form.fieldname"}" />
   93:         <input type="hidden" name="mode" value="$env{"form.mode"}" />
   94:         <input type="submit" name="createdir" value="$text{'createdir'}" />
   95:       </td>
   96:     </tr>
   97:   </form>
   98: </table>
   99: TABLE
  100:     my @tree = split (/\//,$current_path);
  101:     $r->print('<font size="+2">'.&make_anchor($url,$port_path,'/',$env{"form.mode"},$env{"form.fieldname"},$env{"form.continue"},$group).'/');
  102:     if (@tree > 1){
  103:         my $newCurrentPath = '';
  104:         for (my $i = 1; $i< @tree; $i++){
  105:             $newCurrentPath .= $tree[$i].'/';
  106:             $r->print(&make_anchor($url,$tree[$i],'/'.$newCurrentPath, $env{"form.mode"},$env{"form.fieldname"}, $env{"form.continue"},$group).'/');
  107:         }
  108:     }
  109:     $r->print('</font>');
  110:     &Apache::lonhtmlcommon::store_recent($namespace,$current_path,$current_path);
  111:     $r->print('<br /><form method=post action="'.$url.'?mode='.$env{"form.mode"}.'&fieldname='.$env{"form.fieldname"});
  112:     if (defined($group)) {
  113:         $r->print('&group='.$group);
  114:     }
  115:     $r->print('">'.
  116: 	      &Apache::lonhtmlcommon::select_recent($namespace,'currentpath',
  117: 						    'this.form.submit();'));
  118:     $r->print("</form>");
  119: }
  120: sub display_directory {
  121:     my ($r,$url,$current_path,$is_empty,$dir_list,$group)=@_;
  122:     my $iconpath= $r->dir_config('lonIconsURL') . "/";
  123:     my ($groupitem,$groupecho);
  124:     my $display_out;
  125:     my $select_mode;
  126:     my $checked_files;
  127:     my $port_path = &get_port_path($group);
  128:     my ($uname,$udom) = &get_name_dom($group);
  129:     if (defined($group)) {
  130:        $groupitem = '<input type="hidden" name="group" value="'.$group.'" />'; 
  131:        $groupecho = '&amp;group='.$group;
  132:     }
  133:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
  134:                                                                         $uname);
  135:     my %locked_files = &Apache::lonnet::get_marked_as_readonly_hash(
  136:                                                   $current_permissions,$group);
  137:     my %access_controls = &Apache::lonnet::get_access_controls($current_permissions,$group);
  138:     my $now = time;
  139:     my $curr_access;
  140:     my $pub_access = 0;
  141:     foreach my $key (sort(keys(%access_controls))) {
  142:         my ($scope,$end,$start) = ($key =~ /^\d+:(\w+)_(\d*)_?(\d*)$/);
  143:         if (($now > $start) && (!$end || $end > $now)) {
  144:             if ($scope eq 'public')  {
  145:                 $pub_access = 1;
  146:             }
  147:          }
  148:     }
  149:     if (!$pub_access) {
  150:         $curr_access = 'Private'
  151:     } elsif ($pub_access) {
  152:         $curr_access = 'Public';
  153:     }
  154:     if ($env{"form.mode"} eq 'selectfile'){
  155: 	&select_files($r);
  156: 	$checked_files =&Apache::lonnet::files_in_path($uname,$env{'form.currentpath'});
  157: 	$select_mode = 'true';
  158:     } 
  159:     if ($is_empty && ($current_path ne '/')) {
  160:         $display_out = '<form method="post" action="'.$url.'">'.$groupitem.
  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.'" />'.
  165:         '</form>';
  166:         
  167:         $r->print($display_out);
  168: 	return;
  169:     }
  170:     if ($select_mode eq 'true') {
  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:         $r->print('<form method="post" name="checkselect" action="'.$url.'">');
  174:     } else {
  175:         $r->print('<table border="0" cellspacing="2" cellpadding="2">'.
  176:             '<tr><th colspan="2">Actions</th><th>&nbsp;</th><th>Name</th><th>Size</th><th>Last Modified</th><th>Current Access Status</th></tr>');
  177:         $r->print('<form method="post" action="'.$url.'">');
  178:     }
  179:     if (defined($group)) {
  180:         $r->print("\n".$groupitem."\n");
  181:     }
  182:     my $href_location="/uploaded/$udom/$uname/$port_path".$current_path;
  183:     my $href_edit_location="/editupload/$udom/$uname/$port_path".$current_path;
  184:     foreach my $line (sort 
  185: 		      { 
  186: 			  my ($afile)=split('&',$a,2);
  187: 			  my ($bfile)=split('&',$b,2);
  188: 			  return (lc($afile) cmp lc($bfile));
  189: 		      } (@$dir_list)) {
  190:     	#$strip holds directory/file name
  191:     	#$dom 
  192:     	my ($filename,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef)=split(/\&/,$line,16); 
  193:     	$filename =~ s/\s+$//;
  194:     	if (($filename ne '.') && ($filename ne '..') && ($filename !~ /\.meta$/ ) && ($filename !~ /(.*)\.(\d+)\.([^\.]*)$/)) {
  195:             if ($dirptr&$testdir) {
  196:                 if ($select_mode eq 'true'){
  197:                     $r->print('<tr bgcolor="#FFAA99"><td><img src="'.$iconpath.'folder_closed.gif"></td>');
  198:                 } else {
  199:                     $r->print('<tr bgcolor="#FFAA99"><td colspan="2"><img src="'.$iconpath.'folder_closed.gif"></td>');
  200:                 }
  201:                 $r->print('<td>Go to ...</td>');
  202:                 $r->print('<td>'.&make_anchor($url,$filename.'/',$current_path.$filename.'/',$env{'form.mode'},$env{"form.fieldname"},$env{'form.continue'},$group).'</td>'); 
  203:                 $r->print('</tr>'); 
  204:             } else {
  205:                 $r->print('<tr bgcolor="#CCCCFF">');
  206:                 if ($select_mode eq 'true'){
  207:                     $r->print('<td><input type="checkbox" name="checkfile" value="'.$filename.'"');
  208:                     if ($$checked_files{$filename} eq 'selected') {
  209:                         $r->print("CHECKED");
  210:                     }
  211:                     $r->print('></td>');
  212:                 } else {
  213:                     if (exists $locked_files{$current_path.$filename}){
  214:                         $r->print('<td colspan="2"><a href="'.$url.'?lockinfo='.$current_path.$filename.$groupecho.'">Locked</a></td>');
  215:                     } else {
  216: 			my $cat='<img alt="'.&mt('Catalog Information').
  217: 			    '" src="'.&Apache::loncommon::lonhttpdurl('/res/adm/pages/catalog.gif').'" />';
  218:                         $r->print('<td><input type="checkbox" name="selectfile" value="'.$filename.'" />
  219:                             <a href="'.$url.'?rename='.$filename.'&amp;currentpath='.$current_path.$groupecho.'">Rename</a></td>
  220:                             <td><a href="'.$href_edit_location.$filename.'.meta">'.$cat.'</a>
  221:                             </td>');
  222:                     }
  223:                 }
  224:                 $r->print('<td><img src="'.&Apache::loncommon::icon($filename).'"></td>');
  225:                 $r->print('<td><a href="'.$href_location.$filename.'">'.
  226: 			    $filename.'</a></td>'); 
  227:                 $r->print('<td>'.$size.'</td>');
  228:                 $r->print('<td>'.&Apache::lonlocal::locallocaltime($mtime).'</td>');
  229:                 $r->print('<td><nobr>'.&mt($curr_access).'&nbsp;&nbsp;&nbsp;'.
  230:                           '<a href="'.$url.'?access='.$filename.
  231:                           '&amp;currentpath='.$current_path.$groupecho.
  232:                           '">'.&mt('View/Change').'</a></nobr></td>');
  233:                 $r->print('</tr>'); 
  234:             }
  235:         }
  236:     }
  237:     if ($select_mode eq 'true') {
  238:         $r->print('</table>
  239:             <input type="hidden" name="continue" value="true">
  240:             <input type="hidden" name="fieldname" value="'.$env{'form.fieldname'}.'">
  241:             <input type="hidden" name="mode" value="selectfile">
  242:             <input type="submit" name="submit" value="Select checked files, and continue selecting." /><br />
  243:             <input type="button" name="doit" onClick= "finishSelect();" value="Select checked files, and close window" />
  244:             <input type="hidden" name="currentpath" value="'.$current_path.'" />
  245:         </form>');        
  246:     } else {
  247:         $r->print('</table>
  248:         <input type="submit" name="doit" value="Delete Checked Files" />
  249:         <input type="hidden" name="action" value="delete" />
  250:         <input type="hidden" name="currentpath" value="'.$current_path.'" />
  251:         </form>');
  252:     }
  253: }
  254: 
  255: sub open_form {
  256:     my ($r,$url)=@_;
  257:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
  258:     $r->print('<form name="portform" method="post" action="'.$url.'">');
  259:     $r->print('<input type="hidden" name="action" value="'.
  260: 	      $env{'form.action'}.'" />');
  261:     $r->print('<input type="hidden" name="confirmed" value="1" />');
  262:     foreach (@files) {
  263:         $r->print('<input type="hidden" name="selectfile" value="'.
  264: 	      $_.'" />');
  265:     }
  266:     $r->print('<input type="hidden" name="currentpath" value="'.
  267: 	      $env{'form.currentpath'}.'" />');
  268: }
  269: 
  270: sub close_form {
  271:     my ($r,$url,$group)=@_;
  272:     $r->print('<p><input type="submit" value="'.&mt('Continue').'" />');
  273:     if (defined($group)) {
  274:        $r->print("\n".'<input type="hidden" name="group" value="'.
  275:               $group.'" />');
  276:     }
  277:     $r->print('</p></form>');
  278:     $r->print('<form action="'.$url.'" method="POST">
  279:                <p>
  280:               <input type="hidden" name="currentpath" value="'.
  281: 	      $env{'form.currentpath'}.'" />');
  282:     if (defined($group)) {
  283:        $r->print("\n".'<input type="hidden" name="group" value="'.
  284:               $group.'" />');
  285:     }
  286:     $r->print("\n".'   <input type="submit" value="'.&mt('Cancel').'" />
  287:                </p></form>'); 
  288: }
  289: 
  290: sub display_file {
  291:     my ($path,$filename)=@_;
  292:     my $display_file_text;
  293:     if (!defined($path)) { $path=$env{'form.currentpath'}; }
  294:     if (!defined($filename)) { 
  295:         $filename=$env{'form.selectfile'};
  296:         $display_file_text = '<tt>'.$path.$filename.'</tt>';
  297:     } elsif (ref($filename) eq "ARRAY") {
  298:         foreach (@$filename) {
  299:             $display_file_text .= '<tt>'.$path.$_.'</tt><br />';
  300:         }
  301:     } elsif (ref($filename) eq "SCALAR") {
  302:         $display_file_text = '<tt>'.$path.$filename.'</tt>';        
  303:     }
  304:     return $display_file_text;
  305: }
  306: 
  307: sub done {
  308:     my ($message,$url,$group)=@_;
  309:     unless (defined $message) {
  310:         $message='Done';
  311:     }
  312:     my $result = '<h3><a href="'.$url.'?currentpath='.
  313: 	         $env{'form.currentpath'}.
  314: 	         '&fieldname='.$env{'form.fieldname'}.
  315: 	         '&mode='.$env{'form.mode'};
  316:     if (defined($group)) {
  317:         $result .= '&group='.$group;
  318:     }
  319:     $result .= '">'.&mt($message).'</a></h3>';
  320:     return $result;
  321: }
  322: 
  323: sub delete {
  324:     my ($r,$url,$group)=@_;
  325:     my @check;
  326:     my $file_name = $env{'form.currentpath'}.$env{'form.selectfile'};
  327:     $file_name = &prepend_group($file_name,$group);
  328:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
  329:     my ($uname,$udom) = &get_name_dom($group);
  330:     if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
  331:         $r->print ("The file is locked and cannot be deleted.<br />");
  332:         $r->print(&done('Back',$url,$group));
  333:     } else {
  334:         if (scalar(@files)) {
  335:             &open_form($r,$url);
  336:             $r->print('<p>'.&mt('Delete').' '.&display_file(undef,\@files).'?</p>');
  337:             &close_form($r,$url,$group);
  338:         } else {
  339:             $r->print("No file was checked to delete.<br />");
  340:             $r->print(&done(undef,$url,$group));
  341:         }
  342:     }
  343: } 
  344: 
  345: sub delete_confirmed {
  346:     my ($r,$url,$group)=@_;
  347:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
  348:     my $result;
  349:     my ($uname,$udom) = &get_name_dom($group);
  350:     my $port_path = &get_port_path($group);
  351:     foreach my $delete_file (@files) {
  352:         $result=&Apache::lonnet::removeuserfile($uname,$udom,$port_path.
  353: 					       $env{'form.currentpath'}.
  354: 					       $delete_file);
  355:         if ($result ne 'ok') {
  356: 	$r->print('<font color="red"> An error occured ('.$result.
  357: 		  ') while trying to delete '.&display_file(undef, $delete_file).'</font><br />');
  358:         }
  359:     }
  360:     $r->print(&done(undef,$url,$group));
  361: }
  362: 
  363: sub delete_dir {
  364:     my ($r,$url,$group)=@_;
  365:     &open_form($r,$url);
  366:     $r->print('<p>'.&mt('Delete').' '.&display_file().'?</p>');
  367:     &close_form($r,$url,$group);
  368: } 
  369: 
  370: sub delete_dir_confirmed {
  371:     my ($r,$url,$group)=@_;
  372:     my $directory_name = $env{'form.currentpath'};
  373:     $directory_name =~ s|/$||; # remove any trailing slash
  374:     my ($uname,$udom) = &get_name_dom($group);
  375:     my $namespace = &get_namespace($group);
  376:     my $port_path = &get_port_path($group);
  377:     my $result=&Apache::lonnet::removeuserfile($uname,$udom,$port_path.
  378: 					       $directory_name);
  379: 					       
  380:     if ($result ne 'ok') {
  381: 	$r->print('<font color="red"> An error occured (dir) ('.$result.
  382: 		  ') while trying to delete '.$directory_name.'</font><br />');
  383:     } else {
  384:         # now remove from recent
  385: #        $r->print('<br /> removing '.$directory_name.'<br /');
  386:         &Apache::lonhtmlcommon::remove_recent($namespace,[$directory_name.'/']);
  387:         my @dirs = split m!/!, $directory_name;
  388:         
  389: #        $directory_name =~ m/^(\/*\/)(\/*.)$/;
  390:         $directory_name='/';
  391:         for (my $i=1; $i < (@dirs - 1); $i ++){
  392:             $directory_name .= $dirs[$i].'/';
  393:         }
  394:         $env{'form.currentpath'} = $directory_name;
  395:     }
  396:     $r->print(&done(undef,$url,$group));
  397: }
  398: 
  399: sub rename {
  400:     my ($r,$url,$group)=@_;
  401:     my $file_name = $env{'form.currentpath'}.$env{'form.rename'};
  402:     my ($uname,$udom) = &get_name_dom($group);
  403:     $file_name = &prepend_group($file_name,$group);
  404:     if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
  405:         $r->print ("The file is locked and cannot be renamed.<br />");
  406:         $r->print(&done(undef,$url,$group));
  407:     } else {
  408:         &open_form($r,$url);
  409:         $r->print('<p>'.&mt('Rename').' '.&display_file().' to 
  410:                    <input name="filenewname" type="input" size="50" />?</p>');
  411:         &close_form($r,$url,$group);
  412:     }
  413: }
  414: 
  415: sub rename_confirmed {
  416:     my ($r,$url,$group)=@_;
  417:     my $filenewname=&Apache::lonnet::clean_filename($env{'form.filenewname'});
  418:     my ($uname,$udom) = &get_name_dom($group);
  419:     my $port_path = &get_port_path($group);
  420:     if ($filenewname eq '') {
  421: 	$r->print('<font color="red">'.
  422: 		  &mt("Error: no valid filename was provided to rename to.").
  423: 		  '</font><br />');
  424: 	$r->print(&done(undef,$url,$group));
  425: 	return;
  426:     } 
  427:     my $result=
  428: 	&Apache::lonnet::renameuserfile($uname,$udom,
  429:             $port_path.$env{'form.currentpath'}.$env{'form.selectfile'},
  430:             $port_path.$env{'form.currentpath'}.$filenewname);
  431:     if ($result ne 'ok') {
  432: 	$r->print('<font color="red"> An errror occured ('.$result.
  433: 		  ') while trying to rename '.&display_file().' to '.
  434: 		  &display_file(undef,$filenewname).'</font><br />');
  435:     }
  436:     if ($filenewname ne $env{'form.filenewname'}) {
  437:         $r->print("The new file name was changed from:<br /><strong>".$env{'form.filenewname'}."</strong> to <strong>$filenewname </strong>");
  438:     }
  439:     $r->print(&done(undef,$url,$group));
  440: }
  441: 
  442: sub select_files {
  443:     my ($r,$group)=@_;
  444:     if ($env{'form.continue'} eq 'true') {
  445:         # here we update the selections for the currentpath
  446:         # eventually, have to handle removing those not checked, but . . . 
  447:         my @items=&Apache::loncommon::get_env_multiple('form.checkfile');
  448:         if (scalar(@items)){
  449:              &Apache::lonnet::save_selected_files($env{'user.name'}, $env{'form.currentpath'}, @items);
  450:         }
  451:     } else {
  452:             #empty the file for a fresh start
  453:             &Apache::lonnet::clear_selected_files($env{'user.name'});
  454:     }
  455:     my @files = &Apache::lonnet::files_not_in_path($env{'user.name'}, $env{'form.currentpath'});
  456:     my $java_files = join ",", @files;
  457:     if ($java_files) {
  458:         $java_files.=',';
  459:     }
  460:     my $javascript =(<<ENDSMP);
  461:         <script language='javascript'>
  462:         function finishSelect() {
  463: ENDSMP
  464:     $javascript .= 'fileList = "'.$java_files.'";';
  465:     $javascript .= (<<ENDSMP);
  466:             for (i=0;i<document.forms.checkselect.length;i++) { 
  467:                 if (document.forms.checkselect[i].checked){
  468:                     fileList = fileList + document.forms.checkselect.currentpath.value + document.forms.checkselect[i].value + "," ;
  469:                 }
  470:             }
  471:             opener.document.forms.lonhomework.
  472: ENDSMP
  473:     $javascript .= $env{'form.fieldname'};
  474:     $javascript .= (<<ENDSMP);
  475:         .value=fileList;
  476:             self.close();
  477:         }
  478:         </script>
  479: ENDSMP
  480:     $r->print($javascript);
  481:     $r->print("<h1>Select portfolio files</h1>
  482:                 Check as many as you wish in response to the problem.<br />");
  483:     my @otherfiles=&Apache::lonnet::files_not_in_path($env{'user.name'}, $env{'form.currentpath'});
  484:     if (@otherfiles) {
  485: 	$r->print("<strong>Files selected from other directories:</strong><br />");
  486: 	foreach my $file (@otherfiles) {
  487: 	    $r->print($file."<br />");
  488: 	}
  489:     }
  490: }
  491: sub upload {
  492:     my ($r,$url,$group)=@_;
  493:     my $fname=$env{'form.uploaddoc.filename'};
  494:     my $filesize = (length($env{'form.uploaddoc'})) / 1000; #express in k (1024?)
  495:     my $disk_quota = 20000; # expressed in k
  496:     $fname=&Apache::lonnet::clean_filename($fname);
  497: 
  498:     my $portfolio_root=&get_portfolio_root($group);
  499:     my ($uname,$udom) = &get_name_dom($group);
  500:     my $port_path = &get_port_path($group);
  501:     # Fixme --- Move the checking for existing file to LOND error return
  502:     my @dir_list=&get_dir_list($portfolio_root,$group);
  503:     my $found_file = 0;
  504:     my $locked_file = 0;
  505:     foreach my $line (@dir_list) {
  506:         my ($file_name)=split(/\&/,$line,2);
  507:         if ($file_name eq $fname){
  508:             $file_name = $env{'form.currentpath'}.$file_name;
  509:             $file_name = &prepend_group($file_name,$group);
  510:             $found_file = 1;
  511:             if (defined($group)) {
  512:                 $file_name = $group.'/'.$file_name;
  513:             }
  514:             if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
  515:                 $locked_file = 1;
  516:             } 
  517:         }
  518:     }
  519:     my $current_disk_usage = &Apache::lonnet::diskusage($udom,$uname,$portfolio_root);
  520:     if (($current_disk_usage + $filesize) > $disk_quota){
  521:         $r->print('<font color="red">Unable to upload <strong>'.$fname.' (size = '.$filesize.' kilobytes)</strong>. Disk quota will be exceeded.'.
  522:                   '<br />Disk quota is '.$disk_quota.' kilobytes. Your current disk usage is '.$current_disk_usage.' kilobytes.');
  523:         $r->print(&done('Back',$url,$group));
  524:     } 
  525:     elsif ($found_file){
  526:         if ($locked_file){
  527:             $r->print('<font color="red">Unable to upload <strong>'.$fname.'</strong>, a <strong>locked</strong> file by that name was found in <strong>'.$port_path.$env{'form.currentpath'}.'</strong></font>'.
  528:                   '<br />You will be able to rename or delete existing '.$fname.' after a grade has been assigned.');
  529:             $r->print(&done('Back',$url,$group));      
  530:         } else {   
  531:             $r->print('<font color="red">Unable to upload <strong>'.$fname.'</strong>, a file by that name was found in <strong>'.$port_path.$env{'form.currentpath'}.'</strong></font>'.
  532:                   '<br />To upload, rename or delete existing '.$fname.' in '.$port_path.$env{'form.currentpath'});
  533:             $r->print(&done('Back',$url,$group));
  534:         }
  535:     } else {
  536:         my $result=&Apache::lonnet::userfileupload('uploaddoc','',
  537: 	        	 $port_path.$env{'form.currentpath'});
  538:         if ($result !~ m|^/uploaded/|) {
  539:             $r->print('<font color="red"> An errror occured ('.$result.
  540: 	              ') while trying to upload '.&display_file().'</font><br />');
  541: 	    $r->print(&done('Back',$url,$group));
  542:         } else {
  543:             $r->print(&done(undef,$url,$group));
  544:         }
  545:     }
  546: }
  547: sub lock_info {
  548:     my ($r,$url,$group) = @_;
  549:     my ($uname,$udom) = &get_name_dom($group);
  550:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
  551:                                                                        $uname);
  552:     my $file_name = $env{'form.lockinfo'};
  553:     $file_name = &prepend_group($file_name,$group);
  554:     if (defined($file_name) && defined($$current_permissions{$file_name})) {
  555:         foreach my $array_item (@{$$current_permissions{$file_name}}) {
  556:             if (ref($array_item) eq 'ARRAY') {
  557:                 if ($$array_item[0] eq 'access' ||
  558:                     $$array_item[0] eq 'accesscount' ) {
  559:                     next;
  560:                 }
  561:                 my $filetext;
  562:                 if (defined($group)) {
  563:                     $filetext = '<strong>'.$env{'form.lockinfo'}.
  564:                                     '</strong> (group: '.$group.')'; 
  565:                 } else {
  566:                     $filetext = '<strong>'.$file_name.'</strong>';
  567:                 } 
  568:                 $r->print(&mt('[_1] was submitted in response to problem: ',
  569:                               $filetext).
  570:                           '<strong>'.&Apache::lonnet::gettitle($$array_item[0]).
  571:                           '</strong><br />');
  572:                 my %course_description = &Apache::lonnet::coursedescription($$array_item[1]);
  573:                 $r->print(&mt('In the course: <strong>[_1]</strong><br />',
  574:                               $course_description{'description'}));
  575:                 # $r->print('the third is '.$$array_item[2].'<br>');
  576:                 # $r->print("item is $$array_item[0]<br> and $$array_item[0]");
  577:             }
  578:         }
  579:     }
  580:     $r->print(&done('Back',$url,$group));
  581:     return 'ok';
  582: }
  583: sub createdir {
  584:     my ($r,$url,$group)=@_;
  585:     my $newdir=&Apache::lonnet::clean_filename($env{'form.newdir'});
  586:     if ($newdir eq '') {
  587:     	$r->print('<font color="red">'.
  588: 	    	  &mt("Error: no directory name was provided.").
  589: 		      '</font><br />');
  590: 	    $r->print(&done(undef,$url,$group));
  591: 	    return;
  592:     }
  593:     my $portfolio_root = &get_portfolio_root($group); 
  594:     my @dir_list=&get_dir_list($portfolio_root,$group);
  595:     my $found_file = 0;
  596:     foreach my $line (@dir_list) {
  597:         my ($filename)=split(/\&/,$line,2);
  598:         if ($filename eq $newdir){
  599:             $found_file = 1;
  600:         }
  601:     }
  602:     if ($found_file){
  603:     	    $r->print('<font color="red"> Unable to create a directory named <strong>'.$newdir.
  604:     	            ' </strong>a file or directory by that name already exists.</font><br />');
  605:     } else {
  606:         my ($uname,$udom) = &get_name_dom($group);
  607:         my $port_path = &get_port_path($group);
  608:         my $result=&Apache::lonnet::mkdiruserfile($uname,$udom,
  609: 	         $port_path.$env{'form.currentpath'}.$newdir);
  610:         if ($result ne 'ok') {
  611:     	    $r->print('<font color="red"> An errror occured ('.$result.
  612: 	    	      ') while trying to create a new directory '.&display_file().'</font><br />');
  613:         }
  614:     }
  615:     if ($newdir ne $env{'form.newdir'}) {
  616:         $r->print("The new directory name was changed from:<br /><strong>".$env{'form.newdir'}."</strong> to <strong>$newdir </strong>");  
  617:     }
  618:     $r->print(&done(undef,$url,$group));
  619: }
  620: 
  621: sub get_portfolio_root {
  622:     my ($group) = @_;
  623:     my ($portfolio_root,$udom,$uname,$path);
  624:     ($uname,$udom) = &get_name_dom($group);
  625:     if (defined($group)) {
  626:         $path = '/userfiles/groups/'.$group.'/portfolio';
  627:     } else {
  628:         $path = '/userfiles/portfolio';
  629:     }
  630:     return (&Apache::loncommon::propath($udom,$uname).$path);
  631: }
  632: 
  633: sub get_dir_list {
  634:     my ($portfolio_root,$group) = @_;
  635:     my ($uname,$udom) = &get_name_dom($group);
  636:     return &Apache::lonnet::dirlist($env{'form.currentpath'},
  637:                                           $udom,$uname,$portfolio_root);
  638: }
  639: 
  640: sub get_name_dom {
  641:     my ($group) = @_;
  642:     my ($uname,$udom);
  643:     if (defined($group)) {
  644:         $udom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  645:         $uname = $env{'course.'.$env{'request.course.id'}.'.num'};
  646:     } else {
  647:         $udom = $env{'user.domain'};
  648:         $uname = $env{'user.name'};
  649:     }
  650:     return ($uname,$udom);
  651: }
  652: 
  653: sub prepend_group {
  654:     my ($filename,$group) = @_;
  655:     if (defined($group)) {
  656:         $filename = $group.'/'.$filename;
  657:     }
  658:     return $filename;
  659: }
  660: 
  661: sub get_namespace {
  662:     my ($group) = @_;
  663:     my $namespace = 'portfolio';
  664:     if (defined($group)) {
  665:         my ($uname,$udom) = &get_name_dom($group);
  666:         $namespace .= '_'.$udom.'_'.$uname.'_'.$group;
  667:     }
  668:     return $namespace;
  669: }
  670: 
  671: sub get_port_path {
  672:     my ($group) = @_;
  673:     my $port_path;
  674:     if (defined($group)) {
  675:        $port_path = "groups/$group/portfolio";
  676:     } else {
  677:        $port_path = 'portfolio';
  678:     }
  679:     return $port_path;
  680: }
  681: 
  682: sub handler {
  683:     # this handles file management
  684:     my $r = shift;
  685:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  686:          ['selectfile','currentpath','meta','lockinfo','currentfile',
  687: 	  'action','fieldname','mode','rename','continue','group','access']);
  688:     my ($uname,$udom,$portfolio_root,$url,$group,$caller,$title);
  689:     if ($r->uri =~ m|^(/adm/)([^/]+)|) {
  690:         $url = $1.$2;
  691:         $caller = $2;
  692:     }
  693:     if ($caller eq 'coursegrp_portfolio') {
  694:     #  Needs to be in a course
  695:         if (! ($env{'request.course.fn'})) {
  696:         # Not in a course
  697:             $env{'user.error.msg'}=
  698:      "/adm/coursegrp_portfolio:rgf:0:0:Cannot view group portfolio";
  699:             return HTTP_NOT_ACCEPTABLE;
  700:         }
  701:         my $earlyout = 0;
  702:         my $view_permission = &Apache::lonnet::allowed('vcg',
  703:                                                 $env{'request.course.id'});
  704:         $group = $env{'form.group'};
  705:         $group =~ s/\W//g;
  706:         if ($group) {
  707:             ($uname,$udom) = &get_name_dom($group);
  708:             my %curr_groups = &Apache::longroup::coursegroups($udom,$uname,
  709: 							       $group); 
  710:             if (%curr_groups) {
  711:                 if (($view_permission) || (&Apache::lonnet::allowed('rgf',
  712:                                       $env{'request.course.id'}.'/'.$group))) {
  713:                     $portfolio_root = &get_portfolio_root($group);
  714:                 } else {
  715:                     $r->print('You do not have the privileges required to access the shared files space for this group');
  716:                     $earlyout = 1;
  717:                 }
  718:             } else {
  719:                 $r->print('Not a valid group for this course');
  720:                 $earlyout = 1;
  721:             }
  722:             $title = &mt('Group files').' for '.$group; 
  723:         } else {
  724:             $r->print('Invalid group');
  725:             $earlyout = 1;
  726:         }
  727:         if ($earlyout) { return OK; }
  728:     } else {
  729:         ($uname,$udom) = &get_name_dom();
  730:         $portfolio_root = &get_portfolio_root();
  731:         $title = &mt('Portfolio Manager');
  732:     }
  733: 
  734:     &Apache::loncommon::no_cache($r);
  735:     &Apache::loncommon::content_type($r,'text/html');
  736:     $r->send_http_header;
  737:     # Give the LON-CAPA page header
  738:     if ($env{"form.mode"} eq 'selectfile'){
  739:         $r->print(&Apache::loncommon::start_page($title,undef,
  740: 						 {'only_body' => 1}));
  741:     } else {
  742:         $r->print(&Apache::loncommon::start_page($title));
  743:     }
  744:     $r->rflush();
  745: 	if (($env{'form.storeupl'}) & (!$env{'form.uploaddoc.filename'})){
  746:    	    $r->print('<font color="red"> No file was selected to upload.'.
  747:    	            'To upload a file, click <strong>Browse...</strong>'.
  748:    	            ', select a file, then click <strong>Upload</strong>,</font>');
  749: 	}
  750:     if ($env{'form.meta'}) {
  751:         &open_form($r,$url);
  752: #        $r->print(&edit_meta_data($r, $env{'form.currentpath'}.$env{'form.selectfile'}));
  753:         $r->print('Edit the meta data<br />');
  754:         &close_form($r,$url,$group);
  755:     }
  756:     if ($env{'form.store'}) {
  757:     }
  758: 
  759:     if ($env{'form.uploaddoc.filename'}) {
  760: 	&upload($r,$url,$group);
  761:     } elsif ($env{'form.action'} eq 'delete' && $env{'form.confirmed'}) {
  762: 	&delete_confirmed($r,$url,$group);
  763:     } elsif ($env{'form.action'} eq 'delete') {
  764: 	&delete($r,$url,$group);
  765:     } elsif ($env{'form.action'} eq 'deletedir' && $env{'form.confirmed'}) {
  766: 	&delete_dir_confirmed($r,$url,$group);
  767:     } elsif ($env{'form.action'} eq 'deletedir'){
  768: 	&delete_dir($r,$url,$group);
  769:     } elsif ($env{'form.action'} eq 'rename' && $env{'form.confirmed'}) {
  770: 	&rename_confirmed($r,$url,$group);
  771:     } elsif ($env{'form.rename'}) {
  772:         $env{'form.selectfile'} = $env{'form.rename'};
  773:         $env{'form.action'} = 'rename';
  774: 	&rename($r,$url,$group);
  775:     } elsif ($env{'form.createdir'}) {
  776: 	&createdir($r,$url,$group);
  777:     } elsif ($env{'form.lockinfo'}) {
  778:         &lock_info($r,$url,$group);
  779:     } else {
  780: 	my $current_path='/';
  781: 	if ($env{'form.currentpath'}) {
  782: 	    $current_path = $env{'form.currentpath'};
  783: 	}
  784:         my @dir_list=&get_dir_list($portfolio_root,$group);
  785: 	if ($dir_list[0] eq 'no_such_dir'){
  786: 	    # two main reasons for this:
  787:             #    1) never been here, so directory structure not created
  788: 	    #    2) back-button navigation after deleting a directory
  789: 	    if ($current_path eq '/'){
  790: 	        &Apache::lonnet::mkdiruserfile($uname,$udom,
  791: 					       &get_port_path($group));
  792: 	    } else {
  793:                 # some directory that snuck in get rid of the directory
  794:                 # from the recent pulldown, just in case
  795: 		&Apache::lonhtmlcommon::remove_recent('portfolio',
  796: 						      [$current_path]);
  797: 		$current_path = '/'; # force it back to the root        
  798: 	    }
  799: 	    # now grab the directory list again, for the first time
  800: 	    @dir_list=&Apache::lonnet::dirlist($current_path,
  801: 					    $udom,$uname,$portfolio_root);
  802:         }
  803: 	# need to know if directory is empty so it can be removed if desired
  804: 	my $is_empty=(@dir_list == 2);
  805: 	&display_common($r,$url,$current_path,$is_empty,\@dir_list,$group);
  806:         &display_directory($r,$url,$current_path,$is_empty,\@dir_list,$group);
  807: 	$r->print(&Apache::loncommon::end_page());
  808:     }
  809:     return OK;
  810: }
  811: 1;
  812: __END__

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