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

1.3       banghart    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: 
1.1       banghart   24: package Apache::portfolio;
                     25: use strict;
                     26: use Apache::Constants qw(:common :http);
1.2       banghart   27: use Apache::loncommon;
1.1       banghart   28: use Apache::lonnet;
1.2       banghart   29: use Apache::lontexconvert;
                     30: use Apache::lonfeedback;
                     31: use Apache::lonlocal;
1.82      albertel   32: use Apache::lonnet;
1.99      raeburn    33: use Apache::longroup;
1.113     albertel   34: use HTML::Entities;
1.101     www        35: use LONCAPA;
1.16      banghart   36: 
                     37: # receives a file name and path stub from username/userfiles/portfolio/
                     38: # returns an anchor tag consisting encoding filename and currentpath
1.23      albertel   39: sub make_anchor {
1.94      raeburn    40:     my ($url, $filename, $current_path, $current_mode, $field_name,
                     41:         $continue_select,$group) = @_;
1.83      banghart   42:     if ($continue_select ne 'true') {$continue_select = 'false'};
1.113     albertel   43:     my $anchor = '<a href="'.$url.'?selectfile='.$filename.'&amp;currentpath='.$current_path.'&amp;mode='.$current_mode.'&amp;continue='.$continue_select.'&amp;fieldname='.$field_name;
1.94      raeburn    44:     if (defined($group)) {
1.113     albertel   45:         $anchor .= '&amp;group='.$group;
1.94      raeburn    46:     }
                     47:     $anchor .= '">'.$filename.'</a>';
1.8       albertel   48:     return $anchor;
1.6       banghart   49: }
1.24      albertel   50: my $dirptr=16384;
1.48      banghart   51: sub display_common {
1.94      raeburn    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:     } 
1.18      banghart   59:     my $iconpath= $r->dir_config('lonIconsURL') . "/";
1.88      albertel   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:');
1.113     albertel   66:     my $escuri = &HTML::Entities::encode($r->uri,'&<>"');
1.88      albertel   67:     $r->print(<<"TABLE"); 
1.114     albertel   68: <table id="LC_portfolio_actions">
                     69:   <tr id="LC_portfolio_upload">
                     70:     <td class="LC_label">
1.113     albertel   71:       $text{'upload_label'}
                     72:     </td>
1.114     albertel   73:     <td class="LC_value">$groupitem
1.113     albertel   74:       <form method="post" enctype="multipart/form-data" action="$escuri">
1.88      albertel   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'}" />
1.113     albertel   81:       </form>
                     82:     </td>
                     83:   </tr>
1.114     albertel   84:   <tr id="LC_portfolio_createdir">
                     85:     <td class="LC_label">
1.113     albertel   86:       $text{'createdir_label'}
                     87:     </td>
1.114     albertel   88:     <td class="LC_value">
1.113     albertel   89:       <form method="post" action="$escuri">
1.94      raeburn    90:         <input name="newdir" type="input" />$groupitem
1.88      albertel   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'}" />
1.113     albertel   96:       </form>
                     97:     </td>
                     98:   </tr>
1.88      albertel   99: </table>
                    100: TABLE
1.24      albertel  101:     my @tree = split (/\//,$current_path);
1.94      raeburn   102:     $r->print('<font size="+2">'.&make_anchor($url,$port_path,'/',$env{"form.mode"},$env{"form.fieldname"},$env{"form.continue"},$group).'/');
1.19      banghart  103:     if (@tree > 1){
                    104:         my $newCurrentPath = '';
                    105:         for (my $i = 1; $i< @tree; $i++){
                    106:             $newCurrentPath .= $tree[$i].'/';
1.94      raeburn   107:             $r->print(&make_anchor($url,$tree[$i],'/'.$newCurrentPath, $env{"form.mode"},$env{"form.fieldname"}, $env{"form.continue"},$group).'/');
1.19      banghart  108:         }
                    109:     }
                    110:     $r->print('</font>');
1.94      raeburn   111:     &Apache::lonhtmlcommon::store_recent($namespace,$current_path,$current_path);
1.113     albertel  112:     $r->print('<br /><form method="post" action="'.$url.'?mode='.$env{"form.mode"}.'&amp;fieldname='.$env{"form.fieldname"});
1.94      raeburn   113:     if (defined($group)) {
1.113     albertel  114:         $r->print('&amp;group='.$group);
1.94      raeburn   115:     }
                    116:     $r->print('">'.
                    117: 	      &Apache::lonhtmlcommon::select_recent($namespace,'currentpath',
1.22      albertel  118: 						    'this.form.submit();'));
1.21      banghart  119:     $r->print("</form>");
1.48      banghart  120: }
                    121: sub display_directory {
1.94      raeburn   122:     my ($r,$url,$current_path,$is_empty,$dir_list,$group)=@_;
1.48      banghart  123:     my $iconpath= $r->dir_config('lonIconsURL') . "/";
1.94      raeburn   124:     my ($groupitem,$groupecho);
1.48      banghart  125:     my $display_out;
1.77      banghart  126:     my $select_mode;
                    127:     my $checked_files;
1.94      raeburn   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:     }
1.102     raeburn   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;
1.82      albertel  140:     if ($env{"form.mode"} eq 'selectfile'){
1.77      banghart  141: 	&select_files($r);
1.94      raeburn   142: 	$checked_files =&Apache::lonnet::files_in_path($uname,$env{'form.currentpath'});
1.77      banghart  143: 	$select_mode = 'true';
                    144:     } 
1.45      banghart  145:     if ($is_empty && ($current_path ne '/')) {
1.94      raeburn   146:         $display_out = '<form method="post" action="'.$url.'">'.$groupitem.
1.30      banghart  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:         
1.48      banghart  153:         $r->print($display_out);
1.31      albertel  154: 	return;
                    155:     }
