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

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