File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.101: download - view: text, annotated - select for diffs
Tue May 30 12:46:09 2006 UTC (18 years ago) by www
Branches: MAIN
CVS tags: HEAD
&Apache::lonnet::unescape -> &unescape
&Apache::lonnet::escape -> &escape

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

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