1.77      banghart  156:     if ($select_mode eq 'true') {
1.113     albertel  157:         $r->print('<form method="post" name="checkselect" action="'.$url.'">');
1.77      banghart  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 {
1.113     albertel  161:         $r->print('<form method="post" action="'.$url.'">');
1.77      banghart  162:         $r->print('<table border="0" cellspacing="2" cellpadding="2">'.
1.102     raeburn   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>');
1.94      raeburn   164:     }
                    165:     if (defined($group)) {
                    166:         $r->print("\n".$groupitem."\n");
1.77      banghart  167:     }
1.94      raeburn   168:     my $href_location="/uploaded/$udom/$uname/$port_path".$current_path;
                    169:     my $href_edit_location="/editupload/$udom/$uname/$port_path".$current_path;
1.105     banghart  170:     my @dir_lines;
                    171:     my %versioned;
1.26      albertel  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)) {
1.18      banghart  178:     	#$strip holds directory/file name
                    179:     	#$dom 
1.23      albertel  180:     	my ($filename,$dom,undef,$testdir,undef,undef,undef,undef,$size,undef,$mtime,undef,undef,undef,$obs,undef)=split(/\&/,$line,16); 
1.77      banghart  181:     	$filename =~ s/\s+$//;
1.105     banghart  182:     	my ($fname,$version,$extension) = &Apache::grades::file_name_version_ext($filename);
                    183:     	if ($version) {
                    184:     	    $versioned{$fname} .= $version.",";
                    185:     	}
1.106     albertel  186:         push(@dir_lines, [$filename,$dom,$testdir,$size,$mtime,$obs]);
1.105     banghart  187:     }
1.106     albertel  188:     foreach my $line (@dir_lines) {
1.105     banghart  189:         my ($filename,$dom,$testdir,$size,$mtime,$obs) = @$line;
                    190:         my ($fname,$version,$extension) = &Apache::grades::file_name_version_ext($filename);
1.93      albertel  191:     	if (($filename ne '.') && ($filename ne '..') && ($filename !~ /\.meta$/ ) && ($filename !~ /(.*)\.(\d+)\.([^\.]*)$/)) {
1.23      albertel  192:             if ($dirptr&$testdir) {
1.77      banghart  193:                 if ($select_mode eq 'true'){
1.113     albertel  194:                     $r->print('<tr bgcolor="#FFAA99"><td><img alt="'.&mt('open folder').'" src="'.$iconpath.'folder_closed.gif" /></td>');
1.64      banghart  195:                 } else {
1.113     albertel  196:                     $r->print('<tr bgcolor="#FFAA99"><td colspan="2"><img alt="'.&mt('closed folder').'" src="'.$iconpath.'folder_closed.gif" /></td>');
1.64      banghart  197:                 }
1.47      banghart  198:                 $r->print('<td>Go to ...</td>');
1.94      raeburn   199:                 $r->print('<td>'.&make_anchor($url,$filename.'/',$current_path.$filename.'/',$env{'form.mode'},$env{"form.fieldname"},$env{'form.continue'},$group).'</td>'); 
1.47      banghart  200:                 $r->print('</tr>'); 
                    201:             } else {
                    202:                 $r->print('<tr bgcolor="#CCCCFF">');
1.105     banghart  203:                 my $version_flag;
                    204:                 if (exists($versioned{$fname})) {
                    205:                    $version_flag = "*";
                    206:                 } else {
                    207:                     $version_flag = "";
                    208:                 }
                    209:                my $fullpath = $current_path.$filename;
1.103     raeburn   210:                 $fullpath = &prepend_group($fullpath,$group);
1.77      banghart  211:                 if ($select_mode eq 'true'){
1.102     raeburn   212:                     $r->print('<td><input type="checkbox" name="checkfile" value="'.$filename.'"');
1.77      banghart  213:                     if ($$checked_files{$filename} eq 'selected') {
                    214:                         $r->print("CHECKED");
                    215:                     }
                    216:                     $r->print('></td>');
                    217:                 } else {
1.103     raeburn   218:                     if (exists $locked_files{$fullpath}) {
1.94      raeburn   219:                         $r->print('<td colspan="2"><a href="'.$url.'?lockinfo='.$current_path.$filename.$groupecho.'">Locked</a></td>');
1.77      banghart  220:                     } else {
1.89      albertel  221: 			my $cat='<img alt="'.&mt('Catalog Information').
                    222: 			    '" src="'.&Apache::loncommon::lonhttpdurl('/res/adm/pages/catalog.gif').'" />';
1.77      banghart  223:                         $r->print('<td><input type="checkbox" name="selectfile" value="'.$filename.'" />
1.94      raeburn   224:                             <a href="'.$url.'?rename='.$filename.'&amp;currentpath='.$current_path.$groupecho.'">Rename</a></td>
1.89      albertel  225:                             <td><a href="'.$href_edit_location.$filename.'.meta">'.$cat.'</a>
1.77      banghart  226:                             </td>');
                    227:                     }
1.61      banghart  228:                 }
1.103     raeburn   229:                 my $curr_access;
                    230:                 my $pub_access = 0;
1.108     raeburn   231:                 my $guest_access = 0;
                    232:                 my $cond_access = 0;
1.103     raeburn   233:                 foreach my $key (sort(keys(%{$access_controls{$fullpath}}))) {
1.108     raeburn   234:                     my ($num,$scope,$end,$start) = &unpack_acc_key($key);
1.103     raeburn   235:                     if (($now > $start) && (!$end || $end > $now)) {
                    236:                         if ($scope eq 'public')  {
                    237:                             $pub_access = 1;
1.108     raeburn   238:                         } elsif ($scope eq 'guest') {
                    239:                             $guest_access = 1;
                    240:                         } else {
                    241:                             $cond_access = 1;
1.103     raeburn   242:                         }
                    243:                     }
                    244:                 }
1.108     raeburn   245:                 if (!$pub_access && !$guest_access && !$cond_access) {
                    246:                     $curr_access = &mt('Private');
1.104     raeburn   247:                 } else {
1.108     raeburn   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);
1.103     raeburn   259:                 }
1.113     albertel  260:                 $r->print('<td><img alt="" src="'.&Apache::loncommon::icon($filename).'" /></td>');
1.47      banghart  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>');
1.112     albertel  265:                 $r->print('<td><span style="white-space: nowrap">'.&mt($curr_access).'&nbsp;&nbsp;&nbsp;'.
1.102     raeburn   266:                           '<a href="'.$url.'?access='.$filename.
                    267:                           '&amp;currentpath='.$current_path.$groupecho.
1.112     albertel  268:                           '">'.&mt('View/Change').'</a></span></td>');
1.47      banghart  269:                 $r->print('</tr>'); 
                    270:             }
                    271:         }
                    272:     }
1.77      banghart  273:     if ($select_mode eq 'true') {
                    274:         $r->print('</table>
1.60      banghart  275:             <input type="hidden" name="continue" value="true">
1.82      albertel  276:             <input type="hidden" name="fieldname" value="'.$env{'form.fieldname'}.'">
1.60      banghart  277:             <input type="hidden" name="mode" value="selectfile">
                    278:             <input type="submit" name="submit" value="Select checked files, and continue selecting." /><br />
1.48      banghart  279:             <input type="button" name="doit" onClick= "finishSelect();" value="Select checked files, and close window" />
                    280:             <input type="hidden" name="currentpath" value="'.$current_path.'" />
1.77      banghart  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:     }
1.47      banghart  289: }
1.72      banghart  290: 
1.24      albertel  291: sub open_form {
1.94      raeburn   292:     my ($r,$url)=@_;
1.65      banghart  293:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
1.102     raeburn   294:     $r->print('<form name="portform" method="post" action="'.$url.'">');
1.24      albertel  295:     $r->print('<input type="hidden" name="action" value="'.
1.82      albertel  296: 	      $env{'form.action'}.'" />');
1.24      albertel  297:     $r->print('<input type="hidden" name="confirmed" value="1" />');
1.65      banghart  298:     foreach (@files) {
                    299:         $r->print('<input type="hidden" name="selectfile" value="'.
                    300: 	      $_.'" />');
                    301:     }
1.24      albertel  302:     $r->print('<input type="hidden" name="currentpath" value="'.
1.82      albertel  303: 	      $env{'form.currentpath'}.'" />');
1.24      albertel  304: }
                    305: 
                    306: sub close_form {
1.108     raeburn   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'}.'" />');
1.94      raeburn   315:     if (defined($group)) {
                    316:        $r->print("\n".'<input type="hidden" name="group" value="'.
                    317:               $group.'" />');
                    318:     }
                    319:     $r->print('</p></form>');
1.112     albertel  320:     $r->print('<form action="'.$url.'" method="post">
1.24      albertel  321:                <p>
                    322:               <input type="hidden" name="currentpath" value="'.
1.94      raeburn   323: 	      $env{'form.currentpath'}.'" />');
                    324:     if (defined($group)) {
                    325:        $r->print("\n".'<input type="hidden" name="group" value="'.
                    326:               $group.'" />');
                    327:     }
1.108     raeburn   328:     $r->print("\n".'   <input type="submit" value="'.$button_text->{'cancel'}.'" />
1.94      raeburn   329:                </p></form>'); 
1.24      albertel  330: }
                    331: 
                    332: sub display_file {
1.27      albertel  333:     my ($path,$filename)=@_;
1.65      banghart  334:     my $display_file_text;
1.82      albertel  335:     if (!defined($path)) { $path=$env{'form.currentpath'}; }
1.65      banghart  336:     if (!defined($filename)) { 
1.82      albertel  337:         $filename=$env{'form.selectfile'};
1.65      banghart  338:         $display_file_text = '<tt>'.$path.$filename.'</tt>';
                    339:     } elsif (ref($filename) eq "ARRAY") {
                    340:         foreach (@$filename) {
1.66      banghart  341:             $display_file_text .= '<tt>'.$path.$_.'</tt><br />';
1.65      banghart  342:         }
                    343:     } elsif (ref($filename) eq "SCALAR") {
                    344:         $display_file_text = '<tt>'.$path.$filename.'</tt>';        
                    345:     }
                    346:     return $display_file_text;
1.24      albertel  347: }
                    348: 
                    349: sub done {
1.94      raeburn   350:     my ($message,$url,$group)=@_;
1.76      banghart  351:     unless (defined $message) {
                    352:         $message='Done';
                    353:     }
1.94      raeburn   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;
1.24      albertel  363: }
                    364: 
                    365: sub delete {
1.94      raeburn   366:     my ($r,$url,$group)=@_;
1.55      banghart  367:     my @check;
1.82      albertel  368:     my $file_name = $env{'form.currentpath'}.$env{'form.selectfile'};
1.102     raeburn   369:     $file_name = &prepend_group($file_name,$group);
1.65      banghart  370:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
1.94      raeburn   371:     my ($uname,$udom) = &get_name_dom($group);
                    372:     if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
1.55      banghart  373:         $r->print ("The file is locked and cannot be deleted.<br />");
1.94      raeburn   374:         $r->print(&done('Back',$url,$group));
1.55      banghart  375:     } else {
1.66      banghart  376:         if (scalar(@files)) {
1.94      raeburn   377:             &open_form($r,$url);
1.66      banghart  378:             $r->print('<p>'.&mt('Delete').' '.&display_file(undef,\@files).'?</p>');
1.94      raeburn   379:             &close_form($r,$url,$group);
1.66      banghart  380:         } else {
                    381:             $r->print("No file was checked to delete.<br />");
1.94      raeburn   382:             $r->print(&done(undef,$url,$group));
1.66      banghart  383:         }
1.55      banghart  384:     }
1.24      albertel  385: } 
                    386: 
                    387: sub delete_confirmed {
1.94      raeburn   388:     my ($r,$url,$group)=@_;
1.65      banghart  389:     my @files=&Apache::loncommon::get_env_multiple('form.selectfile');
                    390:     my $result;
1.94      raeburn   391:     my ($uname,$udom) = &get_name_dom($group);
                    392:     my $port_path = &get_port_path($group);
1.65      banghart  393:     foreach my $delete_file (@files) {
1.94      raeburn   394:         $result=&Apache::lonnet::removeuserfile($uname,$udom,$port_path.
1.82      albertel  395: 					       $env{'form.currentpath'}.
1.65      banghart  396: 					       $delete_file);
                    397:         if ($result ne 'ok') {
1.30      banghart  398: 	$r->print('<font color="red"> An error occured ('.$result.
1.65      banghart  399: 		  ') while trying to delete '.&display_file(undef, $delete_file).'</font><br />');
                    400:         }
1.24      albertel  401:     }
1.94      raeburn   402:     $r->print(&done(undef,$url,$group));
1.24      albertel  403: }
                    404: 
