File:  [LON-CAPA] / loncom / interface / portfolio.pm
Revision 1.119: download - view: text, annotated - select for diffs
Thu Jun 22 20:09:51 2006 UTC (17 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- cstr space and portfoli now use same css attr to enusre similarity of display

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

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