Annotation of loncom/interface/portfolio.pm, revision 1.137

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

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