File:  [LON-CAPA] / loncom / interface / lonsimplepage.pm
Revision 1.107: download - view: text, annotated - select for diffs
Tue Nov 30 15:55:37 2021 UTC (2 years, 5 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, version_2_11_X, version_2_11_4_uiuc, version_2_11_4_msu, version_2_11_4, HEAD
- Bug 6955 IP-based blocking. Pass user'sIP address as third arg to
  loncommon::blockcheck() and second to loncommon::blocking_status().

    1: # The LearningOnline Network
    2: # Simple Page Editor
    3: #
    4: # $Id: lonsimplepage.pm,v 1.107 2021/11/30 15:55:37 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::lonhtmlgateway;
   37: use Apache::lonlocal;
   38: use Apache::lonprintout;
   39: use Apache::lonxml;
   40: use Apache::longroup;
   41: use Apache::lonnavmaps();
   42: use HTML::Entities();
   43: use LONCAPA;
   44: 
   45: sub get_db_name {
   46:     my ($url,$marker,$cdom,$cnum) = @_;
   47:     my ($udom,$uname,$timemark)=(split(m{/},$url))[2,3,4];
   48:     if ($marker eq '') {
   49:         $marker = $timemark;
   50:     }
   51:     if (($cdom eq '') || ($cnum eq '')) {
   52:         $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
   53:         $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
   54:     }
   55:     my $db_name;
   56: 
   57:     if ($cdom && $cnum && ($udom eq $cdom) && ($uname eq $cnum)) {
   58:         $marker =~ s/\W//g;
   59:         $db_name = 'grppage_'.$marker;
   60:     } else {
   61:         $marker=~s/\D//g;
   62:         $db_name = 'smppage_'.$marker;
   63:     }
   64:     return if (!defined($marker));
   65:     return $db_name;
   66: }
   67: 
   68: sub handler {
   69:     my $r = shift;
   70:     &Apache::loncommon::content_type($r,'text/html');
   71:     $r->send_http_header;
   72:     return OK if $r->header_only;
   73:     my $target=$env{'form.grade_target'};
   74: # ------------------------------------------------------------ Print the screen
   75:     if ($target eq 'tex') {
   76:         $r->print(&Apache::lonprintout::print_latex_header($env{'form.latex_type'}));
   77:     }
   78: 
   79: # Is this even in a course?
   80:     unless ($env{'request.course.id'}) {
   81:         if ($target ne 'tex') {
   82:             &Apache::loncommon::simple_error_page($r,'','Not in a course');
   83:         } else {
   84:             $r->print('\textbf{Not in a course}\end{document}');
   85:         }
   86:         return OK;
   87:     }
   88: 
   89:     my $marker = (split(m{/},$r->uri))[4];
   90:     my $db_name = &get_db_name($r->uri,$marker);
   91: 
   92:     my $dom = $env{'course.'.$env{'request.course.id'}.'.domain'};
   93:     my $crs = $env{'course.'.$env{'request.course.id'}.'.num'};
   94:     my ($group,$group_desc);
   95: 
   96:     my %curr_group = ();
   97:     my %groupinfo = ();
   98:     if ($db_name =~ /^grppage_/) {
   99:         $group = $marker;
  100:         $group =~ s/\W//g;
  101:         my %curr_groups = &Apache::longroup::coursegroups($dom,$crs,$group);
  102:         if (!%curr_groups) {
  103:             &Apache::loncommon::simple_error_page($r,'','Invalid group name');
  104:             return OK;
  105:         }
  106:         %groupinfo =
  107:         &Apache::longroup::get_group_settings($curr_groups{$group});
  108:         $group_desc = &unescape($groupinfo{'description'});
  109:     }
  110: 
  111:     if (!$db_name) {
  112:         &Apache::loncommon::simple_error_page($r,'','Invalid call');
  113:         return OK;
  114:     }
  115: 
  116: # --------------------------------------------------------- The syllabus fields
  117:     my %syllabusfields=&Apache::lonlocal::texthash(
  118:        'aaa_title'         => 'Page Title',
  119:        'bbb_content'       => ($target eq 'tex'?'':'Content'),
  120:        'ccc_webreferences' => 'Web References');
  121:     if ($group ne '') {
  122:         $syllabusfields{'abb_links'} = &mt('Available Group Tools');
  123:     }
  124: 
  125: 
  126: # ------------------------------------------------------------ Get query string
  127:     &Apache::loncommon::get_unprocessed_cgi
  128:                         ($ENV{'QUERY_STRING'},['forceedit','todocs',
  129:                                                'register','ref']);
  130: # --------------------------------------------------------------- Force Student
  131:     my ($forceedit,$forcestudent);
  132:     $forceedit = $env{'form.forceedit'};
  133:     if (!$forceedit) {
  134:         $forcestudent=1;
  135:     }
  136: 
  137:     my $refarg;
  138:     if ($env{'form.ref'}) {
  139:         $refarg = '&ref='.$env{'form.ref'};
  140:     }
  141: 
  142:     my %syllabus=&Apache::lonnet::dump($db_name,$dom,$crs);
  143: 
  144: # --------------------------------------- There is such a user, get environment
  145:     my ($registered,$group_view_perm,$group_edit_perm,$group_home_view,
  146:         $group_home_edit,$has_group_access);
  147:     my $brcrum = [];
  148:     if ($group eq '') {
  149:         $registered = $env{'form.register'};
  150:     } else {
  151:         unless ($env{'form.ref'} eq 'grouplist') {
  152:             $registered = $env{'form.register'};
  153:         }
  154:         $group_view_perm =
  155:                &Apache::lonnet::allowed('vcg',$env{'request.course.id'}.
  156:                ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''));
  157:         $group_edit_perm =
  158:                &Apache::lonnet::allowed('mdg',$env{'request.course.id'}.
  159:                ($env{'request.course.sec'}?'/'.$env{'request.course.sec'}:''));
  160:         $group_home_view = &Apache::lonnet::allowed('vgh',
  161:                                          $env{'request.course.id'}.'/'.$group);
  162:         $group_home_edit = &Apache::lonnet::allowed('mgh',
  163:                                          $env{'request.course.id'}.'/'.$group);
  164:         if ($group_view_perm || $group_edit_perm || $group_home_view ||
  165:             $group_home_edit || &Apache::longroup::check_group_access($group)) {
  166:             $has_group_access = 1;
  167:             if (($env{'form.ref'} eq 'grouplist') && ($target ne 'tex') &&
  168:                 (!$registered)) {
  169:                 $brcrum = &grouppage_breadcrumbs($dom,$crs,$group,$group_desc);
  170:             }
  171:         }
  172:     }
  173: 
  174:     if ($target ne 'tex') {
  175:         my $title = ($group eq '')? 'Simple Course Page':'Simple Group Page';
  176:         my $start_page =
  177:         &Apache::loncommon::start_page($title,undef,
  178:                        {'domain'         => $dom,
  179:                         'group'          => $group,
  180:                         'bread_crumbs'   => $brcrum,
  181:                         'force_register' => $registered,
  182:                        }); 
  183:         $r->print($start_page);
  184:     }
  185: 
  186:     if ($group ne '') {
  187:         if ($has_group_access) {
  188:             if ((!$group_home_edit) && (!$group_home_view) &&
  189:                 (!$group_view_perm) && (!$group_edit_perm)) {
  190:                 $r->print(&Apache::longroup::display_group_links($r,$env{'form.grade_target'},$group,
  191:                                                            'view',$refarg,undef,undef,%groupinfo));
  192:                 if ($env{'form.grade_target'} ne 'tex') {
  193:                     $r->print(&Apache::loncommon::end_page());
  194:                 } else {
  195:                     $r->print('\end{document}');
  196:                 }
  197:                 return OK;
  198:             }
  199:         } else {
  200:             my $msg =
  201:             &mt('You do not currently have rights to view this group.');
  202:             if ($target ne 'tex') {
  203:                 $r->print('<p class="LC_warning">'.$msg.'</p>'.
  204:                 &Apache::loncommon::end_page());
  205:             } else {
  206:                 $r->print('\textbf{'.$msg.'}\end{document}');
  207:             }
  208:             return OK;
  209:         }
  210:         my $clientip = &Apache::lonnet::get_requestor_ip($r);
  211:         my ($blocked,$blocktext) =
  212:              &Apache::loncommon::blocking_status('groups',$clientip);
  213:         if ($blocked) {
  214:             $r->print($blocktext);
  215:             $r->print(&Apache::loncommon::end_page());
  216:             return OK;
  217:         }
  218:     }
  219: 
  220:     my $allowed;
  221: 
  222:     if ($group ne '') {
  223:         $allowed  = $group_edit_perm;
  224:         if (!$allowed) {
  225:             $allowed = $group_home_edit;
  226:         }
  227:     } else {
  228:         $allowed=&Apache::lonnet::allowed('mdc',$env{'request.course.id'});
  229:     }
  230: 
  231:     if ($forcestudent or $target eq 'tex') { $allowed=0; }
  232: 
  233:     if (($env{'form.uploaddoc.filename'} and $target ne 'tex') &&
  234:     ($env{'form.storeupl'}) && ($allowed)) {
  235:         if ($env{'form.uploaddoc.filename'}=~/\.(gif|jpg|png|jpeg)$/i) {
  236:             if ($syllabus{'uploaded.photourl'}) {
  237:                 &Apache::lonnet::removeuploadedurl($syllabus{'uploaded.photourl'});
  238:             }
  239:             if ($group ne '') {
  240:                 $syllabus{'uploaded.photourl'}=&Apache::lonnet::userfileupload(
  241:                                               'uploaddoc','coursedoc',
  242:                                               "grouppage/$group");
  243:             } else {
  244:                 $syllabus{'uploaded.photourl'}=
  245:                 &Apache::lonnet::userfileupload('uploaddoc','coursedoc',
  246:                                                 "simplepage/$marker");
  247:             }
  248:         }
  249:         $syllabus{'uploaded.lastmodified'}=time;
  250:         &Apache::lonnet::put($db_name,\%syllabus,$dom,$crs);
  251:     }
  252: #    if ($allowed && $env{'form.delupl'}) {
  253: #        if ($syllabus{'uploaded.photourl'}) {
  254: #            &Apache::lonnet::removeuploadedurl($syllabus{'uploaded.photourl'});
  255: #            delete($syllabus{'uploaded.photourl'});
  256: #            &Apache::lonnet::del('simplepage',['uploaded.photourl']);
  257: #        }
  258: #    }
  259:     if (($allowed) && ($env{'form.storesyl'})) {
  260:         foreach my $syl_field (keys(%syllabusfields)) {
  261:             my $field=$env{'form.'.$syl_field};
  262:             chomp($field);
  263:             my $gateway = Apache::lonhtmlgateway->new();
  264:             $field = $gateway->process_incoming_html($field,1);
  265:             $syllabus{$syl_field}=$field;
  266:         }
  267:         $syllabus{'uploaded.lastmodified'}=time;
  268:         &Apache::lonnet::put($db_name,\%syllabus,$dom,$crs);
  269:     }
  270: 
  271: #---Print help Text
  272:     if($target ne 'tex'){
  273:         if ($allowed) {
  274:             $r->print(&Apache::loncommon::help_open_topic('Uploaded_Templates_TextBoxes',&mt('Help with filling in text boxes')));
  275:         }
  276:     }
  277: 
  278: # ---------------------------------------------------------------- Get syllabus
  279:     if ((($syllabus{'uploaded.lastmodified'}) &&
  280:          (($group ne '' && ($group_home_view || $group_edit_perm ||
  281:            $group_view_perm)) || ($group eq ''))) || ($allowed)) {
  282:         #Print the title
  283:         my $titletext=&HTML::Entities::encode($syllabus{'aaa_title'},'<>&"');
  284:         if ($target ne 'tex') {
  285:             if ($allowed) {
  286:             }
  287:             $r->print('<h2>'.$titletext.'</h2>');
  288:         } else {
  289:             my $safeinit;
  290:             $r->print(&Apache::lonxml::xmlparse($r,'tex','<h1>'.$titletext.'</h1>'));
  291:         }
  292:         if ($allowed) {
  293:             if ($env{'form.grade_target'} ne 'tex') {
  294:                 #editbox for title
  295:                 $r->print('<form method="post" action="" enctype="multipart/form-data">'."\n".
  296:                           '<input type="hidden" register="'.$registered.'" />');
  297: 				&Apache::lontemplate::print_start_template($r,&mt('Title'),'LC_Box');
  298: 				$r->print($titletext);
  299: 				$r->print("<br /><div>");
  300: 				&Apache::lontemplate::print_textarea_template($r, $syllabus{'aaa_title'},
  301: 					'aaa_title', Apache::lontemplate->RICH_TEXT_ALWAYS_OFF);
  302: 				&Apache::lontemplate::print_saveall_template($r);
  303: 				$r->print("</div>");
  304: 				&Apache::lontemplate::print_end_template($r);
  305:             } else {
  306:                 my $safeinit;
  307:                 $r->print(&Apache::lonxml::xmlparse($r,'tex',$syllabus{'aaa_title'},$safeinit));
  308:             }
  309:         }
  310: 
  311:         #print the image
  312:         my $image='';
  313:         if ($syllabus{'uploaded.photourl'}) {
  314:             &Apache::lonnet::allowuploaded('/adm/smppg',
  315:                     $syllabus{'uploaded.photourl'});
  316: 
  317:             $image='<img src="'.$syllabus{'uploaded.photourl'}
  318:                   .'" alt="'.&mt('Image').'" />';
  319:             if ($target eq 'tex') {
  320:                 $image=&Apache::lonxml::xmlparse($r,'tex',$image);
  321:             }
  322:         }
  323: 
  324:         if ($allowed) {
  325:             &Apache::lontemplate::print_start_template($r, &mt('Upload a Photo'),'LC_Box');
  326:             $r->print($image);
  327:             $r->print("<br /><br />");
  328:             $r->print(
  329:                 '<input type="hidden" name="forceedit" value="edit" />'.
  330:                 '<input type="file" name="uploaddoc" size="50" />'.
  331:                 '<input type="submit" name="storeupl" value="'.&mt('Upload').'" />'.
  332:                 '<input type="hidden" name="forceedit" value="edit" />');
  333:             &Apache::lontemplate::print_end_template($r);
  334:     
  335: 
  336: #            if ($syllabus{'uploaded.photourl'}) {
  337: #                $r->print('<input type="submit" name="delupl"'
  338: #                         .' value="'.&mt('Delete Photo').'" />');
  339: #            }
  340:         }
  341:         #Image in Student view and printout.
  342:         else {
  343:             $r->print($image);
  344:         }
  345:        	
  346:         my $links_handler = sub { 
  347:         	my ($r, $field, $message, $group, $data_ref, $fields_ref, $target, $allowed) = @_;
  348: 			if ($group ne '') {
  349: 				my %data = %{$data_ref};
  350: 				my %fields = %{$fields_ref};
  351: 				$r->print('<br /><input type="hidden" name="'.$field.
  352: 					'" value="'.$data{$field}.'" />');
  353:                                 $r->print(&Apache::longroup::display_group_links($r,$target,$group,'edit',
  354: 				                                                 $refarg,undef,undef,%groupinfo));
  355: 				$r->print('<br />');
  356: 			}
  357:         };
  358:         my $title_handler = sub {};
  359: 		my %custom_handlers = (
  360: 			'abb_links' => $links_handler,
  361: 			'aaa_title' => $title_handler
  362: 		);
  363: 		&Apache::lontemplate::print_template_fields($r, \%syllabus, \%syllabusfields, 
  364: 			$target, $allowed, Apache::lontemplate->RICH_TEXT_DETECT_HTML, \%custom_handlers, $group);
  365: 		
  366:         if ($allowed && ($env{'form.grade_target'} ne 'tex')) {
  367:             $r->print(&Apache::lonhtmlcommon::htmlareaselectactive().
  368:                       '</form>');
  369:         }
  370: 
  371:     } else {
  372:         if ($group ne '') {
  373:             $r->print(&Apache::longroup::display_group_links($r,$target,$group,'view',$refarg,undef,undef,%groupinfo));
  374:         } else {
  375:             my $text=&mt('No page information provided.');
  376:             if ($target ne 'tex') {
  377:                 $r->print('<p class="LC_info">'.$text.'</p>');
  378:             } else {
  379:             $r->print($text)
  380:             }
  381:         }
  382:     }
  383:     if ($env{'form.grade_target'} ne 'tex') {
  384:         $r->print(&Apache::loncommon::end_page());
  385:     } else {
  386:         $r->print('\end{document}');
  387:     }
  388:     return OK;
  389: }
  390: 
  391: sub grouppage_breadcrumbs {
  392:     my ($cdom,$cnum,$group,$description) = @_;
  393:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  394:     return [{href=>"/adm/coursegroups",
  395:              text=>"Groups",
  396:              title=>"Display Groups"},
  397:             {href=>"/adm/$cdom/$cnum/$group/smppg?ref=grouplist",
  398:              text=>&mt('Group').": $description",
  399:              title=>&mt("Go to group's home page"),
  400:              no_mt=>1},
  401:            ];
  402: }
  403: 
  404: 1;
  405: __END__

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