1.30      banghart  405: sub delete_dir {
1.94      raeburn   406:     my ($r,$url,$group)=@_;
                    407:     &open_form($r,$url);
1.30      banghart  408:     $r->print('<p>'.&mt('Delete').' '.&display_file().'?</p>');
1.94      raeburn   409:     &close_form($r,$url,$group);
1.30      banghart  410: } 
                    411: 
                    412: sub delete_dir_confirmed {
1.94      raeburn   413:     my ($r,$url,$group)=@_;
1.82      albertel  414:     my $directory_name = $env{'form.currentpath'};
1.81      albertel  415:     $directory_name =~ s|/$||; # remove any trailing slash
1.94      raeburn   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.
1.30      banghart  420: 					       $directory_name);
1.32      banghart  421: 					       
1.30      banghart  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 />');
1.32      banghart  425:     } else {
1.41      banghart  426:         # now remove from recent
                    427: #        $r->print('<br /> removing '.$directory_name.'<br /');
1.94      raeburn   428:         &Apache::lonhtmlcommon::remove_recent($namespace,[$directory_name.'/']);
1.32      banghart  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:         }
1.82      albertel  436:         $env{'form.currentpath'} = $directory_name;
1.30      banghart  437:     }
1.94      raeburn   438:     $r->print(&done(undef,$url,$group));
1.30      banghart  439: }
                    440: 
1.24      albertel  441: sub rename {
1.94      raeburn   442:     my ($r,$url,$group)=@_;
1.82      albertel  443:     my $file_name = $env{'form.currentpath'}.$env{'form.rename'};
1.94      raeburn   444:     my ($uname,$udom) = &get_name_dom($group);
1.102     raeburn   445:     $file_name = &prepend_group($file_name,$group);
1.94      raeburn   446:     if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
1.55      banghart  447:         $r->print ("The file is locked and cannot be renamed.<br />");
1.94      raeburn   448:         $r->print(&done(undef,$url,$group));
1.55      banghart  449:     } else {
1.94      raeburn   450:         &open_form($r,$url);
1.55      banghart  451:         $r->print('<p>'.&mt('Rename').' '.&display_file().' to 
                    452:                    <input name="filenewname" type="input" size="50" />?</p>');
1.94      raeburn   453:         &close_form($r,$url,$group);
1.55      banghart  454:     }
1.24      albertel  455: }
                    456: 
                    457: sub rename_confirmed {
1.94      raeburn   458:     my ($r,$url,$group)=@_;
1.82      albertel  459:     my $filenewname=&Apache::lonnet::clean_filename($env{'form.filenewname'});
1.94      raeburn   460:     my ($uname,$udom) = &get_name_dom($group);
                    461:     my $port_path = &get_port_path($group);
1.27      albertel  462:     if ($filenewname eq '') {
                    463: 	$r->print('<font color="red">'.
                    464: 		  &mt("Error: no valid filename was provided to rename to.").
                    465: 		  '</font><br />');
1.94      raeburn   466: 	$r->print(&done(undef,$url,$group));
1.27      albertel  467: 	return;
                    468:     } 
                    469:     my $result=
1.94      raeburn   470: 	&Apache::lonnet::renameuserfile($uname,$udom,
                    471:             $port_path.$env{'form.currentpath'}.$env{'form.selectfile'},
                    472:             $port_path.$env{'form.currentpath'}.$filenewname);
1.27      albertel  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:     }
1.82      albertel  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>");
1.66      banghart  480:     }
1.94      raeburn   481:     $r->print(&done(undef,$url,$group));
1.27      albertel  482: }
1.102     raeburn   483: 
1.104     raeburn   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");
1.108     raeburn   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>');
1.104     raeburn   495:     &access_setting_table($r,$access_controls{$file_name});
1.108     raeburn   496:     my $button_text = {
                    497:                         'continue' => &mt('Proceed'),
                    498:                         'cancel' => &mt('Back to directory listing'),
                    499:                       };
                    500:     &close_form($r,$url,$group,$button_text);
1.104     raeburn   501: }
                    502: 
                    503: sub update_access {
                    504:     my ($r,$url,$group) = @_;
1.108     raeburn   505:     my $function = &Apache::loncommon::get_users_function();
                    506:     my $tablecolor = &Apache::loncommon::designparm($function.'.tabbg');
1.104     raeburn   507:     my $totalprocessed = 0;
                    508:     my %processing;
                    509:     my %title  = (
1.108     raeburn   510:                          'activate' => 'New control(s) added',
                    511:                          'delete'   => 'Existing control(s) deleted',
                    512:                          'update'   => 'Existing control(s) modified',
1.104     raeburn   513:                      );
1.108     raeburn   514:     my $changes;
1.104     raeburn   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} = 
1.108     raeburn   526:                             &build_access_record($num,$scope,$start,$end,$chg);
1.104     raeburn   527:             }
                    528:         }
                    529:     }
                    530:     my $file_name = $env{'form.currentpath'}.$env{'form.selectfile'};
1.108     raeburn   531:     $r->print('<h3>'.&mt('Allowing others to retrieve portfolio file: [_1]',
                    532:               $file_name).'</h3>'."\n");
1.104     raeburn   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) =
1.108     raeburn   538:         &Apache::lonnet::modify_access_controls($file_name,$changes,$udom,
                    539:                                                 $uname);
1.104     raeburn   540:     }
1.108     raeburn   541:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
                    542:                                                                        $uname);
1.109     albertel  543:     my %access_controls = 
                    544: 	&Apache::lonnet::get_access_controls($current_permissions,
                    545: 					     $group,$file_name);
1.104     raeburn   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());
1.110     albertel  551:             $r->print(&Apache::loncommon::start_data_table_header_row());
1.108     raeburn   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>');
1.110     albertel  555:             $r->print(&Apache::loncommon::end_data_table_header_row());
1.104     raeburn   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());
1.108     raeburn   566:                     $r->print('<td rowspan="'.$numchgs.'">'.&mt($title{$chg}).
                    567:                               '.</td>');
1.104     raeburn   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:                         }
1.108     raeburn   573:                         my ($num,$scope,$end,$start) = &unpack_acc_key($key); 
1.104     raeburn   574:                         my $newkey = $key;
                    575:                         if ($chg eq 'activate') {
                    576:                             $newkey =~ s/^(\d+)/$$translation{$1}/;
                    577:                         }
