File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.196.2.2: download - view: text, annotated - select for diffs
Sat Jan 3 19:39:37 2009 UTC (15 years, 5 months ago) by raeburn
Branches: version_2_8_X
CVS tags: version_2_8_2, version_2_8_1, version_2_8_0, version_2_7_99_1, GCI_1
- Backport 1.204, 1.206 and part of 1.205.

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

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