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

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

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