File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.108: download - view: text, annotated - select for diffs
Thu Jun 22 13:27:13 2006 UTC (17 years, 11 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
Additional portfolio access control can now be defined: passphrase-protected, or conditional access (requires log-in) based on username/domain, domain, course or group affiliation (possible restriction by role, status, sectio and group).

    1: # Copyright Michigan State University Board of Trustees
    2: #
    3: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    4: #
    5: # LON-CAPA is free software; you can redistribute it and/or modify
    6: # it under the terms of the GNU General Public License as published by
    7: # the Free Software Foundation; either version 2 of the License, or 
    8: # (at your option) any later version.
    9: #
   10: # LON-CAPA is distributed in the hope that it will be useful,
   11: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   12: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13: # GNU General Public License for more details.
   14: #
   15: # You should have received a copy of the GNU General Public License
   16: # along with LON-CAPA; if not, write to the Free Software
   17: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   18: #
   19: # /home/httpd/html/adm/gpl.txt
   20: #
   21: # http://www.lon-capa.org/
   22: #
   23: 
   24: package Apache::portfolio;
   25: use strict;
   26: use Apache::Constants qw(:common :http);
   27: use Apache::loncommon;
   28: use Apache::lonnet;
   29: use Apache::lontexconvert;
   30: use Apache::lonfeedback;
   31: use Apache::lonlocal;
   32: use Apache::lonnet;
   33: use Apache::longroup;
   34: use lib '/home/httpd/lib/perl';
   35: use LONCAPA;
   36: 
   37: # receives a file name and path stub from username/userfiles/portfolio/
   38: # returns an anchor tag consisting encoding filename and currentpath
   39: sub make_anchor {
   40:     my ($url, $filename, $current_path, $current_mode, $field_name,
   41:         $continue_select,$group) = @_;
   42:     if ($continue_select ne 'true') {$continue_select = 'false'};
   43:     my $anchor = '<a href="'.$url.'?selectfile='.$filename.'&currentpath='.$current_path.'&mode='.$current_mode.'&continue='.$continue_select.'&fieldname='.$field_name;
   44:     if (defined($group)) {
   45:         $anchor .= '&group='.$group;
   46:     }
   47:     $anchor .= '">'.$filename.'</a>';
   48:     return $anchor;
   49: }
   50: my $dirptr=16384;
   51: sub display_common {
   52:     my ($r,$url,$current_path,$is_empty,$dir_list,$group)=@_;
   53:     my $groupitem;
   54:     my $namespace = &get_namespace($group);
   55:     my $port_path = &get_port_path($group);
   56:     if (defined($group)) {
   57:         $groupitem = '<input type="hidden" name="group" value="'.$group.'" />';
   58:     } 
   59:     my $iconpath= $r->dir_config('lonIconsURL') . "/";
   60:     my %text=&Apache::lonlocal::texthash('upload' => 'Upload',
   61: 					 'upload_label' =>  
   62: 					 'Upload file to current directory:',
   63: 					 'createdir' => 'Create Subdirectory',
   64: 					 'createdir_label' => 
   65: 					 'Create subdirectory in current directory:');
   66:     $r->print(<<"TABLE"); 
   67: <table border="0" cellspacing="2" cellpadding="2">
   68:   <form method="post" enctype="multipart/form-data">
   69:     <tr valign="middle">
   70:       <td bgcolor="#ccddaa" align="right">
   71:         $text{'upload_label'}
   72:       </td>
   73:       <td bgcolor="#ccddaa" align="left">$groupitem
   74:         <input name="uploaddoc" type="file" />
   75: 	<input type="hidden" name="currentpath" value="$current_path" />
   76: 	<input type="hidden" name="action" value="$env{"form.action"}" />
   77: 	<input type="hidden" name="fieldname" value="$env{"form.fieldname"}" />
   78: 	<input type="hidden" name="mode" value="$env{"form.mode"}" />
   79: 	<input type="submit" name="storeupl" value="$text{'upload'}" />
   80:       </td>
   81:     </tr>
   82:   </form>
   83:   <form method="post">
   84:     <tr>
   85:       <td bgcolor="#ccddaa" align="right">
   86:         $text{'createdir_label'}
   87:       </td>
   88:       <td bgcolor="#ccddaa" align="left">
   89:         <input name="newdir" type="input" />$groupitem
   90:         <input type="hidden" name="currentpath" value="$current_path" />
   91:         <input type="hidden" name="action" value="$env{"form.action"}" />
   92:         <input type="hidden" name="fieldname" value="$env{"form.fieldname"}" />
   93:         <input type="hidden" name="mode" value="$env{"form.mode"}" />
   94:         <input type="submit" name="createdir" value="$text{'createdir'}" />
   95:       </td>
   96:     </tr>
   97:   </form>
   98: </table>
   99: TABLE
  100:     my @tree = split (/\//,$current_path);
  101:     $r->print('<font size="+2">'.&make_anchor($url,$port_path,'/',$env{"form.mode"},$env{"form.fieldname"},$env{"form.continue"},$group).'/');
  102:     if (@tree > 1){
  103:         my $newCurrentPath = '';
  104:         for (my $i = 1; $i< @tree; $i++){
  105:             $newCurrentPath .= $tree[$i].'/';
  106:             $r->print(&make_anchor($url,$tree[$i],'/'.$newCurrentPath, $env{"form.mode"},$env{"form.fieldname"}, $env{"form.continue"},$group).'/');
  107:         }
  108:     }
  109:     $r->print('</font>');
  110:     &Apache::lonhtmlcommon::store_recent($namespace,$current_path,$current_path);
  111:     $r->print('<br /><form method=post action="'.$url.'?mode='.$env{"form.mode"}.'&fieldname='.$env{"form.fieldname"});
  112:     if (defined($group)) {
  113:         $r->print('&group='.$group);
  114:     }
  115:     $r->print('">'.
  116: 	      &Apache::lonhtmlcommon::select_recent($namespace,'currentpath',
  117: 						    'this.form.submit();'));
  118:     $r->print("</form>");
  119: }
  120: sub display_directory {
  121:     my ($r,$url,$current_path,$is_empty,$dir_list,$group)=@_;
  122:     my $iconpath= $r->dir_config('lonIconsURL') . "/";
  123:     my ($groupitem,$groupecho);
  124:     my $display_out;
  125:     my $select_mode;
  126:     my $checked_files;
  127:     my $port_path = &get_port_path($group);
  128:     my ($uname,$udom) = &get_name_dom($group);
  129:     if (defined($group)) {
  130:        $groupitem = '<input type="hidden" name="group" value="'.$group.'" />'; 
  131:        $groupecho = '&amp;group='.$group;
  132:     }
  133:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
  134:                                                                         $uname);
  135:     my %locked_files = &Apache::lonnet::get_marked_as_readonly_hash(
  136:                                                   $current_permissions,$group);
  137:     my %access_controls = &Apache::lonnet::get_access_controls($current_permissions,$group);
  138:     my $now = time;
  139:     if ($env{"form.mode"} eq 'selectfile'){
  140: 	&select_files($r);
  141: 	$checked_files =&Apache::lonnet::files_in_path($uname,$env{'form.currentpath'});
  142: 	$select_mode = 'true';
  143:     } 
  144:     if ($is_empty && ($current_path ne '/')) {
  145:         $display_out = '<form method="post" action="'.$url.'">'.$groupitem.
  146:         '<input type="hidden" name="action" value="deletedir" />'.
  147:         '<input type="submit" name="deletedir" value="'.&mt("Delete Directory").'" />'.
  148:         '<input type="hidden" name="selectfile" value="" />'.
  149:         '<input type="hidden" name="currentpath" value="'.$current_path.'" />'.
  150:         '</form>';
  151:         
  152:         $r->print($display_out);
  153: 	return;
  154:     }
  155:     if ($select_mode eq 'true') {
  156:         $r->print('<table border="0" cellspacing="2" cellpadding="2">'.
  157:             '<tr><th>Select</th><th>&nbsp;</th><th>Name</th><th>Size</th><th>Last Modified</th></tr>');
  158:         $r->print('<form method="post" name="checkselect" action="'.$url.'">');
  159:     } else {
  160:         $r->print('<table border="0" cellspacing="2" cellpadding="2">'.
  161:             '<tr><th colspan="2">Actions</th><th>&nbsp;</th><th>Name</th><th>Size</th><th>Last Modified</th><th>Current Access Status</th></tr>');
  162:         $r->print('<form method="post" action="'.$url.'">');
  163:     }
  164:     if (defined($group)) {
  165:         $r->print("\n".$groupitem."\n");
  166:     }
  167:     my $href_location="/uploaded/$udom/$uname/$port_path".$current_path;
  168:     my $href_edit_location="/editupload/$udom/$uname/$port_path".$current_path;
  169:     my @dir_lines;
  170:     my %versioned;
  171:     foreach my $line (sort 
  172: 		      { 
  173: 			  my ($afile)=split('&',$a,2);
  174: 			  my ($bfile)=split('&',$b,2);
  175: 			  return (lc($afile) cmp lc($bfile));
  176: 		      } (@$dir_list)) {
  177:     	#$strip holds directory/file name
  178:     	#$dom 
  179:     	my ($filename,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef)=split(/\&/,$line,16); 
  180:     	$filename =~ s/\s+$//;
  181:     	my ($fname,$version,$extension) = &Apache::grades::file_name_version_ext($filename);
  182:     	if ($version) {
  183:     	    $versioned{$fname} .= $version.",";
  184:     	}
  185:         push(@dir_lines, [$filename,$dom,$testdir,$size,$mtime,$obs]);
  186:     }
  187:     foreach my $line (@dir_lines) {
  188:         my ($filename,$dom,$testdir,$size,$mtime,$obs) = @$line;
  189:         my ($fname,$version,$extension) = &Apache::grades::file_name_version_ext($filename);
  190:     	if (($filename ne '.') && ($filename ne '..') && ($filename !~ /\.meta$/ ) && ($filename !~ /(.*)\.(\d+)\.([^\.]*)$/)) {
  191:             if ($dirptr&$testdir) {
  192:                 if ($select_mode eq 'true'){
  193:                     $r->print('<tr bgcolor="#FFAA99"><td><img src="'.$iconpath.'folder_closed.gif"></td>');
  194:                 } else {
  195:                     $r->print('<tr bgcolor="#FFAA99"><td colspan="2"><img src="'.$iconpath.'folder_closed.gif"></td>');
  196:                 }
  197:                 $r->print('<td>Go to ...</td>');
  198:                 $r->print('<td>'.&make_anchor($url,$filename.'/',$current_path.$filename.'/',$env{'form.mode'},$env{"form.fieldname"},$env{'form.continue'},$group).'</td>'); 
  199:                 $r->print('</tr>'); 
  200:             } else {
  201:                 $r->print('<tr bgcolor="#CCCCFF">');
  202:                 my $version_flag;
  203:                 if (exists($versioned{$fname})) {
  204:                    $version_flag = "*";
  205:                 } else {
  206:                     $version_flag = "";
  207:                 }
  208:                my $fullpath = $current_path.$filename;
  209:                 $fullpath = &prepend_group($fullpath,$group);
  210:                 if ($select_mode eq 'true'){
  211:                     $r->print('<td><input type="checkbox" name="checkfile" value="'.$filename.'"');
  212:                     if ($$checked_files{$filename} eq 'selected') {
  213:                         $r->print("CHECKED");
  214:                     }
  215:                     $r->print('></td>');
  216:                 } else {
  217:                     if (exists $locked_files{$fullpath}) {
  218:                         $r->print('<td colspan="2"><a href="'.$url.'?lockinfo='.$current_path.$filename.$groupecho.'">Locked</a></td>');
  219:                     } else {
  220: 			my $cat='<img alt="'.&mt('Catalog Information').
  221: 			    '" src="'.&Apache::loncommon::lonhttpdurl('/res/adm/pages/catalog.gif').'" />';
  222:                         $r->print('<td><input type="checkbox" name="selectfile" value="'.$filename.'" />
  223:                             <a href="'.$url.'?rename='.$filename.'&amp;currentpath='.$current_path.$groupecho.'">Rename</a></td>
  224:                             <td><a href="'.$href_edit_location.$filename.'.meta">'.$cat.'</a>
  225:                             </td>');
  226:                     }
  227:                 }
  228:                 my $curr_access;
  229:                 my $pub_access = 0;
  230:                 my $guest_access = 0;
  231:                 my $cond_access = 0;
  232:                 foreach my $key (sort(keys(%{$access_controls{$fullpath}}))) {
  233:                     my ($num,$scope,$end,$start) = &unpack_acc_key($key);
  234:                     if (($now > $start) && (!$end || $end > $now)) {
  235:                         if ($scope eq 'public')  {
  236:                             $pub_access = 1;
  237:                         } elsif ($scope eq 'guest') {
  238:                             $guest_access = 1;
  239:                         } else {
  240:                             $cond_access = 1;
  241:                         }
  242:                     }
  243:                 }
  244:                 if (!$pub_access && !$guest_access && !$cond_access) {
  245:                     $curr_access = &mt('Private');
  246:                 } else {
  247:                     my @allaccesses; 
  248:                     if ($pub_access) {
  249:                         push(@allaccesses,&mt('Public'));
  250:                     }
  251:                     if ($guest_access) {
  252:                         push(@allaccesses,&mt('Password-protected'));
  253:                     }
  254:                     if ($cond_access) {
  255:                         push(@allaccesses,&mt('Conditional'));
  256:                     }
  257:                     $curr_access = join('+ ',@allaccesses);
  258:                 }
  259:                 $r->print('<td><img src="'.&Apache::loncommon::icon($filename).'"></td>');
  260:                 $r->print('<td><a href="'.$href_location.$filename.'">'.
  261: 			    $filename.'</a></td>'); 
  262:                 $r->print('<td>'.$size.'</td>');
  263:                 $r->print('<td>'.&Apache::lonlocal::locallocaltime($mtime).'</td>');
  264:                 $r->print('<td><nobr>'.&mt($curr_access).'&nbsp;&nbsp;&nbsp;'.
  265:                           '<a href="'.$url.'?access='.$filename.
  266:                           '&amp;currentpath='.$current_path.$groupecho.
  267:                           '">'.&mt('View/Change').'</a></nobr></td>');
  268:                 $r->print('</tr>'); 
  269:             }
  270:         }
  271:     }
  272:     if ($select_mode eq 'true') {
  273:         $r->print('</table>
  274:             <input type="hidden" name="continue" value="true">
  275:             <input type="hidden" name="fieldname" value="'.$env{'form.fieldname'}.'">
  276:             <input type="hidden" name="mode" value="selectfile">
  277:             <input type="submit" name="submit" value="Select checked files, and continue selecting." /><br />
  278:             <input type="button" name="doit" onClick= "finishSelect();" value="Select checked files, and close window" />
  279:             <input type="hidden" name="currentpath" value="'.$current_path.'" />
  280:         </form>');        
  281:     } else {
  282:         $r->print('</table>
  283:         <input type="submit" name="doit" value="Delete Checked Files" />
  284:         <input type="hidden" name="action" value="delete" />
  285:         <input type="hidden" name="currentpath" value="'.$current_path.'" />
  286:         </form>');
  287:     }
  288: }
  289: 
  290: sub open_form {
  291:     my ($r,$url)=@_;
  292:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
  293:     $r->print('<form name="portform" method="post" action="'.$url.'">');
  294:     $r->print('<input type="hidden" name="action" value="'.
  295: 	      $env{'form.action'}.'" />');
  296:     $r->print('<input type="hidden" name="confirmed" value="1" />');
  297:     foreach (@files) {
  298:         $r->print('<input type="hidden" name="selectfile" value="'.
  299: 	      $_.'" />');
  300:     }
  301:     $r->print('<input type="hidden" name="currentpath" value="'.
  302: 	      $env{'form.currentpath'}.'" />');
  303: }
  304: 
  305: sub close_form {
  306:     my ($r,$url,$group,$button_text)=@_;
  307:     if (!defined($button_text)) {
  308:         $button_text = {
  309:                          'continue' => &mt('Continue'),
  310:                          'cancel'   => &mt('Cancel'),
  311:                        };
  312:     }
  313:     $r->print('<p><input type="submit" value="'.$button_text->{'continue'}.'" />');
  314:     if (defined($group)) {
  315:        $r->print("\n".'<input type="hidden" name="group" value="'.
  316:               $group.'" />');
  317:     }
  318:     $r->print('</p></form>');
  319:     $r->print('<form action="'.$url.'" method="POST">
  320:                <p>
  321:               <input type="hidden" name="currentpath" value="'.
  322: 	      $env{'form.currentpath'}.'" />');
  323:     if (defined($group)) {
  324:        $r->print("\n".'<input type="hidden" name="group" value="'.
  325:               $group.'" />');
  326:     }
  327:     $r->print("\n".'   <input type="submit" value="'.$button_text->{'cancel'}.'" />
  328:                </p></form>'); 
  329: }
  330: 
  331: sub display_file {
  332:     my ($path,$filename)=@_;
  333:     my $display_file_text;
  334:     if (!defined($path)) { $path=$env{'form.currentpath'}; }
  335:     if (!defined($filename)) { 
  336:         $filename=$env{'form.selectfile'};
  337:         $display_file_text = '<tt>'.$path.$filename.'</tt>';
  338:     } elsif (ref($filename) eq "ARRAY") {
  339:         foreach (@$filename) {
  340:             $display_file_text .= '<tt>'.$path.$_.'</tt><br />';
  341:         }
  342:     } elsif (ref($filename) eq "SCALAR") {
  343:         $display_file_text = '<tt>'.$path.$filename.'</tt>';        
  344:     }
  345:     return $display_file_text;
  346: }
  347: 
  348: sub done {
  349:     my ($message,$url,$group)=@_;
  350:     unless (defined $message) {
  351:         $message='Done';
  352:     }
  353:     my $result = '<h3><a href="'.$url.'?currentpath='.
  354: 	         $env{'form.currentpath'}.
  355: 	         '&fieldname='.$env{'form.fieldname'}.
  356: 	         '&mode='.$env{'form.mode'};
  357:     if (defined($group)) {
  358:         $result .= '&group='.$group;
  359:     }
  360:     $result .= '">'.&mt($message).'</a></h3>';
  361:     return $result;
  362: }
  363: 
  364: sub delete {
  365:     my ($r,$url,$group)=@_;
  366:     my @check;
  367:     my $file_name = $env{'form.currentpath'}.$env{'form.selectfile'};
  368:     $file_name = &prepend_group($file_name,$group);
  369:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
  370:     my ($uname,$udom) = &get_name_dom($group);
  371:     if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
  372:         $r->print ("The file is locked and cannot be deleted.<br />");
  373:         $r->print(&done('Back',$url,$group));
  374:     } else {
  375:         if (scalar(@files)) {
  376:             &open_form($r,$url);
  377:             $r->print('<p>'.&mt('Delete').' '.&display_file(undef,\@files).'?</p>');
  378:             &close_form($r,$url,$group);
  379:         } else {
  380:             $r->print("No file was checked to delete.<br />");
  381:             $r->print(&done(undef,$url,$group));
  382:         }
  383:     }
  384: } 
  385: 
  386: sub delete_confirmed {
  387:     my ($r,$url,$group)=@_;
  388:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
  389:     my $result;
  390:     my ($uname,$udom) = &get_name_dom($group);
  391:     my $port_path = &get_port_path($group);
  392:     foreach my $delete_file (@files) {
  393:         $result=&Apache::lonnet::removeuserfile($uname,$udom,$port_path.
  394: 					       $env{'form.currentpath'}.
  395: 					       $delete_file);
  396:         if ($result ne 'ok') {
  397: 	$r->print('<font color="red"> An error occured ('.$result.
  398: 		  ') while trying to delete '.&display_file(undef, $delete_file).'</font><br />');
  399:         }
  400:     }
  401:     $r->print(&done(undef,$url,$group));
  402: }
  403: 
  404: sub delete_dir {
  405:     my ($r,$url,$group)=@_;
  406:     &open_form($r,$url);
  407:     $r->print('<p>'.&mt('Delete').' '.&display_file().'?</p>');
  408:     &close_form($r,$url,$group);
  409: } 
  410: 
  411: sub delete_dir_confirmed {
  412:     my ($r,$url,$group)=@_;
  413:     my $directory_name = $env{'form.currentpath'};
  414:     $directory_name =~ s|/$||; # remove any trailing slash
  415:     my ($uname,$udom) = &get_name_dom($group);
  416:     my $namespace = &get_namespace($group);
  417:     my $port_path = &get_port_path($group);
  418:     my $result=&Apache::lonnet::removeuserfile($uname,$udom,$port_path.
  419: 					       $directory_name);
  420: 					       
  421:     if ($result ne 'ok') {
  422: 	$r->print('<font color="red"> An error occured (dir) ('.$result.
  423: 		  ') while trying to delete '.$directory_name.'</font><br />');
  424:     } else {
  425:         # now remove from recent
  426: #        $r->print('<br /> removing '.$directory_name.'<br /');
  427:         &Apache::lonhtmlcommon::remove_recent($namespace,[$directory_name.'/']);
  428:         my @dirs = split m!/!, $directory_name;
  429:         
  430: #        $directory_name =~ m/^(\/*\/)(\/*.)$/;
  431:         $directory_name='/';
  432:         for (my $i=1; $i < (@dirs - 1); $i ++){
  433:             $directory_name .= $dirs[$i].'/';
  434:         }
  435:         $env{'form.currentpath'} = $directory_name;
  436:     }
  437:     $r->print(&done(undef,$url,$group));
  438: }
  439: 
  440: sub rename {
  441:     my ($r,$url,$group)=@_;
  442:     my $file_name = $env{'form.currentpath'}.$env{'form.rename'};
  443:     my ($uname,$udom) = &get_name_dom($group);
  444:     $file_name = &prepend_group($file_name,$group);
  445:     if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
  446:         $r->print ("The file is locked and cannot be renamed.<br />");
  447:         $r->print(&done(undef,$url,$group));
  448:     } else {
  449:         &open_form($r,$url);
  450:         $r->print('<p>'.&mt('Rename').' '.&display_file().' to 
  451:                    <input name="filenewname" type="input" size="50" />?</p>');
  452:         &close_form($r,$url,$group);
  453:     }
  454: }
  455: 
  456: sub rename_confirmed {
  457:     my ($r,$url,$group)=@_;
  458:     my $filenewname=&Apache::lonnet::clean_filename($env{'form.filenewname'});
  459:     my ($uname,$udom) = &get_name_dom($group);
  460:     my $port_path = &get_port_path($group);
  461:     if ($filenewname eq '') {
  462: 	$r->print('<font color="red">'.
  463: 		  &mt("Error: no valid filename was provided to rename to.").
  464: 		  '</font><br />');
  465: 	$r->print(&done(undef,$url,$group));
  466: 	return;
  467:     } 
  468:     my $result=
  469: 	&Apache::lonnet::renameuserfile($uname,$udom,
  470:             $port_path.$env{'form.currentpath'}.$env{'form.selectfile'},
  471:             $port_path.$env{'form.currentpath'}.$filenewname);
  472:     if ($result ne 'ok') {
  473: 	$r->print('<font color="red"> An errror occured ('.$result.
  474: 		  ') while trying to rename '.&display_file().' to '.
  475: 		  &display_file(undef,$filenewname).'</font><br />');
  476:     }
  477:     if ($filenewname ne $env{'form.filenewname'}) {
  478:         $r->print("The new file name was changed from:<br /><strong>".$env{'form.filenewname'}."</strong> to <strong>$filenewname </strong>");
  479:     }
  480:     $r->print(&done(undef,$url,$group));
  481: }
  482: 
  483: sub display_access {
  484:     my ($r,$url,$group) = @_;
  485:     my ($uname,$udom) = &get_name_dom($group);
  486:     my $file_name = $env{'form.currentpath'}.$env{'form.access'};
  487:     $file_name = &prepend_group($file_name,$group);
  488:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
  489:                                                                         $uname);
  490:     my %access_controls = &Apache::lonnet::get_access_controls($current_permissions,$group,$file_name);
  491:     &open_form($r,$url);
  492:     $r->print('<h3>'.&mt('Allowing others to retrieve portfolio file: [_1]',$env{'form.currentpath'}.$env{'form.access'}).'</h3>'."\n");
  493:     $r->print(&mt('Access to this file by others can be set to be one or more of the following types: public, password-protected or conditional.').'<br /><ul><li>'.&mt('Public files are available to anyone without the need for login.').'</li><li>'.&mt('Password-protected files do not require log-in, but will require the viewer to enter the password you set.').'</li><li>'.&mt('Conditional files are accessible to logged-in users with accounts in the LON-CAPA network, who satify the conditions you set.').'<br />'.&mt('The conditions can include affiliation with a particular course or group, or a user account in a specific domain.').'<br />'.&mt('Alternatively you can grant access to people with specific LON-CAPA usernames and domains.').'</li></ul>');
  494:     &access_setting_table($r,$access_controls{$file_name});
  495:     my $button_text = {
  496:                         'continue' => &mt('Proceed'),
  497:                         'cancel' => &mt('Back to directory listing'),
  498:                       };
  499:     &close_form($r,$url,$group,$button_text);
  500: }
  501: 
  502: sub update_access {
  503:     my ($r,$url,$group) = @_;
  504:     my $function = &Apache::loncommon::get_users_function();
  505:     my $tablecolor = &Apache::loncommon::designparm($function.'.tabbg');
  506:     my $totalprocessed = 0;
  507:     my %processing;
  508:     my %title  = (
  509:                          'activate' => 'New control(s) added',
  510:                          'delete'   => 'Existing control(s) deleted',
  511:                          'update'   => 'Existing control(s) modified',
  512:                      );
  513:     my $changes;
  514:     foreach my $chg (sort(keys(%title))) {     
  515:         @{$processing{$chg}} = &Apache::loncommon::get_env_multiple('form.'.$chg);
  516:         $totalprocessed += @{$processing{$chg}};
  517:         foreach my $num (@{$processing{$chg}}) {
  518:             my $scope = $env{'form.scope_'.$num};
  519:             my ($start,$end) = &get_dates_from_form($num);
  520:             my $newkey = $num.':'.$scope.'_'.$end.'_'.$start;
  521:             if ($chg eq 'delete') {
  522:                 $$changes{$chg}{$newkey} = 1;
  523:             } else {
  524:                 $$changes{$chg}{$newkey} = 
  525:                             &build_access_record($num,$scope,$start,$end,$chg);
  526:             }
  527:         }
  528:     }
  529:     my $file_name = $env{'form.currentpath'}.$env{'form.selectfile'};
  530:     $r->print('<h3>'.&mt('Allowing others to retrieve portfolio file: [_1]',
  531:               $file_name).'</h3>'."\n");
  532:     $file_name = &prepend_group($file_name,$group);
  533:     my ($uname,$udom) = &get_name_dom($group);
  534:     my ($errors,$outcome,$deloutcome,$new_values,$translation);
  535:     if ($totalprocessed) {
  536:         ($outcome,$deloutcome,$new_values,$translation) =
  537:         &Apache::lonnet::modify_access_controls($file_name,$changes,$udom,
  538:                                                 $uname);
  539:     }
  540:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
  541:                                                                        $uname);
  542:     my %access_controls = &Apache::lonnet::get_access_controls(
  543:                                        $current_permissions,$group,$file_name);
  544:     if ($totalprocessed) {
  545:         if ($outcome eq 'ok') {
  546:             my $updated_controls = $access_controls{$file_name};
  547:             my ($showstart,$showend);
  548:             $r->print(&Apache::loncommon::start_data_table());
  549:             $r->print(&Apache::loncommon::start_data_table_row());
  550:             $r->print('<th>'.&mt('Type of change').'</th><th>'.
  551:                       &mt('Access control').'</th><th>'.&mt('Dates available').
  552:                       '</th><th>'.&mt('Additional information').'</th>');
  553:             $r->print(&Apache::loncommon::end_data_table_row());
  554:             foreach my $chg (sort(keys(%processing))) {
  555:                 if (@{$processing{$chg}} > 0) {
  556:                     if ($chg eq 'delete') {
  557:                         if (!($deloutcome eq 'ok')) {
  558:                             $errors .= &mt('A problem occurred deleting access controls: [_1]',$deloutcome);
  559:                             next;
  560:                         }
  561:                     }
  562:                     my $numchgs = @{$processing{$chg}};
  563:                     $r->print(&Apache::loncommon::start_data_table_row());
  564:                     $r->print('<td rowspan="'.$numchgs.'">'.&mt($title{$chg}).
  565:                               '.</td>');
  566:                     my $count = 0;
  567:                     foreach my $key (sort(keys(%{$$changes{$chg}}))) {
  568:                         if ($count) {
  569:                             $r->print(&Apache::loncommon::start_data_table_row());
  570:                         }
  571:                         my ($num,$scope,$end,$start) = &unpack_acc_key($key); 
  572:                         my $newkey = $key;
  573:                         if ($chg eq 'activate') {
  574:                             $newkey =~ s/^(\d+)/$$translation{$1}/;
  575:                         }
  576:                         my %content = &Apache::lonnet::parse_access_controls(
  577:                                                      $$updated_controls{$newkey});
  578:                         if ($chg eq 'delete') {
  579:                             $showstart = &mt('Deleted');
  580:                             $showend = $showstart;
  581:                         } else {
  582:                             $showstart = localtime($start);
  583:                             if ($end == 0) {
  584:                                 $showend = &mt('No end date');
  585:                             } else {
  586:                                 $showend = localtime($end);
  587:                             }
  588:                         }
  589:                         $r->print('<td>'.&mt($scope));
  590:                         if (($scope eq 'course') || ($scope eq 'group')) {
  591:                             if ($chg ne 'delete') {
  592:                                 my $cid = $content{'domain'}.'_'.$content{'number'};
  593:                                 my %course_description = &Apache::lonnet::coursedescription($cid);
  594:                                 $r->print('<br />('.$course_description{'description'}.')');
  595:                             }
  596:                         }  
  597:                         $r->print('</td><td>'.&mt('Start: ').$showstart.
  598:                                   '<br />'.&mt('End: ').$showend.'</td><td>');
  599:                         if ($chg ne 'delete') {
  600:                             if ($scope eq 'guest') {
  601:                                 $r->print(&mt('Password').': '.$content{'password'});
  602:                             } elsif ($scope eq 'course' || $scope eq 'group') {
  603:                                 $r->print('<table border="0"><tr bgcol = "'.
  604:                                           $tablecolor.'">');
  605:                                 $r->print('<th>'.&mt('Roles').'</th><th>'.
  606:                                           &mt('Access').'</th><th>'.
  607:                                           &mt('Sections').'</th>');
  608:                                 if ($scope eq 'course') {
  609:                                     $r->print('<th>'.&mt('Groups').'</th>');
  610:                                 } else {
  611:                                     $r->print('<th>'.&mt('Teams').'</th>');
  612:                                 }
  613:                                 $r->print('</tr>');
  614:                                 foreach my $id (sort(keys(%{$content{'roles'}}))) {
  615:                                     $r->print('<tr>');
  616:                                     foreach my $item ('role','access','section','group') {
  617:                                         $r->print('<td>');
  618:                                         if ($item eq 'role') {
  619:                                             my $ucscope = $scope;
  620:                                             $ucscope =~ s/^(\w)/uc($1)/;
  621:                                             my $role_output;  
  622:                                             foreach my $role (@{$content{'roles'}{$id}{$item}}) {
  623:                                                 if ($role eq 'all') {
  624:                                                     $role_output .= $role.',';
  625:                                                 } elsif ($role =~ /^cr/) {
  626:                                                     $role_output .= (split('/',$role))[3].',';
  627:                                                 } else {
  628:                                                     $role_output .= &Apache::lonnet::plaintext($role,$ucscope).',';
  629:                                                 }
  630:                                             }
  631:                                             $role_output =~ s/,$//;
  632:                                             $r->print($role_output);  
  633:                                         } else {
  634:                                             $r->print(join(',',@{$content{'roles'}{$id}{$item}}));
  635:                                         }
  636:                                         $r->print('</td>');
  637:                                     }
  638:                                 }
  639:                                 $r->print(&Apache::loncommon::end_data_table_row());
  640:                                 $r->print(&Apache::loncommon::end_data_table());
  641:                             } elsif ($scope eq 'domains') {
  642:                                 $r->print(&mt('Domains: ').join(',',@{$content{'dom'}}));
  643:                             } elsif ($scope eq 'users') {
  644:                                 my $curr_user_list = &sort_users($content{'users'});
  645:                                 $r->print(&mt('Users: ').$curr_user_list);
  646:                             } else {
  647:                                 $r->print('&nbsp;');
  648:                             }
  649:                         } else {
  650:                             $r->print('&nbsp;');
  651:                         }
  652:                         $r->print('</td>');
  653:                         $r->print(&Apache::loncommon::end_data_table_row());
  654:                         $count ++;
  655:                     }
  656:                 }
  657:             }
  658:             $r->print(&Apache::loncommon::end_data_table());
  659:         } else {
  660:             if ((@{$processing{'activate'}} > 0) || (@{$processing{'update'}} > 0)) {
  661:                 $errors .= &mt('A problem occurred storing access control settings: [_1]',$outcome);
  662:             }
  663:         }
  664:         if ($errors) { 
  665:             $r->print($errors);
  666:         }
  667:     }
  668:     my $allnew = 0;
  669:     my $totalnew = 0;
  670:     my $status = 'new';
  671:     my ($firstitem,$lastitem);
  672:     foreach my $newitem ('course','group','domains','users') {
  673:         $allnew += $env{'form.new'.$newitem};
  674:     }
  675:     if ($allnew > 0) {
  676:         my $now = time;
  677:         my $then = $now + (60*60*24*180); # six months approx.
  678:         &open_form($r,$url,$group);
  679:         foreach my $newitem ('course','group','domains','users') {
  680:             if ($env{'form.new'.$newitem} > 0) {
  681:                 $r->print('<br />'.&mt('Add new <b>[_1]-based</b> access control for portfolio file: <b>[_2]</b>',$newitem,$env{'form.currentpath'}.$env{'form.selectfile'}).'<br /><br />');
  682:                 $firstitem = $totalnew;
  683:                 $lastitem = $totalnew + $env{'form.new'.$newitem};
  684:                 $totalnew = $lastitem;
  685:                 my @numbers;   
  686:                 for (my $i=$firstitem; $i<$lastitem; $i++) {
  687:                     push (@numbers,$i);
  688:                 }
  689:                 &display_access_row($r,$status,$newitem,\@numbers,
  690:                                     $access_controls{$file_name},$now,$then);
  691:             }
  692:         }
  693:         &close_form($r,$url,$group);
  694:     } else {
  695:         $r->print('<br /><a href="'.$url.'?access='.$env{'form.selectfile'}.
  696:                   '&currentpath='.$env{'form.currentpath'}.'">'.
  697:                    &mt('Display all access settings for this file').'</a>');
  698:     }
  699:     return;
  700: }
  701: 
  702: sub build_access_record {
  703:     my ($num,$scope,$start,$end,$chg) = @_;
  704:     my $record = '<scope type="'.$scope.'"><start>'.$start.'</start><end>'.
  705:                  $end.'</end>';
  706:     if ($scope eq 'guest') {
  707:         $record .= '<password>'.$env{'form.password'}.'</password>';
  708:     } elsif (($scope eq 'course') || ($scope eq 'group')) {
  709:         $record .= '<domain>'.$env{'form.crsdom_'.$num}.'</domain><number>'.
  710:                    $env{'form.crsnum_'.$num}.'</number>';
  711:         my @role_ids;
  712:         my @delete_role_ids;
  713:         if (exists($env{'form.delete_role_'.$num})) {
  714:             @delete_role_ids = &Apache::loncommon::get_env_multiple('form.delete_role_'.$num);
  715:         }
  716:         if (exists($env{'form.preserve_role_'.$num})) {
  717:             my @preserves = &Apache::loncommon::get_env_multiple('form.preserve_role_'.$num);
  718:             if (@delete_role_ids > 0) {
  719:                 foreach my $id (@preserves) {
  720:                     if (grep/^\Q$id\E$/,@delete_role_ids) {
  721:                         next;
  722:                     }
  723:                     push(@role_ids,$id); 
  724:                 }
  725:             } else {
  726:                 push(@role_ids,@preserves);
  727:             }
  728:         }
  729:         my $next_id = '';
  730:         if (exists($env{'form.add_role_'.$num})) {
  731:             $next_id = $env{'form.add_role_'.$num};
  732:             if ($next_id) {
  733:                 push(@role_ids,$next_id);
  734:             }
  735:         }
  736:         foreach my $id (@role_ids) {
  737:             my (@roles,@accesses,@sections,@groups);
  738:             if (($id == $next_id) && ($chg eq 'update')) {
  739:                 @roles = split(/,/,$env{'form.role_'.$num.'_'.$next_id});
  740:                 @accesses = split(/,/,$env{'form.access_'.$num.'_'.$next_id});
  741:                 @sections = split(/,/,$env{'form.section_'.$num.'_'.$next_id});
  742:                 @groups = split(/,/,$env{'form.group_'.$num.'_'.$next_id});
  743:             } else {
  744:                 @roles = &Apache::loncommon::get_env_multiple('form.role_'.$num.'_'.$id);
  745:                 @accesses = &Apache::loncommon::get_env_multiple('form.access_'.$num.'_'.$id);
  746:                 @sections = &Apache::loncommon::get_env_multiple('form.section_'.$num.'_'.$id);
  747:                 @groups = &Apache::loncommon::get_env_multiple('form.group_'.$num.'_'.$id);
  748:             }
  749:             $record .= '<roles id="'.$id.'">';
  750:             foreach my $role (@roles) {
  751:                 $record .= '<role>'.$role.'</role>';
  752:             }
  753:             foreach my $access (@accesses) {
  754:                 $record .= '<access>'.$access.'</access>';
  755:             }
  756:             foreach my $section (@sections) {
  757:                 $record .= '<section>'.$section.'</section>';
  758:             }
  759:             foreach my $group (@groups) {
  760:                 $record .= '<group>'.$group.'</group>';
  761:             }
  762:             $record .= '</roles>';
  763:         }
  764:     } elsif ($scope eq 'domains') {
  765:         my @doms = &Apache::loncommon::get_env_multiple('form.dom_'.$num);
  766:         foreach my $dom (@doms) {
  767:             if ($dom ne '') {
  768:                 $record .= '<dom>'.$dom.'</dom>'; 
  769:             }
  770:         }
  771:     } elsif ($scope eq 'users') {
  772:         my $userlist = $env{'form.users_'.$num};
  773:         $userlist =~ s/[\r\n\f]+//g;
  774:         $userlist =~ s/\s+//g;
  775:         my @users = split/,/,$userlist;
  776:         my %userhash;
  777:         my @unique_users;
  778:         foreach my $user (@users) {
  779:             if (!exists($userhash{$user})) {
  780:                 $userhash{$user} = 1;
  781:                 push(@unique_users,$user);
  782:             }
  783:         }
  784:         $record .= '<users>';
  785:         foreach my $user (@unique_users) {
  786:             my ($uname,$udom) = split(/:/,$user);
  787:             $record .= '<user><uname>'.$uname.'</uname><udom>'.$udom.
  788:                        '</udom></user>';
  789:         }
  790:         $record .= '</users>';
  791:     }
  792:     $record .= '</scope>';
  793:     return $record;
  794: }
  795: 
  796: sub get_dates_from_form {
  797:     my ($id) = @_;
  798:     my $startdate;
  799:     my $enddate;
  800:     $startdate = &Apache::lonhtmlcommon::get_date_from_form('startdate_'.$id);
  801:     $enddate   = &Apache::lonhtmlcommon::get_date_from_form('enddate_'.$id);
  802:     if ( exists ($env{'form.noend_'.$id}) ) {
  803:         $enddate = 0;
  804:     }
  805:     return ($startdate,$enddate);
  806: }
  807: 
  808: sub sort_users {
  809:     my ($users_hash) = @_; 
  810:     my @curr_users;
  811:     foreach my $user (keys(%{$users_hash})) {
  812:         $curr_users[$$users_hash{$user}] = $user;
  813:     }
  814:     my $curr_user_list = join(",\n",@curr_users);
  815:     return $curr_user_list;
  816: }
  817: 
  818: sub access_setting_table {
  819:     my ($r,$access_controls) = @_;
  820:     my ($public,$publictext);
  821:     $publictext = &mt('Off');
  822:     my ($guest,$guesttext);
  823:     $guesttext = &mt('Off');
  824:     my @courses = ();
  825:     my @groups = ();
  826:     my @domains = ();
  827:     my @users = ();
  828:     my $now = time;
  829:     my $then = $now + (60*60*24*180); # six months approx.
  830:     my ($num,$scope,$publicnum,$guestnum);
  831:     my (%acl_count,%end,%start);
  832:     foreach my $key (sort(keys(%{$access_controls}))) {
  833:         ($num,$scope,$end{$key},$start{$key}) = &unpack_acc_key($key);
  834:         if ($scope eq 'public') {
  835:             $public = $key;
  836:             $publicnum = $num;
  837:             $publictext = &acl_status($start{$key},$end{$key},$now);
  838:         } elsif ($scope eq 'guest') {
  839:             $guest=$key;
  840:             $guestnum = $num;  
  841:             $guesttext = &acl_status($start{$key},$end{$key},$now);
  842:         } elsif ($scope eq 'course') {
  843:             push(@courses,$key);
  844:         } elsif ($scope eq 'group') {
  845:             push(@groups,$key);
  846:         } elsif ($scope eq 'domains') {
  847:             push(@domains,$key);
  848:         } elsif ($scope eq 'users') {
  849:             push(@users,$key);
  850:         }
  851:         $acl_count{$scope} ++;
  852:     }
  853:     $r->print('<table border="0"><tr><td valign="top">');
  854:     $r->print('<h3>'.&mt('Public access:').' '.$publictext.'</h3>');
  855:     $r->print(&Apache::loncommon::start_data_table());
  856:     $r->print(&Apache::loncommon::start_data_table_row());
  857:     $r->print('<th>'.&mt('Action').'</th><th>'.&mt('Dates available').'</th>');
  858:     $r->print(&Apache::loncommon::end_data_table_row());
  859:     $r->print(&Apache::loncommon::start_data_table_row());
  860:     if ($public) {
  861:         $r->print('<td>'.&actionbox('old',$publicnum,'public').'</td><td>'.
  862:                   &dateboxes($publicnum,$start{$public},$end{$public}).'</td>');
  863:     } else {
  864:         $r->print('<td>'.&actionbox('new','0','public').'</td><td>'.
  865:                   &dateboxes('0',$now,$then).'</td>');
  866:     }
  867:     $r->print(&Apache::loncommon::end_data_table_row());
  868:     $r->print(&Apache::loncommon::end_data_table());
  869:     $r->print('</td><td width="40">&nbsp;</td><td valign="top">');
  870:     $r->print('<h3>'.&mt('Password-protected access:').' '.$guesttext.'</h3>');
  871:     $r->print(&Apache::loncommon::start_data_table());
  872:     $r->print(&Apache::loncommon::start_data_table_row());
  873:     $r->print('<th>'.&mt('Action').'</th><th>'.&mt('Dates available').
  874:               '</th><th>'. &mt('Password').'</th>');
  875:     $r->print(&Apache::loncommon::end_data_table_row());
  876:     $r->print(&Apache::loncommon::start_data_table_row());
  877:     my $passwd;
  878:     if ($guest) {
  879:         my %content = &Apache::lonnet::parse_access_controls(
  880:                                          $$access_controls{$guest});
  881:         $passwd = $content{'password'};
  882:         $r->print('<td>'.&actionbox('old',$guestnum,'guest').'</td><td>'.
  883:                   &dateboxes($guestnum,$start{$guest},$end{$guest}).'</td>');
  884:     } else {
  885:         $r->print('<td>'.&actionbox('new','1','guest').'</td><td>'.
  886:                   &dateboxes('1',$now,$then).'</td>');
  887:     }
  888:     $r->print('<td><input type="text" size="15" name="password" value="'.
  889:               $passwd.'" /></td>');
  890:     $r->print(&Apache::loncommon::end_data_table_row());
  891:     $r->print(&Apache::loncommon::end_data_table());
  892:     $r->print('</td></tr><tr><td colspan="3">&nbsp;</td></tr><tr><td valign="top">');
  893:     &access_element($r,'domains',\%acl_count,\@domains,$access_controls,$now,$then);
  894:     $r->print('</td><td>&nbsp;</td><td valign="top">');
  895:     &access_element($r,'users',\%acl_count,\@users,$access_controls,$now,$then);
  896:     $r->print('</td></tr><tr><td colspan="3"></td></tr><tr>');
  897:     if (@courses > 0 || @groups > 0) {
  898:         $r->print('<td colspan="3" valign="top">');
  899:     } else {
  900:         $r->print('<td valign="top">');
  901:     }
  902:     &access_element($r,'course',\%acl_count,\@courses,$access_controls,$now,$then);
  903:     $r->print('</td>');
  904:     if (@courses > 0 || @groups > 0) {
  905:         $r->print('</tr><tr><td colspan="3">&nbsp;</td></tr><tr><td colspan="3" valign="top">');
  906:     } else {
  907:         $r->print('<td>&nbsp;</td><td valign="top">');
  908:     }
  909:     &access_element($r,'group',\%acl_count,\@groups,$access_controls,$now,$then);
  910:     $r->print('</td></tr></table>');
  911: }
  912: 
  913: sub acl_status {
  914:     my ($start,$end,$now) = @_;
  915:     if ($start > $now) {
  916:         return &mt('Inactive');
  917:     }
  918:     if ($end && $end<$now) {
  919:         return &mt('Inactive');
  920:     }
  921:     return &mt('Active');
  922: }
  923: 
  924: sub access_element {
  925:     my ($r,$type,$acl_count,$items,$access_controls,$now,$then) = @_;
  926:     my $title = $type;
  927:     $title =~ s/s$//;
  928:     $title =~ s/^(\w)/uc($1)/e;
  929:     $r->print('<h3>'.&mt('[_1]-based conditional access: ',$title));
  930:     if ($$acl_count{$type}) {
  931:         $r->print($$acl_count{$type}.' ');
  932:         if ($$acl_count{$type} > 1) {
  933:             $r->print(&mt('conditions'));
  934:         } else {
  935:             $r->print(&mt('condition'));
  936:         }
  937:     } else {
  938:         $r->print(&mt('Off'));
  939:     }
  940:     $r->print('</h3>');
  941:     &display_access_row($r,'old',$type,$items,$access_controls,$now,$then);
  942:     return;
  943: }
  944: 
  945: sub display_access_row {
  946:     my ($r,$status,$type,$items,$access_controls,$now,$then) = @_;
  947:     if (@{$items} > 0) {
  948:         my @all_doms;
  949:         my $tablecolor;
  950:         my $colspan = 3;
  951:         my $uctype = $type;
  952:         $uctype =~ s/^(\w)/uc($1)/e;
  953:         $r->print(&Apache::loncommon::start_data_table());
  954:         $r->print(&Apache::loncommon::start_data_table_row());
  955:         $r->print('<th>'.&mt('Action?').'</th><th>'.&mt($uctype).'</td><th>'.
  956:               &mt('Dates available').'</th>');
  957:         if (($type eq 'course') || ($type eq 'group')) {
  958:             $r->print('<th>'.&mt('Allowed [_1] member affiliations',$type).
  959:                       '</th>');
  960:             $colspan ++;
  961:             my $function = &Apache::loncommon::get_users_function();
  962:             $tablecolor=&Apache::loncommon::designparm($function.'.tabbg');
  963:         } elsif ($type eq 'domains') {
  964:             @all_doms = &Apache::loncommon::get_domains();
  965:         }
  966:         $r->print(&Apache::loncommon::end_data_table_row());
  967:         foreach my $key (@{$items}) {
  968:             if (($type eq 'course') || ($type eq 'group')) {
  969:                 &course_row($r,$status,$type,$key,$access_controls,
  970:                             $tablecolor,$now,$then);
  971:             } elsif ($type eq 'domains') {
  972:                 &domains_row($r,$status,$key,\@all_doms,$access_controls,$now,
  973:                             $then);
  974:             } elsif ($type eq 'users') {
  975:                 &users_row($r,$status,$key,$access_controls,$now,$then);
  976:             }
  977:         }
  978:         $r->print(&Apache::loncommon::end_data_table_row());
  979:         $r->print(&Apache::loncommon::start_data_table_row());
  980:         if ($status eq 'old') {
  981:             $r->print('<td colspan="',$colspan.'">'.&additional_item($type).
  982:                       '</td>');
  983:         }
  984:         $r->print(&Apache::loncommon::end_data_table());
  985:     } else {
  986:         $r->print(&mt('No [_1]-based conditions defined.<br />',$type).
  987:                   &additional_item($type));
  988:     }
  989:     return;
  990: }
  991: 
  992: sub course_row {
  993:     my ($r,$status,$type,$item,$access_controls,$tablecolor,$now,$then) = @_;
  994:     my %content;
  995:     my $defdom = $env{'user.domain'};
  996:     if ($status eq 'old') {
  997:         %content = &Apache::lonnet::parse_access_controls(
  998:                                                      $$access_controls{$item});
  999:         $defdom = $content{'domain'}; 
 1000:     }
 1001:     $r->print(&Apache::loncommon::coursebrowser_javascript($defdom));
 1002:     $r->print(qq|
 1003: <script type="text/javascript">
 1004: function setRoleOptions(caller,num,cdom,cnum,type) {
 1005:     addIndexnum = getCallerIndex(caller);
 1006:     updateIndexnum = getIndex('update',num);
 1007:     if (caller.checked) {
 1008:         document.portform.elements[updateIndexnum].checked = true;
 1009:         var url = '/adm/portfolio?action=rolepicker&setroles='+addIndexnum+'&cnum='+cnum+'&cdom='+cdom+'&type='+type;
 1010:         var title = 'Roles_Chooser';
 1011:         var options = 'scrollbars=1,resizable=1,menubar=0';
 1012:         options += ',width=700,height=600';
 1013:         rolebrowser = open(url,title,options,'1');
 1014:         rolebrowser.focus();
 1015:     } else {
 1016:         for (var j=0;j<5;j++) {
 1017:             document.portform.elements[addIndexnum+j].value = '';
 1018:         }
 1019:     }
 1020: }
 1021: 
 1022: function getCallerIndex(caller) {
 1023:     for (var i=0;i<document.portform.elements.length;i++) {
 1024:         if (document.portform.elements[i] == caller) {
 1025:             return i;
 1026:         }
 1027:     }
 1028:     return -1;
 1029: }
 1030: 
 1031: function getIndex(name,value) {
 1032:     for (var i=0;i<document.portform.elements.length;i++) {
 1033:         if (document.portform.elements[i].name == name && document.portform.elements[i].value == value) {
 1034:             return i;
 1035:         }
 1036:     }
 1037:     return -1;
 1038: }
 1039: 
 1040: </script>
 1041: |);
 1042:     $r->print(&Apache::loncommon::start_data_table_row());
 1043:     my $crsgrptext = 'Groups';
 1044:     if ($type eq 'group') {
 1045:         $crsgrptext = 'Teams';
 1046:     }
 1047:     my ($num,$scope,$end,$start) = &set_identifiers($status,$item,$now,$then,
 1048:                                                     $type);
 1049:     $r->print('<td>'.&actionbox($status,$num,$scope).'</td>');
 1050:     if ($status eq 'old') {
 1051:         my $cid = $content{'domain'}.'_'.$content{'number'};
 1052:         my %course_description = &Apache::lonnet::coursedescription($cid);
 1053:         $r->print('<td><input type="hidden" name="crsdom_'.$num.'" value="'.$content{'domain'}.'" /><input type="hidden" name="crsnum_'.$num.'" value="'.$content{'number'}.'" />'.$course_description{'description'}.'</td>');
 1054:     } elsif ($status eq 'new') {
 1055:         my $uctype = $type;
 1056:         $uctype =~ s/^(\w)/uc($1)/e;
 1057:         $r->print('<td>'.&Apache::loncommon::selectcourse_link('portform','crsnum_'.$num,'crsdom_'.$num,'description_'.$num,undef,undef,$uctype).'&nbsp;&nbsp;<input type="text" name="description_'.$num.'" size="30" /><input type="hidden" name="crsdom_'.$num.'" /><input type="hidden" name="crsnum_'.$num.'" /></td>');
 1058:     }
 1059:     $r->print('<td>'.&dateboxes($num,$start,$end).'</td>');
 1060:     $r->print('<td><table border="0"><tr bgcolor="'.$tablecolor.'">');
 1061:     $r->print('<th>'.&mt('Action').'</th><th>'.&mt('Roles').'</th><th>'.
 1062:               &mt('Access').'</th><th>'.&mt('Sections').'</th><th>'.
 1063:               &mt($crsgrptext).'</th></tr>');
 1064:     if ($status eq 'old') {
 1065:         my $max_id = 0;
 1066:         foreach my $role_id (sort(keys(%{$content{'roles'}}))) {
 1067:             if ($role_id > $max_id) {
 1068:                 $max_id = $role_id;
 1069:             }
 1070:             $max_id ++;
 1071:             my $role_selects = &role_selectors($num,$role_id,$status,$type,\%content,'display');
 1072:             $r->print('<tr><td><nobr><label><input type="checkbox" name="delete_role_'.$num.'" value="'.$role_id.'" />'.&mt('Delete').'</label></nobr><br /><input type="hidden" name="preserve_role_'.$num.'" value="'.$role_id.'" /></td>'.$role_selects.'</tr>');
 1073:         }
 1074:         $r->print('</table><br />'.&mt('Add a roles-based condition').'&nbsp;<input type="checkbox" name ="add_role_'.$num.'" onClick="javascript:setRoleOptions(this,'."'$num','$content{domain}','$content{number}','Course'".')" value="'.$max_id.'" /><input type="hidden" name="role_'.$num.'_'.$max_id.'" /><input type="hidden" name="access_'.$num.'_'.$max_id.'" /><input type="hidden" name="section_'.$num.'_'.$max_id.'" /><input type="hidden" name="group_'.$num.'_'.$max_id.'" /></td>');
 1075:     } elsif ($status eq 'new') {
 1076:         my $role_id = 1;
 1077:         my $role_selects = &role_selectors($num,$role_id,$status,$type,undef,'display');
 1078:         $r->print('<tr><td><input type="checkbox" name="add_role_'.$num.'" value="'.$role_id.'" checked="checked" />'.&mt('Add').'<input type="hidden" name="grplist_'.$num.'_'.$role_id.'" /></td>'.$role_selects);
 1079:         $r->print('</tr></table></td>');
 1080:     }
 1081:     $r->print(&Apache::loncommon::end_data_table_row());
 1082:     return;
 1083: }
 1084: 
 1085: sub domains_row {
 1086:     my ($r,$status,$item,$all_doms,$access_controls,$now,$then) = @_;
 1087:     $r->print(&Apache::loncommon::start_data_table_row());
 1088:     my ($num,$scope,$end,$start) = &set_identifiers($status,$item,$now,$then,
 1089:                                                     'domains');
 1090:     my $dom_select = '<select name="dom_'.$num.'" size="4" multiple="true"/>'.
 1091:                      ' <option value="">'.&mt('Please select').'</option>';
 1092:     $r->print(&Apache::loncommon::start_data_table_row());
 1093:     if ($status eq 'old') {
 1094:         my %content = &Apache::lonnet::parse_access_controls(
 1095:                                                       $$access_controls{$item});
 1096:         my @curr_doms = @{$content{'dom'}};
 1097:         foreach my $dom (@{$all_doms}) {
 1098:             if ((@{$content{'dom'}} > 0) && (grep(/^\Q$dom\E$/,@{$content{'dom'}}))) {
 1099:                 $dom_select .= '<option value="'.$dom.'" selected>'.
 1100:                                $dom.'</option>';
 1101:             } else {
 1102:                 $dom_select .= '<option value="'.$dom.'">'.$dom.'</option>';
 1103:             }
 1104:         }
 1105:     } else {
 1106:         foreach my $dom (@{$all_doms}) {
 1107:             $dom_select .= '<option value="'.$dom.'">'.$dom.'</option>';
 1108:         }
 1109:     }
 1110:     $r->print('<td>'.&actionbox($status,$num,$scope).'</td><td>'.$dom_select.
 1111:               '</td><td>'.&dateboxes($num,$start,$end).'</td>');
 1112:     $r->print(&Apache::loncommon::end_data_table_row());
 1113:     $r->print(&Apache::loncommon::start_data_table_row());
 1114: }
 1115: 
 1116: sub users_row {
 1117:     my ($r,$status,$item,$access_controls,$now,$then) = @_;
 1118:     $r->print(&Apache::loncommon::start_data_table_row());
 1119:     my ($num,$scope,$end,$start) = &set_identifiers($status,$item,$now,$then,
 1120:                                                     'users');
 1121:     my $curr_user_list;
 1122:     if ($status eq 'old') {
 1123:         my %content = &Apache::lonnet::parse_access_controls(
 1124:                                                      $$access_controls{$item});
 1125:         $curr_user_list = &sort_users($content{'users'});
 1126:     }
 1127:     $r->print('<td>'.&actionbox($status,$num,$scope).'</td><td>'.&mt("Format for users' username:domain information:").'<br /><tt>sparty:msu,illini:uiuc  ... etc.</tt><br /><textarea name="users_'.$num.'" cols="30"  rows="5">'.$curr_user_list.'</textarea></td><td>'.&dateboxes($num,$start,$end).'</td>');
 1128:     $r->print(&Apache::loncommon::end_data_table_row());
 1129: }
 1130: 
 1131: sub additional_item {
 1132:     my ($type) = @_;
 1133:     my $output = &mt('Add new [_1] condition(s)?',$type).'&nbsp;'.&mt('Number to add: ').'<input type="text" name="new'.$type.'" size="3" value="0" />';
 1134:     return $output;
 1135: }
 1136: 
 1137: sub actionbox {
 1138:     my ($status,$num,$scope) = @_;
 1139:     my $output = '<nobr><label>';
 1140:     if ($status eq 'new') {
 1141:         $output .= '<input type="checkbox" name="activate" value="'.$num.'" />'.
 1142:         &mt('Activate');
 1143:     } else {
 1144:         $output .= '<input type="checkbox" name="delete" value="'.$num.
 1145:                    '" />'.&mt('Delete').'</label><br /><nobr>'.
 1146:                    '<label><input type="checkbox" name="update" value="'.
 1147:                    $num.'" />'.&mt('Update');
 1148:     }
 1149:     $output .= '</label></nobr><input type="hidden" name="scope_'.$num.                '" value="'.$scope.'" />';
 1150:     return $output;
 1151: }
 1152:                                                                                    
 1153: sub dateboxes {
 1154:     my ($num,$start,$end) = @_;
 1155:     my $noend;
 1156:     if ($end == 0) {
 1157:         $noend = 'checked="checked"';
 1158:     }
 1159:     my $startdate = &Apache::lonhtmlcommon::date_setter('portform',
 1160:                            'startdate_'.$num,$start,undef,undef,undef,1,undef,
 1161:                             undef,undef,1);
 1162:     my $enddate = &Apache::lonhtmlcommon::date_setter('portform',
 1163:                                'enddate_'.$num,$end,undef,undef,undef,1,undef,
 1164:                                 undef,undef,1). '&nbsp;&nbsp;<nobr><label>'.
 1165:                                 '<input type="checkbox" name="noend_'.
 1166:                                 $num.'" '.$noend.' />'.&mt('No end date').
 1167:                                 '</label></nobr>';
 1168:                                                                                    
 1169:     my $output = &mt('Start: ').$startdate.'<br />'.&mt('End: ').$enddate;
 1170:     return $output;
 1171: }
 1172: 
 1173: sub unpack_acc_key {
 1174:     my ($acc_key) = @_;
 1175:     my ($num,$scope,$end,$start) = ($acc_key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
 1176:     return ($num,$scope,$end,$start);
 1177: }
 1178: 
 1179: sub set_identifiers {
 1180:     my ($status,$item,$now,$then,$scope) = @_;
 1181:     if ($status eq 'old') {
 1182:         return(&unpack_acc_key($item));
 1183:     } else {
 1184:         return($item,$scope,$then,$now);
 1185:     }
 1186: } 
 1187: 
 1188: sub role_selectors {
 1189:     my ($num,$role_id,$status,$type,$content,$caller) = @_;
 1190:     my ($output,$cdom,$cnum,$longid);
 1191:     if ($caller eq 'display') {
 1192:         $longid = '_'.$num.'_'.$role_id;
 1193:         if ($status eq 'new') {
 1194:             foreach my $item ('role','access','section','group') {
 1195:                 $output .= '<td><select name="'.$item.$longid.'">'.
 1196:                            '<option value="">'.&mt('Pick [_1] first',$type).
 1197:                            '</option></select></td>';
 1198:             }
 1199:             return $output;
 1200:         } else {
 1201:             $cdom = $$content{'domain'};
 1202:             $cnum = $$content{'number'};
 1203:         }
 1204:     } elsif ($caller eq 'rolepicker') {
 1205:          $cdom = $env{'form.cdom'};
 1206:          $cnum = $env{'form.cnum'};
 1207:     }
 1208:     my ($sections,$groups,$allroles,$rolehash,$accesshash) =
 1209:             &Apache::loncommon::get_secgrprole_info($cdom,$cnum,1,$type);
 1210:     if (!@{$sections}) {
 1211:         @{$sections} = ('none');
 1212:     } else {
 1213:         unshift(@{$sections},('all','none'));
 1214:     }
 1215:     if (!@{$groups}) {
 1216:         @{$groups} = ('none');
 1217:     } else {
 1218:         unshift(@{$groups},('all','none'));
 1219:     }
 1220:     my @allacesses = sort(keys(%{$accesshash}));
 1221:     my (%sectionhash,%grouphash);
 1222:     foreach my $sec (@{$sections}) {
 1223:         $sectionhash{$sec} = $sec;
 1224:     }
 1225:     foreach my $grp (@{$groups}) {
 1226:         $grouphash{$grp} = $grp;
 1227:     }
 1228:     my %lookup = (
 1229:                    'role' => $rolehash,
 1230:                    'access' => $accesshash,
 1231:                    'section' => \%sectionhash,
 1232:                    'group' => \%grouphash,
 1233:                  );
 1234:     my @allaccesses = sort(keys(%{$accesshash}));
 1235:     my %allitems = (
 1236:                     'role' => $allroles,
 1237:                     'access' => \@allaccesses,
 1238:                     'section' => $sections,
 1239:                     'group' => $groups, 
 1240:                    );
 1241:     foreach my $item ('role','access','section','group') {
 1242:         $output .= '<td><select name="'.$item.$longid.'" multiple="true" size="4">'."\n";
 1243:         foreach my $entry (@{$allitems{$item}}) {
 1244:             if ($caller eq 'display') {
 1245:                 if ((@{$$content{'roles'}{$role_id}{$item}} > 0) && 
 1246:                     (grep(/^\Q$entry\E$/,@{$$content{'roles'}{$role_id}{$item}}))) {
 1247:                     $output .= '  <option value="'.$entry.'" selected>'.
 1248:                                   $lookup{$item}{$entry}.'</option>';
 1249:                     next;
 1250:                 }
 1251:             }
 1252:             $output .= '  <option value="'.$entry.'">'.
 1253:                        $lookup{$item}{$entry}.'</option>';
 1254:         }
 1255:         $output .= '</select>';
 1256:     }
 1257:     $output .= '</td>';
 1258:     return $output;
 1259: }
 1260: 
 1261: sub role_options_window {
 1262:     my ($r) = @_;
 1263:     my $cdom = $env{'form.cdom'};
 1264:     my $cnum = $env{'form.cnum'};
 1265:     my $type = $env{'form.type'};
 1266:     my $addindex = $env{'form.setroles'};
 1267:     my $role_selects = &role_selectors(1,1,'new',$type,undef,'rolepicker');
 1268:     $r->print(<<"END_SCRIPT");
 1269: <script type="text/javascript">
 1270: function setRoles() {
 1271:     var addidx = $addindex+1;
 1272:     for (var i=0; i<4; i++) {
 1273:         var copylist = '';
 1274:         for (var j=0; j<document.rolepicker.elements[i].length; j++) {
 1275:             if (document.rolepicker.elements[i].options[j].selected) {
 1276:                 copylist = copylist + document.rolepicker.elements[i].options[j].value + ',';
 1277:             }
 1278:         }
 1279:         copylist = copylist.substr(0,copylist.length-1);
 1280:         opener.document.portform.elements[addidx+i].value = copylist;
 1281:     }
 1282:     self.close();
 1283: }
 1284: </script>
 1285: END_SCRIPT
 1286:     $r->print(&mt('Select roles, course status, section(s) and group(s) for users who will be able to access the portfolio file.'));
 1287:     $r->print('<form name="rolepicker" action="/adm/portfolio" method="post"><table><tr><th>'.&mt('Roles').'</th><th>'.&mt('[_1] status',$type).'</th><th>'.&mt('Sections').'</th><th>'.&mt('Groups').'</th></tr><tr>'.$role_selects.'</tr></table><br /><input type="button" name="rolepickbutton" value="Save selections" onclick="setRoles()" />');
 1288:     return;
 1289: }
 1290: 
 1291: sub select_files {
 1292:     my ($r,$group) = @_;
 1293:     if ($env{'form.continue'} eq 'true') {
 1294:         # here we update the selections for the currentpath
 1295:         # eventually, have to handle removing those not checked, but . . . 
 1296:         my @items=&Apache::loncommon::get_env_multiple('form.checkfile');
 1297:         if (scalar(@items)){
 1298:              &Apache::lonnet::save_selected_files($env{'user.name'}, $env{'form.currentpath'}, @items);
 1299:         }
 1300:     } else {
 1301:             #empty the file for a fresh start
 1302:             &Apache::lonnet::clear_selected_files($env{'user.name'});
 1303:     }
 1304:     my @files = &Apache::lonnet::files_not_in_path($env{'user.name'}, $env{'form.currentpath'});
 1305:     my $java_files = join ",", @files;
 1306:     if ($java_files) {
 1307:         $java_files.=',';
 1308:     }
 1309:     my $javascript =(<<ENDSMP);
 1310:         <script language='javascript'>
 1311:         function finishSelect() {
 1312: ENDSMP
 1313:     $javascript .= 'fileList = "'.$java_files.'";';
 1314:     $javascript .= (<<ENDSMP);
 1315:             for (i=0;i<document.forms.checkselect.length;i++) { 
 1316:                 if (document.forms.checkselect[i].checked){
 1317:                     fileList = fileList + document.forms.checkselect.currentpath.value + document.forms.checkselect[i].value + "," ;
 1318:                 }
 1319:             }
 1320:             opener.document.forms.lonhomework.
 1321: ENDSMP
 1322:     $javascript .= $env{'form.fieldname'};
 1323:     $javascript .= (<<ENDSMP);
 1324:         .value=fileList;
 1325:             self.close();
 1326:         }
 1327:         </script>
 1328: ENDSMP
 1329:     $r->print($javascript);
 1330:     $r->print("<h1>Select portfolio files</h1>
 1331:                 Check as many as you wish in response to the problem.<br />");
 1332:     my @otherfiles=&Apache::lonnet::files_not_in_path($env{'user.name'}, $env{'form.currentpath'});
 1333:     if (@otherfiles) {
 1334: 	$r->print("<strong>Files selected from other directories:</strong><br />");
 1335: 	foreach my $file (@otherfiles) {
 1336: 	    $r->print($file."<br />");
 1337: 	}
 1338:     }
 1339: }
 1340: sub upload {
 1341:     my ($r,$url,$group)=@_;
 1342:     my $fname=$env{'form.uploaddoc.filename'};
 1343:     my $filesize = (length($env{'form.uploaddoc'})) / 1000; #express in k (1024?)
 1344:     my $disk_quota = 20000; # expressed in k
 1345:     $fname=&Apache::lonnet::clean_filename($fname);
 1346: 
 1347:     my $portfolio_root=&get_portfolio_root($group);
 1348:     my ($uname,$udom) = &get_name_dom($group);
 1349:     my $port_path = &get_port_path($group);
 1350:     # Fixme --- Move the checking for existing file to LOND error return
 1351:     my @dir_list=&get_dir_list($portfolio_root,$group);
 1352:     my $found_file = 0;
 1353:     my $locked_file = 0;
 1354:     foreach my $line (@dir_list) {
 1355:         my ($file_name)=split(/\&/,$line,2);
 1356:         if ($file_name eq $fname){
 1357:             $file_name = $env{'form.currentpath'}.$file_name;
 1358:             $file_name = &prepend_group($file_name,$group);
 1359:             $found_file = 1;
 1360:             if (defined($group)) {
 1361:                 $file_name = $group.'/'.$file_name;
 1362:             }
 1363:             if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
 1364:                 $locked_file = 1;
 1365:             } 
 1366:         }
 1367:     }
 1368:     my $current_disk_usage = &Apache::lonnet::diskusage($udom,$uname,$portfolio_root);
 1369:     if (($current_disk_usage + $filesize) > $disk_quota){
 1370:         $r->print('<font color="red">Unable to upload <strong>'.$fname.' (size = '.$filesize.' kilobytes)</strong>. Disk quota will be exceeded.'.
 1371:                   '<br />Disk quota is '.$disk_quota.' kilobytes. Your current disk usage is '.$current_disk_usage.' kilobytes.');
 1372:         $r->print(&done('Back',$url,$group));
 1373:     } 
 1374:     elsif ($found_file){
 1375:         if ($locked_file){
 1376:             $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>'.
 1377:                   '<br />You will be able to rename or delete existing '.$fname.' after a grade has been assigned.');
 1378:             $r->print(&done('Back',$url,$group));      
 1379:         } else {   
 1380:             $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>'.
 1381:                   '<br />To upload, rename or delete existing '.$fname.' in '.$port_path.$env{'form.currentpath'});
 1382:             $r->print(&done('Back',$url,$group));
 1383:         }
 1384:     } else {
 1385:         my $result=&Apache::lonnet::userfileupload('uploaddoc','',
 1386: 	        	 $port_path.$env{'form.currentpath'});
 1387:         if ($result !~ m|^/uploaded/|) {
 1388:             $r->print('<font color="red"> An errror occured ('.$result.
 1389: 	              ') while trying to upload '.&display_file().'</font><br />');
 1390: 	    $r->print(&done('Back',$url,$group));
 1391:         } else {
 1392:             $r->print(&done(undef,$url,$group));
 1393:         }
 1394:     }
 1395: }
 1396: sub lock_info {
 1397:     my ($r,$url,$group) = @_;
 1398:     my ($uname,$udom) = &get_name_dom($group);
 1399:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
 1400:                                                                        $uname);
 1401:     my $file_name = $env{'form.lockinfo'};
 1402:     $file_name = &prepend_group($file_name,$group);
 1403:     if (defined($file_name) && defined($$current_permissions{$file_name})) {
 1404:         foreach my $array_item (@{$$current_permissions{$file_name}}) {
 1405:             if (ref($array_item) eq 'ARRAY') {
 1406:                 my $filetext;
 1407:                 if (defined($group)) {
 1408:                     $filetext = '<strong>'.$env{'form.lockinfo'}.
 1409:                                     '</strong> (group: '.$group.')'; 
 1410:                 } else {
 1411:                     $filetext = '<strong>'.$file_name.'</strong>';
 1412:                 } 
 1413:                 $r->print(&mt('[_1] was submitted in response to problem: ',
 1414:                               $filetext).
 1415:                           '<strong>'.&Apache::lonnet::gettitle($$array_item[0]).
 1416:                           '</strong><br />');
 1417:                 my %course_description = &Apache::lonnet::coursedescription($$array_item[1]);
 1418:                 $r->print(&mt('In the course: <strong>[_1]</strong><br />',
 1419:                               $course_description{'description'}));
 1420:                 # $r->print('the third is '.$$array_item[2].'<br>');
 1421:                 # $r->print("item is $$array_item[0]<br> and $$array_item[0]");
 1422:             }
 1423:         }
 1424:     }
 1425:     $r->print(&done('Back',$url,$group));
 1426:     return 'ok';
 1427: }
 1428: sub createdir {
 1429:     my ($r,$url,$group)=@_;
 1430:     my $newdir=&Apache::lonnet::clean_filename($env{'form.newdir'});
 1431:     if ($newdir eq '') {
 1432:     	$r->print('<font color="red">'.
 1433: 	    	  &mt("Error: no directory name was provided.").
 1434: 		      '</font><br />');
 1435: 	    $r->print(&done(undef,$url,$group));
 1436: 	    return;
 1437:     }
 1438:     my $portfolio_root = &get_portfolio_root($group); 
 1439:     my @dir_list=&get_dir_list($portfolio_root,$group);
 1440:     my $found_file = 0;
 1441:     foreach my $line (@dir_list) {
 1442:         my ($filename)=split(/\&/,$line,2);
 1443:         if ($filename eq $newdir){
 1444:             $found_file = 1;
 1445:         }
 1446:     }
 1447:     if ($found_file){
 1448:     	    $r->print('<font color="red"> Unable to create a directory named <strong>'.$newdir.
 1449:     	            ' </strong>a file or directory by that name already exists.</font><br />');
 1450:     } else {
 1451:         my ($uname,$udom) = &get_name_dom($group);
 1452:         my $port_path = &get_port_path($group);
 1453:         my $result=&Apache::lonnet::mkdiruserfile($uname,$udom,
 1454: 	         $port_path.$env{'form.currentpath'}.$newdir);
 1455:         if ($result ne 'ok') {
 1456:     	    $r->print('<font color="red"> An errror occured ('.$result.
 1457: 	    	      ') while trying to create a new directory '.&display_file().'</font><br />');
 1458:         }
 1459:     }
 1460:     if ($newdir ne $env{'form.newdir'}) {
 1461:         $r->print("The new directory name was changed from:<br /><strong>".$env{'form.newdir'}."</strong> to <strong>$newdir </strong>");  
 1462:     }
 1463:     $r->print(&done(undef,$url,$group));
 1464: }
 1465: 
 1466: sub get_portfolio_root {
 1467:     my ($group) = @_;
 1468:     my ($portfolio_root,$udom,$uname,$path);
 1469:     ($uname,$udom) = &get_name_dom($group);
 1470:     if (defined($group)) {
 1471:         $path = '/userfiles/groups/'.$group.'/portfolio';
 1472:     } else {
 1473:         $path = '/userfiles/portfolio';
 1474:     }
 1475:     return (&Apache::loncommon::propath($udom,$uname).$path);
 1476: }
 1477: 
 1478: sub get_dir_list {
 1479:     my ($portfolio_root,$group) = @_;
 1480:     my ($uname,$udom) = &get_name_dom($group);
 1481:     return &Apache::lonnet::dirlist($env{'form.currentpath'},
 1482:                                           $udom,$uname,$portfolio_root);
 1483: }
 1484: 
 1485: sub get_name_dom {
 1486:     my ($group) = @_;
 1487:     my ($uname,$udom);
 1488:     if (defined($group)) {
 1489:         $udom = $env{'course.'.$env{'request.course.id'}.'.domain'};
 1490:         $uname = $env{'course.'.$env{'request.course.id'}.'.num'};
 1491:     } else {
 1492:         $udom = $env{'user.domain'};
 1493:         $uname = $env{'user.name'};
 1494:     }
 1495:     return ($uname,$udom);
 1496: }
 1497: 
 1498: sub prepend_group {
 1499:     my ($filename,$group) = @_;
 1500:     if (defined($group)) {
 1501:         $filename = $group.'/'.$filename;
 1502:     }
 1503:     return $filename;
 1504: }
 1505: 
 1506: sub get_namespace {
 1507:     my ($group) = @_;
 1508:     my $namespace = 'portfolio';
 1509:     if (defined($group)) {
 1510:         my ($uname,$udom) = &get_name_dom($group);
 1511:         $namespace .= '_'.$udom.'_'.$uname.'_'.$group;
 1512:     }
 1513:     return $namespace;
 1514: }
 1515: 
 1516: sub get_port_path {
 1517:     my ($group) = @_;
 1518:     my $port_path;
 1519:     if (defined($group)) {
 1520:        $port_path = "groups/$group/portfolio";
 1521:     } else {
 1522:        $port_path = 'portfolio';
 1523:     }
 1524:     return $port_path;
 1525: }
 1526: 
 1527: sub handler {
 1528:     # this handles file management
 1529:     my $r = shift;
 1530:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
 1531:          ['selectfile','currentpath','meta','lockinfo','currentfile','action',
 1532: 	  'fieldname','mode','rename','continue','group','access','setnum',
 1533:           'cnum','cdom','type','setroles']);
 1534:     my ($uname,$udom,$portfolio_root,$url,$group,$caller,$title);
 1535:     if ($r->uri =~ m|^(/adm/)([^/]+)|) {
 1536:         $url = $1.$2;
 1537:         $caller = $2;
 1538:     }
 1539:     if ($caller eq 'coursegrp_portfolio') {
 1540:     #  Needs to be in a course
 1541:         if (! ($env{'request.course.fn'})) {
 1542:         # Not in a course
 1543:             $env{'user.error.msg'}=
 1544:      "/adm/coursegrp_portfolio:rgf:0:0:Cannot view group portfolio";
 1545:             return HTTP_NOT_ACCEPTABLE;
 1546:         }
 1547:         my $earlyout = 0;
 1548:         my $view_permission = &Apache::lonnet::allowed('vcg',
 1549:                                                 $env{'request.course.id'});
 1550:         $group = $env{'form.group'};
 1551:         $group =~ s/\W//g;
 1552:         if ($group) {
 1553:             ($uname,$udom) = &get_name_dom($group);
 1554:             my %curr_groups = &Apache::longroup::coursegroups($udom,$uname,
 1555: 							       $group); 
 1556:             if (%curr_groups) {
 1557:                 if (($view_permission) || (&Apache::lonnet::allowed('rgf',
 1558:                                       $env{'request.course.id'}.'/'.$group))) {
 1559:                     $portfolio_root = &get_portfolio_root($group);
 1560:                 } else {
 1561:                     $r->print('You do not have the privileges required to access the shared files space for this group');
 1562:                     $earlyout = 1;
 1563:                 }
 1564:             } else {
 1565:                 $r->print('Not a valid group for this course');
 1566:                 $earlyout = 1;
 1567:             }
 1568:             $title = &mt('Group files').' for '.$group; 
 1569:         } else {
 1570:             $r->print('Invalid group');
 1571:             $earlyout = 1;
 1572:         }
 1573:         if ($earlyout) { return OK; }
 1574:     } else {
 1575:         ($uname,$udom) = &get_name_dom();
 1576:         $portfolio_root = &get_portfolio_root();
 1577:         $title = &mt('Portfolio Manager');
 1578:     }
 1579: 
 1580:     &Apache::loncommon::no_cache($r);
 1581:     &Apache::loncommon::content_type($r,'text/html');
 1582:     $r->send_http_header;
 1583:     # Give the LON-CAPA page header
 1584:     if ($env{"form.mode"} eq 'selectfile'){
 1585:         $r->print(&Apache::loncommon::start_page($title,undef,
 1586: 						 {'only_body' => 1}));
 1587:     } elsif ($env{'form.action'} eq 'rolepicker') {
 1588:         $r->print(&Apache::loncommon::start_page('New role-based condition',undef,
 1589:                                                  {'no_nav_bar'  => 1, }));
 1590:     } else {
 1591:         $r->print(&Apache::loncommon::start_page($title));
 1592:     }
 1593:     $r->rflush();
 1594: 	if (($env{'form.storeupl'}) & (!$env{'form.uploaddoc.filename'})){
 1595:    	    $r->print('<font color="red"> No file was selected to upload.'.
 1596:    	            'To upload a file, click <strong>Browse...</strong>'.
 1597:    	            ', select a file, then click <strong>Upload</strong>,</font>');
 1598: 	}
 1599:     if ($env{'form.meta'}) {
 1600:         &open_form($r,$url);
 1601: #        $r->print(&edit_meta_data($r, $env{'form.currentpath'}.$env{'form.selectfile'}));
 1602:         $r->print('Edit the meta data<br />');
 1603:         &close_form($r,$url,$group);
 1604:     }
 1605:     if ($env{'form.store'}) {
 1606:     }
 1607: 
 1608:     if ($env{'form.uploaddoc.filename'}) {
 1609: 	&upload($r,$url,$group);
 1610:     } elsif ($env{'form.action'} eq 'delete' && $env{'form.confirmed'}) {
 1611: 	&delete_confirmed($r,$url,$group);
 1612:     } elsif ($env{'form.action'} eq 'delete') {
 1613: 	&delete($r,$url,$group);
 1614:     } elsif ($env{'form.action'} eq 'deletedir' && $env{'form.confirmed'}) {
 1615: 	&delete_dir_confirmed($r,$url,$group);
 1616:     } elsif ($env{'form.action'} eq 'deletedir'){
 1617: 	&delete_dir($r,$url,$group);
 1618:     } elsif ($env{'form.action'} eq 'rename' && $env{'form.confirmed'}) {
 1619: 	&rename_confirmed($r,$url,$group);
 1620:     } elsif ($env{'form.rename'}) {
 1621:         $env{'form.selectfile'} = $env{'form.rename'};
 1622:         $env{'form.action'} = 'rename';
 1623: 	&rename($r,$url,$group);
 1624:     } elsif ($env{'form.access'}) {
 1625:         $env{'form.selectfile'} = $env{'form.access'};
 1626:         $env{'form.action'} = 'chgaccess';
 1627:         &display_access($r,$url,$group);
 1628:     } elsif ($env{'form.action'} eq 'chgaccess') {
 1629:         &update_access($r,$url,$group);
 1630:     } elsif ($env{'form.action'} eq 'rolepicker') {
 1631:         &role_options_window($r);
 1632:     } elsif ($env{'form.createdir'}) {
 1633: 	&createdir($r,$url,$group);
 1634:     } elsif ($env{'form.lockinfo'}) {
 1635:         &lock_info($r,$url,$group);
 1636:     } else {
 1637: 	my $current_path='/';
 1638: 	if ($env{'form.currentpath'}) {
 1639: 	    $current_path = $env{'form.currentpath'};
 1640: 	}
 1641:         my @dir_list=&get_dir_list($portfolio_root,$group);
 1642: 	if ($dir_list[0] eq 'no_such_dir'){
 1643: 	    # two main reasons for this:
 1644:             #    1) never been here, so directory structure not created
 1645: 	    #    2) back-button navigation after deleting a directory
 1646: 	    if ($current_path eq '/'){
 1647: 	        &Apache::lonnet::mkdiruserfile($uname,$udom,
 1648: 					       &get_port_path($group));
 1649: 	    } else {
 1650:                 # some directory that snuck in get rid of the directory
 1651:                 # from the recent pulldown, just in case
 1652: 		&Apache::lonhtmlcommon::remove_recent('portfolio',
 1653: 						      [$current_path]);
 1654: 		$current_path = '/'; # force it back to the root        
 1655: 	    }
 1656: 	    # now grab the directory list again, for the first time
 1657: 	    @dir_list=&Apache::lonnet::dirlist($current_path,
 1658: 					    $udom,$uname,$portfolio_root);
 1659:         }
 1660: 	# need to know if directory is empty so it can be removed if desired
 1661: 	my $is_empty=(@dir_list == 2);
 1662: 	&display_common($r,$url,$current_path,$is_empty,\@dir_list,$group);
 1663:         &display_directory($r,$url,$current_path,$is_empty,\@dir_list,$group);
 1664: 	$r->print(&Apache::loncommon::end_page());
 1665:     }
 1666:     return OK;
 1667: }
 1668: 1;
 1669: __END__

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