File:  [LON-CAPA] / loncom / interface / lonsimplepage.pm
Revision 1.94: download - view: text, annotated - select for diffs
Tue Feb 23 14:18:12 2010 UTC (14 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- New routine in longroup.pm: &get_group_link() to get link to group homepage.
  - supports situations where group homepage has encrypted url.

    1: # The LearningOnline Network
    2: # Simple Page Editor
    3: #
    4: # $Id: lonsimplepage.pm,v 1.94 2010/02/23 14:18:12 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::lonsimplepage;
   30: 
   31: use strict;
   32: use Apache::Constants qw(:common);
   33: use Apache::loncommon;
   34: use Apache::lontemplate;
   35: use Apache::lonnet;
   36: use Apache::lontexconvert;
   37: use Apache::lonfeedback;
   38: use Apache::lonlocal;
   39: use Apache::lonprintout;
   40: use Apache::lonxml;
   41: use Apache::longroup;
   42: use Apache::lonnavmaps();
   43: use HTML::Entities();
   44: use LONCAPA;
   45: 
   46: sub get_db_name {
   47:     my ($url) = @_;
   48:     my ($udom,$uname,$marker)=(split(m{/},$url))[2,3,4];
   49:     my $dom = $env{'course.'.$env{'request.course.id'}.'.domain'};
   50:     my $crs = $env{'course.'.$env{'request.course.id'}.'.num'};
   51: 
   52:     my $db_name;
   53: 
   54:     if ($dom && $crs && ($udom eq $dom) && ($uname eq $crs)) {
   55:         $marker =~ s/\W//g;
   56:         $db_name = 'grppage_'.$marker;
   57:     } else {
   58:         $marker=~s/\D//g;
   59:         $db_name = 'smppage_'.$marker;
   60:     }
   61:     return if (!defined($marker));
   62: 
   63:     return $db_name;
   64: }
   65: 
   66: sub handler {
   67:     my $r = shift;
   68:     &Apache::loncommon::content_type($r,'text/html');
   69:     $r->send_http_header;
   70:     return OK if $r->header_only;
   71:     my $target=$env{'form.grade_target'};
   72: # ------------------------------------------------------------ Print the screen
   73:     if ($target eq 'tex') {
   74:         $r->print(&Apache::lonprintout::print_latex_header($env{'form.latex_type'}));
   75:     }
   76: 
   77: # Is this even in a course?
   78:     unless ($env{'request.course.id'}) {
   79:         if ($target ne 'tex') {
   80:             &Apache::loncommon::simple_error_page($r,'','Not in a course');
   81:         } else {
   82:             $r->print('\textbf{Not in a course}\end{document}');
   83:         }
   84:         return OK;
   85:     }
   86: 
   87:     my $db_name = &get_db_name($r->uri);
   88: 
   89:     my $dom = $env{'course.'.$env{'request.course.id'}.'.domain'};
   90:     my $crs = $env{'course.'.$env{'request.course.id'}.'.num'};
   91:     my ($group,$group_desc,$group_home_view,$group_home_edit,
   92:         $group_view_perm,$group_edit_perm);
   93:     my %curr_group = ();
   94:     my %groupinfo = ();
   95:     if ($db_name =~ /^grppage_/) {
   96:         $group = (split(m{/},$r->uri))[4];
   97:         $group =~ s/\W//g;
   98:         my %curr_groups = &Apache::longroup::coursegroups($dom,$crs,$group);
   99:         if (!%curr_groups) {
  100:             &Apache::loncommon::simple_error_page($r,'','Invalid group name');
  101:             return OK;
  102:         }
  103:         %groupinfo =
  104:         &Apache::longroup::get_group_settings($curr_groups{$group});
  105:         $group_desc = &unescape($groupinfo{'description'});
  106:     }
  107: 
  108:     if (!$db_name) {
  109:         &Apache::loncommon::simple_error_page($r,'','Invalid call');
  110:         return OK;
  111:     }
  112: 
  113: # --------------------------------------------------------- The syllabus fields
  114:     my %syllabusfields=&Apache::lonlocal::texthash(
  115:        'aaa_title'         => 'Page Title',
  116:        'bbb_content'       => 'Content',
  117:        'ccc_webreferences' => 'Web References');
  118:     if ($group ne '') {
  119:         $syllabusfields{'abb_links'} = &mt('Available Group Tools');
  120:     }
  121: 
  122: 
  123: # ------------------------------------------------------------ Get query string
  124:     &Apache::loncommon::get_unprocessed_cgi
  125:                         ($ENV{'QUERY_STRING'},['forcestudent','forceedit',
  126:                                                'register','ref']);
  127: # --------------------------------------------------------------- Force Student
  128:     my $forcestudent='';
  129:     if ($env{'form.forcestudent'} || $target eq 'tex' ) { $forcestudent='student'; };
  130:     my $forceedit='';
  131:     if ($env{'form.forceedit'}) { $forceedit='edit'; }
  132: 
  133:     my $refarg;
  134:     if ($env{'form.ref'}) {
  135:         $refarg = '&ref='.$env{'form.ref'};
  136:     }
  137: 
  138:     my %syllabus=&Apache::lonnet::dump($db_name,$dom,$crs);
  139: 
  140: # --------------------------------------- There is such a user, get environment
  141: 
  142:     if ($target ne 'tex') {
  143:         my $title = 'Simple Course Page';
  144:         if ($group ne '') {
  145:             $title = 'Simple Group Page';
  146:         }
  147:         my $start_page =
  148:         &Apache::loncommon::start_page($title,undef,
  149:                        {'function'       => $forcestudent,
  150:                         'domain'         => $dom,
  151:                         'force_register' =>
  152:                         $env{'form.register'},});
  153:         $r->print($start_page);
  154:     }
  155: 
  156:     if ($group ne '') {
  157:         my $group_view_perm =
  158:                &Apache::lonnet::allowed('vcg',$env{'request.course.id'}.
  159:                ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''));
  160:         $group_edit_perm =
  161:                &Apache::lonnet::allowed('mdg',$env{'request.course.id'}.
  162:                ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''));
  163:         $group_home_view = &Apache::lonnet::allowed('vgh',
  164:                                          $env{'request.course.id'}.'/'.$group);
  165:         $group_home_edit = &Apache::lonnet::allowed('mgh',
  166:                                          $env{'request.course.id'}.'/'.$group);
  167:         if ($group_view_perm || $group_edit_perm || $group_home_view ||
  168:             $group_home_edit || &Apache::longroup::check_group_access($group)) {
  169:             if (($env{'form.ref'} eq 'grouplist') && ($target ne 'tex')) {
  170:                 $r->print(&grouppage_breadcrumbs($dom,$crs,$group,$group_desc));
  171:             }
  172:             if ((!$group_home_edit) && (!$group_home_view) &&
  173:                 (!$group_view_perm) && (!$group_edit_perm)) {
  174:                 &display_group_links($r,$target,$group,'view',$refarg,%groupinfo);
  175:                 if ($env{'form.grade_target'} ne 'tex') {
  176:                     $r->print(&Apache::loncommon::end_page());
  177:                 } else {
  178:                     $r->print('\end{document}');
  179:                 }
  180:                 return OK;
  181:             }
  182:         } else {
  183:             my $msg =
  184:             &mt('You do not currently have rights to view this group.');
  185:             if ($target ne 'tex') {
  186:                 $r->print('<p class="LC_warning">'.$msg.'</p>'.
  187:                 &Apache::loncommon::end_page());
  188:             } else {
  189:                 $r->print('\textbf{'.$msg.'}\end{document}');
  190:             }
  191:             return OK;
  192:         }
  193:         my ($blocked,$blocktext) =
  194:              &Apache::loncommon::blocking_status('groups');
  195:         if ($blocked) {
  196:             $r->print($blocktext);
  197:             $r->print(&Apache::loncommon::end_page());
  198:             return OK;
  199:         }
  200:     }
  201: 
  202:     my $allowed;
  203: 
  204:     if ($group ne '') {
  205:         $allowed  = $group_edit_perm;
  206:         if (!$allowed) {
  207:             $allowed = $group_home_edit;
  208:         }
  209:     } else {
  210:         $allowed=&Apache::lonnet::allowed('mdc',$env{'request.course.id'});
  211:     }
  212:     my $privileged=$allowed;
  213:     if (($syllabus{'uploaded.lastmodified'}) && (!$forceedit)) {
  214:         $forcestudent='student';
  215:     }
  216: 
  217:     if ($forcestudent or $target eq 'tex') { $allowed=0; }
  218: 
  219:     if (($env{'form.uploaddoc.filename'} and $target ne 'tex') &&
  220:     ($env{'form.storeupl'}) && ($allowed)) {
  221:         if ($env{'form.uploaddoc.filename'}=~/\.(gif|jpg|png|jpeg)$/i) {
  222:             if ($syllabus{'uploaded.photourl'}) {
  223:                 &Apache::lonnet::removeuploadedurl($syllabus{'uploaded.photourl'});
  224:             }
  225:             if ($group ne '') {
  226:                 $syllabus{'uploaded.photourl'}=&Apache::lonnet::userfileupload(
  227:                                               'uploaddoc',1,"grouppage/$group");
  228:             } else {
  229:                 $syllabus{'uploaded.photourl'}=
  230:                 &Apache::lonnet::userfileupload('uploaddoc',1,'simplepage');
  231:             }
  232:         }
  233:         $syllabus{'uploaded.lastmodified'}=time;
  234:         &Apache::lonnet::put($db_name,\%syllabus,$dom,$crs);
  235:     }
  236: #    if ($allowed && $env{'form.delupl'}) {
  237: #        if ($syllabus{'uploaded.photourl'}) {
  238: #            &Apache::lonnet::removeuploadedurl($syllabus{'uploaded.photourl'});
  239: #            delete($syllabus{'uploaded.photourl'});
  240: #            &Apache::lonnet::del('simplepage',['uploaded.photourl']);
  241: #        }
  242: #    }
  243:     if (($allowed) && ($env{'form.storesyl'})) {
  244:         foreach my $syl_field (keys(%syllabusfields)) {
  245:             my $field=$env{'form.'.$syl_field};
  246:             chomp($field);
  247:             $field=~s/\s+$//s;
  248:             $field=~s/^\s+//s;
  249:             $field=~s/\<br\s*\/*\>$//s;
  250:             $field=&Apache::lonfeedback::clear_out_html($field,1);
  251:             $syllabus{$syl_field}=$field;
  252:         }
  253:         $syllabus{'uploaded.lastmodified'}=time;
  254:         &Apache::lonnet::put($db_name,\%syllabus,$dom,$crs);
  255:     }
  256: 
  257: #---Print Function
  258:     if ($target ne 'tex') {
  259:         if ($allowed || $privileged) {
  260:             my $functions=&Apache::lonhtmlcommon::start_funclist();
  261:             my $url = $r->uri;
  262:             if ($group ne '') {
  263:                 my $navmap=Apache::lonnavmaps::navmap->new();
  264:                 if (ref($navmap)) {
  265:                     $url = &Apache::longroup::get_group_link($dom,$crs,$group,$navmap);
  266:                 }
  267:             }
  268:             if ($allowed) {
  269:                 $url .= (($url=~/\?/)?'&amp;':'?').'forcestudent=1';
  270:                 $functions.=&Apache::lonhtmlcommon::add_item_funclist(
  271:                                 '<a href="'.$url.'">'
  272:                                .&mt('Show Student View').'</a>'
  273:                                .&Apache::loncommon::help_open_topic(
  274:                                     'Uploaded_Templates_PublicView'));
  275:             } elsif ($privileged) {
  276:                 my $edittext = &mt('Edit');
  277:                 $url .= (($url=~/\?/)?'&amp;':'?').'forceedit=edit'.$refarg;
  278:                 if ($group ne '') {
  279:                     $edittext = &mt('Edit Group Homepage');
  280:                 }
  281:                 $functions.=&Apache::lonhtmlcommon::add_item_funclist(
  282:                                 '<a href="'.$url.'">'
  283:                                .$edittext.'</a>');
  284:                 if ($group ne '') {
  285:                     if ($group_edit_perm) {
  286:                         $functions.=&Apache::lonhtmlcommon::add_item_funclist(
  287:                                         '<a href="/adm/coursegroups'
  288:                                        .'?action=modify&amp;refpage=grouplist'
  289:                                        .'&amp;state=pick_task&amp;groupname='
  290:                                        .$group.'">'
  291:                                        .&mt('Edit Group Settings')
  292:                                        .'</a>');
  293:                        }
  294:                     }
  295:             }
  296:             $functions.=&Apache::lonhtmlcommon::end_funclist();
  297:             $r->print(&Apache::loncommon::head_subbox($functions));
  298:         }
  299:     }
  300: 
  301: #---Print help Text
  302:     if($target ne 'tex'){
  303:         if ($allowed) {
  304:             $r->print(&Apache::loncommon::help_open_topic('Uploaded_Templates_TextBoxes',&mt('Help with filling in text boxes')));
  305:         }
  306:     }
  307: 
  308: # ---------------------------------------------------------------- Get syllabus
  309:     if ((($syllabus{'uploaded.lastmodified'}) &&
  310:          (($group ne '' && ($group_home_view || $group_edit_perm ||
  311:            $group_view_perm)) || ($group eq ''))) || ($allowed)) {
  312:         #Print the title
  313:         my $titletext=&HTML::Entities::encode($syllabus{'aaa_title'},'<>&"');
  314:         if ($target ne 'tex') {
  315:             if ($allowed) {
  316:             }
  317:             $r->print('<h2>'.$titletext.'</h2>');
  318:         } else {
  319:             my $safeinit;
  320:             $r->print(&Apache::lonxml::xmlparse($r,'tex','<h1>'.$titletext.'</h1>'));
  321:         }
  322:         if ($allowed) {
  323:             if ($env{'form.grade_target'} ne 'tex') {
  324:                 #editbox for title
  325:                 $r->print('<form method="post" action="" enctype="multipart/form-data">');
  326: 				&Apache::lontemplate::print_start_template($r,&mt('Title'),'LC_Box');
  327: 				$r->print($titletext);
  328: 				$r->print("<br /><div>");
  329: 				&Apache::lontemplate::print_textarea_template($r, $syllabus{'aaa_title'},
  330: 					'aaa_title', Apache::lontemplate->RICH_TEXT_ALWAYS_OFF);
  331: 				&Apache::lontemplate::print_saveall_template($r);
  332: 				$r->print("</div>");
  333: 				&Apache::lontemplate::print_end_template($r);
  334:             } else {
  335:                 my $safeinit;
  336:                 $r->print(&Apache::lonxml::xmlparse($r,'tex',$syllabus{'aaa_title'},$safeinit));
  337:             }
  338:         }
  339: 
  340:         #print the image
  341:         my $image='';
  342:         if ($syllabus{'uploaded.photourl'}) {
  343:             &Apache::lonnet::allowuploaded('/adm/smppg',
  344:                     $syllabus{'uploaded.photourl'});
  345: 
  346:             $image='<img src="'.$syllabus{'uploaded.photourl'}
  347:                   .'" alt="'.&mt('Image').'" />';
  348:             if ($target eq 'tex') {
  349:                 $image=&Apache::lonxml::xmlparse($r,'tex',$image);
  350:             }
  351:         }
  352: 
  353:         if ($allowed) {
  354:             &Apache::lontemplate::print_start_template($r, &mt('Upload a Photo'),'LC_Box');
  355:             $r->print($image);
  356:             $r->print("<br /><br />");
  357:             $r->print(
  358:                 '<input type="hidden" name="forceedit" value="edit" />'.
  359:                 '<input type="file" name="uploaddoc" size="50" />'.
  360:                 '<input type="submit" name="storeupl" value="'.&mt('Upload').'" />'.
  361:                 '<input type="hidden" name="forceedit" value="edit" />');
  362:             &Apache::lontemplate::print_end_template($r);
  363:     
  364: 
  365: #            if ($syllabus{'uploaded.photourl'}) {
  366: #                $r->print('<input type="submit" name="delupl"'
  367: #                         .' value="'.&mt('Delete Photo').'" />');
  368: #            }
  369:         }
  370:         #Image in Student view and printout.
  371:         else {
  372:             $r->print($image);
  373:         }
  374:        	
  375:         my $links_handler = sub { 
  376:         	my ($r, $field, $message, $group, $data_ref, $fields_ref, $target, $allowed) = @_;
  377: 			if ($group ne '') {
  378: 				my %data = %{$data_ref};
  379: 				my %fields = %{$fields_ref};
  380: 				$r->print('<br /><input type="hidden" name="'.$field.
  381: 					'" value="'.$data{$field}.'" />');
  382: 				&display_group_links($r, $target, $group, 'edit', $refarg, %groupinfo);
  383: 				$r->print('<br />');
  384: 			}
  385:         };
  386:         my $title_handler = sub {};
  387: 		my %custom_handlers = (
  388: 			'abb_links' => $links_handler,
  389: 			'aaa_title' => $title_handler
  390: 		);
  391: 		&Apache::lontemplate::print_template_fields($r, \%syllabus, \%syllabusfields, 
  392: 			$target, $allowed, Apache::lontemplate->RICH_TEXT_DETECT_HTML, \%custom_handlers, $group);
  393: 		
  394:         if ($allowed && ($env{'form.grade_target'} ne 'tex')) {
  395:             $r->print(&Apache::lonhtmlcommon::htmlareaselectactive
  396:                 ('bbb_content').'</form>');
  397:         }
  398: 
  399:     } else {
  400:         if ($group ne '') {
  401:             &display_group_links($r,$target,$group,'view',$refarg,%groupinfo);
  402:         } else {
  403:             my $text=&mt('No page information provided.');
  404:             if ($target ne 'tex') {
  405:                 $r->print('<p class="LC_info">'.$text.'</p>');
  406:             } else {
  407:             $r->print($text)
  408:             }
  409:         }
  410:     }
  411:     if ($env{'form.grade_target'} ne 'tex') {
  412:         $r->print(&Apache::loncommon::end_page());
  413:     } else {
  414:         $r->print('\end{document}');
  415:     }
  416:     return OK;
  417: }
  418: 
  419: sub display_group_links {
  420:     my ($r,$target,$group,$context,$refarg,%groupinfo) = @_;
  421:     my @available = ();
  422:     my %menu = ();
  423:     %{$menu{'email'}} = (
  424:                         text => 'Group Message',
  425:                         href => '/adm/email?compose=group&amp;group='.$group.
  426:                                 $refarg,
  427:                       );
  428:     %{$menu{'discussion'}} = (
  429:                         text => 'Discussion Boards',
  430:                         href => '/adm/groupboards?group='.$group.$refarg,
  431:                       );
  432:     %{$menu{'chat'}} = (
  433:                         text => 'Group Chat Room',
  434:                         href => "javascript:group_chat('$group')",
  435:                       );
  436:     %{$menu{'files'}} = (
  437:                         text => 'Group Portfolio',
  438:                         href => '/adm/coursegrp_portfolio?group='.$group.
  439:                                 $refarg,
  440:                       );
  441:     %{$menu{'roster'}} = (
  442:                         text => 'Membership Roster',
  443:                         href => '/adm/grouproster?group='.$group.$refarg,
  444:                       );
  445:     foreach my $tool (sort(keys(%menu))) {
  446:         if ($groupinfo{functions}{$tool} eq 'on') {
  447:             push(@available,$tool);
  448:         }
  449:     }
  450:     if (@available > 0) {
  451:         my $output = '';
  452:         if ($target eq 'tex') {
  453:             $output = '<table cellspacing="4" cellpadding="4">';
  454:         } else {
  455:             $output = &Apache::loncommon::start_data_table();
  456:         }
  457:         foreach my $tool (@available) {
  458:             if ($target eq 'tex') {
  459:                 $output .= '<tr><td>'.&mt($menu{$tool}{text}).'</td></tr>';
  460:             } else {
  461:                 $output .= &Apache::loncommon::start_data_table_row()
  462:                           .'<td><a href="'.$menu{$tool}{href}.'">'
  463:                           .&mt($menu{$tool}{text}).'</a></td>'
  464:                           .&Apache::loncommon::end_data_table_row();
  465:             }
  466:         }
  467:         if ($target eq 'tex') {
  468:             $output .= '</table>';
  469:         } else {
  470:             $output .= &Apache::loncommon::end_data_table();
  471:         }
  472:         if ($target eq 'tex') {
  473:             $r->print(&Apache::lonxml::xmlparse($r,'tex',&mt('Available functions').'<br /><br />'.$output));
  474:         } else {
  475:             $r->print('<h3>'.&mt('Available Group Tools').'</h3>'.$output);
  476:         }
  477:     } else {
  478:         my $output;
  479:         if ($context eq 'edit') {
  480:             $output = &mt('No group functionality.');
  481:         } else {
  482:             $output = &mt('No group functionality (e.g., e-mail, discussion, chat room or file upload) is currently available to you in this group: [_1].','<b>'.&unescape($groupinfo{'description'}).'</b>');
  483:         }
  484:         if ($target eq 'tex') {
  485:             $r->print(&Apache::lonxml::xmlparse($r,'tex',$output));
  486:         } else {
  487:             $r->print($output);
  488:         }
  489:     }
  490: }
  491: 
  492: sub grouppage_breadcrumbs {
  493:     my ($cdom,$cnum,$group,$description) = @_;
  494:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  495:     &Apache::lonhtmlcommon::add_breadcrumb
  496:         ({href=>"/adm/coursegroups",
  497:           text=>"Groups",
  498:           title=>"Display Groups"},
  499:         {href=>"/adm/$cdom/$cnum/$group/smppg?ref=grouplist",
  500:           text=>&mt('Group:')." $description",
  501:           title=>"Go to group's home page"},
  502:         );
  503:     my $output .= &Apache::lonhtmlcommon::breadcrumbs(&mt('Group page - [_1]',
  504:                                                            $description));
  505:     return $output;
  506: }
  507: 
  508: 1;
  509: __END__

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