File:  [LON-CAPA] / loncom / interface / lonaboutme.pm
Revision 1.70.2.3: download - view: text, annotated - select for diffs
Wed Mar 18 03:14:18 2009 UTC (15 years, 2 months ago) by raeburn
Branches: version_2_8_X
CVS tags: version_2_8_2, version_2_8_1, GCI_1
Diff to branchpoint 1.70: preferred, unified
- Backport 1.90.

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

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