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

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

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