File:  [LON-CAPA] / loncom / interface / lonaboutme.pm
Revision 1.72: download - view: text, annotated - select for diffs
Tue Nov 18 10:34:45 2008 UTC (15 years, 6 months ago) by amueller
Branches: MAIN
CVS tags: HEAD
created lontemplate.pm and edit lonaboutme.pm with functions from lontemplate.pm by Niels Neumann

    1: # The LearningOnline Network
    2: # "About Me" Personal Information
    3: #
    4: # $Id: lonaboutme.pm,v 1.72 2008/11/18 10:34:45 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 src="$syllabus{'uploaded.photourl'}" style="vertical-align:text-top;float:right;" />};
  206: 	   if ($target eq 'tex') {
  207: 	       $image=&Apache::lonxml::xmlparse($r,'tex',$image);
  208: 	   }
  209: 	  # $r->print($image); #Print old Image
  210:        }
  211:        if ($allowed) {
  212:            $r->print(
  213: 	 '<form method="post">
  214:                <input type="submit" name="delupl" value="'.&mt('Delete Photo').'" />
  215:           </form>'.
  216: 	 '<form method="post" enctype="multipart/form-data">'.
  217:          '<h3>'.&mt('Upload a Photo').'</h3>'.
  218:          '<input type="file" name="uploaddoc" size="50" />'.
  219:          '<input type="submit" name="storeupl" value="'.&mt('Upload').'" />'.
  220:          '<input type="hidden" name="popup" value="'.$env{'form.popup'}.'" />'.
  221: 	 '</form><form method="post">');
  222: 
  223:        }
  224: 
  225: 	
  226:        foreach my $field (sort(keys(%syllabusfields))) {
  227:           if (($syllabus{$field}) || ($allowed)) {
  228:               my $message=$syllabus{$field};
  229: 	      &Apache::lonfeedback::newline_to_br(\$message);
  230:               $message
  231:              =~s/(http\:\/\/[^\s]+)/\<a href=\"$1\"\>\<tt\>$1\<\/tt\>\<\/a\>/g;
  232: 	      if ($allowed) {
  233: 		  $message=&Apache::lonspeller::markeduptext($message);
  234: 	      }
  235: 	      $message=&Apache::lontexconvert::msgtexconverted($message);
  236: 	      if ($target ne 'tex') {
  237: 				
  238: 		Apache::lontemplate::print_template($r,$syllabusfields{$field},$message);
  239: 				
  240: 				
  241: 				
  242: 	      } else {
  243: 		     $r->print('\\\\\textbf{'.$syllabusfields{$field}.'}\\\\'.
  244: 			       &Apache::lonxml::xmlparse($r,'tex',$message).'\\\\');
  245: 	      }
  246:               if ($allowed) {
  247: 		Apache::lontemplate::print_editbox_template($r,$syllabusfields{$field},$field);
  248: 	      }
  249: 	  }
  250:        }
  251:        if($target ne 'tex')
  252:        {
  253: 		
  254: 		  &print_portfiles_link($r,$is_course);
  255:  
  256:       		 $r->print('</div>');
  257: 			$r->print('<div style="margin:0 0 0 75">');
  258:         $r->print($image);
  259: 
  260: 	$r->print('Kontakt');
  261: 	$r->print(&Apache::loncommon::messagewrapper(&mt('Send me a message'),$cnum,$cdom).'</p>'.&Apache::lonrss::advertisefeeds($cnum,$cdom));
  262: 
  263: 
  264:        }
  265: 
  266:        if ($allowed) {
  267:            if ($env{'form.popup'}) {
  268:                $r->print('<input type="hidden" name="popup" value="'.
  269:                          $env{'form.popup'}.'" />');
  270:            }
  271: 	   $r->print('</form>');
  272:        }
  273:        if ($target ne 'tex') {$r->print('<br />');} else {$r->print('\\\\');}
  274:     } else {
  275:        $r->print('<p>'.&mt('No personal information provided').'.</p>');
  276:     }
  277: 
  278:     if ($target ne 'tex') {
  279:         # &print_portfiles_link($r,$is_course); #old Print files
  280: 
  281:     }
  282: 
  283:     if ($env{'request.course.id'}
  284: 	&& &Apache::lonnet::allowed('srm',$env{'request.course.id'})
  285: 	&& &in_course($cdom,$cnum)) {
  286: 	if ($target ne 'tex') {
  287: 	    $r->print('<a name="coursecomment" />');
  288: 	    $r->print('<hr /><h3>'.
  289: 		      &mt('User Notes, Records of Face-To-Face Discussions, and Critical Messages in Course').'</h3>'.
  290: 		      &mt('Shared by course faculty and staff').
  291: 		      &Apache::loncommon::help_open_topic("Course_Face_To_Face_Records,Course_Critical_Message").
  292: 		      '<br />');
  293: 	    &Apache::lonmsgdisplay::disfacetoface($r,$cnum,$cdom);
  294: 	    $r->print('<hr />');
  295: 	    if (&Apache::lonnet::allowed('vsa',
  296: 					 $env{'request.course.id'}) ||
  297: 		&Apache::lonnet::allowed('vsa',
  298: 					 $env{'request.course.id'}.'/'.
  299: 					 $env{'request.course.sec'})) {
  300: 		$r->print(&Apache::loncommon::track_student_link
  301: 			  ('View recent activity by this student',
  302: 			   $cnum,$cdom).('&nbsp;'x2));
  303: 	    }
  304: 	    $r->print(&Apache::loncommon::noteswrapper('Add Records',$cnum,$cdom));
  305: 	} else {
  306: 	    $r->print('\\\\\textbf{'.&mt('User Notes, Records of Face-To-Face Discussions, and Critical Messages in Course').'}\\\\'.&mt('Shared by course faculty and staff').'\\\\\\\\');
  307: 	    &Apache::lonmsgdisplay::disfacetoface($r,$cnum,$cdom);
  308: 	}
  309:     }
  310:     if ($target ne 'tex') {
  311:         if ($env{'form.popup'}) {
  312:             $r->print('<p><a href="javascript:window.close()">'.&mt('Close window').'</a>');
  313:         }
  314: 	$r->print('<br />'.&mt('Last updated').': '.$lastmod);
  315: 
  316: 	$r->print(&Apache::loncommon::end_page());
  317:     } else {
  318: 	$r->print('\end{document}');
  319:     }
  320: 
  321:   
  322: 	
  323:     return OK;
  324: }
  325: 
  326: sub in_course {
  327:     my ($udom,$uname,$cdom,$cnum,$type) = @_;
  328:     $type ||= 'any';
  329:     if (!defined($cdom) || !defined($cnum)) {
  330: 	my $cid  = $env{'request.course.id'};
  331: 	$cdom = $env{'course.'.$cid.'.domain'};
  332: 	$cnum = $env{'course.'.$cid.'.num'};
  333:     }
  334:     my %roles = &Apache::lonnet::dump('roles',$udom,$uname);
  335:     my @course_roles = grep(m{^/\Q$cdom\E/\Q$cnum\E[/_]}, keys(%roles));
  336:     return 0 if (!@course_roles);
  337:     return 1 if ($type eq 'any');
  338:     my $now = time();
  339:     foreach my $role (@course_roles) {
  340: 	my (undef,$role_end,$role_start)=split(/\_/,$roles{$role});
  341: 	my $status = 'active';
  342: 	if ($role_start > 0 && $now < $role_start) {
  343: 	    $status = 'future';
  344: 	}
  345: 	if ($role_end > 0 && $now > $role_end) {
  346: 	    $status = 'previous';
  347: 	}
  348: 	return 1 if ($status eq $type);
  349:     }
  350:     return 0;
  351: }
  352: 
  353: sub aboutme_info {
  354:     my ($r,$is_course) = @_;
  355:     my (undef,undef,$cdom,$cnum)=split(/\//,$r->uri);
  356:     my $name;
  357:     if (!$is_course) {
  358:         $name = &Apache::loncommon::plainname($cnum,$cdom);
  359:     }
  360:     return ($cdom,$cnum,$name);
  361: }
  362: 
  363: sub print_portfiles_link {
  364:     my ($r,$is_course) = @_;
  365:     my ($cdom,$cnum,$name) = &aboutme_info($r,$is_course);
  366:     my $filecounts = &portfolio_files($r,'showlink',undef,$is_course,
  367:                                       $cdom,$cnum,$name);
  368:     my $query_string = &build_query_string();
  369:     my $output;
  370:     my %lt = &Apache::lonlocal::texthash(
  371:                          vpfi => 'Viewable portfolio files',
  372:                          vgpf => 'Viewable group portfolio files',
  373:                          difl => 'Display file listing',
  374:              );                     
  375:     if ($filecounts->{'both'} > 0) {
  376: 	$output = '<fieldset><legend><b>';
  377: 	$output .= ($is_course?$lt{'vgpf'}:$lt{'vpfi'}).'</b></legend><br />';
  378:         
  379: 	#$output = '<h3>'.($is_course?$lt{'vgpf'}:$lt{'vpfi'}).'</h3>';
  380:         $output .= '<a href="/adm/'.$cdom.'/'.$cnum.'/aboutme/portfolio'.
  381:                    $query_string.'">'.$lt{'difl'}.
  382:                    '</a><br /><br />';
  383:         if ($filecounts->{'both'} == 1) {
  384:             if ($is_course) {
  385:                 $output .= &mt('One group portfolio file is available.').'<ul>';
  386:             } else {
  387:                 $output .= &mt('One portfolio file owned by [_1] is available.',$name).'<ul>';
  388:             }
  389:         } else {
  390:             if ($is_course) {
  391:                 $output .= &mt('A total of [_1] group portfolio files are available.',$filecounts->{'both'}).'<ul>';
  392:             } else {
  393:                 $output .= &mt('A total of [_1] portfolio files owned by [_2] are available.',$filecounts->{'both'},$name).'<ul>';
  394:             }
  395:         }
  396:         if ($filecounts->{'withoutpass'}) {
  397: 	    $output .= '<li>'.&mt('[quant,_1,file is,files are] publicly accessible.',$filecounts->{'withoutpass'}).'</li>';
  398:         }
  399:         if ($filecounts->{'withpass'}) {
  400: 	    $output .= '<li>'.&mt('[quant,_1,file requires,files require] a passphrase for access.',$filecounts->{'withpass'}).'</li>';
  401:         }
  402:         $output .= '</ul>';
  403: 	$output .='</fieldset>';
  404:     }
  405:     $r->print($output);
  406:     return;
  407: }
  408: 
  409: sub build_query_string {
  410:     my ($new_items) = @_;
  411:     my $query_string;
  412:     my @formelements = ('register'); 
  413:     my $new = 0;
  414:     if (ref($new_items) eq 'HASH') {
  415:         $new = 1;
  416:         if (!defined($new_items->{'forceedit'}) && 
  417:             !defined($new_items->{'forcestudent'})) {
  418:             push(@formelements,('forceedit','forcestudent'));
  419:         }
  420:     } else {
  421:         push(@formelements,('forceedit','forcestudent'));
  422:     }
  423:     foreach my $element (@formelements) {
  424:         if (exists($env{'form.'.$element})) {
  425:             if ((!$new) || (!defined($new_items->{$element}))) {
  426:                 $query_string .= '&amp;'.$element.'='.$env{'form.'.$element};
  427:             }
  428:         }
  429:     }
  430:     if ($new) {
  431:         foreach my $key (keys(%{$new_items})) {
  432:             $query_string .= '&amp;'.$key.'='.$new_items->{$key};
  433:         }
  434:     }
  435:     $query_string =~ s/^\&amp;/\?/;
  436:     return $query_string;
  437: }
  438: 
  439: sub display_portfolio_header {
  440:     my ($r,$is_course) = @_;
  441:     my ($cdom,$cnum,$name) = &aboutme_info($r,$is_course);
  442:     my $query_string = &build_query_string();
  443:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  444:     my $forcestudent='';
  445:     if ($env{'form.forcestudent'}) { $forcestudent='student'; };
  446: 
  447:     my $output;
  448:     if ($is_course) {
  449:         $output = 
  450:             &Apache::loncommon::start_page('Viewable group portfolio files',undef,
  451:                                             {'function' => $forcestudent,
  452:                                              'domain'   => $cdom,});
  453:         $output .= '<h3>'.&mt('Group Portfolio files').'</h3>';
  454:     } else {
  455:         $output  =
  456:             &Apache::loncommon::start_page('Viewable portfolio files',undef,
  457:                                             {'function' => $forcestudent,
  458:                                              'domain'   => $cdom,});
  459:         if (!($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public')) {
  460:             &Apache::lonhtmlcommon::add_breadcrumb
  461:                 ({href=>"/adm/$cdom/$cnum/aboutme".$query_string,
  462:                   text=>"Personal information - $name",
  463:                   title=>"Go to personal information page for $name"},                 {href=>"/adm/$cdom/$cnum/aboutme/portfolio",
  464:                   text=>"Viewable files - $name",
  465:                   title=>"Viewable portfolio files for $name"}
  466:             );
  467:             $output .= &Apache::lonhtmlcommon::breadcrumbs(&mt('Viewable portfolio files.'));
  468:         }
  469:         $output .= '<h3>'.&mt('Portfolio files for [_1]',$name).'</h3>';
  470:     }
  471:     $r->print($output);
  472:     return;
  473: }
  474: 
  475: sub display_portfolio_files {
  476:     my ($r,$is_course) = @_;
  477:     my ($cdom,$cnum,$name) = &aboutme_info($r,$is_course);
  478:     my %lt = ( withoutpass => 'passphrase not required',
  479: 	       withpass    => 'passphrase protected',
  480: 	       both        => 'all access types ',);
  481:     %lt = &Apache::lonlocal::texthash(%lt);
  482: 
  483:     my $portaccess = 'withoutpass';
  484:     if (exists($env{'form.portaccess'})) {
  485:         $portaccess = $env{'form.portaccess'};
  486:     }
  487: 
  488:     my $output = '<form action="'.&HTML::Entities::encode($r->uri,'<>&"')
  489: 	.'" name="displaystatus" method="post">'.
  490: 	&mt('File access type: ').'<select name="portaccess">';
  491:     foreach my $type ('withoutpass','withpass','both') {
  492:         $output .= '<option value="'.$type.'" ';
  493:         if ($portaccess eq $type) {
  494:             $output .= 'selected="selected"';
  495:         }
  496:         $output .= '>'.$lt{$type}.'</option>';
  497:     }
  498:     $output .= '</select>'."\n".
  499:                '<input type="submit" name="portaccessbutton" value="'.
  500:                &mt('Update display').'" />';
  501:     $output .= '</form><br /><br />';
  502:     $r->print($output);
  503:     my $filecounts = &portfolio_files($r,'listfiles',\%lt,$is_course,
  504:                                       $cdom,$cnum,$name);
  505:     if (!($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public')) {
  506:         my $query_string = &build_query_string();
  507:         $r->print('<br /><br /><a href="/adm/'.$cdom.'/'.$cnum.
  508:                   '/aboutme'.$query_string.'">');
  509:         if ($is_course) {
  510:             $r->print(&mt('Course Information page'));
  511:         } else {
  512:             $r->print(&mt('Information about [_1]',$name));
  513:         }
  514:         $r->print('</a>');
  515:     }
  516:     return;
  517: }
  518: 
  519: sub portfolio_files {
  520:     my ($r,$mode,$lt,$is_course,$cdom,$cnum,$name) = @_;
  521:     my $filecounts = {
  522:                        withpass    => 0,
  523:                        withoutpass => 0,
  524:                        both        => 0,
  525:                      };
  526:     my $current_permissions =
  527: 	&Apache::lonnet::get_portfile_permissions($cdom,$cnum);
  528:     my %access_controls = 
  529: 	&Apache::lonnet::get_access_controls($current_permissions);
  530:     my $portaccess;
  531:     if ($mode eq 'showlink') {
  532:         $portaccess = 'both';
  533:     } else {
  534:         $portaccess = 'withoutpass';
  535:         if (exists($env{'form.portaccess'})) {
  536:             $portaccess = $env{'form.portaccess'};
  537:         }
  538:     }
  539: 
  540:     my $diroutput;
  541:     if ($is_course) {
  542:         my %files_by_group;
  543:         foreach my $filename (sort(keys(%access_controls))) {
  544:             my ($group,$path) = split('/',$filename,2);
  545:             $files_by_group{$group}{$path} = $access_controls{$filename}; 
  546:         }
  547:         foreach my $group (sort(keys(%files_by_group))) {
  548:             my %fileshash;
  549:             my $grpout .= &build_hierarchy($r,$cdom,$cnum,$portaccess,
  550:                                            $is_course,$filecounts,$mode,
  551:                                            $files_by_group{$group},
  552:                                            \%fileshash,$group);
  553:             if ($grpout) {
  554:                 $diroutput .= '<h3>'.$group.'</h3>'.$grpout.'<br />';
  555:             }
  556:         }
  557:     } else {
  558:         my %allfileshash;
  559:         $diroutput = &build_hierarchy($r,$cdom,$cnum,$portaccess,$is_course,
  560:                                       $filecounts,$mode,\%access_controls,
  561:                                       \%allfileshash);
  562:     }
  563:     if ($mode eq 'listfiles') {
  564:         if ($filecounts->{'both'}) {
  565:              $r->print($diroutput);
  566:         } else {
  567:             my $access_text;
  568:             if (ref($lt) eq 'HASH') {
  569:                 $access_text = $lt->{$portaccess};   
  570:             }
  571:             $r->print(&mt('There are no available files of the specified access type: [_1]',$access_text));
  572:         }
  573:     }
  574:     return $filecounts;
  575: }
  576: 
  577: { 
  578:     my $count=0;
  579:     sub portfolio_table_start {
  580: 	$count=0;
  581: 	return '<table class="LC_aboutme_port">';
  582:     }
  583:     sub portfolio_row_start {
  584: 	$count++;
  585: 	my $class = ($count%2)?'LC_odd_row'
  586: 	                      :'LC_even_row';
  587: 	return '<tr class="'.$class.'">';
  588:     }
  589: }
  590: 
  591: sub build_hierarchy {
  592:     my ($r,$cdom,$cnum,$portaccess,$is_course,$filecounts,$mode,$access_info,
  593:         $allfileshash,$group) = @_;
  594:     foreach my $filename (sort(keys(%{$access_info}))) {
  595:         my $access_status =
  596:            &Apache::lonnet::get_portfolio_access($cdom,$cnum,$filename,$group,                                                 $$access_info{$filename});
  597:         if ($portaccess eq 'both') {
  598:             if (($access_status ne 'ok') &&
  599:                 ($access_status !~  /^[^:]+:guest_/)) {
  600:                 next;
  601:             }
  602:         } elsif ($portaccess eq 'withoutpass') {
  603:             if ($access_status ne 'ok') {
  604:                 next;
  605:             }
  606:         } elsif ($portaccess eq 'withpass') {
  607:             if ($access_status !~  /^[^:]+:guest_/) {
  608:                 next;
  609:             }
  610:         }
  611:         if ($mode eq 'listfiles') {
  612:             $filename =~ s/^\///;
  613:             my @pathitems = split('/',$filename);
  614:             my $lasthash = $allfileshash;
  615:             while (@pathitems > 1) {
  616:                 my $newlevel = shift(@pathitems);
  617:                 if (!exists($lasthash->{$newlevel})) {
  618:                     $lasthash->{$newlevel} = {};
  619:                 }
  620:                 $lasthash = $lasthash->{$newlevel};
  621:             }
  622:             $lasthash->{$pathitems[0]} = $filename;
  623:         }
  624:         if ($access_status eq 'ok') {
  625:             $filecounts->{'withoutpass'} ++;
  626:         } elsif ($access_status =~  /^[^:]+:guest_/) {
  627:             $filecounts->{'withpass'} ++;
  628:         }
  629:     }
  630:     $filecounts->{'both'} =  $filecounts->{'withoutpass'} +
  631:                               $filecounts->{'withpass'};
  632:     my $output;
  633:     if ($mode eq 'listfiles') {
  634:         if ($filecounts->{'both'} > 0) {
  635:             $output = &portfolio_table_start();
  636:             $output .= &parse_directory($r,0,$allfileshash,'',$is_course,
  637:                                         $group);
  638:             $output .= '</table>';
  639:         }
  640:     }
  641:     return $output;
  642: }
  643: 
  644: sub parse_directory {
  645:     my ($r,$depth,$currhash,$path,$is_course,$group) = @_;
  646:     my ($cdom,$cnum,$name) = &aboutme_info($r,$is_course);
  647:     $depth++;
  648:     my $output;
  649: 
  650:     my $portfolio_root = &Apache::portfolio::get_portfolio_root($cdom,$cnum,
  651:                                                                 $group);
  652:     my $getpropath = 1;
  653:     my %dirlist = map {
  654: 	    ((split('&',$_,2))[0],1)
  655: 	} &Apache::lonnet::dirlist($portfolio_root.$path,$cdom,$cnum,$getpropath);
  656:     foreach my $item (sort(keys(%{$currhash}))) {
  657:         $output .= &portfolio_row_start();
  658:         $output .= '<td style="padding-left: '.($depth*25).'px">';
  659:         if (ref($currhash->{$item}) eq 'HASH') {
  660:             my $title=&HTML::Entities::encode($item,'<>&"');
  661:             $output .= '<img src="'.&Apache::loncommon::lonhttpdurl("/adm/lonIcons/navmap.folder.open.gif").'" alt="'.&mt('Folder').' '.$title.'" class="LC_icon" />&nbsp;'.$title;
  662: 	    $output .= '</td><td></td></tr>';
  663:             $output .= &parse_directory($r,$depth,$currhash->{$item},
  664: 					$path.'/'.$item,$is_course,$group);
  665:         } else {
  666: 	    my $file_name; 
  667: 	    if ($currhash->{$item} =~ m|/([^/]+)$|) {
  668: 		$file_name = $1;
  669: 	    } else {
  670: 		$file_name = $currhash->{$item};
  671: 	    }
  672: 	    my $have_meta = exists($dirlist{$file_name.'.meta'});
  673:             my $url;
  674:             if ($is_course) {
  675:                 $url = '/uploaded/'.$cdom.'/'.$cnum.'/groups/'.$group.
  676:                        '/portfolio/'.$currhash->{$item};
  677:             } else { 
  678: 	        $url = '/uploaded/'.$cdom.'/'.$cnum.'/portfolio/'.
  679: 		       $currhash->{$item};
  680:             }
  681:             my $showname;
  682: 	    if ($have_meta) {
  683: 		$showname = &Apache::lonnet::metadata($url,'title');
  684: 	    }
  685: 	    if ($showname eq '') {
  686: 		$showname = $file_name;
  687: 	    } else {
  688: 		$showname = $file_name.' ('.$showname.')';
  689: 	    }
  690: 
  691:             $showname=&HTML::Entities::encode($showname,'<>&"');
  692:             $output .= '<a href="'.$url.'">'.
  693: 		'<img alt="" src="'.&Apache::loncommon::icon($currhash->{$item}).'" class="LC_icon" />'.
  694: 		'&nbsp;'.$showname.'</a>';
  695: 	    $output.='</td><td>';
  696: 	    if ($have_meta) {
  697: 		$output.= '<a href="'.$url.'.meta"><img alt="'.&mt('Catalog Information').'" src="'.
  698: 		&Apache::loncommon::lonhttpdurl('/res/adm/pages/catalog.gif').
  699: 		'" class="LC_icon" /></a>';
  700: 	    }
  701: 	    $output .= '</td></tr>';
  702:         }
  703:     }
  704:     return $output;
  705: }
  706: 
  707: 1;
  708: __END__

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