File:  [LON-CAPA] / loncom / interface / lonsimplepage.pm
Revision 1.99: download - view: text, annotated - select for diffs
Thu Nov 8 21:15:02 2012 UTC (11 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Consistent Interface for templated pages (bug 6128).
- Functions for Templated "simple page" moved into standard location.

    1: # The LearningOnline Network
    2: # Simple Page Editor
    3: #
    4: # $Id: lonsimplepage.pm,v 1.99 2012/11/08 21:15:02 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);
   92: 
   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'       => ($target eq 'tex'?'':'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'},['forceedit',
  126:                                                'register','ref']);
  127: # --------------------------------------------------------------- Force Student
  128:     my ($forceedit,$forcestudent);
  129:     $forceedit = $env{'form.forceedit'};
  130:     if (!$forceedit) {
  131:         $forcestudent=1;
  132:     }
  133: 
  134:     my $refarg;
  135:     if ($env{'form.ref'}) {
  136:         $refarg = '&ref='.$env{'form.ref'};
  137:     }
  138: 
  139:     my %syllabus=&Apache::lonnet::dump($db_name,$dom,$crs);
  140: 
  141: # --------------------------------------- There is such a user, get environment
  142:     my ($group_view_perm,$group_edit_perm,$group_home_view,$group_home_edit,
  143:         $has_group_access);
  144:     my $brcrum = []; 
  145:     if ($group ne '') {
  146:         $group_view_perm =
  147:                &Apache::lonnet::allowed('vcg',$env{'request.course.id'}.
  148:                ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''));
  149:         $group_edit_perm =
  150:                &Apache::lonnet::allowed('mdg',$env{'request.course.id'}.
  151:                ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''));
  152:         $group_home_view = &Apache::lonnet::allowed('vgh',
  153:                                          $env{'request.course.id'}.'/'.$group);
  154:         $group_home_edit = &Apache::lonnet::allowed('mgh',
  155:                                          $env{'request.course.id'}.'/'.$group);
  156:         if ($group_view_perm || $group_edit_perm || $group_home_view ||
  157:             $group_home_edit || &Apache::longroup::check_group_access($group)) {
  158:             $has_group_access = 1;
  159:             if (($env{'form.ref'} eq 'grouplist') && ($target ne 'tex') &&
  160:                 (!$env{'form.register'})) {
  161:                 $brcrum = &grouppage_breadcrumbs($dom,$crs,$group,$group_desc);
  162:             }
  163:         }
  164:     }
  165: 
  166:     if ($target ne 'tex') {
  167:         my $title = 'Simple Course Page';
  168:         if ($group ne '') {
  169:             $title = 'Simple Group Page';
  170:         }
  171:         my $start_page =
  172:         &Apache::loncommon::start_page($title,undef,
  173:                        {'domain'         => $dom,
  174:                         'group'          => $group,
  175:                         'bread_crumbs'   => $brcrum,
  176:                         'force_register' => $env{'form.register'},
  177:                        }); 
  178:         $r->print($start_page);
  179:     }
  180: 
  181:     if ($group ne '') {
  182:         if ($has_group_access) {
  183:             if ((!$group_home_edit) && (!$group_home_view) &&
  184:                 (!$group_view_perm) && (!$group_edit_perm)) {
  185:                 &display_group_links($r,$target,$group,'view',$refarg,%groupinfo);
  186:                 if ($env{'form.grade_target'} ne 'tex') {
  187:                     $r->print(&Apache::loncommon::end_page());
  188:                 } else {
  189:                     $r->print('\end{document}');
  190:                 }
  191:                 return OK;
  192:             }
  193:         } else {
  194:             my $msg =
  195:             &mt('You do not currently have rights to view this group.');
  196:             if ($target ne 'tex') {
  197:                 $r->print('<p class="LC_warning">'.$msg.'</p>'.
  198:                 &Apache::loncommon::end_page());
  199:             } else {
  200:                 $r->print('\textbf{'.$msg.'}\end{document}');
  201:             }
  202:             return OK;
  203:         }
  204:         my ($blocked,$blocktext) =
  205:              &Apache::loncommon::blocking_status('groups');
  206:         if ($blocked) {
  207:             $r->print($blocktext);
  208:             $r->print(&Apache::loncommon::end_page());
  209:             return OK;
  210:         }
  211:     }
  212: 
  213:     my $allowed;
  214: 
  215:     if ($group ne '') {
  216:         $allowed  = $group_edit_perm;
  217:         if (!$allowed) {
  218:             $allowed = $group_home_edit;
  219:         }
  220:     } else {
  221:         $allowed=&Apache::lonnet::allowed('mdc',$env{'request.course.id'});
  222:     }
  223: 
  224:     if ($forcestudent or $target eq 'tex') { $allowed=0; }
  225: 
  226:     if (($env{'form.uploaddoc.filename'} and $target ne 'tex') &&
  227:     ($env{'form.storeupl'}) && ($allowed)) {
  228:         if ($env{'form.uploaddoc.filename'}=~/\.(gif|jpg|png|jpeg)$/i) {
  229:             if ($syllabus{'uploaded.photourl'}) {
  230:                 &Apache::lonnet::removeuploadedurl($syllabus{'uploaded.photourl'});
  231:             }
  232:             if ($group ne '') {
  233:                 $syllabus{'uploaded.photourl'}=&Apache::lonnet::userfileupload(
  234:                                               'uploaddoc','coursedoc',"grouppage/$group");
  235:             } else {
  236:                 $syllabus{'uploaded.photourl'}=
  237:                 &Apache::lonnet::userfileupload('uploaddoc','coursedoc','simplepage');
  238:             }
  239:         }
  240:         $syllabus{'uploaded.lastmodified'}=time;
  241:         &Apache::lonnet::put($db_name,\%syllabus,$dom,$crs);
  242:     }
  243: #    if ($allowed && $env{'form.delupl'}) {
  244: #        if ($syllabus{'uploaded.photourl'}) {
  245: #            &Apache::lonnet::removeuploadedurl($syllabus{'uploaded.photourl'});
  246: #            delete($syllabus{'uploaded.photourl'});
  247: #            &Apache::lonnet::del('simplepage',['uploaded.photourl']);
  248: #        }
  249: #    }
  250:     if (($allowed) && ($env{'form.storesyl'})) {
  251:         foreach my $syl_field (keys(%syllabusfields)) {
  252:             my $field=$env{'form.'.$syl_field};
  253:             chomp($field);
  254:             $field=~s/\s+$//s;
  255:             $field=~s/^\s+//s;
  256:             $field=~s/\<br\s*\/*\>$//s;
  257:             $field=&Apache::lonfeedback::clear_out_html($field,1);
  258:             $syllabus{$syl_field}=$field;
  259:         }
  260:         $syllabus{'uploaded.lastmodified'}=time;
  261:         &Apache::lonnet::put($db_name,\%syllabus,$dom,$crs);
  262:     }
  263: 
  264: #---Print help Text
  265:     if($target ne 'tex'){
  266:         if ($allowed) {
  267:             $r->print(&Apache::loncommon::help_open_topic('Uploaded_Templates_TextBoxes',&mt('Help with filling in text boxes')));
  268:         }
  269:     }
  270: 
  271: # ---------------------------------------------------------------- Get syllabus
  272:     if ((($syllabus{'uploaded.lastmodified'}) &&
  273:          (($group ne '' && ($group_home_view || $group_edit_perm ||
  274:            $group_view_perm)) || ($group eq ''))) || ($allowed)) {
  275:         #Print the title
  276:         my $titletext=&HTML::Entities::encode($syllabus{'aaa_title'},'<>&"');
  277:         if ($target ne 'tex') {
  278:             if ($allowed) {
  279:             }
  280:             $r->print('<h2>'.$titletext.'</h2>');
  281:         } else {
  282:             my $safeinit;
  283:             $r->print(&Apache::lonxml::xmlparse($r,'tex','<h1>'.$titletext.'</h1>'));
  284:         }
  285:         if ($allowed) {
  286:             if ($env{'form.grade_target'} ne 'tex') {
  287:                 #editbox for title
  288:                 $r->print('<form method="post" action="" enctype="multipart/form-data">'."\n".
  289:                           '<input type="hidden" register="'.$env{'form.register'}.'" />');
  290: 				&Apache::lontemplate::print_start_template($r,&mt('Title'),'LC_Box');
  291: 				$r->print($titletext);
  292: 				$r->print("<br /><div>");
  293: 				&Apache::lontemplate::print_textarea_template($r, $syllabus{'aaa_title'},
  294: 					'aaa_title', Apache::lontemplate->RICH_TEXT_ALWAYS_OFF);
  295: 				&Apache::lontemplate::print_saveall_template($r);
  296: 				$r->print("</div>");
  297: 				&Apache::lontemplate::print_end_template($r);
  298:             } else {
  299:                 my $safeinit;
  300:                 $r->print(&Apache::lonxml::xmlparse($r,'tex',$syllabus{'aaa_title'},$safeinit));
  301:             }
  302:         }
  303: 
  304:         #print the image
  305:         my $image='';
  306:         if ($syllabus{'uploaded.photourl'}) {
  307:             &Apache::lonnet::allowuploaded('/adm/smppg',
  308:                     $syllabus{'uploaded.photourl'});
  309: 
  310:             $image='<img src="'.$syllabus{'uploaded.photourl'}
  311:                   .'" alt="'.&mt('Image').'" />';
  312:             if ($target eq 'tex') {
  313:                 $image=&Apache::lonxml::xmlparse($r,'tex',$image);
  314:             }
  315:         }
  316: 
  317:         if ($allowed) {
  318:             &Apache::lontemplate::print_start_template($r, &mt('Upload a Photo'),'LC_Box');
  319:             $r->print($image);
  320:             $r->print("<br /><br />");
  321:             $r->print(
  322:                 '<input type="hidden" name="forceedit" value="edit" />'.
  323:                 '<input type="file" name="uploaddoc" size="50" />'.
  324:                 '<input type="submit" name="storeupl" value="'.&mt('Upload').'" />'.
  325:                 '<input type="hidden" name="forceedit" value="edit" />');
  326:             &Apache::lontemplate::print_end_template($r);
  327:     
  328: 
  329: #            if ($syllabus{'uploaded.photourl'}) {
  330: #                $r->print('<input type="submit" name="delupl"'
  331: #                         .' value="'.&mt('Delete Photo').'" />');
  332: #            }
  333:         }
  334:         #Image in Student view and printout.
  335:         else {
  336:             $r->print($image);
  337:         }
  338:        	
  339:         my $links_handler = sub { 
  340:         	my ($r, $field, $message, $group, $data_ref, $fields_ref, $target, $allowed) = @_;
  341: 			if ($group ne '') {
  342: 				my %data = %{$data_ref};
  343: 				my %fields = %{$fields_ref};
  344: 				$r->print('<br /><input type="hidden" name="'.$field.
  345: 					'" value="'.$data{$field}.'" />');
  346: 				&display_group_links($r, $target, $group, 'edit', $refarg, %groupinfo);
  347: 				$r->print('<br />');
  348: 			}
  349:         };
  350:         my $title_handler = sub {};
  351: 		my %custom_handlers = (
  352: 			'abb_links' => $links_handler,
  353: 			'aaa_title' => $title_handler
  354: 		);
  355: 		&Apache::lontemplate::print_template_fields($r, \%syllabus, \%syllabusfields, 
  356: 			$target, $allowed, Apache::lontemplate->RICH_TEXT_DETECT_HTML, \%custom_handlers, $group);
  357: 		
  358:         if ($allowed && ($env{'form.grade_target'} ne 'tex')) {
  359:             $r->print(&Apache::lonhtmlcommon::htmlareaselectactive().
  360:                       '</form>');
  361:         }
  362: 
  363:     } else {
  364:         if ($group ne '') {
  365:             &display_group_links($r,$target,$group,'view',$refarg,%groupinfo);
  366:         } else {
  367:             my $text=&mt('No page information provided.');
  368:             if ($target ne 'tex') {
  369:                 $r->print('<p class="LC_info">'.$text.'</p>');
  370:             } else {
  371:             $r->print($text)
  372:             }
  373:         }
  374:     }
  375:     if ($env{'form.grade_target'} ne 'tex') {
  376:         $r->print(&Apache::loncommon::end_page());
  377:     } else {
  378:         $r->print('\end{document}');
  379:     }
  380:     return OK;
  381: }
  382: 
  383: sub display_group_links {
  384:     my ($r,$target,$group,$context,$refarg,%groupinfo) = @_;
  385:     my @available = ();
  386:     my %menu = ();
  387:     %{$menu{'email'}} = (
  388:                         text => 'Group Message',
  389:                         href => '/adm/email?compose=group&amp;group='.$group.
  390:                                 $refarg,
  391:                       );
  392:     %{$menu{'discussion'}} = (
  393:                         text => 'Discussion Boards',
  394:                         href => '/adm/groupboards?group='.$group.$refarg,
  395:                       );
  396:     %{$menu{'chat'}} = (
  397:                         text => 'Group Chat Room',
  398:                         href => "javascript:group_chat('$group')",
  399:                       );
  400:     %{$menu{'files'}} = (
  401:                         text => 'Group Portfolio',
  402:                         href => '/adm/coursegrp_portfolio?group='.$group.
  403:                                 $refarg,
  404:                       );
  405:     %{$menu{'roster'}} = (
  406:                         text => 'Membership Roster',
  407:                         href => '/adm/grouproster?group='.$group.$refarg,
  408:                       );
  409:     foreach my $tool (sort(keys(%menu))) {
  410:         if ($groupinfo{functions}{$tool} eq 'on') {
  411:             push(@available,$tool);
  412:         }
  413:     }
  414:     if (@available > 0) {
  415:         my $output = '';
  416:         if ($target eq 'tex') {
  417:             $output = '<table cellspacing="4" cellpadding="4">';
  418:         } else {
  419:             $output = &Apache::loncommon::start_data_table();
  420:         }
  421:         foreach my $tool (@available) {
  422:             if ($target eq 'tex') {
  423:                 $output .= '<tr><td>'.&mt($menu{$tool}{text}).'</td></tr>';
  424:             } else {
  425:                 $output .= &Apache::loncommon::start_data_table_row()
  426:                           .'<td><a href="'.$menu{$tool}{href}.'">'
  427:                           .&mt($menu{$tool}{text}).'</a></td>'
  428:                           .&Apache::loncommon::end_data_table_row();
  429:             }
  430:         }
  431:         if ($target eq 'tex') {
  432:             $output .= '</table>';
  433:         } else {
  434:             $output .= &Apache::loncommon::end_data_table();
  435:         }
  436:         if ($target eq 'tex') {
  437:             $r->print(&Apache::lonxml::xmlparse($r,'tex',&mt('Available functions').'<br /><br />'.$output));
  438:         } else {
  439:             $r->print('<h3>'.&mt('Available Group Tools').'</h3>'.$output);
  440:         }
  441:     } else {
  442:         my $output;
  443:         if ($context eq 'edit') {
  444:             $output = &mt('No group functionality.');
  445:         } else {
  446:             $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>');
  447:         }
  448:         if ($target eq 'tex') {
  449:             $r->print(&Apache::lonxml::xmlparse($r,'tex',$output));
  450:         } else {
  451:             $r->print($output);
  452:         }
  453:     }
  454: }
  455: 
  456: sub grouppage_breadcrumbs {
  457:     my ($cdom,$cnum,$group,$description) = @_;
  458:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  459:     return [{href=>"/adm/coursegroups",
  460:              text=>"Groups",
  461:              title=>"Display Groups"},
  462:             {href=>"/adm/$cdom/$cnum/$group/smppg?ref=grouplist",
  463:              text=>&mt('Group:')." $description",
  464:              title=>"Go to group's home page"},
  465:            ];
  466: }
  467: 
  468: 1;
  469: __END__

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