1.109     albertel  578:                         my $content = $$updated_controls{$newkey};
1.104     raeburn   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:                         }
1.108     raeburn   590:                         $r->print('<td>'.&mt($scope));
                    591:                         if (($scope eq 'course') || ($scope eq 'group')) {
                    592:                             if ($chg ne 'delete') {
1.109     albertel  593:                                 my $cid = $content->{'domain'}.'_'.$content->{'number'};
1.108     raeburn   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') {
1.109     albertel  602:                                 $r->print(&mt('Password').': '.$content->{'password'});
1.108     raeburn   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>');
1.109     albertel  615:                                 foreach my $id (sort(keys(%{$content->{'roles'}}))) {
1.108     raeburn   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;  
1.109     albertel  623:                                             foreach my $role (@{$content->{'roles'}{$id}{$item}}) {
1.108     raeburn   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 {
1.109     albertel  635:                                             $r->print(join(',',@{$content->{'roles'}{$id}{$item}}));
1.108     raeburn   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') {
1.109     albertel  643:                                 $r->print(&mt('Domains: ').join(',',@{$content->{'dom'}}));
1.108     raeburn   644:                             } elsif ($scope eq 'users') {
1.109     albertel  645:                                 my $curr_user_list = &sort_users($content->{'users'});
1.108     raeburn   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>');
1.104     raeburn   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:     }
1.108     raeburn   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'}.
1.113     albertel  697:                   '&amp;currentpath='.$env{'form.currentpath'}.'">'.
1.108     raeburn   698:                    &mt('Display all access settings for this file').'</a>');
                    699:     }
1.104     raeburn   700:     return;
                    701: }
                    702: 
                    703: sub build_access_record {
1.108     raeburn   704:     my ($num,$scope,$start,$end,$chg) = @_;
1.109     albertel  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'};
1.108     raeburn   715:     } elsif (($scope eq 'course') || ($scope eq 'group')) {
1.109     albertel  716:         $record->{'domain'} = $env{'form.crsdom_'.$num};
                    717: 	$record->{'number'} = $env{'form.crsnum_'.$num};
1.108     raeburn   718:         my @role_ids;
1.109     albertel  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: 
1.108     raeburn   739:         foreach my $id (@role_ids) {
                    740:             my (@roles,@accesses,@sections,@groups);
                    741:             if (($id == $next_id) && ($chg eq 'update')) {
1.109     albertel  742:                 @roles    = split(/,/,$env{'form.role_'.$num.'_'.$next_id});
1.108     raeburn   743:                 @accesses = split(/,/,$env{'form.access_'.$num.'_'.$next_id});
                    744:                 @sections = split(/,/,$env{'form.section_'.$num.'_'.$next_id});
1.109     albertel  745:                 @groups   = split(/,/,$env{'form.group_'.$num.'_'.$next_id});
1.108     raeburn   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:             }
1.109     albertel  752: 	    $record->{'roles'}{$id}{'role'}    = \@roles;
                    753: 	    $record->{'roles'}{$id}{'access'}  = \@accesses;
                    754: 	    $record->{'roles'}{$id}{'section'} = \@sections;
                    755: 	    $record->{'roles'}{$id}{'group'}   = \@groups;
1.108     raeburn   756:         }
                    757:     } elsif ($scope eq 'domains') {
                    758:         my @doms = &Apache::loncommon::get_env_multiple('form.dom_'.$num);
1.109     albertel  759: 	$record->{'dom'} = \@doms;
1.108     raeburn   760:     } elsif ($scope eq 'users') {
                    761:         my $userlist = $env{'form.users_'.$num};
1.109     albertel  762:         $userlist =~ s/\s+//sg;
                    763: 	my %userhash = map { ($_,1) } (split(/,/,$userlist));
                    764:         foreach my $user (keys(%userhash)) {
1.108     raeburn   765:             my ($uname,$udom) = split(/:/,$user);
1.109     albertel  766: 	    push(@{$record->{'users'}}, {
                    767: 		'uname' => $uname,
                    768: 		'udom'  => $udom
                    769: 		});
                    770: 	}
1.108     raeburn   771:     }
1.104     raeburn   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: 
1.108     raeburn   787: sub sort_users {
1.109     albertel  788:     my ($users) = @_; 
                    789:     my @curr_users = map {
                    790: 	$_->{'uname'}.':'.$_->{'udom'}
                    791:     } (@{$users});
                    792:     my $curr_user_list = join(",\n",sort(@curr_users));
1.108     raeburn   793:     return $curr_user_list;
                    794: }
                    795: 
1.104     raeburn   796: sub access_setting_table {
                    797:     my ($r,$access_controls) = @_;
                    798:     my ($public,$publictext);
1.108     raeburn   799:     $publictext = &mt('Off');
1.104     raeburn   800:     my ($guest,$guesttext);
1.108     raeburn   801:     $guesttext = &mt('Off');
1.104     raeburn   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.
1.108     raeburn   808:     my ($num,$scope,$publicnum,$guestnum);
                    809:     my (%acl_count,%end,%start);
1.104     raeburn   810:     foreach my $key (sort(keys(%{$access_controls}))) {
1.108     raeburn   811:         ($num,$scope,$end{$key},$start{$key}) = &unpack_acc_key($key);
1.104     raeburn   812:         if ($scope eq 'public') {
                    813:             $public = $key;
1.108     raeburn   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);
1.104     raeburn   828:         }
1.108     raeburn   829:         $acl_count{$scope} ++;
1.104     raeburn   830:     }
1.108     raeburn   831:     $r->print('<table border="0"><tr><td valign="top">');
                    832:     $r->print('<h3>'.&mt('Public access:').' '.$publictext.'</h3>');
1.104     raeburn   833:     $r->print(&Apache::loncommon::start_data_table());
1.110     albertel  834:     $r->print(&Apache::loncommon::start_data_table_header_row());
1.108     raeburn   835:     $r->print('<th>'.&mt('Action').'</th><th>'.&mt('Dates available').'</th>');
1.110     albertel  836:     $r->print(&Apache::loncommon::end_data_table_header_row());
1.104     raeburn   837:     $r->print(&Apache::loncommon::start_data_table_row());
1.108     raeburn   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>');
1.104     raeburn   849:     $r->print(&Apache::loncommon::start_data_table());
1.110     albertel  850:     $r->print(&Apache::loncommon::start_data_table_header_row());
1.108     raeburn   851:     $r->print('<th>'.&mt('Action').'</th><th>'.&mt('Dates available').
                    852:               '</th><th>'. &mt('Password').'</th>');
1.110     albertel  853:     $r->print(&Apache::loncommon::end_data_table_header_row());
1.108     raeburn   854:     $r->print(&Apache::loncommon::start_data_table_row());
                    855:     my $passwd;
                    856:     if ($guest) {
1.109     albertel  857:         $passwd = $$access_controls{$guest}{'password'};
1.108     raeburn   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>');
1.104     raeburn   863:     }
1.108     raeburn   864:     $r->print('<td><input type="text" size="15" name="password" value="'.
                    865:               $passwd.'" /></td>');
1.104     raeburn   866:     $r->print(&Apache::loncommon::end_data_table_row());
                    867:     $r->print(&Apache::loncommon::end_data_table());
1.108     raeburn   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);
1.104     raeburn   879:     $r->print('</td>');
1.108     raeburn   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());
1.110     albertel  930:         $r->print(&Apache::loncommon::start_data_table_header_row());
                    931:         $r->print('<th>'.&mt('Action?').'</th><th>'.&mt($uctype).'</th><th>'.
1.108     raeburn   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:         }
1.110     albertel  942:         $r->print(&Apache::loncommon::end_data_table_header_row());
                    943: 	$r->print(&Apache::loncommon::start_data_table_row());
1.108     raeburn   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') {
1.111     albertel  957: 	    $r->print(&Apache::loncommon::start_data_table_row());
1.108     raeburn   958:             $r->print('<td colspan="',$colspan.'">'.&additional_item($type).
                    959:                       '</td>');
