File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.95: download - view: text, annotated - select for diffs
Wed Mar 15 19:41:26 2006 UTC (18 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- converting some interface pages to stop generating </body> <head> </head> </html> and use the helper functions instead

    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: 
   34: # receives a file name and path stub from username/userfiles/portfolio/
   35: # returns an anchor tag consisting encoding filename and currentpath
   36: sub make_anchor {
   37:     my ($url, $filename, $current_path, $current_mode, $field_name,
   38:         $continue_select,$group) = @_;
   39:     if ($continue_select ne 'true') {$continue_select = 'false'};
   40:     my $anchor = '<a href="'.$url.'?selectfile='.$filename.'&currentpath='.$current_path.'&mode='.$current_mode.'&continue='.$continue_select.'&fieldname='.$field_name;
   41:     if (defined($group)) {
   42:         $anchor .= '&group='.$group;
   43:     }
   44:     $anchor .= '">'.$filename.'</a>';
   45:     return $anchor;
   46: }
   47: my $dirptr=16384;
   48: sub display_common {
   49:     my ($r,$url,$current_path,$is_empty,$dir_list,$group)=@_;
   50:     my $groupitem;
   51:     my $namespace = &get_namespace($group);
   52:     my $port_path = &get_port_path($group);
   53:     if (defined($group)) {
   54:         $groupitem = '<input type="hidden" name="group" value="'.$group.'" />';
   55:     } 
   56:     my $iconpath= $r->dir_config('lonIconsURL') . "/";
   57:     my %text=&Apache::lonlocal::texthash('upload' => 'Upload',
   58: 					 'upload_label' =>  
   59: 					 'Upload file to current directory:',
   60: 					 'createdir' => 'Create Subdirectory',
   61: 					 'createdir_label' => 
   62: 					 'Create subdirectory in current directory:');
   63:     $r->print(<<"TABLE"); 
   64: <table border="0" cellspacing="2" cellpadding="2">
   65:   <form method="post" enctype="multipart/form-data">
   66:     <tr valign="middle">
   67:       <td bgcolor="#ccddaa" align="right">
   68:         $text{'upload_label'}
   69:       </td>
   70:       <td bgcolor="#ccddaa" align="left">$groupitem
   71:         <input name="uploaddoc" type="file" />
   72: 	<input type="hidden" name="currentpath" value="$current_path" />
   73: 	<input type="hidden" name="action" value="$env{"form.action"}" />
   74: 	<input type="hidden" name="fieldname" value="$env{"form.fieldname"}" />
   75: 	<input type="hidden" name="mode" value="$env{"form.mode"}" />
   76: 	<input type="submit" name="storeupl" value="$text{'upload'}" />
   77:       </td>
   78:     </tr>
   79:   </form>
   80:   <form method="post">
   81:     <tr>
   82:       <td bgcolor="#ccddaa" align="right">
   83:         $text{'createdir_label'}
   84:       </td>
   85:       <td bgcolor="#ccddaa" align="left">
   86:         <input name="newdir" type="input" />$groupitem
   87:         <input type="hidden" name="currentpath" value="$current_path" />
   88:         <input type="hidden" name="action" value="$env{"form.action"}" />
   89:         <input type="hidden" name="fieldname" value="$env{"form.fieldname"}" />
   90:         <input type="hidden" name="mode" value="$env{"form.mode"}" />
   91:         <input type="submit" name="createdir" value="$text{'createdir'}" />
   92:       </td>
   93:     </tr>
   94:   </form>
   95: </table>
   96: TABLE
   97:     my @tree = split (/\//,$current_path);
   98:     $r->print('<font size="+2">'.&make_anchor($url,$port_path,'/',$env{"form.mode"},$env{"form.fieldname"},$env{"form.continue"},$group).'/');
   99:     if (@tree > 1){
  100:         my $newCurrentPath = '';
  101:         for (my $i = 1; $i< @tree; $i++){
  102:             $newCurrentPath .= $tree[$i].'/';
  103:             $r->print(&make_anchor($url,$tree[$i],'/'.$newCurrentPath, $env{"form.mode"},$env{"form.fieldname"}, $env{"form.continue"},$group).'/');
  104:         }
  105:     }
  106:     $r->print('</font>');
  107:     &Apache::lonhtmlcommon::store_recent($namespace,$current_path,$current_path);
  108:     $r->print('<br /><form method=post action="'.$url.'?mode='.$env{"form.mode"}.'&fieldname='.$env{"form.fieldname"});
  109:     if (defined($group)) {
  110:         $r->print('&group='.$group);
  111:     }
  112:     $r->print('">'.
  113: 	      &Apache::lonhtmlcommon::select_recent($namespace,'currentpath',
  114: 						    'this.form.submit();'));
  115:     $r->print("</form>");
  116: }
  117: sub display_directory {
  118:     my ($r,$url,$current_path,$is_empty,$dir_list,$group)=@_;
  119:     my $iconpath= $r->dir_config('lonIconsURL') . "/";
  120:     my ($groupitem,$groupecho);
  121:     my $display_out;
  122:     my $select_mode;
  123:     my $checked_files;
  124:     my $port_path = &get_port_path($group);
  125:     my ($uname,$udom) = &get_name_dom($group);
  126:     my $namespace = &get_namespace($group); 
  127:     if (defined($group)) {
  128:        $groupitem = '<input type="hidden" name="group" value="'.$group.'" />'; 
  129:        $groupecho = '&amp;group='.$group;
  130:     }
  131:     my %locked_files = &Apache::lonnet::get_marked_as_readonly_hash ($namespace,$udom,$uname);
  132:     if ($env{"form.mode"} eq 'selectfile'){
  133: 	&select_files($r);
  134: 	$checked_files =&Apache::lonnet::files_in_path($uname,$env{'form.currentpath'});
  135: 	$select_mode = 'true';
  136:     } 
  137:     if ($is_empty && ($current_path ne '/')) {
  138:         $display_out = '<form method="post" action="'.$url.'">'.$groupitem.
  139:         '<input type="hidden" name="action" value="deletedir" />'.
  140:         '<input type="submit" name="deletedir" value="'.&mt("Delete Directory").'" />'.
  141:         '<input type="hidden" name="selectfile" value="" />'.
  142:         '<input type="hidden" name="currentpath" value="'.$current_path.'" />'.
  143:         '</form>';
  144:         
  145:         $r->print($display_out);
  146: 	return;
  147:     }
  148:     if ($select_mode eq 'true') {
  149:         $r->print('<table border="0" cellspacing="2" cellpadding="2">'.
  150:             '<tr><th>Select</th><th>&nbsp;</th><th>Name</th><th>Size</th><th>Last Modified</th></tr>');
  151:         $r->print('<form method="post" name="checkselect" action="'.$url.'">');
  152:     } else {
  153:         $r->print('<table border="0" cellspacing="2" cellpadding="2">'.
  154:             '<tr><th colspan="2">Actions</th><th>&nbsp;</th><th>Name</th><th>Size</th><th>Last Modified</th></tr>');
  155:         $r->print('<form method="post" action="'.$url.'">');
  156:     }
  157:     if (defined($group)) {
  158:         $r->print("\n".$groupitem."\n");
  159:     }
  160:     my $href_location="/uploaded/$udom/$uname/$port_path".$current_path;
  161:     my $href_edit_location="/editupload/$udom/$uname/$port_path".$current_path;
  162:     foreach my $line (sort 
  163: 		      { 
  164: 			  my ($afile)=split('&',$a,2);
  165: 			  my ($bfile)=split('&',$b,2);
  166: 			  return (lc($afile) cmp lc($bfile));
  167: 		      } (@$dir_list)) {
  168:     	#$strip holds directory/file name
  169:     	#$dom 
  170:     	my ($filename,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef)=split(/\&/,$line,16); 
  171:     	$filename =~ s/\s+$//;
  172:     	if (($filename ne '.') && ($filename ne '..') && ($filename !~ /\.meta$/ ) && ($filename !~ /(.*)\.(\d+)\.([^\.]*)$/)) {
  173:             if ($dirptr&$testdir) {
  174:                 if ($select_mode eq 'true'){
  175:                     $r->print('<tr bgcolor="#FFAA99"><td><img src="'.$iconpath.'folder_closed.gif"></td>');
  176:                 } else {
  177:                     $r->print('<tr bgcolor="#FFAA99"><td colspan="2"><img src="'.$iconpath.'folder_closed.gif"></td>');
  178:                 }
  179:                 $r->print('<td>Go to ...</td>');
  180:                 $r->print('<td>'.&make_anchor($url,$filename.'/',$current_path.$filename.'/',$env{'form.mode'},$env{"form.fieldname"},$env{'form.continue'},$group).'</td>'); 
  181:                 $r->print('</tr>'); 
  182:             } else {
  183:                 $r->print('<tr bgcolor="#CCCCFF">');
  184:                 if ($select_mode eq 'true'){
  185:                     $r->print('<td><input type="checkbox" name="checkfile" value="'.$filename.'"'); 
  186:                     if ($$checked_files{$filename} eq 'selected') {
  187:                         $r->print("CHECKED");
  188:                     }
  189:                     $r->print('></td>');
  190:                 } else {
  191:                     if (exists $locked_files{$current_path.$filename}){
  192:                         $r->print('<td colspan="2"><a href="'.$url.'?lockinfo='.$current_path.$filename.$groupecho.'">Locked</a></td>');
  193:                     } else {
  194: 			my $cat='<img alt="'.&mt('Catalog Information').
  195: 			    '" src="'.&Apache::loncommon::lonhttpdurl('/res/adm/pages/catalog.gif').'" />';
  196:                         $r->print('<td><input type="checkbox" name="selectfile" value="'.$filename.'" />
  197:                             <a href="'.$url.'?rename='.$filename.'&amp;currentpath='.$current_path.$groupecho.'">Rename</a></td>
  198:                             <td><a href="'.$href_edit_location.$filename.'.meta">'.$cat.'</a>
  199:                             </td>');
  200:                     }
  201:                 }
  202:                 $r->print('<td><img src="'.&Apache::loncommon::icon($filename).'"></td>');
  203:                 $r->print('<td><a href="'.$href_location.$filename.'">'.
  204: 			    $filename.'</a></td>'); 
  205:                 $r->print('<td>'.$size.'</td>');
  206:                 $r->print('<td>'.&Apache::lonlocal::locallocaltime($mtime).'</td>');
  207:                 $r->print('</tr>'); 
  208:             }
  209:         }
  210:     }
  211:     if ($select_mode eq 'true') {
  212:         $r->print('</table>
  213:             <input type="hidden" name="continue" value="true">
  214:             <input type="hidden" name="fieldname" value="'.$env{'form.fieldname'}.'">
  215:             <input type="hidden" name="mode" value="selectfile">
  216:             <input type="submit" name="submit" value="Select checked files, and continue selecting." /><br />
  217:             <input type="button" name="doit" onClick= "finishSelect();" value="Select checked files, and close window" />
  218:             <input type="hidden" name="currentpath" value="'.$current_path.'" />
  219:         </form>');        
  220:     } else {
  221:         $r->print('</table>
  222:         <input type="submit" name="doit" value="Delete Checked Files" />
  223:         <input type="hidden" name="action" value="delete" />
  224:         <input type="hidden" name="currentpath" value="'.$current_path.'" />
  225:         </form>');
  226:     }
  227: }
  228: 
  229: sub open_form {
  230:     my ($r,$url)=@_;
  231:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
  232:     $r->print('<form method="post" action="'.$url.'">');
  233:     $r->print('<input type="hidden" name="action" value="'.
  234: 	      $env{'form.action'}.'" />');
  235:     $r->print('<input type="hidden" name="confirmed" value="1" />');
  236:     foreach (@files) {
  237:         $r->print('<input type="hidden" name="selectfile" value="'.
  238: 	      $_.'" />');
  239:     }
  240:     $r->print('<input type="hidden" name="currentpath" value="'.
  241: 	      $env{'form.currentpath'}.'" />');
  242: }
  243: 
  244: sub close_form {
  245:     my ($r,$url,$group)=@_;
  246:     $r->print('<p><input type="submit" value="'.&mt('Continue').'" />');
  247:     if (defined($group)) {
  248:        $r->print("\n".'<input type="hidden" name="group" value="'.
  249:               $group.'" />');
  250:     }
  251:     $r->print('</p></form>');
  252:     $r->print('<form action="'.$url.'" method="POST">
  253:                <p>
  254:               <input type="hidden" name="currentpath" value="'.
  255: 	      $env{'form.currentpath'}.'" />');
  256:     if (defined($group)) {
  257:        $r->print("\n".'<input type="hidden" name="group" value="'.
  258:               $group.'" />');
  259:     }
  260:     $r->print("\n".'   <input type="submit" value="'.&mt('Cancel').'" />
  261:                </p></form>'); 
  262: }
  263: 
  264: sub display_file {
  265:     my ($path,$filename)=@_;
  266:     my $display_file_text;
  267:     if (!defined($path)) { $path=$env{'form.currentpath'}; }
  268:     if (!defined($filename)) { 
  269:         $filename=$env{'form.selectfile'};
  270:         $display_file_text = '<tt>'.$path.$filename.'</tt>';
  271:     } elsif (ref($filename) eq "ARRAY") {
  272:         foreach (@$filename) {
  273:             $display_file_text .= '<tt>'.$path.$_.'</tt><br />';
  274:         }
  275:     } elsif (ref($filename) eq "SCALAR") {
  276:         $display_file_text = '<tt>'.$path.$filename.'</tt>';        
  277:     }
  278:     return $display_file_text;
  279: }
  280: 
  281: sub done {
  282:     my ($message,$url,$group)=@_;
  283:     unless (defined $message) {
  284:         $message='Done';
  285:     }
  286:     my $result = '<h3><a href="'.$url.'?currentpath='.
  287: 	         $env{'form.currentpath'}.
  288: 	         '&fieldname='.$env{'form.fieldname'}.
  289: 	         '&mode='.$env{'form.mode'};
  290:     if (defined($group)) {
  291:         $result .= '&group='.$group;
  292:     }
  293:     $result .= '">'.&mt($message).'</a></h3>';
  294:     return $result;
  295: }
  296: 
  297: sub delete {
  298:     my ($r,$url,$group)=@_;
  299:     my @check;
  300:     my $file_name = $env{'form.currentpath'}.$env{'form.selectfile'};
  301:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
  302:     my ($uname,$udom) = &get_name_dom($group);
  303:     if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
  304:         $r->print ("The file is locked and cannot be deleted.<br />");
  305:         $r->print(&done('Back',$url,$group));
  306:     } else {
  307:         if (scalar(@files)) {
  308:             &open_form($r,$url);
  309:             $r->print('<p>'.&mt('Delete').' '.&display_file(undef,\@files).'?</p>');
  310:             &close_form($r,$url,$group);
  311:         } else {
  312:             $r->print("No file was checked to delete.<br />");
  313:             $r->print(&done(undef,$url,$group));
  314:         }
  315:     }
  316: } 
  317: 
  318: sub delete_confirmed {
  319:     my ($r,$url,$group)=@_;
  320:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
  321:     my $result;
  322:     my ($uname,$udom) = &get_name_dom($group);
  323:     my $port_path = &get_port_path($group);
  324:     foreach my $delete_file (@files) {
  325:         $result=&Apache::lonnet::removeuserfile($uname,$udom,$port_path.
  326: 					       $env{'form.currentpath'}.
  327: 					       $delete_file);
  328:         if ($result ne 'ok') {
  329: 	$r->print('<font color="red"> An error occured ('.$result.
  330: 		  ') while trying to delete '.&display_file(undef, $delete_file).'</font><br />');
  331:         }
  332:     }
  333:     $r->print(&done(undef,$url,$group));
  334: }
  335: 
  336: sub delete_dir {
  337:     my ($r,$url,$group)=@_;
  338:     &open_form($r,$url);
  339:     $r->print('<p>'.&mt('Delete').' '.&display_file().'?</p>');
  340:     &close_form($r,$url,$group);
  341: } 
  342: 
  343: sub delete_dir_confirmed {
  344:     my ($r,$url,$group)=@_;
  345:     my $directory_name = $env{'form.currentpath'};
  346:     $directory_name =~ s|/$||; # remove any trailing slash
  347:     my ($uname,$udom) = &get_name_dom($group);
  348:     my $namespace = &get_namespace($group);
  349:     my $port_path = &get_port_path($group);
  350:     my $result=&Apache::lonnet::removeuserfile($uname,$udom,$port_path.
  351: 					       $directory_name);
  352: 					       
  353:     if ($result ne 'ok') {
  354: 	$r->print('<font color="red"> An error occured (dir) ('.$result.
  355: 		  ') while trying to delete '.$directory_name.'</font><br />');
  356:     } else {
  357:         # now remove from recent
  358: #        $r->print('<br /> removing '.$directory_name.'<br /');
  359:         &Apache::lonhtmlcommon::remove_recent($namespace,[$directory_name.'/']);
  360:         my @dirs = split m!/!, $directory_name;
  361:         
  362: #        $directory_name =~ m/^(\/*\/)(\/*.)$/;
  363:         $directory_name='/';
  364:         for (my $i=1; $i < (@dirs - 1); $i ++){
  365:             $directory_name .= $dirs[$i].'/';
  366:         }
  367:         $env{'form.currentpath'} = $directory_name;
  368:     }
  369:     $r->print(&done(undef,$url,$group));
  370: }
  371: 
  372: sub rename {
  373:     my ($r,$url,$group)=@_;
  374:     my $file_name = $env{'form.currentpath'}.$env{'form.rename'};
  375:     my ($uname,$udom) = &get_name_dom($group);
  376:     if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
  377:         $r->print ("The file is locked and cannot be renamed.<br />");
  378:         $r->print(&done(undef,$url,$group));
  379:     } else {
  380:         &open_form($r,$url);
  381:         $r->print('<p>'.&mt('Rename').' '.&display_file().' to 
  382:                    <input name="filenewname" type="input" size="50" />?</p>');
  383:         &close_form($r,$url,$group);
  384:     }
  385: }
  386: 
  387: sub rename_confirmed {
  388:     my ($r,$url,$group)=@_;
  389:     my $filenewname=&Apache::lonnet::clean_filename($env{'form.filenewname'});
  390:     my ($uname,$udom) = &get_name_dom($group);
  391:     my $port_path = &get_port_path($group);
  392:     if ($filenewname eq '') {
  393: 	$r->print('<font color="red">'.
  394: 		  &mt("Error: no valid filename was provided to rename to.").
  395: 		  '</font><br />');
  396: 	$r->print(&done(undef,$url,$group));
  397: 	return;
  398:     } 
  399:     my $result=
  400: 	&Apache::lonnet::renameuserfile($uname,$udom,
  401:             $port_path.$env{'form.currentpath'}.$env{'form.selectfile'},
  402:             $port_path.$env{'form.currentpath'}.$filenewname);
  403:     if ($result ne 'ok') {
  404: 	$r->print('<font color="red"> An errror occured ('.$result.
  405: 		  ') while trying to rename '.&display_file().' to '.
  406: 		  &display_file(undef,$filenewname).'</font><br />');
  407:     }
  408:     if ($filenewname ne $env{'form.filenewname'}) {
  409:         $r->print("The new file name was changed from:<br /><strong>".$env{'form.filenewname'}."</strong> to <strong>$filenewname </strong>");
  410:     }
  411:     $r->print(&done(undef,$url,$group));
  412: }
  413: sub select_files {
  414:     my ($r,$group)=@_;
  415:     if ($env{'form.continue'} eq 'true') {
  416:         # here we update the selections for the currentpath
  417:         # eventually, have to handle removing those not checked, but . . . 
  418:         my @items=&Apache::loncommon::get_env_multiple('form.checkfile');
  419:         if (scalar(@items)){
  420:              &Apache::lonnet::save_selected_files($env{'user.name'}, $env{'form.currentpath'}, @items);
  421:         }
  422:     } else {
  423:             #empty the file for a fresh start
  424:             &Apache::lonnet::clear_selected_files($env{'user.name'});
  425:     }
  426:     my @files = &Apache::lonnet::files_not_in_path($env{'user.name'}, $env{'form.currentpath'});
  427:     my $java_files = join ",", @files;
  428:     if ($java_files) {
  429:         $java_files.=',';
  430:     }
  431:     my $javascript =(<<ENDSMP);
  432:         <script language='javascript'>
  433:         function finishSelect() {
  434: ENDSMP
  435:     $javascript .= 'fileList = "'.$java_files.'";';
  436:     $javascript .= (<<ENDSMP);
  437:             for (i=0;i<document.forms.checkselect.length;i++) { 
  438:                 if (document.forms.checkselect[i].checked){
  439:                     fileList = fileList + document.forms.checkselect.currentpath.value + document.forms.checkselect[i].value + "," ;
  440:                 }
  441:             }
  442:             opener.document.forms.lonhomework.
  443: ENDSMP
  444:     $javascript .= $env{'form.fieldname'};
  445:     $javascript .= (<<ENDSMP);
  446:         .value=fileList;
  447:             self.close();
  448:         }
  449:         </script>
  450: ENDSMP
  451:     $r->print($javascript);
  452:     $r->print("<h1>Select portfolio files</h1>
  453:                 Check as many as you wish in response to the problem.<br />");
  454:     my @otherfiles=&Apache::lonnet::files_not_in_path($env{'user.name'}, $env{'form.currentpath'});
  455:     if (@otherfiles) {
  456: 	$r->print("<strong>Files selected from other directories:</strong><br />");
  457: 	foreach my $file (@otherfiles) {
  458: 	    $r->print($file."<br />");
  459: 	}
  460:     }
  461: }
  462: sub upload {
  463:     my ($r,$url,$group)=@_;
  464:     my $fname=$env{'form.uploaddoc.filename'};
  465:     my $filesize = (length($env{'form.uploaddoc'})) / 1000; #express in k (1024?)
  466:     my $disk_quota = 20000; # expressed in k
  467:     $fname=&Apache::lonnet::clean_filename($fname);
  468: 
  469:     my $portfolio_root=&get_portfolio_root($group);
  470:     my ($uname,$udom) = &get_name_dom($group);
  471:     my $port_path = &get_port_path($group);
  472:     # Fixme --- Move the checking for existing file to LOND error return
  473:     my @dir_list=&get_dir_list($portfolio_root,$group);
  474:     my $found_file = 0;
  475:     my $locked_file = 0;
  476:     foreach my $line (@dir_list) {
  477:         my ($file_name)=split(/\&/,$line,2);
  478:         if ($file_name eq $fname){
  479:             $found_file = 1;
  480:             if (&Apache::lonnet::is_locked($env{'form.currentpath'}.$file_name,$udom,$uname) eq 'true') {
  481:                 $locked_file = 1;
  482:             } 
  483:         }
  484:     }
  485:     my $current_disk_usage = &Apache::lonnet::diskusage($udom,$uname,$portfolio_root);
  486:     if (($current_disk_usage + $filesize) > $disk_quota){
  487:         $r->print('<font color="red">Unable to upload <strong>'.$fname.' (size = '.$filesize.' kilobytes)</strong>. Disk quota will be exceeded.'.
  488:                   '<br />Disk quota is '.$disk_quota.' kilobytes. Your current disk usage is '.$current_disk_usage.' kilobytes.');
  489:         $r->print(&done('Back',$url,$group));
  490:     } 
  491:     elsif ($found_file){
  492:         if ($locked_file){
  493:             $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>'.
  494:                   '<br />You will be able to rename or delete existing '.$fname.' after a grade has been assigned.');
  495:             $r->print(&done('Back',$url,$group));      
  496:         } else {   
  497:             $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>'.
  498:                   '<br />To upload, rename or delete existing '.$fname.' in '.$port_path.$env{'form.currentpath'});
  499:             $r->print(&done('Back',$url,$group));
  500:         }
  501:     } else {
  502:         my $result=&Apache::lonnet::userfileupload('uploaddoc','',
  503: 	        	 $port_path.$env{'form.currentpath'});
  504:         print STDERR "result was $result for $port_path.$env{'form.currentpath'}\n";
  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:             my %curr_groups = ();
  653:             ($uname,$udom) = &get_name_dom($group);
  654:             if (&Apache::loncommon::coursegroups(\%curr_groups,$udom,$uname,
  655:                                                        $group)) {
  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:     my $html=&Apache::lonxml::xmlbegin();
  684:     $r->print($html.&Apache::loncommon::head($title)."\n");
  685:     if ($env{"form.mode"} eq 'selectfile'){
  686:         $r->print(&Apache::loncommon::bodytag($title,undef,undef,1));
  687:     } else {
  688:         $r->print(&Apache::loncommon::bodytag($title));
  689:     }
  690:     $r->rflush();
  691: 	if (($env{'form.storeupl'}) & (!$env{'form.uploaddoc.filename'})){
  692:    	    $r->print('<font color="red"> No file was selected to upload.'.
  693:    	            'To upload a file, click <strong>Browse...</strong>'.
  694:    	            ', select a file, then click <strong>Upload</strong>,</font>');
  695: 	}
  696:     if ($env{'form.meta'}) {
  697:         &open_form($r,$url);
  698: #        $r->print(&edit_meta_data($r, $env{'form.currentpath'}.$env{'form.selectfile'}));
  699:         $r->print('Edit the meta data<br />');
  700:         &close_form($r,$url,$group);
  701:     }
  702:     if ($env{'form.store'}) {
  703:     }
  704: 
  705:     if ($env{'form.uploaddoc.filename'}) {
  706: 	&upload($r,$url,$group);
  707:     } elsif ($env{'form.action'} eq 'delete' && $env{'form.confirmed'}) {
  708: 	&delete_confirmed($r,$url,$group);
  709:     } elsif ($env{'form.action'} eq 'delete') {
  710: 	&delete($r,$url,$group);
  711:     } elsif ($env{'form.action'} eq 'deletedir' && $env{'form.confirmed'}) {
  712: 	&delete_dir_confirmed($r,$url,$group);
  713:     } elsif ($env{'form.action'} eq 'deletedir'){
  714: 	&delete_dir($r,$url,$group);
  715:     } elsif ($env{'form.action'} eq 'rename' && $env{'form.confirmed'}) {
  716: 	&rename_confirmed($r,$url,$group);
  717:     } elsif ($env{'form.rename'}) {
  718:         $env{'form.selectfile'} = $env{'form.rename'};
  719:         $env{'form.action'} = 'rename';
  720: 	&rename($r,$url,$group);
  721:     } elsif ($env{'form.createdir'}) {
  722: 	&createdir($r,$url,$group);
  723:     } elsif ($env{'form.lockinfo'}) {
  724:         &lock_info($r,$url,$group);
  725:     } else {
  726: 	my $current_path='/';
  727: 	if ($env{'form.currentpath'}) {
  728: 	    $current_path = $env{'form.currentpath'};
  729: 	}
  730:         my @dir_list=&get_dir_list($portfolio_root,$group);
  731: 	if ($dir_list[0] eq 'no_such_dir'){
  732: 	    # two main reasons for this:
  733:             #    1) never been here, so directory structure not created
  734: 	    #    2) back-button navigation after deleting a directory
  735: 	    if ($current_path eq '/'){
  736: 	        &Apache::lonnet::mkdiruserfile($uname,$udom,'portfolio');
  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>