File:  [LON-CAPA] / loncom / interface / lonaboutme.pm
Revision 1.165: download - view: text, annotated - select for diffs
Sat May 13 20:36:27 2023 UTC (12 months, 1 week ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Use cached aboutme page availability unless user is accessing their own
  aboutme page.

    1: # The LearningOnline Network
    2: # Personal Information Page
    3: #
    4: # $Id: lonaboutme.pm,v 1.165 2023/05/13 20:36:27 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: =pod
   30: 
   31: =head1 NAME
   32: 
   33: pache::lonaboutme
   34: 
   35: =head1 SYNOPSIS
   36: 
   37: (empty)
   38: 
   39: This is part of the LearningOnline Network with CAPA project
   40: described at http://www.lon-capa.org.
   41: 
   42: =head1 OVERVIEW
   43: 
   44: (empty)
   45: 
   46: 
   47: =head1 SUBROUTINES
   48: 
   49: =over
   50: 
   51: =item handler()
   52: 
   53: =item aboutme_info()
   54: 
   55: =item print_portfiles_link()
   56: 
   57: =item build_query_string()
   58: 
   59: =item display_portfolio_header()
   60: 
   61: =item display_portfolio_files()
   62: 
   63: =item portfolio_files()
   64: 
   65: =item build_hierarchy()
   66: 
   67: =item parse_directory()
   68: 
   69: =back
   70: 
   71: =cut
   72: 
   73: 
   74: package Apache::lonaboutme;
   75: 
   76: use strict;
   77: use Apache::Constants qw(:common);
   78: use Apache::loncommon;
   79: use Apache::lonnet;
   80: use Apache::lontexconvert;
   81: use Apache::lonhtmlgateway;
   82: use Apache::lonrss();
   83: use Apache::lonlocal;
   84: use Apache::lonmsgdisplay();
   85: use Apache::lontemplate;
   86: use Apache::longroup;
   87: use Apache::lonhtmlcommon();
   88: use HTML::Entities();
   89: use Image::Magick;
   90: 
   91: sub handler {
   92:     my $r = shift;
   93:     &Apache::loncommon::content_type($r,'text/html');
   94:     $r->send_http_header;
   95:     return OK if $r->header_only;
   96:     my $target=$env{'form.grade_target'};
   97: # ------------------------------------------------------------ Print the screen
   98:     if ($target eq 'tex') {
   99:         $r->print(&Apache::lonprintout::print_latex_header($env{'form.latex_type'}));
  100:     }
  101:     my (undef,undef,$cdom,$cnum,undef,$action)=split(/\//,$r->uri);
  102:     my $is_course;
  103: # Is this even a user?
  104:     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
  105:         &Apache::loncommon::simple_error_page($r,'No info',
  106:             'No user information available');
  107:         return OK;
  108:     } else {
  109:         $is_course = &Apache::lonnet::is_course($cdom,$cnum);
  110:     }
  111: 
  112:     my $clientip = &Apache::lonnet::get_requestor_ip($r);
  113:     my $candisplay = 1;
  114:     if (!$is_course) {
  115:         if ($action ne 'portfolio') {
  116:             if (($env{'user.name'} eq $cnum) && ($env{'user.domain'} eq $cdom)) {
  117:                 $candisplay = &Apache::lonnet::usertools_access($cnum,$cdom,'aboutme');
  118:             } else {
  119:                 $candisplay = &Apache::loncommon::aboutme_on($cnum,$cdom);
  120:             }
  121:             if ((!$candisplay) && ($env{'request.course.id'})) {
  122:                 $candisplay = &aboutme_access($cnum,$cdom);
  123:             }
  124:             if (!$candisplay) {
  125:                 if ($target eq 'tex') {
  126:                     $r->print('\noindent{\large\textbf{'.&mt('No user personal information page available').'}}\\\\\\\\');
  127:                 } else {
  128:                     $r->print(&Apache::loncommon::start_page("Personal Information Page"));
  129:                     $r->print('<p class="_LC_info">'.&mt('No user personal information page available') .'</p>'.
  130:                         &mt('This is a result of one of the following:').'<ul>'.
  131:                         '<li>'.&mt('The administrator of this domain has disabled personal information page functionality for this specific user.').'</li>'.
  132:                         '<li>'.&mt('The domain has been configured to disable, by default, personal information page functionality for all users in the domain.').'</li>'.
  133:                         '</ul>');
  134:                     $r->print(&Apache::loncommon::end_page());
  135:                 }
  136:                 return OK;
  137:             }
  138:         }
  139:     }
  140: 
  141: # --------------------------------------------------------- The syllabus fields
  142:     my %syllabusfields=&Apache::lonlocal::texthash(
  143:        'aaa_contactinfo'   => 'Contact Information',
  144:        'bbb_aboutme'       => 'Personal Information',
  145:        'ccc_webreferences' => 'Web References');
  146: 
  147: # ------------------------------------------------------------ Get Query String
  148:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  149:                         ['forceedit','forcestudent','todocs',
  150:                          'register','popup','folderpath','title']);
  151: # ----------------------------------------------- Available Portfolio file display
  152:     if (($target ne 'tex') && ($action eq 'portfolio')) {
  153:         &display_portfolio_header($r,$is_course);
  154:         if ((!$is_course) && (!&Apache::lonnet::usertools_access($cnum,$cdom,'portfolio'))) {
  155:             $r->print('<h2>'.&mt('No user portfolio available') .'</h2>'.
  156:                 &mt('This is a result of one of the following:').'<ul>'.
  157:                 '<li>'.&mt('The administrator of this domain has disabled portfolio functionality for this specific user.').'</li>'.
  158:                 '<li>'.&mt('The domain has been configured to disable, by default, portfolio functionality for all users in the domain.').'</li>'.
  159:                 '</ul>');
  160:         } else {
  161:             my ($blocked,$blocktext) =
  162:                 &Apache::loncommon::blocking_status('port',$clientip,$cnum,$cdom);
  163:             if (!$blocked) {
  164:                 &display_portfolio_files($r,$is_course);
  165:             } else {
  166:                 $r->print($blocktext);
  167:             }
  168:         }
  169:         $r->print(&Apache::loncommon::end_page());
  170:         return OK;
  171:     }
  172: 
  173:     if ($is_course) {
  174:         if ($target ne 'tex') {
  175:             my $args = {'function'       => undef,
  176:                         'domain'         => $cdom};
  177:             if ($env{'form.register'}) {
  178:                 $args->{'force_register'} = $env{'form.register'};
  179:             } else {
  180:                 my %coursedescription = 
  181:                     &Apache::lonnet::coursedescription($cdom.'_'.$cnum);
  182:                 my $cdescr = $coursedescription{'description'};
  183:                 my $brcrum = [{href=>"/adm/$cdom/$cnum/aboutme",
  184:                                text=>&mt('Course Information - [_1]',$cdescr),
  185:                                no_mt=>1}
  186:                              ];
  187:                 $args->{'bread_crumbs'} = $brcrum;
  188:             }
  189:             my $start_page = &Apache::loncommon::start_page(
  190:                                  "Course Information",undef,$args);
  191:             $r->print($start_page);
  192:             $r->print('<h2>'.&mt('Group Portfolio').'</h2>');
  193:             &print_portfiles_link($r,$is_course);
  194:             $r->print(&Apache::loncommon::end_page());
  195:         }
  196:         return OK;
  197:     }
  198: 
  199: #------------Get rights
  200:     my %courseenv=&Apache::lonnet::dump('environment',$cdom,$cnum);
  201:     my %syllabus=&Apache::lonnet::dump('aboutme',$cdom,$cnum);
  202:     my ($allowed,$coursedomain,$coursenum);
  203:     if ($env{'request.course.id'}) {
  204:         $coursedomain = $env{'course.'.$env{'request.course.id'}.'.domain'};
  205:         $coursenum = $env{'course.'.$env{'request.course.id'}.'.num'};
  206:     }
  207:     my ($cfile) = 
  208:         &Apache::lonnet::can_edit_resource($env{'request.noversionuri'},
  209:                                            $coursenum,$coursedomain,
  210:                                            $env{'request.noversionuri'},
  211:                                            $env{'request.symb'});
  212:     if ($cfile ne '') {
  213:         $allowed = 1;
  214:     }
  215: 
  216:     if (!$env{'form.forceedit'} or $target eq 'tex') { $allowed=0; }
  217: 
  218: # --------------------------------------- There is such a user, get environment
  219: 
  220:     if ($target ne 'tex') {
  221:         my $rss_link = &Apache::lonrss::rss_link($cnum,$cdom);
  222:         my $args = {'function' => undef,
  223:                     'domain'   => $cdom,
  224:                     'force_register' => $env{'form.register'},
  225:                    };
  226:         if ($env{'form.popup'}) { # Don't show breadcrumbs in popup window 
  227:             $args->{'no_nav_bar'} = 1;
  228:         } elsif (!$env{'form.register'}) { #Don't show breadcrumbs twice, when this page is part of course content and you call it
  229:             if (($env{'request.course.id'}) &&
  230:                 ($env{'form.folderpath'} =~ /^supplemental/)) {
  231:                 &Apache::loncommon::validate_folderpath(1,'',$coursenum,$coursedomain);
  232:                 my $crstype = &Apache::loncommon::course_type();
  233:                 my $title = $env{'form.title'};
  234:                 if ($title eq '') {
  235:                     $title = &mt('Personal Information Page');
  236:                 }
  237:                 $title = &HTML::Entities::encode($title,'\'"<>&');
  238:                 my $brcrum =
  239:                     &Apache::lonhtmlcommon::docs_breadcrumbs(undef,$crstype,undef,$title,1);
  240:                 if (ref($brcrum) eq 'ARRAY') {
  241:                     $args->{'bread_crumbs'} = $brcrum;
  242:                 }
  243:             } else {
  244:                 $args->{'bread_crumbs'} = [{href=>"/adm/$cdom/$cnum/aboutme",
  245:                                             text=>"Personal Information Page"}];
  246:             }
  247:         }
  248:         my $start_page = &Apache::loncommon::start_page('Personal Information Page',$rss_link,$args);
  249:         $r->print($start_page);
  250:    }
  251:    my ($blocked,$blocktext) =
  252:        &Apache::loncommon::blocking_status('about',$clientip,$cnum,$cdom);
  253:    if ($blocked) {
  254:        if ($target eq 'tex') {
  255:            $r->print('\noindent{\large\textbf{'.&mt('No user personal information page available').'}}\\\\\\\\');
  256:        } else {
  257:            $r->print($blocktext);
  258:        }
  259:        $r->print(&Apache::loncommon::end_page());
  260:        return OK;
  261:    }
  262: 
  263: #----------------Print Privacy note (edit mode) or last modified date. 
  264: 
  265:     if ($target ne 'tex') {
  266:         #Print Privacy Note
  267:         if ($allowed) {
  268:             $r->print('<div class="LC_info">'
  269:                 .'<b>'.&mt('Privacy Note:').'</b> '
  270:                 .&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.')
  271:                 .'</div>'
  272:             );
  273:         } elsif ($syllabus{'uploaded.lastmodified'}) {
  274:         #Print last modified
  275:             my $lastmod=$syllabus{'uploaded.lastmodified'};
  276:             $lastmod=($lastmod?&Apache::lonlocal::locallocaltime($lastmod):&mt('never'));
  277:             $r->print('<div class="LC_info">');
  278:             $r->print(&mt('Last updated').': '.$lastmod . '');
  279:             $r->print('</div>');
  280:         }
  281:     }
  282: 
  283: #------Print Headtitle
  284:      if ($target ne 'tex') {
  285:          $r->print('<div class="LC_Box">'.
  286:                    '<h2 class="LC_hcell">'.&Apache::loncommon::plainname($cnum,$cdom).'</h2>');
  287:          if ($allowed) {
  288:              $r->print('<div style="margin: 0; float:left;">');
  289:              if ($courseenv{'nickname'}) {
  290:                  $r->print('<h2>&quot;'.$courseenv{'nickname'}.'&quot;</h2>');
  291:              }
  292:              $r->print('<h3>'.&Apache::lonnet::domain($cdom,'description').'</h3></div>');
  293:              #Print Help Text
  294:              $r->print('<div style="margin: 0; float:right;">'.
  295:                        &Apache::loncommon::help_open_topic('Uploaded_Templates_TextBoxes',&mt('Help with filling in text boxes')).
  296:                        '</div><br clear="all" />');
  297:          } else {
  298:              if ($courseenv{'nickname'}) {
  299:                  $r->print('<h2>&quot;'.$courseenv{'nickname'}.'&quot;</h2>');
  300:              }
  301:              $r->print('<h3>'.&Apache::lonnet::domain($cdom,'description').'</h3>');
  302:          }
  303:      } else {
  304:         $r->print('\noindent{\large\textbf{'.&Apache::loncommon::plainname($cnum,$cdom).'}}\\\\\\\\');
  305:         $r->print('\textbf{'.&Apache::lonnet::domain($cdom,'description').'}\\\\');
  306:     }
  307: # does this user have privileges to post, etc?
  308: 
  309: 
  310:     my $query_string;
  311: 
  312:     if (($env{'form.uploaddoc.filename'}) &&
  313:           ($env{'form.storeupl'}) && ($allowed)) {
  314:         if ($env{'form.uploaddoc.filename'}=~/\.(gif|jpg|png|jpeg)$/i) {
  315:             if ($syllabus{'uploaded.photourl'}) {
  316:                 &Apache::lonnet::removeuploadedurl($syllabus{'uploaded.photourl'});
  317:             }
  318:             $syllabus{'uploaded.photourl'}=
  319:                 &Apache::lonnet::userfileupload('uploaddoc',undef,'aboutme',
  320:                     undef,undef,undef,undef,undef,undef,undef,'400','500');
  321:          }
  322:         $syllabus{'uploaded.lastmodified'}=time;
  323:         &Apache::lonnet::put('aboutme',\%syllabus,$cdom,$cnum);
  324:     }
  325:     if ($allowed && $env{'form.delupl'}) {
  326:         if ($syllabus{'uploaded.photourl'}) {
  327:             &Apache::lonnet::removeuploadedurl($syllabus{'uploaded.photourl'});
  328:             delete($syllabus{'uploaded.photourl'});
  329:             &Apache::lonnet::del('aboutme',['uploaded.photourl'],$cdom,$cnum);
  330:         }
  331:     }
  332:     if (($allowed) && ($env{'form.storesyl'})) {
  333:         foreach my $syl_field (keys(%syllabusfields)) {
  334:             my $field=$env{'form.'.$syl_field};
  335:             chomp($field);
  336:             my $gateway = Apache::lonhtmlgateway->new();
  337:             $field = $gateway->process_incoming_html($field,1);
  338:             $syllabus{$syl_field}=$field;
  339:         }
  340:         $syllabus{'uploaded.lastmodified'}=time;
  341:         &Apache::lonnet::put('aboutme',\%syllabus,$cdom,$cnum);
  342:     }
  343: 
  344:     my $image;
  345: # ---------------------------------------------------------------- Get syllabus
  346:     if (($syllabus{'uploaded.lastmodified'}) || ($allowed)) {
  347:         if ($syllabus{'uploaded.photourl'}) {
  348:             &Apache::lonnet::allowuploaded('/adm/aboutme',$syllabus{'uploaded.photourl'});
  349: 
  350:             $image=qq|<img name="userPhoto" src="$syllabus{'uploaded.photourl'} " class="LC_AboutMe_Image" alt="Photo of the user" />|;
  351: 
  352:             if ($target eq 'tex') {
  353:                 $image=&Apache::lonxml::xmlparse($r,'tex',$image);
  354:             }
  355:         }
  356: 
  357:         if ($allowed) {
  358:             $r->print(
  359:                 '<form name="UploadPhoto" method="post" enctype="multipart/form-data" action="">'.
  360:                 '<h3>'.&mt('Upload a Photo').'</h3>'.
  361:                 '<p class="LC_info">'.
  362:                 &mt('LON-CAPA will automatically scale your uploaded file so the image will not exceed a width of 400px and a height of 500px.').'</p>'.
  363:                 '<input type="file" name="uploaddoc" size="50" />'.
  364:                 '<input type="submit" name="storeupl" value="'.&mt('Upload').'" />'.
  365:                 '<input type="hidden" name="popup" value="'.$env{'form.popup'}.'" />'.
  366:                 '</form>');
  367:             if ($syllabus{'uploaded.photourl'}) {
  368:                 $r->print('<form name="delPhoto" method="post" action="" ><input type="submit" name="delupl" value="'.&mt('Delete Photo').'" /> </form>')
  369:             }
  370:             $r->print('<p></p>');
  371:         }
  372: 
  373:         if($allowed) {
  374:             $r->print('<form name="lonaboutmeFields" method="post" action="" >');
  375:         }
  376: 
  377:         if ($target ne 'tex') { #print Image
  378:             $r->print($image.'<div class="LC_clear_float_footer"></div>');
  379: 
  380:         } #End Print Image
  381: 
  382:        #Print Content eg. Contactinfo aboutme,...
  383:         &Apache::lontemplate::print_aboutme_content_template($r,$allowed,$target,\%syllabusfields,\%syllabus);
  384:        #End Print Content
  385: 
  386:         if ($target ne 'tex') { #Begin Print RSS and portfiles
  387:             &print_portfiles_link($r,$is_course);
  388:             if (&Apache::lonrss::advertisefeeds($cnum,$cdom) ne '') {
  389:                 &Apache::lontemplate::print_start_template($r,'RSS Feeds and Blogs','LC_Box');
  390:                 $r->print(&Apache::lonrss::advertisefeeds($cnum,$cdom));
  391:                 &Apache::lontemplate::print_end_template($r);
  392:             }
  393: 
  394:         } #End  Print RSS and portfiles
  395: 
  396: 
  397:         if ($allowed) {
  398:             if ($env{'form.popup'}) {
  399:                 $r->print('<input type="hidden" name="popup" value="'.
  400:                     $env{'form.popup'}.'" />');
  401:             }
  402:             $r->print('</form>');
  403:         }
  404:         if ($target ne 'tex') {$r->print('<br />');} else {$r->print('\\\\');}
  405:     } else {
  406:         $r->print('<p class="LC_info">'.&mt('No personal information provided').'.</p>');
  407:     }
  408: 
  409:     if ($env{'request.course.id'}
  410:         && &Apache::lonnet::allowed('srm',$env{'request.course.id'})
  411:         && &Apache::lonnet::in_course($cdom,$cnum,$coursedomain,$coursenum,undef,1)) {
  412:         if ($target ne 'tex') {
  413:             $r->print('<a name="coursecomment" />');
  414:             &Apache::lontemplate::print_start_template($r,&mt('User Notes, Records of Face-To-Face Discussions, and Critical Messages in Course'),'LC_Box');
  415:             $r->print('<span class="LC_info">');
  416:             $r->print(&mt('Shared by course faculty and staff').&Apache::loncommon::help_open_topic("Course_Face_To_Face_Records,Course_Critical_Message"));
  417:             $r->print('</span>');
  418:             &Apache::lonmsgdisplay::disfacetoface($r,$cnum,$cdom);
  419:             &Apache::lontemplate::print_end_template($r);
  420: 
  421:         } else {
  422:             $r->print('\\\\\textbf{'.&mt('User Notes, Records of Face-To-Face Discussions, and Critical Messages in Course').'}\\\\'.&mt('Shared by course faculty and staff').'\\\\\\\\');
  423:             &Apache::lonmsgdisplay::disfacetoface($r,$cnum,$cdom,$target);
  424:         }
  425:     }
  426:     if ($target ne 'tex') {
  427:         $r->print('</div>');
  428:         if ($env{'form.popup'}) {
  429:             $r->print('<p><a href="javascript:window.close()">'.&mt('Close window').'</a></p>');
  430:         }
  431:         $r->print(&Apache::loncommon::end_page());
  432:     } else {
  433:         $r->print('\end{document}');
  434:     }
  435: 
  436: 
  437: 
  438:     return OK;
  439: }
  440: 
  441: sub aboutme_info {
  442:     my ($r,$is_course) = @_;
  443:     my (undef,undef,$cdom,$cnum)=split(/\//,$r->uri);
  444:     my $name;
  445:     if (!$is_course) {
  446:         $name = &Apache::loncommon::plainname($cnum,$cdom);
  447:     }
  448:     return ($cdom,$cnum,$name);
  449: }
  450: 
  451: sub print_portfiles_link {
  452:     my ($r,$is_course) = @_;
  453:     my ($cdom,$cnum,$name) = &aboutme_info($r,$is_course);
  454:     my $filecounts = &portfolio_files($r,'showlink',undef,$is_course,
  455:                                       $cdom,$cnum,$name);
  456:     my $query_string = &build_query_string();
  457:     my $output;
  458:     my %lt = &Apache::lonlocal::texthash(
  459:         'vpfi' => 'Viewable portfolio files',
  460:         'vgpf' => 'Viewable group portfolio files',
  461:         'difl' => 'Display file listing',
  462:     );
  463:     if ($filecounts->{'both'} > 0) {
  464:         $output = '<div class="LC_Box"><h4 class="LC_hcell">';
  465:         $output .= ($is_course?$lt{'vgpf'}:$lt{'vpfi'}).'</h4>';
  466: 
  467:        #$output = '<h4>'.($is_course?$lt{'vgpf'}:$lt{'vpfi'}).'</h4>';
  468:         $output .= '<a href="/adm/'.$cdom.'/'.$cnum.'/aboutme/portfolio'.
  469:             $query_string.'">'.$lt{'difl'}.
  470:             '</a><br /><br />';
  471:         if ($filecounts->{'both'} == 1) {
  472:             if ($is_course) {
  473:                 $output .= &mt('One group portfolio file is available.').'<ul>';
  474:             } else {
  475:                 $output .= &mt('One portfolio file owned by [_1] is available.',$name).'<ul>';
  476:             }
  477:         } else {
  478:             if ($is_course) {
  479:                 $output .= &mt('A total of [_1] group portfolio files are available.',$filecounts->{'both'}).'<ul>';
  480:             } else {
  481:                 $output .= &mt('A total of [_1] portfolio files owned by [_2] are available.',$filecounts->{'both'},$name).'<ul>';
  482:             }
  483:         }
  484:         if ($filecounts->{'withoutpass'}) {
  485:             $output .= '<li>'.&mt('[quant,_1,file is,files are] publicly accessible.',$filecounts->{'withoutpass'}).'</li>';
  486:         }
  487:         if ($filecounts->{'withpass'}) {
  488:             $output .= '<li>'.&mt('[quant,_1,file requires,files require] a passphrase for access.',$filecounts->{'withpass'}).'</li>';
  489:         }
  490:         $output .= '</ul>';
  491:         $output .= '</div>';
  492:     } elsif ($is_course) {
  493:         $output .= '<div class="LC_info">'.&mt('There are currently no publicly accessible or password protected group portfolio files.').'</div>'; 
  494:     }
  495:     $r->print($output);
  496:     return;
  497: }
  498: 
  499: sub build_query_string {
  500:     my ($new_items) = @_;
  501:     my $query_string;
  502:     my @formelements = ('register');
  503:     my $new = 0;
  504:     if (ref($new_items) eq 'HASH') {
  505:         $new = 1;
  506:         if (!defined($new_items->{'forceedit'}) &&
  507:             !defined($new_items->{'forcestudent'})) {
  508:             push(@formelements,('forceedit','forcestudent'));
  509:         }
  510:     } else {
  511:         push(@formelements,('forceedit','forcestudent'));
  512:     }
  513:     foreach my $element (@formelements) {
  514:         if (exists($env{'form.'.$element})) {
  515:             if ((!$new) || (!defined($new_items->{$element}))) {
  516:                 $query_string .= '&amp;'.$element.'='.$env{'form.'.$element};
  517:             }
  518:         }
  519:     }
  520:     if ($new) {
  521:         foreach my $key (keys(%{$new_items})) {
  522:             $query_string .= '&amp;'.$key.'='.$new_items->{$key};
  523:         }
  524:     }
  525:     $query_string =~ s/^\&amp;/\?/;
  526:     return $query_string;
  527: }
  528: 
  529: sub display_portfolio_header {
  530:     my ($r,$is_course) = @_;
  531:     my ($cdom,$cnum,$name) = &aboutme_info($r,$is_course);
  532:     my $query_string = &build_query_string();
  533:     my $args = {'domain' => $cdom};
  534:     if ($env{'form.forcestudent'}) {
  535:         $args->{'function'} = 'student';
  536:     }
  537:     my $output;
  538:     if ($is_course) {
  539:         if (($env{'request.course.id'} eq $cdom.'_'.$cnum) && 
  540:             ($env{'form.register'})) {
  541:             $args->{force_register} = $env{'form.register'};
  542:         } else {
  543:             my %coursedescription = &Apache::lonnet::coursedescription($cdom.'_'.$cnum);
  544:             my $cdescr = $coursedescription{'description'}; 
  545:             my $brcrum = [{href=>"/adm/$cdom/$cnum/aboutme".$query_string,
  546:                            text=>&mt('Course Information - [_1]',$cdescr),
  547:                            no_mt=>1},
  548:                           {href=>"/adm/$cdom/$cnum/aboutme/portfolio".$query_string,
  549:                            text=>'Viewable group portfolio files'}
  550:                          ];
  551:             $args->{bread_crumbs} = $brcrum;
  552:         }
  553:         $output = &Apache::loncommon::start_page('Viewable group portfolio files',undef,$args).
  554:                   '<h3>'.&mt('Group Portfolio files').'</h3>';
  555:     } else {
  556:         if ($env{'request.course.id'} && $env{'form.register'}) {
  557:             $args->{force_register} = $env{'form.register'};
  558:         } else {
  559:             my $brcrum = [{href  => "/adm/$cdom/$cnum/aboutme".$query_string,
  560:                           text  => &mt('Personal Information Page - [_1]',$name),
  561:                           title => &mt('Go to personal information page for [_1]',$name),
  562:                           no_mt => 1},
  563:                          {href  => "/adm/$cdom/$cnum/aboutme/portfolio".$query_string,
  564:                           text  => &mt('Viewable files'),
  565:                           title => &mt('Viewable portfolio files for [_1]',$name),
  566:                           no_mt => 1}
  567:                          ];
  568:             $args->{bread_crumbs} = $brcrum;
  569:         } 
  570:         $output  = 
  571:             &Apache::loncommon::start_page('Viewable portfolio files',
  572:                                            undef,$args).
  573:             '<h3>'.&mt('Portfolio files for [_1]',$name).'</h3>';
  574:     }
  575:     $r->print($output);
  576:     return;
  577: }
  578: 
  579: sub display_portfolio_files {
  580:     my ($r,$is_course) = @_;
  581:     my ($cdom,$cnum,$name) = &aboutme_info($r,$is_course);
  582:     my %lt = &Apache::lonlocal::texthash(
  583:         'withoutpass' => 'passphrase not required',
  584:         'withpass'    => 'passphrase protected',
  585:         'both'        => 'all access types ',
  586:     );
  587: 
  588:     my $portaccess = 'withoutpass';
  589:     if (exists($env{'form.portaccess'})) {
  590:         $portaccess = $env{'form.portaccess'};
  591:     }
  592: 
  593:     my $output = '<form action="'.&HTML::Entities::encode($r->uri,'<>&"')
  594:         .'" name="displaystatus" method="post">'.
  595:         &mt('File access type: ').'<select name="portaccess">';
  596:     foreach my $type ('withoutpass','withpass','both') {
  597:         $output .= '<option value="'.$type.'" ';
  598:         if ($portaccess eq $type) {
  599:             $output .= 'selected="selected"';
  600:         }
  601:         $output .= '>'.$lt{$type}.'</option>';
  602:     }
  603:     $output .= '</select>'."\n";
  604:     if ($env{'form.register'}) {
  605:         $output .= '<input type="hidden" name="register" value="'.$env{'form.register'}.'" />'."\n";
  606:     }
  607:     $output .= '<input type="submit" name="portaccessbutton" value="'.
  608:                &mt('Update display').'" />'.
  609:                '</form><br /><br />';
  610:     $r->print($output);
  611:     my $filecounts = &portfolio_files($r,'listfiles',\%lt,$is_course,
  612:                                       $cdom,$cnum,$name);
  613:     if (!($env{'user.name'} eq 'public' && $env{'user.domain'} eq 'public')) {
  614:         if ($env{'request.course.id'} && $env{'form.register'}) {
  615:             my $query_string = &build_query_string();
  616:             $r->print('<br /><a href="/adm/'.$cdom.'/'.$cnum.
  617:                       '/aboutme'.$query_string.'">');
  618:             if ($is_course) {
  619:                 $r->print(&mt('Course Information page'));
  620:             } else {
  621:                 $r->print(&mt('Information about [_1]',$name));
  622:             }
  623:             $r->print('</a>');
  624:         }
  625:     }
  626:     return;
  627: }
  628: 
  629: sub portfolio_files {
  630:     my ($r,$mode,$lt,$is_course,$cdom,$cnum,$name) = @_;
  631:     my $filecounts = {
  632:                        withpass    => 0,
  633:                        withoutpass => 0,
  634:                        both        => 0,
  635:                      };
  636:     my $current_permissions =
  637:     &Apache::lonnet::get_portfile_permissions($cdom,$cnum);
  638:     my %access_controls =
  639:     &Apache::lonnet::get_access_controls($current_permissions);
  640:     my $portaccess;
  641:     if ($mode eq 'showlink') {
  642:         $portaccess = 'both';
  643:     } else {
  644:         $portaccess = 'withoutpass';
  645:         if (exists($env{'form.portaccess'})) {
  646:             $portaccess = $env{'form.portaccess'};
  647:         }
  648:     }
  649: 
  650:     my $diroutput;
  651:     if ($is_course) {
  652:         my %files_by_group;
  653:         my %curr_groups = &Apache::longroup::coursegroups($cdom,$cnum);
  654:         foreach my $filename (sort(keys(%access_controls))) {
  655:             my ($group,$path) = split('/',$filename,2);
  656:             if (exists($curr_groups{$group})) {
  657:                 $files_by_group{$group}{$path} = $access_controls{$filename};
  658:             }
  659:         }
  660:         foreach my $group (sort(keys(%files_by_group))) {
  661:             my %fileshash;
  662:             my $grpout .= &build_hierarchy($r,$cdom,$cnum,$portaccess,
  663:                                            $is_course,$filecounts,$mode,
  664:                                            $files_by_group{$group},
  665:                                            \%fileshash,$group);
  666:             if ($grpout) {
  667:                 $diroutput .= '<h3>'.$group.'</h3>'.$grpout.'<br />';
  668:             }
  669:         }
  670:     } else {
  671:         my %allfileshash;
  672:         $diroutput = &build_hierarchy($r,$cdom,$cnum,$portaccess,$is_course,
  673:                                       $filecounts,$mode,\%access_controls,
  674:                                       \%allfileshash);
  675:     }
  676:     if ($mode eq 'listfiles') {
  677:         if ($filecounts->{'both'}) {
  678:              $r->print($diroutput);
  679:         } else {
  680:             my $access_text;
  681:             if (ref($lt) eq 'HASH') {
  682:                 $access_text = $lt->{$portaccess};
  683:             }
  684:             $r->print(&mt('There are no available files of the specified access type: [_1]',$access_text));
  685:         }
  686:     }
  687:     return $filecounts;
  688: }
  689: 
  690: sub build_hierarchy {
  691:     my ($r,$cdom,$cnum,$portaccess,$is_course,$filecounts,$mode,$access_info,
  692:         $allfileshash,$group) = @_;
  693:     my $clientip = &Apache::lonnet::get_requestor_ip($r);
  694:     foreach my $filename (sort(keys(%{$access_info}))) {
  695:         my $access_status =
  696:            &Apache::lonnet::get_portfolio_access($cdom,$cnum,$filename,$group,$clientip,
  697:                                                  $access_info->{$filename});
  698:         if ($portaccess eq 'both') {
  699:             if (($access_status ne 'ok') &&
  700:                 ($access_status !~  /^[^:]+:guest_/)) {
  701:                 next;
  702:             }
  703:         } elsif ($portaccess eq 'withoutpass') {
  704:             if ($access_status ne 'ok') {
  705:                 next;
  706:             }
  707:         } elsif ($portaccess eq 'withpass') {
  708:             if ($access_status !~  /^[^:]+:guest_/) {
  709:                 next;
  710:             }
  711:         }
  712:         if ($mode eq 'listfiles') {
  713:             $filename =~ s/^\///;
  714:             my @pathitems = split('/',$filename);
  715:             my $lasthash = $allfileshash;
  716:             while (@pathitems > 1) {
  717:                 my $newlevel = shift(@pathitems);
  718:                 if (!exists($lasthash->{$newlevel})) {
  719:                     $lasthash->{$newlevel} = {};
  720:                 }
  721:                 $lasthash = $lasthash->{$newlevel};
  722:             }
  723:             $lasthash->{$pathitems[0]} = $filename;
  724:         }
  725:         if ($access_status eq 'ok') {
  726:             $filecounts->{'withoutpass'} ++;
  727:         } elsif ($access_status =~  /^[^:]+:guest_/) {
  728:             $filecounts->{'withpass'} ++;
  729:         }
  730:     }
  731:     $filecounts->{'both'} =  $filecounts->{'withoutpass'} +
  732:                               $filecounts->{'withpass'};
  733:     my $output;
  734:     if ($mode eq 'listfiles') {
  735:         if ($filecounts->{'both'} > 0) {
  736:             $output = &Apache::loncommon::start_data_table();
  737:             $output .= &parse_directory($r,0,$allfileshash,'',$is_course,
  738:                                         $group);
  739:             $output .= &Apache::loncommon::end_data_table();
  740:         }
  741:     }
  742:     return $output;
  743: }
  744: 
  745: sub parse_directory {
  746:     my ($r,$depth,$currhash,$path,$is_course,$group) = @_;
  747:     my ($cdom,$cnum,$name) = &aboutme_info($r,$is_course);
  748:     $depth++;
  749:     my $output;
  750: 
  751:     my $portfolio_root = &Apache::portfolio::get_portfolio_root($cdom,$cnum,
  752:                                                                 $group);
  753:     my $getpropath = 1;
  754:     my ($listref,$listerror) =
  755:         &Apache::lonnet::dirlist($portfolio_root.$path,$cdom,$cnum,$getpropath);
  756:     my %dirlist;
  757:     if (ref($listref) eq 'ARRAY') {
  758:         %dirlist = map { ((split('&',$_,2))[0],1) } @{$listref};
  759:     }
  760:     foreach my $item (sort(keys(%{$currhash}))) {
  761:         $output .= &Apache::loncommon::start_data_table_row();
  762:         $output .= '<td style="padding-left: '.($depth*25).'px">';
  763:         if (ref($currhash->{$item}) eq 'HASH') {
  764:             my $title=&HTML::Entities::encode($item,'<>&"');
  765:             $output .= '<img src="'.&Apache::loncommon::lonhttpdurl("/adm/lonIcons/navmap.folder.open.gif").'" alt="'.&mt('Folder').' '.$title.'" class="LC_icon" />&nbsp;'.$title;
  766:             $output .= '</td><td>&nbsp;</td>'
  767:                       .&Apache::loncommon::end_data_table_row();
  768:             $output .= &parse_directory($r,$depth,$currhash->{$item},
  769:                     $path.'/'.$item,$is_course,$group);
  770:         } else {
  771:             my $file_name;
  772:             if ($currhash->{$item} =~ m|/([^/]+)$|) {
  773:                 $file_name = $1;
  774:             } else {
  775:                 $file_name = $currhash->{$item};
  776:             }
  777:             my $have_meta = exists($dirlist{$file_name.'.meta'});
  778:             my $url;
  779:             if ($is_course) {
  780:                 $url = '/uploaded/'.$cdom.'/'.$cnum.'/groups/'.$group.
  781:                     '/portfolio/'.$currhash->{$item};
  782:             } else {
  783:                 $url = '/uploaded/'.$cdom.'/'.$cnum.'/portfolio/'.$currhash->{$item};
  784:             }
  785:             my $showname;
  786:             if ($have_meta) {
  787:                 $showname = &Apache::lonnet::metadata($url,'title');
  788:             }
  789:             if ($showname eq '') {
  790:                 $showname = $file_name;
  791:             } else {
  792:                 $showname = $file_name.' ('.$showname.')';
  793:             }
  794: 
  795:             $showname=&HTML::Entities::encode($showname,'<>&"');
  796:             $output .= '<a href="'.$url.'">'.
  797:                 '<img alt="" src="'.&Apache::loncommon::icon($currhash->{$item}).'" class="LC_icon" />'.
  798:                 '&nbsp;'.$showname.'</a>';
  799:             $output.='</td><td>';
  800:             if ($have_meta) {
  801:                 $output.= '<a href="'.$url.'.meta"><img alt="'.&mt('Metadata').'" src="'.
  802:                     &Apache::loncommon::lonhttpdurl('/res/adm/pages/catalog.png').
  803:                     '" class="LC_icon" /></a>';
  804:             }
  805:             $output .= '</td>'
  806:                 .&Apache::loncommon::end_data_table_row();
  807:         }
  808:     } 
  809:     return $output;
  810: }
  811: 
  812: sub aboutme_access {
  813:     my ($uname,$udom) = @_;
  814:     my $privcheck = $env{'request.course.id'};
  815:     if ($env{'request.course.sec'} ne '') {
  816:         $privcheck .= '/'.$env{'request.course.sec'};
  817:     }
  818:     my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
  819:     my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
  820:     if (($cdom eq '') || ($cnum eq '')) {
  821:         my %coursehash = &Apache::lonnet::coursedescription($env{'request.course.id'});
  822:         $cdom = $coursehash{'domain'};
  823:         $cnum = $coursehash{'cnum'};
  824:     }
  825:     if ((&Apache::lonnet::allowed('srm',$privcheck)) ||
  826:         (&Apache::lonnet::allowed('dff',$privcheck))) {
  827:         if (&Apache::lonnet::in_course($uname,$udom,$cnum,$cdom,undef,1)) {
  828:             return 1;
  829:         }
  830:     }
  831:     return;
  832: }
  833: 
  834: 1;
  835: __END__

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