1.111     albertel  960: 	    $r->print(&Apache::loncommon::end_data_table_row());
1.108     raeburn   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: 
1.110     albertel  970: sub course_js {
                    971:     return qq|
1.108     raeburn   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>
1.110     albertel 1010: |;
                   1011: }
                   1012: 
                   1013: sub course_row {
                   1014:     my ($r,$status,$type,$item,$access_controls,$tablecolor,$now,$then) = @_;
1.115   ! raeburn  1015:     my $content;
1.110     albertel 1016:     my $defdom = $env{'user.domain'};
                   1017:     if ($status eq 'old') {
1.115   ! raeburn  1018:         $content = $$access_controls{$item}; 
        !          1019:         $defdom =  $content->{'domain'};
1.110     albertel 1020:     }
                   1021:     my $js = &Apache::loncommon::coursebrowser_javascript($defdom)
                   1022: 	.&course_js();
1.108     raeburn  1023:     my $crsgrptext = 'Groups';
                   1024:     if ($type eq 'group') {
                   1025:         $crsgrptext = 'Teams';
                   1026:     }
                   1027:     my ($num,$scope,$end,$start) = &set_identifiers($status,$item,$now,$then,
                   1028:                                                     $type);
1.110     albertel 1029:     $r->print('<td>'.$js.&actionbox($status,$num,$scope).'</td>');
1.108     raeburn  1030:     if ($status eq 'old') {
1.115   ! raeburn  1031:         my $cid = $content->{'domain'}.'_'.$content->{'number'};
1.108     raeburn  1032:         my %course_description = &Apache::lonnet::coursedescription($cid);
1.115   ! raeburn  1033:         $r->print('<td><input type="hidden" name="crsdom_'.$num.'" value="'.$content->{'domain'}.'" /><input type="hidden" name="crsnum_'.$num.'" value="'.$content->{'number'}.'" />'.$course_description{'description'}.'</td>');
1.108     raeburn  1034:     } elsif ($status eq 'new') {
                   1035:         my $uctype = $type;
                   1036:         $uctype =~ s/^(\w)/uc($1)/e;
                   1037:         $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>');
                   1038:     }
                   1039:     $r->print('<td>'.&dateboxes($num,$start,$end).'</td>');
                   1040:     $r->print('<td><table border="0"><tr bgcolor="'.$tablecolor.'">');
                   1041:     $r->print('<th>'.&mt('Action').'</th><th>'.&mt('Roles').'</th><th>'.
                   1042:               &mt('Access').'</th><th>'.&mt('Sections').'</th><th>'.
                   1043:               &mt($crsgrptext).'</th></tr>');
                   1044:     if ($status eq 'old') {
                   1045:         my $max_id = 0;
1.115   ! raeburn  1046:         foreach my $role_id (sort(keys(%{$content->{'roles'}}))) {
1.108     raeburn  1047:             if ($role_id > $max_id) {
                   1048:                 $max_id = $role_id;
                   1049:             }
                   1050:             $max_id ++;
1.115   ! raeburn  1051:             my $role_selects = &role_selectors($num,$role_id,$status,$type,$content,'display');
1.112     albertel 1052:             $r->print('<tr><td><span style="white-space: nowrap"><label><input type="checkbox" name="delete_role_'.$num.'" value="'.$role_id.'" />'.&mt('Delete').'</label></span><br /><input type="hidden" name="preserve_role_'.$num.'" value="'.$role_id.'" /></td>'.$role_selects.'</tr>');
1.108     raeburn  1053:         }
1.115   ! raeburn  1054:         $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>');
1.108     raeburn  1055:     } elsif ($status eq 'new') {
                   1056:         my $role_id = 1;
                   1057:         my $role_selects = &role_selectors($num,$role_id,$status,$type,undef,'display');
                   1058:         $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);
                   1059:         $r->print('</tr></table></td>');
                   1060:     }
                   1061:     return;
                   1062: }
                   1063: 
                   1064: sub domains_row {
                   1065:     my ($r,$status,$item,$all_doms,$access_controls,$now,$then) = @_;
                   1066:     my ($num,$scope,$end,$start) = &set_identifiers($status,$item,$now,$then,
                   1067:                                                     'domains');
1.112     albertel 1068:     my $dom_select = '<select name="dom_'.$num.'" size="4" multiple="true">'.
1.108     raeburn  1069:                      ' <option value="">'.&mt('Please select').'</option>';
                   1070:     if ($status eq 'old') {
1.109     albertel 1071:         my $content =  $$access_controls{$item};
                   1072: 	foreach my $dom (@{$all_doms}) {
                   1073:             if ((@{$content->{'dom'}} > 0) 
                   1074: 		&& (grep(/^\Q$dom\E$/,@{$content->{'dom'}}))) {
1.108     raeburn  1075:                 $dom_select .= '<option value="'.$dom.'" selected>'.
                   1076:                                $dom.'</option>';
                   1077:             } else {
                   1078:                 $dom_select .= '<option value="'.$dom.'">'.$dom.'</option>';
                   1079:             }
                   1080:         }
                   1081:     } else {
                   1082:         foreach my $dom (@{$all_doms}) {
                   1083:             $dom_select .= '<option value="'.$dom.'">'.$dom.'</option>';
                   1084:         }
                   1085:     }
1.112     albertel 1086:     $dom_select .= '</select>';
1.108     raeburn  1087:     $r->print('<td>'.&actionbox($status,$num,$scope).'</td><td>'.$dom_select.
                   1088:               '</td><td>'.&dateboxes($num,$start,$end).'</td>');
                   1089: }
                   1090: 
                   1091: sub users_row {
                   1092:     my ($r,$status,$item,$access_controls,$now,$then) = @_;
                   1093:     my ($num,$scope,$end,$start) = &set_identifiers($status,$item,$now,$then,
                   1094:                                                     'users');
                   1095:     my $curr_user_list;
                   1096:     if ($status eq 'old') {
1.109     albertel 1097:         my $content = $$access_controls{$item};
                   1098:         $curr_user_list = &sort_users($content->{'users'});
1.108     raeburn  1099:     }
                   1100:     $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>');
                   1101: }
                   1102: 
                   1103: sub additional_item {
                   1104:     my ($type) = @_;
                   1105:     my $output = &mt('Add new [_1] condition(s)?',$type).'&nbsp;'.&mt('Number to add: ').'<input type="text" name="new'.$type.'" size="3" value="0" />';
                   1106:     return $output;
                   1107: }
                   1108: 
                   1109: sub actionbox {
                   1110:     my ($status,$num,$scope) = @_;
1.112     albertel 1111:     my $output = '<span style="white-space: nowrap"><label>';
1.108     raeburn  1112:     if ($status eq 'new') {
                   1113:         $output .= '<input type="checkbox" name="activate" value="'.$num.'" />'.
                   1114:         &mt('Activate');
                   1115:     } else {
                   1116:         $output .= '<input type="checkbox" name="delete" value="'.$num.
1.112     albertel 1117:                    '" />'.&mt('Delete').'</label></span><br /><span style="white-space: nowrap">'.
1.108     raeburn  1118:                    '<label><input type="checkbox" name="update" value="'.
                   1119:                    $num.'" />'.&mt('Update');
                   1120:     }
1.112     albertel 1121:     $output .= '</label></span><input type="hidden" name="scope_'.$num.                '" value="'.$scope.'" />';
1.108     raeburn  1122:     return $output;
                   1123: }
                   1124:                                                                                    
                   1125: sub dateboxes {
                   1126:     my ($num,$start,$end) = @_;
                   1127:     my $noend;
                   1128:     if ($end == 0) {
                   1129:         $noend = 'checked="checked"';
                   1130:     }
                   1131:     my $startdate = &Apache::lonhtmlcommon::date_setter('portform',
                   1132:                            'startdate_'.$num,$start,undef,undef,undef,1,undef,
                   1133:                             undef,undef,1);
                   1134:     my $enddate = &Apache::lonhtmlcommon::date_setter('portform',
                   1135:                                'enddate_'.$num,$end,undef,undef,undef,1,undef,
1.112     albertel 1136:                                 undef,undef,1). '&nbsp;&nbsp;<span style="white-space: nowrap"><label>'.
1.108     raeburn  1137:                                 '<input type="checkbox" name="noend_'.
                   1138:                                 $num.'" '.$noend.' />'.&mt('No end date').
1.112     albertel 1139:                                 '</label></span>';
1.108     raeburn  1140:                                                                                    
                   1141:     my $output = &mt('Start: ').$startdate.'<br />'.&mt('End: ').$enddate;
                   1142:     return $output;
                   1143: }
                   1144: 
                   1145: sub unpack_acc_key {
                   1146:     my ($acc_key) = @_;
                   1147:     my ($num,$scope,$end,$start) = ($acc_key =~ /^([^:]+):([a-z]+)_(\d*)_?(\d*)$/);
                   1148:     return ($num,$scope,$end,$start);
                   1149: }
                   1150: 
                   1151: sub set_identifiers {
                   1152:     my ($status,$item,$now,$then,$scope) = @_;
                   1153:     if ($status eq 'old') {
                   1154:         return(&unpack_acc_key($item));
                   1155:     } else {
                   1156:         return($item,$scope,$then,$now);
                   1157:     }
                   1158: } 
                   1159: 
                   1160: sub role_selectors {
                   1161:     my ($num,$role_id,$status,$type,$content,$caller) = @_;
                   1162:     my ($output,$cdom,$cnum,$longid);
                   1163:     if ($caller eq 'display') {
                   1164:         $longid = '_'.$num.'_'.$role_id;
                   1165:         if ($status eq 'new') {
                   1166:             foreach my $item ('role','access','section','group') {
                   1167:                 $output .= '<td><select name="'.$item.$longid.'">'.
                   1168:                            '<option value="">'.&mt('Pick [_1] first',$type).
                   1169:                            '</option></select></td>';
                   1170:             }
                   1171:             return $output;
                   1172:         } else {
                   1173:             $cdom = $$content{'domain'};
                   1174:             $cnum = $$content{'number'};
                   1175:         }
                   1176:     } elsif ($caller eq 'rolepicker') {
                   1177:          $cdom = $env{'form.cdom'};
                   1178:          $cnum = $env{'form.cnum'};
                   1179:     }
                   1180:     my ($sections,$groups,$allroles,$rolehash,$accesshash) =
                   1181:             &Apache::loncommon::get_secgrprole_info($cdom,$cnum,1,$type);
                   1182:     if (!@{$sections}) {
                   1183:         @{$sections} = ('none');
                   1184:     } else {
                   1185:         unshift(@{$sections},('all','none'));
                   1186:     }
                   1187:     if (!@{$groups}) {
                   1188:         @{$groups} = ('none');
                   1189:     } else {
                   1190:         unshift(@{$groups},('all','none'));
                   1191:     }
                   1192:     my @allacesses = sort(keys(%{$accesshash}));
                   1193:     my (%sectionhash,%grouphash);
                   1194:     foreach my $sec (@{$sections}) {
                   1195:         $sectionhash{$sec} = $sec;
                   1196:     }
                   1197:     foreach my $grp (@{$groups}) {
                   1198:         $grouphash{$grp} = $grp;
                   1199:     }
                   1200:     my %lookup = (
                   1201:                    'role' => $rolehash,
                   1202:                    'access' => $accesshash,
                   1203:                    'section' => \%sectionhash,
                   1204:                    'group' => \%grouphash,
                   1205:                  );
                   1206:     my @allaccesses = sort(keys(%{$accesshash}));
                   1207:     my %allitems = (
                   1208:                     'role' => $allroles,
                   1209:                     'access' => \@allaccesses,
                   1210:                     'section' => $sections,
                   1211:                     'group' => $groups, 
                   1212:                    );
                   1213:     foreach my $item ('role','access','section','group') {
                   1214:         $output .= '<td><select name="'.$item.$longid.'" multiple="true" size="4">'."\n";
                   1215:         foreach my $entry (@{$allitems{$item}}) {
                   1216:             if ($caller eq 'display') {
                   1217:                 if ((@{$$content{'roles'}{$role_id}{$item}} > 0) && 
                   1218:                     (grep(/^\Q$entry\E$/,@{$$content{'roles'}{$role_id}{$item}}))) {
                   1219:                     $output .= '  <option value="'.$entry.'" selected>'.
                   1220:                                   $lookup{$item}{$entry}.'</option>';
                   1221:                     next;
                   1222:                 }
                   1223:             }
                   1224:             $output .= '  <option value="'.$entry.'">'.
                   1225:                        $lookup{$item}{$entry}.'</option>';
                   1226:         }
                   1227:         $output .= '</select>';
                   1228:     }
                   1229:     $output .= '</td>';
                   1230:     return $output;
                   1231: }
                   1232: 
                   1233: sub role_options_window {
                   1234:     my ($r) = @_;
                   1235:     my $cdom = $env{'form.cdom'};
                   1236:     my $cnum = $env{'form.cnum'};
                   1237:     my $type = $env{'form.type'};
                   1238:     my $addindex = $env{'form.setroles'};
                   1239:     my $role_selects = &role_selectors(1,1,'new',$type,undef,'rolepicker');
                   1240:     $r->print(<<"END_SCRIPT");
                   1241: <script type="text/javascript">
                   1242: function setRoles() {
                   1243:     var addidx = $addindex+1;
                   1244:     for (var i=0; i<4; i++) {
                   1245:         var copylist = '';
                   1246:         for (var j=0; j<document.rolepicker.elements[i].length; j++) {
                   1247:             if (document.rolepicker.elements[i].options[j].selected) {
                   1248:                 copylist = copylist + document.rolepicker.elements[i].options[j].value + ',';
                   1249:             }
                   1250:         }
                   1251:         copylist = copylist.substr(0,copylist.length-1);
                   1252:         opener.document.portform.elements[addidx+i].value = copylist;
                   1253:     }
                   1254:     self.close();
                   1255: }
                   1256: </script>
                   1257: END_SCRIPT
                   1258:     $r->print(&mt('Select roles, course status, section(s) and group(s) for users who will be able to access the portfolio file.'));
                   1259:     $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()" />');
                   1260:     return;
1.104     raeburn  1261: }
                   1262: 
