File:  [LON-CAPA] / loncom / interface / lonaboutme.pm
Revision 1.73: download - view: text, annotated - select for diffs
Wed Nov 19 11:51:49 2008 UTC (15 years, 7 months ago) by amueller
Branches: MAIN
CVS tags: HEAD
edit lonsyllabus.pm with funtions from lontemplate.pm.
changed the way how to invoke functions from lontemplate.pm in the file lonaboutme.pm.
Included the new styles in the function print_template in file lontemplate.pm by Torben Ehlers and Alexander Schulze

    1: # The LearningOnline Network
    2: # "About Me" Personal Information
    3: #
    4: # $Id: lonaboutme.pm,v 1.73 2008/11/19 11:51:49 amueller Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: 
   29: package Apache::lonaboutme;
   30: 
   31: use strict;
   32: use Apache::Constants qw(:common);
   33: use Apache::loncommon;
   34: use Apache::lonnet;
   35: use Apache::lontexconvert;
   36: use Apache::lonfeedback;
   37: use Apache::lonrss();
   38: use Apache::lonlocal;
   39: use Apache::lonmsgdisplay();
   40: use Apache::lontemplate;
   41: use HTML::Entities();
   42: 
   43: sub handler {
   44:     my $r = shift;
   45:     &Apache::loncommon::content_type($r,'text/html');
   46:     $r->send_http_header;
   47:     return OK if $r->header_only;
   48:     my $target=$env{'form.grade_target'};
   49: # ------------------------------------------------------------ Print the screen
   50:     if ($target eq 'tex') {
   51: 	$r->print(&Apache::lonprintout::print_latex_header($env{'form.latex_type'}));
   52:     }
   53:     my (undef,undef,$cdom,$cnum,undef,$action)=split(/\//,$r->uri);
   54:     my $is_course;
   55: # Is this even a user?
   56:     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
   57: 	&Apache::loncommon::simple_error_page($r,'No info',
   58: 					      'No user information available');
   59:         return OK;
   60:     } else {
   61:         $is_course = &Apache::lonnet::is_course($cdom,$cnum);
   62:     }
   63: 
   64: # --------------------------------------------------------- The syllabus fields
   65:     my %syllabusfields=&Apache::lonlocal::texthash(
   66:        'aaa_contactinfo'   => 'Contact Information',
   67:        'bbb_aboutme'       => 'About Me',
   68:        'ccc_webreferences' => 'Web References');
   69: 
   70: # ------------------------------------------------------------ Get Query String
   71:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
   72: 					    ['forceedit','forcestudent',
   73: 					     'register','popup']);
   74: 
   75: # ----------------------------------------------- Available Portfolio file display 
   76:     if (($target ne 'tex') && ($action eq 'portfolio')) {
   77:         &display_portfolio_header($r,$is_course);
   78:         my ($blocked,$blocktext) = 
   79:            &Apache::loncommon::blocking_status('port',$cnum,$cdom);
   80:         if (!$blocked) {
   81:             &display_portfolio_files($r,$is_course);
   82:         } else {
   83:             $r->print($blocktext);
   84:         }
   85:         $r->print(&Apache::loncommon::end_page());
   86:         return OK;
   87:     }
   88: 
   89:     if ($is_course) {
   90:         if ($target ne 'tex') {
   91:             my $start_page =
   92:                 &Apache::loncommon::start_page(
   93:                     "Course Information",
   94:                      undef,
   95:                      {'function' => $env{'forcestudent'},
   96:                       'domain'   => $cdom,
   97:                       'force_register' => $env{'forceregister'},});
   98:             $r->print($start_page);
   99:             $r->print('<h2>'.&mt('Group files').'</h2>');
  100:             &print_portfiles_link($r,$is_course);
  101:             $r->print(&Apache::loncommon::end_page());
  102:         }
  103:         return OK;
  104:     }
  105: 
  106: # --------------------------------------------------------------- Force Student
  107:     my $forcestudent='';
  108:     if ($env{'form.forcestudent'}) { $forcestudent='student'; };
  109: 
  110:     my $forceregister = '';
  111:     if ($forcestudent eq '') {
  112:         $forceregister = $env{'form.register'};
  113:     }
  114:        
  115: # --------------------------------------- There is such a user, get environment
  116:     my %courseenv=&Apache::lonnet::dump('environment',$cdom,$cnum);
  117:     if ($target ne 'tex') {
  118: 	my $rss_link = &Apache::lonrss::rss_link($cnum,$cdom);
  119:         my $args = {'function' => $forcestudent,
  120:                     'domain'   => $cdom,
  121:                     'force_register' => $forceregister};
  122:         if ($env{'form.popup'}) {
  123:             $args->{'no_nav_bar'} = 1;
  124:         }
  125: 	my $start_page = 
  126: 	    &Apache::loncommon::start_page("Personal Information",$rss_link,$args);
  127: 	$r->print($start_page);
  128: 	$r->print('<h1>'.&Apache::loncommon::plainname($cnum,$cdom).'</h1>');
  129:     } else {
  130: 	$r->print('\noindent{\large\textbf{'.&Apache::loncommon::plainname($cnum,$cdom).'}}\\\\\\\\');
  131:     }
  132:     if ($courseenv{'nickname'}) {
  133:        $r->print(
  134:          '<h2>&quot;'.$courseenv{'nickname'}.
  135:          '&quot;</h2>');
  136:     }
  137:     if ($target ne 'tex') {
  138: 	$r->print('<h3>'.&Apache::lonnet::domain($cdom,'description').'</h3>');#OLD SendMessage POS
  139:     } else {
  140: 	$r->print('\textbf{'.&Apache::lonnet::domain($cdom,'description').'}\\\\');
  141:     }
  142:     my %syllabus=&Apache::lonnet::dump('aboutme',$cdom,$cnum);
  143:     my $allowed=0;
  144: 
  145: # does this user have privileges to post, etc?
  146: 
  147:        my $privleged=$allowed=(($env{'user.name'} eq $cnum) && 
  148: 			       ($env{'user.domain'} eq $cdom));
  149:        if ($forcestudent or $target eq 'tex') { $allowed=0; }
  150:  
  151:        if ($allowed) {
  152:            my $query_string = &build_query_string({'forcestudent' => '1','popup' => $env{'form.popup'}});
  153: 	   $r->print('<p><b>'.&mt('Privacy Note').':</b> '.
  154: 		     &mt('The information you submit can be viewed by anybody who is logged into LON-CAPA. Do not provide information that you are not ready to share publicly.').
  155: 		     '</p>'.
  156: 		     &Apache::loncommon::help_open_topic('Uploaded_Templates_TextBoxes',&mt('Help with filling in text boxes')).'</p><p><a href="'.$r->uri.$query_string.'">'.&mt('Show Public View').'</a>'.
  157: 		     &Apache::loncommon::help_open_topic('Uploaded_Templates_PublicView').'</p>');
  158:        } elsif ($privleged && $target ne 'tex') {
  159:            my $query_string = &build_query_string({'forceedit' => '1','popup' => $env{'form.popup'}});
  160: 	   $r->print('<p><a href="'.$r->uri.$query_string.'"><font size="+1">'.
  161: 		     &mt('Edit').'</font></a></p>');
  162:        }
  163:       if (($env{'form.uploaddoc.filename'}) &&
  164:           ($env{'form.storeupl'}) && ($allowed)) {
  165:  	  if ($env{'form.uploaddoc.filename'}=~/\.(gif|jpg|png|jpeg)$/i) {
  166: 	      if ($syllabus{'uploaded.photourl'}) {
  167: 		  &Apache::lonnet::removeuploadedurl($syllabus{'uploaded.photourl'});
  168: 	      }
  169: 	      $syllabus{'uploaded.photourl'}=
  170:                  &Apache::lonnet::userfileupload('uploaddoc',undef,'aboutme');
  171:  	  }
  172:           $syllabus{'uploaded.lastmodified'}=time;
  173:           &Apache::lonnet::put('aboutme',\%syllabus,$cdom,$cnum);
  174:        }
  175:     if ($allowed && $env{'form.delupl'}) {
  176: 	if ($syllabus{'uploaded.photourl'}) {
  177: 	    &Apache::lonnet::removeuploadedurl($syllabus{'uploaded.photourl'});
  178: 	    delete($syllabus{'uploaded.photourl'});
  179: 	    &Apache::lonnet::del('aboutme',['uploaded.photourl'],$cdom,$cnum);
  180: 	}
  181:     }
  182:        if (($allowed) && ($env{'form.storesyl'})) {
  183: 	   foreach my $syl_field (keys(%syllabusfields)) {
  184:                my $field=$env{'form.'.$syl_field};
  185:                $field=~s/\s+$//s;
  186:                $field=&Apache::lonfeedback::clear_out_html($field,
  187:                                                            $env{'user.adv'});
  188: 	       $syllabus{$syl_field}=$field;
  189:            }
  190:            $syllabus{'uploaded.lastmodified'}=time;
  191:            &Apache::lonnet::put('aboutme',\%syllabus,$cdom,$cnum);
  192:        }
  193: 
  194: my $lastmod;
  195: my $image; 
  196: # ---------------------------------------------------------------- Get syllabus
  197:     if (($syllabus{'uploaded.lastmodified'}) || ($allowed)) {
  198:        $lastmod=$syllabus{'uploaded.lastmodified'};
  199:        $lastmod=($lastmod?&Apache::lonlocal::locallocaltime($lastmod):&mt('never'));
  200:        # $r->print(&mt('Last updated').': '.$lastmod); #Old Last Modifi Pos
  201:        if ($syllabus{'uploaded.photourl'}) {
  202: 	   &Apache::lonnet::allowuploaded('/adm/aboutme',
  203: 					  $syllabus{'uploaded.photourl'});
  204: 	   $image=
  205:                qq{<img name="userPhoto" src="$syllabus{'uploaded.photourl'}" style="vertical-align:text-top;float:right;" />};
  206: 
  207: 	   if ($target eq 'tex') {
  208: 	       $image=&Apache::lonxml::xmlparse($r,'tex',$image);
  209: 	   }
  210: 	  # $r->print($image); #Print old Image
  211:        }
  212:        if ($allowed) {
  213:            $r->print(
  214: 	 '<form method="post">
  215:                <input type="submit" name="delupl" value="'.&mt('Delete Photo').'" />
  216:           </form>'.
  217: 	 '<form method="post" enctype="multipart/form-data">'.
  218:          '<h3>'.&mt('Upload a Photo').'</h3>'.
  219:          '<input type="file" name="uploaddoc" size="50" />'.
  220:          '<input type="submit" name="storeupl" value="'.&mt('Upload').'" />'.
  221:          '<input type="hidden" name="popup" value="'.$env{'form.popup'}.'" />'.
  222: 	 '</form><form method="post">');
  223: 
  224:        }
  225: 
  226: 	
  227:        foreach my $field (sort(keys(%syllabusfields))) {
  228:           if (($syllabus{$field}) || ($allowed)) {
  229:               my $message=$syllabus{$field};
  230: 	      &Apache::lonfeedback::newline_to_br(\$message);
  231:               $message
  232:              =~s/(http\:\/\/[^\s]+)/\<a href=\"$1\"\>\<tt\>$1\<\/tt\>\<\/a\>/g;
  233: 	      if ($allowed) {
  234: 		  $message=&Apache::lonspeller::markeduptext($message);
  235: 	      }
  236: 	      $message=&Apache::lontexconvert::msgtexconverted($message);
  237: 	      if ($target ne 'tex') {
  238: 				
  239: 		Apache::lontemplate::print_template($r,$syllabusfields{$field},$message);
  240: 				
  241: 				
  242: 				
  243: 	      } else {
  244: 		     $r->print('\\\\\textbf{'.$syllabusfields{$field}.'}\\\\'.
  245: 			       &Apache::lonxml::xmlparse($r,'tex',$message).'\\\\');
  246: 	      }
  247:               if ($allowed) {
  248: 		Apache::lontemplate::print_editbox_template($r,$syllabus{$field},$field);
  249: 	      }
  250: 	  }
  251:        }
  252:        if($target ne 'tex')
  253:        {
  254: 		
  255: 		  &print_portfiles_link($r,$is_course);
  256:  
  257:       		 $r->print('</div>');
  258: 			$r->print('<div style="margin:0 0 0 75">');
  259:         $r->print($image);
  260: 
  261: 	$r->print('Kontakt');
  262: 	$r->print(&Apache::loncommon::messagewrapper(&mt('Send me a message'),$cnum,$cdom).'</p>'.&Apache::lonrss::advertisefeeds($cnum,$cdom));
  263: 
  264: 
  265:        }
  266: 
  267:        if ($allowed) {
  268:            if ($env{'form.popup'}) {
  269:                $r->print('<input type="hidden" name="popup" value="'.
  270:                          $env{'form.popup'}.'" />');
  271:            }
  272: 	   $r->print('</form>');
  273:        }
  274:        if ($target ne 'tex') {$r->print('<br />');} else {$r->print('\\\\');}
  275:     } else {
  276:        $r->print('<p>'.&mt('No personal information provided').'.</p>');
  277:     }
  278: 
  279:     if ($target ne 'tex') {
  280:         # &print_portfiles_link($r,$is_course); #old Print files
  281: 
  282:     }
  283: 
  284:     if ($env{'request.course.id'}
  285: 	&& &Apache::lonnet::allowed('srm',$env{'request.course.id'})
  286: 	&& &in_course($cdom,$cnum)) {
  287: 	if ($target ne 'tex') {
  288: 	    $r->print('<a name="coursecomment" />');
  289: 	    $r->print('<hr /><h3>'.
  290: 		      &mt('User Notes, Records of Face-To-Face Discussions, and Critical Messages in Course').'</h3>'.
  291: 		      &mt('Shared by course faculty and staff').
  292: 		      &Apache::loncommon::help_open_topic("Course_Face_To_Face_Records,Course_Critical_Message").
  293: 		      '<br />');
  294: 	    &Apache::lonmsgdisplay::disfacetoface($r,$cnum,$cdom);
  295: 	    $r->print('<hr />');
  296: 	    if (&Apache::lonnet::allowed('vsa',
  297: 					 $env{'request.course.id'}) ||
  298: 		&Apache::lonnet::allowed('vsa',
  299: 					 $env{'request.course.id'}.'/'.
  300: 					 $env{'request.course.sec'})) {
  301: 		$r->print(&Apache::loncommon::track_student_link
  302: 			  ('View recent activity by this student',
  303: 			   $cnum,$cdom).('&nbsp;'x2));
  304: 	    }
  305: 	    $r->print(&Apache::loncommon::noteswrapper('Add Records',$cnum,$cdom));
  306: 	} else {
  307: 	    $r->print('\\\\\textbf{'.&mt('User Notes, Records of Face-To-Face Discussions, and Critical Messages in Course').'}\\\\'.&mt('Shared by course faculty and staff').'\\\\\\\\');
  308: 	    &Apache::lonmsgdisplay::disfacetoface($r,$cnum,$cdom);
  309: 	}
  310:     }
  311:     if ($target ne 'tex') {
  312:         if ($env{'form.popup'}) {
  313:             $r->print('<p><a href="javascript:window.close()">'.&mt('Close window').'</a>');
  314:         }
  315: 	$r->print('<br />'.&mt('Last updated').': '.$lastmod);
  316: 
  317: 	$r->print(&Apache::loncommon::end_page());
  318:     } else {
  319: 	$r->print('\end{document}');
  320:     }
  321: 
  322:   
  323: 	
  324:     return OK;
  325: }
  326: 
  327: sub in_course {
  328:     my ($udom,$uname,$cdom,$cnum,$type) = @_;
  329:     $type ||= 'any';
  330:     if (!defined($cdom) || !defined($cnum)) {
  331: 	my $cid  = $env{'request.course.id'};
  332: 	$cdom = $env{'course.'.$cid.'.domain'};
  333: 	$cnum = $env{'course.'.$cid.'.num'};
  334:     }
  335:     my %roles = &Apache::lonnet::dump('roles',$udom,$uname);
  336:     my @course_roles = grep(m{^/\Q$cdom\E/\Q$cnum\E[/_]}, keys(%roles));
  337:     return 0 if (!@course_roles);
  338:     return 1 if ($type eq 'any');
  339:     my $now = time();
  340:     foreach my $role (@course_roles) {
  341: 	my (undef,$role_end,$role_start)=split(/\_/,$roles{$role});
  342: 	my $status = 'active';
  343: 	if ($role_start > 0 && $now < $role_start) {
  344: 	    $status = 'future';
  345: 	}
  346: 	if ($role_end > 0 && $now > $role_end) {
  347: 	    $status = 'previous';
  348: 	}
  349: 	return 1 if ($status eq $type);
  350:     }
  351:     return 0;
  352: }
  353: 
  354: sub aboutme_info {
  355:     my ($r,$is_course) = @_;
  356:     my (undef,undef,$cdom,$cnum)=split(/\//,$r->uri);
  357:     my $name;
  358:     if (!$is_course) {
  359:         $name = &Apache::loncommon::plainname($cnum,$cdom);
  360:     }
  361:     return ($cdom,$cnum,$name);
  362: }
  363: 
  364: sub print_portfiles_link {
  365:     my ($r,$is_course) = @_;
  366:     my ($cdom,$cnum,$name) = &aboutme_info($r,$is_course);
  367:     my $filecounts = &portfolio_files($r,'showlink',undef,$is_course,
  368:                                       $cdom,$cnum,$name);
  369:     my $query_string = &build_query_string();
  370:     my $output;
  371:     my %lt = &Apache::lonlocal::texthash(
  372:                          vpfi => 'Viewable portfolio files',
  373:                          vgpf => 'Viewable group portfolio files',
  374:                          difl => 'Display file listing',
  375:              );                     
  376:     if ($filecounts->{'both'} > 0) {
  377: 	$output = '<fieldset><legend><b>';
  378: 	$output .= ($is_course?$lt{'vgpf'}:$lt{'vpfi'}).'</b></legend><br />';
  379:         
  380: 	#$output = '<h3>'.($is_course?$lt{'vgpf'}:$lt{'vpfi'}).'</h3>';
  381:         $output .= '<a href="/adm/'.$cdom.'/'.$cnum.'/aboutme/portfolio'.
  382:                    $query_string.'">'.$lt{'difl'}.
  383:                    '</a><br /><br />';
  384:         if ($filecounts->{'both'} == 1) {
  385:             if ($is_course) {
  386:                 $output .= &mt('One group portfolio file is available.').'<ul>';
  387:             } else {
  388:                 $output .= &mt('One portfolio file owned by [_1] is available.',$name).'<ul>';
  389:             }
  390:         } else {
  391:             if ($is_course) {
  392:                 $output .= &mt('A total of [_1] group portfolio files are available.',$filecounts->{'both'}).'<ul>';
  393:             } else {
  394:                 $output .= &mt('A total of [_1] portfolio files owned by [_2] are available.',$filecounts->{'both'},$name).'<ul>';
  395:             }
  396:         }
  397:         if ($filecounts->{'withoutpass'}) {
  398: 	    $output .= '<li>'.&mt('[quant,_1,file is,files are] publicly accessible.',$filecounts->{'withoutpass'}).'</li>';
  399:         }
  400:         if ($filecounts->{'withpass'}) {
  401: 	    $output .= '<li>'.&mt('[quant,_1,file requires,files require] a passphrase for access.',$filecounts->{'withpass'}).'</li>';
  402:         }
  403:         $output .= '</ul>';
  404: 	$output .='</fieldset>';
  405:     }
  406:     $r->print($output);
  407:     return;
  408: }
  409: 
  410: sub build_query_string {
  411:     my ($new_items) = @_;
  412:     my $query_string;
  413:     my @formelements = ('register'); 
  414:     my $new = 0;
  415:     if (ref($new_items) eq 'HASH') {
  416:         $new = 1;
  417:         if (!defined($new_items->{'forceedit'}) && 
  418:             !defined($new_items->{'forcestudent'})) {
  419:             push(@formelements,('forceedit','forcestudent'));
  420:         }
  421:     } else {
  422:         push(@formelements,('forceedit','forcestudent'));
  423:     }
  424:     foreach my $element (@formelements) {
  425:         if (exists($env{'form.'.$element})) {
  426:             if ((!$new) || (!defined($new_items->{$element}))) {
  427:                 $query_string .= '&amp;'.$element.'='.$env{'form.'.$element};
  428:             }
  429:         }
  430:     }
  431:     if ($new) {
  432:         foreach my $key (keys(%{$new_items})) {
  433:             $query_string .= '&amp;'.$key.'='.$new_items->{$key};
  434:         }
  435:     }
  436:     $query_string =~ s/^\&amp;/\?/;
  437:     return $query_string;
  438: }
  439: 
  440: sub display_portfolio_header {
  441:     my ($r,$is_course) = @_;
  442:     my ($cdom,$cnum,$name) = &aboutme_info($r,$is_course);
  443:     my $query_string = &build_query_string();
  444:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  445:     my $forcestudent='';
  446:     if ($env{'form.forcestudent'}) { $forcestudent='student'; };
  447: 
  448:     my $output;
  449:     if ($is_course) {
  450:         $output = 
  451:             &Apache::loncommon::start_page('Viewable group portfolio files',undef,
  452:                                             {'function' => $forcestudent,
  453:                                              'domain'   => $cdom,});
  454:         $output .= '<h3>'.&mt('Group Portfolio files').'</h3>';
  455:     } else {
  456:         $output  =
  457:             &Apache::loncommon::start_page('Viewable portfolio files',undef,
  458:                                             {'function' => $forcestudent,
  459:                                              'domain'   => $cdom,});
  460:         if (!($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public')) {
  461:             &Apache::lonhtmlcommon::add_breadcrumb
  462:                 ({href=>"/adm/$cdom/$cnum/aboutme".$query_string,
  463:                   text=>"Personal information - $name",
  464:                   title=>"Go to personal information page for $name"},                 {href=>"/adm/$cdom/$cnum/aboutme/portfolio",
  465:                   text=>"Viewable files - $name",
  466:                   title=>"Viewable portfolio files for $name"}
  467:             );
  468:             $output .= &Apache::lonhtmlcommon::breadcrumbs(&mt('Viewable portfolio files.'));
  469:         }
  470:         $output .= '<h3>'.&mt('Portfolio files for [_1]',$name).'</h3>';
  471:     }
  472:     $r->print($output);
  473:     return;
  474: }
  475: 
  476: sub display_portfolio_files {
  477:     my ($r,$is_course) = @_;
  478:     my ($cdom,$cnum,$name) = &aboutme_info($r,$is_course);
  479:     my %lt = ( withoutpass => 'passphrase not required',
  480: 	       withpass    => 'passphrase protected',
  481: 	       both        => 'all access types ',);
  482:     %lt = &Apache::lonlocal::texthash(%lt);
  483: 
  484:     my $portaccess = 'withoutpass';
  485:     if (exists($env{'form.portaccess'})) {
  486:         $portaccess = $env{'form.portaccess'};
  487:     }
  488: 
  489:     my $output = '<form action="'.&HTML::Entities::encode($r->uri,'<>&"')
  490: 	.'" name="displaystatus" method="post">'.
  491: 	&mt('File access type: ').'<select name="portaccess">';
  492:     foreach my $type ('withoutpass','withpass','both') {
  493:         $output .= '<option value="'.$type.'" ';
  494:         if ($portaccess eq $type) {
  495:             $output .= 'selected="selected"';
  496:         }
  497:         $output .= '>'.$lt{$type}.'</option>';
  498:     }
  499:     $output .= '</select>'."\n".
  500:                '<input type="submit" name="portaccessbutton" value="'.
  501:                &mt('Update display').'" />';
  502:     $output .= '</form><br /><br />';
  503:     $r->print($output);
  504:     my $filecounts = &portfolio_files($r,'listfiles',\%lt,$is_course,
  505:                                       $cdom,$cnum,$name);
  506:     if (!($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public')) {
  507:         my $query_string = &build_query_string();
  508:         $r->print('<br /><br /><a href="/adm/'.$cdom.'/'.$cnum.
  509:                   '/aboutme'.$query_string.'">');
  510:         if ($is_course) {
  511:             $r->print(&mt('Course Information page'));
  512:         } else {
  513:             $r->print(&mt('Information about [_1]',$name));
  514:         }
  515:         $r->print('</a>');
  516:     }
  517:     return;
  518: }
  519: 
  520: sub portfolio_files {
  521:     my ($r,$mode,$lt,$is_course,$cdom,$cnum,$name) = @_;
  522:     my $filecounts = {
  523:                        withpass    => 0,
  524:                        withoutpass => 0,
  525:                        both        => 0,
  526:                      };
  527:     my $current_permissions =
  528: 	&Apache::lonnet::get_portfile_permissions($cdom,$cnum);
  529:     my %access_controls = 
  530: 	&Apache::lonnet::get_access_controls($current_permissions);
  531:     my $portaccess;
  532:     if ($mode eq 'showlink') {
  533:         $portaccess = 'both';
  534:     } else {
  535:         $portaccess = 'withoutpass';
  536:         if (exists($env{'form.portaccess'})) {
  537:             $portaccess = $env{'form.portaccess'};
  538:         }
  539:     }
  540: 
  541:     my $diroutput;
  542:     if ($is_course) {
  543:         my %files_by_group;
  544:         foreach my $filename (sort(keys(%access_controls))) {
  545:             my ($group,$path) = split('/',$filename,2);
  546:             $files_by_group{$group}{$path} = $access_controls{$filename}; 
  547:         }
  548:         foreach my $group (sort(keys(%files_by_group))) {
  549:             my %fileshash;
  550:             my $grpout .= &build_hierarchy($r,$cdom,$cnum,$portaccess,
  551:                                            $is_course,$filecounts,$mode,
  552:                                            $files_by_group{$group},
  553:                                            \%fileshash,$group);
  554:             if ($grpout) {
  555:                 $diroutput .= '<h3>'.$group.'</h3>'.$grpout.'<br />';
  556:             }
  557:         }
  558:     } else {
  559:         my %allfileshash;
  560:         $diroutput = &build_hierarchy($r,$cdom,$cnum,$portaccess,$is_course,
  561:                                       $filecounts,$mode,\%access_controls,
  562:                                       \%allfileshash);
  563:     }
  564:     if ($mode eq 'listfiles') {
  565:         if ($filecounts->{'both'}) {
  566:              $r->print($diroutput);
  567:         } else {
  568:             my $access_text;
  569:             if (ref($lt) eq 'HASH') {
  570:                 $access_text = $lt->{$portaccess};   
  571:             }
  572:             $r->print(&mt('There are no available files of the specified access type: [_1]',$access_text));
  573:         }
  574:     }
  575:     return $filecounts;
  576: }
  577: 
  578: { 
  579:     my $count=0;
  580:     sub portfolio_table_start {
  581: 	$count=0;
  582: 	return '<table class="LC_aboutme_port">';
  583:     }
  584:     sub portfolio_row_start {
  585: 	$count++;
  586: 	my $class = ($count%2)?'LC_odd_row'
  587: 	                      :'LC_even_row';
  588: 	return '<tr class="'.$class.'">';
  589:     }
  590: }
  591: 
  592: sub build_hierarchy {
  593:     my ($r,$cdom,$cnum,$portaccess,$is_course,$filecounts,$mode,$access_info,
  594:         $allfileshash,$group) = @_;
  595:     foreach my $filename (sort(keys(%{$access_info}))) {
  596:         my $access_status =
  597:            &Apache::lonnet::get_portfolio_access($cdom,$cnum,$filename,$group,                                                 $$access_info{$filename});
  598:         if ($portaccess eq 'both') {
  599:             if (($access_status ne 'ok') &&
  600:                 ($access_status !~  /^[^:]+:guest_/)) {
  601:                 next;
  602:             }
  603:         } elsif ($portaccess eq 'withoutpass') {
  604:             if ($access_status ne 'ok') {
  605:                 next;
  606:             }
  607:         } elsif ($portaccess eq 'withpass') {
  608:             if ($access_status !~  /^[^:]+:guest_/) {
  609:                 next;
  610:             }
  611:         }
  612:         if ($mode eq 'listfiles') {
  613:             $filename =~ s/^\///;
  614:             my @pathitems = split('/',$filename);
  615:             my $lasthash = $allfileshash;
  616:             while (@pathitems > 1) {
  617:                 my $newlevel = shift(@pathitems);
  618:                 if (!exists($lasthash->{$newlevel})) {
  619:                     $lasthash->{$newlevel} = {};
  620:                 }
  621:                 $lasthash = $lasthash->{$newlevel};
  622:             }
  623:             $lasthash->{$pathitems[0]} = $filename;
  624:         }
  625:         if ($access_status eq 'ok') {
  626:             $filecounts->{'withoutpass'} ++;
  627:         } elsif ($access_status =~  /^[^:]+:guest_/) {
  628:             $filecounts->{'withpass'} ++;
  629:         }
  630:     }
  631:     $filecounts->{'both'} =  $filecounts->{'withoutpass'} +
  632:                               $filecounts->{'withpass'};
  633:     my $output;
  634:     if ($mode eq 'listfiles') {
  635:         if ($filecounts->{'both'} > 0) {
  636:             $output = &portfolio_table_start();
  637:             $output .= &parse_directory($r,0,$allfileshash,'',$is_course,
  638:                                         $group);
  639:             $output .= '</table>';
  640:         }
  641:     }
  642:     return $output;
  643: }
  644: 
  645: sub parse_directory {
  646:     my ($r,$depth,$currhash,$path,$is_course,$group) = @_;
  647:     my ($cdom,$cnum,$name) = &aboutme_info($r,$is_course);
  648:     $depth++;
  649:     my $output;
  650: 
  651:     my $portfolio_root = &Apache::portfolio::get_portfolio_root($cdom,$cnum,
  652:                                                                 $group);
  653:     my $getpropath = 1;
  654:     my %dirlist = map {
  655: 	    ((split('&',$_,2))[0],1)
  656: 	} &Apache::lonnet::dirlist($portfolio_root.$path,$cdom,$cnum,$getpropath);
  657:     foreach my $item (sort(keys(%{$currhash}))) {
  658:         $output .= &portfolio_row_start();
  659:         $output .= '<td style="padding-left: '.($depth*25).'px">';
  660:         if (ref($currhash->{$item}) eq 'HASH') {
  661:             my $title=&HTML::Entities::encode($item,'<>&"');
  662:             $output .= '<img src="'.&Apache::loncommon::lonhttpdurl("/adm/lonIcons/navmap.folder.open.gif").'" alt="'.&mt('Folder').' '.$title.'" class="LC_icon" />&nbsp;'.$title;
  663: 	    $output .= '</td><td></td></tr>';
  664:             $output .= &parse_directory($r,$depth,$currhash->{$item},
  665: 					$path.'/'.$item,$is_course,$group);
  666:         } else {
  667: 	    my $file_name; 
  668: 	    if ($currhash->{$item} =~ m|/([^/]+)$|) {
  669: 		$file_name = $1;
  670: 	    } else {
  671: 		$file_name = $currhash->{$item};
  672: 	    }
  673: 	    my $have_meta = exists($dirlist{$file_name.'.meta'});
  674:             my $url;
  675:             if ($is_course) {
  676:                 $url = '/uploaded/'.$cdom.'/'.$cnum.'/groups/'.$group.
  677:                        '/portfolio/'.$currhash->{$item};
  678:             } else { 
  679: 	        $url = '/uploaded/'.$cdom.'/'.$cnum.'/portfolio/'.
  680: 		       $currhash->{$item};
  681:             }
  682:             my $showname;
  683: 	    if ($have_meta) {
  684: 		$showname = &Apache::lonnet::metadata($url,'title');
  685: 	    }
  686: 	    if ($showname eq '') {
  687: 		$showname = $file_name;
  688: 	    } else {
  689: 		$showname = $file_name.' ('.$showname.')';
  690: 	    }
  691: 
  692:             $showname=&HTML::Entities::encode($showname,'<>&"');
  693:             $output .= '<a href="'.$url.'">'.
  694: 		'<img alt="" src="'.&Apache::loncommon::icon($currhash->{$item}).'" class="LC_icon" />'.
  695: 		'&nbsp;'.$showname.'</a>';
  696: 	    $output.='</td><td>';
  697: 	    if ($have_meta) {
  698: 		$output.= '<a href="'.$url.'.meta"><img alt="'.&mt('Catalog Information').'" src="'.
  699: 		&Apache::loncommon::lonhttpdurl('/res/adm/pages/catalog.gif').
  700: 		'" class="LC_icon" /></a>';
  701: 	    }
  702: 	    $output .= '</td></tr>';
  703:         }
  704:     }
  705:     return $output;
  706: }
  707: 
  708: 1;
  709: __END__

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