File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.100: download - view: text, annotated - select for diffs
Thu May 18 20:29:00 2006 UTC (18 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- create the droups dir if it doesn't exist

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

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