1.47      banghart 1263: sub select_files {
1.104     raeburn  1264:     my ($r,$group) = @_;
1.82      albertel 1265:     if ($env{'form.continue'} eq 'true') {
1.60      banghart 1266:         # here we update the selections for the currentpath
                   1267:         # eventually, have to handle removing those not checked, but . . . 
1.83      banghart 1268:         my @items=&Apache::loncommon::get_env_multiple('form.checkfile');
                   1269:         if (scalar(@items)){
1.85      banghart 1270:              &Apache::lonnet::save_selected_files($env{'user.name'}, $env{'form.currentpath'}, @items);
1.83      banghart 1271:         }
1.62      banghart 1272:     } else {
                   1273:             #empty the file for a fresh start
1.83      banghart 1274:             &Apache::lonnet::clear_selected_files($env{'user.name'});
1.62      banghart 1275:     }
1.82      albertel 1276:     my @files = &Apache::lonnet::files_not_in_path($env{'user.name'}, $env{'form.currentpath'});
1.62      banghart 1277:     my $java_files = join ",", @files;
                   1278:     if ($java_files) {
                   1279:         $java_files.=',';
1.60      banghart 1280:     }
1.63      banghart 1281:     my $javascript =(<<ENDSMP);
1.113     albertel 1282:         <script type="text/javascript">
1.48      banghart 1283:         function finishSelect() {
1.62      banghart 1284: ENDSMP
1.63      banghart 1285:     $javascript .= 'fileList = "'.$java_files.'";';
                   1286:     $javascript .= (<<ENDSMP);
1.49      banghart 1287:             for (i=0;i<document.forms.checkselect.length;i++) { 
                   1288:                 if (document.forms.checkselect[i].checked){
1.54      banghart 1289:                     fileList = fileList + document.forms.checkselect.currentpath.value + document.forms.checkselect[i].value + "," ;
1.49      banghart 1290:                 }
                   1291:             }
                   1292:             opener.document.forms.lonhomework.
                   1293: ENDSMP
1.82      albertel 1294:     $javascript .= $env{'form.fieldname'};
1.63      banghart 1295:     $javascript .= (<<ENDSMP);
1.49      banghart 1296:         .value=fileList;
1.48      banghart 1297:             self.close();
                   1298:         }
                   1299:         </script>
                   1300: ENDSMP
1.63      banghart 1301:     $r->print($javascript);
1.47      banghart 1302:     $r->print("<h1>Select portfolio files</h1>
1.88      albertel 1303:                 Check as many as you wish in response to the problem.<br />");
                   1304:     my @otherfiles=&Apache::lonnet::files_not_in_path($env{'user.name'}, $env{'form.currentpath'});
                   1305:     if (@otherfiles) {
                   1306: 	$r->print("<strong>Files selected from other directories:</strong><br />");
                   1307: 	foreach my $file (@otherfiles) {
                   1308: 	    $r->print($file."<br />");
                   1309: 	}
1.60      banghart 1310:     }
1.47      banghart 1311: }
1.24      albertel 1312: sub upload {
1.94      raeburn  1313:     my ($r,$url,$group)=@_;
1.82      albertel 1314:     my $fname=$env{'form.uploaddoc.filename'};
                   1315:     my $filesize = (length($env{'form.uploaddoc'})) / 1000; #express in k (1024?)
1.38      banghart 1316:     my $disk_quota = 20000; # expressed in k
1.34      banghart 1317:     $fname=&Apache::lonnet::clean_filename($fname);
1.94      raeburn  1318: 
                   1319:     my $portfolio_root=&get_portfolio_root($group);
                   1320:     my ($uname,$udom) = &get_name_dom($group);
                   1321:     my $port_path = &get_port_path($group);
1.38      banghart 1322:     # Fixme --- Move the checking for existing file to LOND error return
1.94      raeburn  1323:     my @dir_list=&get_dir_list($portfolio_root,$group);
1.34      banghart 1324:     my $found_file = 0;
1.76      banghart 1325:     my $locked_file = 0;
1.33      banghart 1326:     foreach my $line (@dir_list) {
1.76      banghart 1327:         my ($file_name)=split(/\&/,$line,2);
                   1328:         if ($file_name eq $fname){
1.102     raeburn  1329:             $file_name = $env{'form.currentpath'}.$file_name;
                   1330:             $file_name = &prepend_group($file_name,$group);
1.33      banghart 1331:             $found_file = 1;
1.102     raeburn  1332:             if (defined($group)) {
                   1333:                 $file_name = $group.'/'.$file_name;
                   1334:             }
                   1335:             if (&Apache::lonnet::is_locked($file_name,$udom,$uname) eq 'true') {
1.76      banghart 1336:                 $locked_file = 1;
                   1337:             } 
1.33      banghart 1338:         }
                   1339:     }
1.94      raeburn  1340:     my $current_disk_usage = &Apache::lonnet::diskusage($udom,$uname,$portfolio_root);
1.87      albertel 1341:     if (($current_disk_usage + $filesize) > $disk_quota){
1.86      albertel 1342:         $r->print('<font color="red">Unable to upload <strong>'.$fname.' (size = '.$filesize.' kilobytes)</strong>. Disk quota will be exceeded.'.
1.38      banghart 1343:                   '<br />Disk quota is '.$disk_quota.' kilobytes. Your current disk usage is '.$current_disk_usage.' kilobytes.');
1.94      raeburn  1344:         $r->print(&done('Back',$url,$group));
1.38      banghart 1345:     } 
1.76      banghart 1346:     elsif ($found_file){
                   1347:         if ($locked_file){
1.94      raeburn  1348:             $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>'.
1.76      banghart 1349:                   '<br />You will be able to rename or delete existing '.$fname.' after a grade has been assigned.');
1.94      raeburn  1350:             $r->print(&done('Back',$url,$group));      
1.76      banghart 1351:         } else {   
1.94      raeburn  1352:             $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>'.
                   1353:                   '<br />To upload, rename or delete existing '.$fname.' in '.$port_path.$env{'form.currentpath'});
                   1354:             $r->print(&done('Back',$url,$group));
1.76      banghart 1355:         }
1.33      banghart 1356:     } else {
                   1357:         my $result=&Apache::lonnet::userfileupload('uploaddoc','',
1.94      raeburn  1358: 	        	 $port_path.$env{'form.currentpath'});
1.33      banghart 1359:         if ($result !~ m|^/uploaded/|) {
1.34      banghart 1360:             $r->print('<font color="red"> An errror occured ('.$result.
                   1361: 	              ') while trying to upload '.&display_file().'</font><br />');
1.94      raeburn  1362: 	    $r->print(&done('Back',$url,$group));
1.76      banghart 1363:         } else {
1.94      raeburn  1364:             $r->print(&done(undef,$url,$group));
1.33      banghart 1365:         }
1.25      albertel 1366:     }
                   1367: }
1.80      banghart 1368: sub lock_info {
1.94      raeburn  1369:     my ($r,$url,$group) = @_;
                   1370:     my ($uname,$udom) = &get_name_dom($group);
1.102     raeburn  1371:     my $current_permissions = &Apache::lonnet::get_portfile_permissions($udom,
                   1372:                                                                        $uname);
1.84      banghart 1373:     my $file_name = $env{'form.lockinfo'};
1.102     raeburn  1374:     $file_name = &prepend_group($file_name,$group);
                   1375:     if (defined($file_name) && defined($$current_permissions{$file_name})) {
                   1376:         foreach my $array_item (@{$$current_permissions{$file_name}}) {
                   1377:             if (ref($array_item) eq 'ARRAY') {
                   1378:                 my $filetext;
                   1379:                 if (defined($group)) {
                   1380:                     $filetext = '<strong>'.$env{'form.lockinfo'}.
                   1381:                                     '</strong> (group: '.$group.')'; 
                   1382:                 } else {
                   1383:                     $filetext = '<strong>'.$file_name.'</strong>';
                   1384:                 } 
                   1385:                 $r->print(&mt('[_1] was submitted in response to problem: ',
                   1386:                               $filetext).
                   1387:                           '<strong>'.&Apache::lonnet::gettitle($$array_item[0]).
                   1388:                           '</strong><br />');
                   1389:                 my %course_description = &Apache::lonnet::coursedescription($$array_item[1]);
                   1390:                 $r->print(&mt('In the course: <strong>[_1]</strong><br />',
                   1391:                               $course_description{'description'}));
                   1392:                 # $r->print('the third is '.$$array_item[2].'<br>');
                   1393:                 # $r->print("item is $$array_item[0]<br> and $$array_item[0]");
1.85      banghart 1394:             }
1.102     raeburn  1395:         }
1.84      banghart 1396:     }
1.94      raeburn  1397:     $r->print(&done('Back',$url,$group));
1.80      banghart 1398:     return 'ok';
                   1399: }
1.25      albertel 1400: sub createdir {
1.94      raeburn  1401:     my ($r,$url,$group)=@_;
1.82      albertel 1402:     my $newdir=&Apache::lonnet::clean_filename($env{'form.newdir'});
1.28      albertel 1403:     if ($newdir eq '') {
1.37      banghart 1404:     	$r->print('<font color="red">'.
                   1405: 	    	  &mt("Error: no directory name was provided.").
                   1406: 		      '</font><br />');
1.94      raeburn  1407: 	    $r->print(&done(undef,$url,$group));
1.37      banghart 1408: 	    return;
1.94      raeburn  1409:     }
                   1410:     my $portfolio_root = &get_portfolio_root($group); 
                   1411:     my @dir_list=&get_dir_list($portfolio_root,$group);
1.37      banghart 1412:     my $found_file = 0;
                   1413:     foreach my $line (@dir_list) {
                   1414:         my ($filename)=split(/\&/,$line,2);
                   1415:         if ($filename eq $newdir){
                   1416:             $found_file = 1;
                   1417:         }
                   1418:     }
                   1419:     if ($found_file){
                   1420:     	    $r->print('<font color="red"> Unable to create a directory named <strong>'.$newdir.
                   1421:     	            ' </strong>a file or directory by that name already exists.</font><br />');
                   1422:     } else {
1.94      raeburn  1423:         my ($uname,$udom) = &get_name_dom($group);
                   1424:         my $port_path = &get_port_path($group);
                   1425:         my $result=&Apache::lonnet::mkdiruserfile($uname,$udom,
                   1426: 	         $port_path.$env{'form.currentpath'}.$newdir);
1.37      banghart 1427:         if ($result ne 'ok') {
                   1428:     	    $r->print('<font color="red"> An errror occured ('.$result.
                   1429: 	    	      ') while trying to create a new directory '.&display_file().'</font><br />');
                   1430:         }
1.24      albertel 1431:     }
1.82      albertel 1432:     if ($newdir ne $env{'form.newdir'}) {
                   1433:         $r->print("The new directory name was changed from:<br /><strong>".$env{'form.newdir'}."</strong> to <strong>$newdir </strong>");  
1.67      banghart 1434:     }
1.94      raeburn  1435:     $r->print(&done(undef,$url,$group));
                   1436: }
                   1437: 
                   1438: sub get_portfolio_root {
                   1439:     my ($group) = @_;
                   1440:     my ($portfolio_root,$udom,$uname,$path);
                   1441:     ($uname,$udom) = &get_name_dom($group);
                   1442:     if (defined($group)) {
                   1443:         $path = '/userfiles/groups/'.$group.'/portfolio';
                   1444:     } else {
                   1445:         $path = '/userfiles/portfolio';
                   1446:     }
                   1447:     return (&Apache::loncommon::propath($udom,$uname).$path);
                   1448: }
                   1449: 
                   1450: sub get_dir_list {
                   1451:     my ($portfolio_root,$group) = @_;
                   1452:     my ($uname,$udom) = &get_name_dom($group);
                   1453:     return &Apache::lonnet::dirlist($env{'form.currentpath'},
                   1454:                                           $udom,$uname,$portfolio_root);
                   1455: }
                   1456: 
                   1457: sub get_name_dom {
                   1458:     my ($group) = @_;
                   1459:     my ($uname,$udom);
                   1460:     if (defined($group)) {
                   1461:         $udom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   1462:         $uname = $env{'course.'.$env{'request.course.id'}.'.num'};
                   1463:     } else {
                   1464:         $udom = $env{'user.domain'};
                   1465:         $uname = $env{'user.name'};
                   1466:     }
                   1467:     return ($uname,$udom);
                   1468: }
                   1469: 
1.102     raeburn  1470: sub prepend_group {
                   1471:     my ($filename,$group) = @_;
                   1472:     if (defined($group)) {
                   1473:         $filename = $group.'/'.$filename;
                   1474:     }
                   1475:     return $filename;
                   1476: }
                   1477: 
1.94      raeburn  1478: sub get_namespace {
                   1479:     my ($group) = @_;
                   1480:     my $namespace = 'portfolio';
                   1481:     if (defined($group)) {
                   1482:         my ($uname,$udom) = &get_name_dom($group);
                   1483:         $namespace .= '_'.$udom.'_'.$uname.'_'.$group;
                   1484:     }
                   1485:     return $namespace;
                   1486: }
                   1487: 
                   1488: sub get_port_path {
                   1489:     my ($group) = @_;
                   1490:     my $port_path;
                   1491:     if (defined($group)) {
                   1492:        $port_path = "groups/$group/portfolio";
                   1493:     } else {
                   1494:        $port_path = 'portfolio';
                   1495:     }
                   1496:     return $port_path;
1.24      albertel 1497: }
                   1498: 
                   1499: sub handler {
                   1500:     # this handles file management
                   1501:     my $r = shift;
1.73      banghart 1502:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.108     raeburn  1503:          ['selectfile','currentpath','meta','lockinfo','currentfile','action',
                   1504: 	  'fieldname','mode','rename','continue','group','access','setnum',
                   1505:           'cnum','cdom','type','setroles']);
1.94      raeburn  1506:     my ($uname,$udom,$portfolio_root,$url,$group,$caller,$title);
                   1507:     if ($r->uri =~ m|^(/adm/)([^/]+)|) {
                   1508:         $url = $1.$2;
                   1509:         $caller = $2;
                   1510:     }
                   1511:     if ($caller eq 'coursegrp_portfolio') {
                   1512:     #  Needs to be in a course
                   1513:         if (! ($env{'request.course.fn'})) {
                   1514:         # Not in a course
                   1515:             $env{'user.error.msg'}=
                   1516:      "/adm/coursegrp_portfolio:rgf:0:0:Cannot view group portfolio";
                   1517:             return HTTP_NOT_ACCEPTABLE;
                   1518:         }
                   1519:         my $earlyout = 0;
                   1520:         my $view_permission = &Apache::lonnet::allowed('vcg',
                   1521:                                                 $env{'request.course.id'});
                   1522:         $group = $env{'form.group'};
                   1523:         $group =~ s/\W//g;
                   1524:         if ($group) {
                   1525:             ($uname,$udom) = &get_name_dom($group);
1.99      raeburn  1526:             my %curr_groups = &Apache::longroup::coursegroups($udom,$uname,
1.98      albertel 1527: 							       $group); 
                   1528:             if (%curr_groups) {
1.94      raeburn  1529:                 if (($view_permission) || (&Apache::lonnet::allowed('rgf',
                   1530:                                       $env{'request.course.id'}.'/'.$group))) {
                   1531:                     $portfolio_root = &get_portfolio_root($group);
                   1532:                 } else {
                   1533:                     $r->print('You do not have the privileges required to access the shared files space for this group');
                   1534:                     $earlyout = 1;
                   1535:                 }
                   1536:             } else {
                   1537:                 $r->print('Not a valid group for this course');
                   1538:                 $earlyout = 1;
                   1539:             }
                   1540:             $title = &mt('Group files').' for '.$group; 
                   1541:         } else {
                   1542:             $r->print('Invalid group');
                   1543:             $earlyout = 1;
                   1544:         }
                   1545:         if ($earlyout) { return OK; }
                   1546:     } else {
                   1547:         ($uname,$udom) = &get_name_dom();
                   1548:         $portfolio_root = &get_portfolio_root();
                   1549:         $title = &mt('Portfolio Manager');
                   1550:     }
                   1551: 
1.24      albertel 1552:     &Apache::loncommon::no_cache($r);
                   1553:     &Apache::loncommon::content_type($r,'text/html');
                   1554:     $r->send_http_header;
                   1555:     # Give the LON-CAPA page header
1.82      albertel 1556:     if ($env{"form.mode"} eq 'selectfile'){
1.96      albertel 1557:         $r->print(&Apache::loncommon::start_page($title,undef,
1.97      albertel 1558: 						 {'only_body' => 1}));
1.108     raeburn  1559:     } elsif ($env{'form.action'} eq 'rolepicker') {
                   1560:         $r->print(&Apache::loncommon::start_page('New role-based condition',undef,
                   1561:                                                  {'no_nav_bar'  => 1, }));
1.74      banghart 1562:     } else {
1.97      albertel 1563:         $r->print(&Apache::loncommon::start_page($title));
1.74      banghart 1564:     }
1.24      albertel 1565:     $r->rflush();
1.88      albertel 1566: 	if (($env{'form.storeupl'}) & (!$env{'form.uploaddoc.filename'})){
1.40      banghart 1567:    	    $r->print('<font color="red"> No file was selected to upload.'.
                   1568:    	            'To upload a file, click <strong>Browse...</strong>'.
                   1569:    	            ', select a file, then click <strong>Upload</strong>,</font>');
                   1570: 	}
1.82      albertel 1571:     if ($env{'form.meta'}) {
1.94      raeburn  1572:         &open_form($r,$url);
1.82      albertel 1573: #        $r->print(&edit_meta_data($r, $env{'form.currentpath'}.$env{'form.selectfile'}));
1.70      banghart 1574:         $r->print('Edit the meta data<br />');
1.94      raeburn  1575:         &close_form($r,$url,$group);
1.70      banghart 1576:     }
1.82      albertel 1577:     if ($env{'form.store'}) {
1.70      banghart 1578:     }
                   1579: 
1.82      albertel 1580:     if ($env{'form.uploaddoc.filename'}) {
1.94      raeburn  1581: 	&upload($r,$url,$group);
1.82      albertel 1582:     } elsif ($env{'form.action'} eq 'delete' && $env{'form.confirmed'}) {
1.94      raeburn  1583: 	&delete_confirmed($r,$url,$group);
1.82      albertel 1584:     } elsif ($env{'form.action'} eq 'delete') {
1.94      raeburn  1585: 	&delete($r,$url,$group);
1.82      albertel 1586:     } elsif ($env{'form.action'} eq 'deletedir' && $env{'form.confirmed'}) {
1.94      raeburn  1587: 	&delete_dir_confirmed($r,$url,$group);
1.82      albertel 1588:     } elsif ($env{'form.action'} eq 'deletedir'){
1.94      raeburn  1589: 	&delete_dir($r,$url,$group);
1.82      albertel 1590:     } elsif ($env{'form.action'} eq 'rename' && $env{'form.confirmed'}) {
1.94      raeburn  1591: 	&rename_confirmed($r,$url,$group);
1.82      albertel 1592:     } elsif ($env{'form.rename'}) {
                   1593:         $env{'form.selectfile'} = $env{'form.rename'};
                   1594:         $env{'form.action'} = 'rename';
1.94      raeburn  1595: 	&rename($r,$url,$group);
1.104     raeburn  1596:     } elsif ($env{'form.access'}) {
                   1597:         $env{'form.selectfile'} = $env{'form.access'};
                   1598:         $env{'form.action'} = 'chgaccess';
                   1599:         &display_access($r,$url,$group);
                   1600:     } elsif ($env{'form.action'} eq 'chgaccess') {
                   1601:         &update_access($r,$url,$group);
1.108     raeburn  1602:     } elsif ($env{'form.action'} eq 'rolepicker') {
                   1603:         &role_options_window($r);
1.82      albertel 1604:     } elsif ($env{'form.createdir'}) {
1.94      raeburn  1605: 	&createdir($r,$url,$group);
1.82      albertel 1606:     } elsif ($env{'form.lockinfo'}) {
1.94      raeburn  1607:         &lock_info($r,$url,$group);
1.24      albertel 1608:     } else {
                   1609: 	my $current_path='/';
1.82      albertel 1610: 	if ($env{'form.currentpath'}) {
                   1611: 	    $current_path = $env{'form.currentpath'};
1.24      albertel 1612: 	}
1.94      raeburn  1613:         my @dir_list=&get_dir_list($portfolio_root,$group);
1.46      albertel 1614: 	if ($dir_list[0] eq 'no_such_dir'){
                   1615: 	    # two main reasons for this:
                   1616:             #    1) never been here, so directory structure not created
                   1617: 	    #    2) back-button navigation after deleting a directory
                   1618: 	    if ($current_path eq '/'){
1.100     albertel 1619: 	        &Apache::lonnet::mkdiruserfile($uname,$udom,
                   1620: 					       &get_port_path($group));
1.46      albertel 1621: 	    } else {
                   1622:                 # some directory that snuck in get rid of the directory
                   1623:                 # from the recent pulldown, just in case
                   1624: 		&Apache::lonhtmlcommon::remove_recent('portfolio',
                   1625: 						      [$current_path]);
                   1626: 		$current_path = '/'; # force it back to the root        
                   1627: 	    }
                   1628: 	    # now grab the directory list again, for the first time
                   1629: 	    @dir_list=&Apache::lonnet::dirlist($current_path,
1.94      raeburn  1630: 					    $udom,$uname,$portfolio_root);
1.43      banghart 1631:         }
1.46      albertel 1632: 	# need to know if directory is empty so it can be removed if desired
                   1633: 	my $is_empty=(@dir_list == 2);
1.94      raeburn  1634: 	&display_common($r,$url,$current_path,$is_empty,\@dir_list,$group);
                   1635:         &display_directory($r,$url,$current_path,$is_empty,\@dir_list,$group);
1.95      albertel 1636: 	$r->print(&Apache::loncommon::end_page());
1.30      banghart 1637:     }
1.90      albertel 1638:     return OK;
1.2       banghart 1639: }
1.1       banghart 1640: 1;
                   1641: __END__

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