File:  [LON-CAPA] / loncom / interface / lonrss.pm
Revision 1.15: download - view: text, annotated - select for diffs
Thu Apr 13 17:57:17 2006 UTC (18 years, 2 months ago) by www
Branches: MAIN
CVS tags: HEAD
Work in progress on making editor work

    1: # The LearningOnline Network
    2: # RSS Feeder
    3: #
    4: # $Id: lonrss.pm,v 1.15 2006/04/13 17:57:17 www 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::lonrss;
   30: 
   31: use strict;
   32: use Apache::Constants qw(:common);
   33: use Apache::loncommon;
   34: use Apache::lonnet;
   35: use Apache::lontexconvert;
   36: use Apache::lonlocal;
   37: use Apache::lonhtmlcommon;
   38: 
   39: my $feedcounter;
   40: 
   41: sub filterfeedname {
   42:     my $filename=shift;
   43:     $filename=~s/(\_rss\.html|\.rss)$//;
   44:     $filename=~s/\W//g;
   45:     $filename=~s/\_rssfeed$//;
   46:     $filename=~s/^nohist\_//;
   47:     return $filename;
   48: }
   49: 
   50: sub feedname {
   51:     return 'nohist_'.&filterfeedname(shift).'_rssfeed';
   52: }
   53: 
   54: sub displayfeedname {
   55:     my ($rawname,$uname,$udom)=@_;
   56:     my $filterfilename=&filterfeedname($rawname);
   57: # do we have a stored name?
   58:     my %stored=&Apache::lonnet::get('nohist_all_rss_feeds',[$filterfilename],$udom,$uname);
   59:     if ($stored{$filterfilename}) { return $stored{$filterfilename}; }
   60: # no, construct a name
   61:     my $name=$filterfilename; 
   62:     if ($name=~/^CourseBlog/) {
   63:         $name=&mt('Course Blog');
   64: 	if ($env{'course.'.$env{'request.course.id'}.'.description'}) {
   65: 	    $name.=' '.$env{'course.'.$env{'request.course.id'}.'.description'};
   66: 	}
   67:     } else {
   68: 	$name=~s/\_/ /g;
   69:     }
   70:     return $name;
   71: }
   72: 
   73: sub renamefeed {
   74:     my ($rawname,$uname,$udom,$newname)=@_;
   75:     return &Apache::lonnet::put('nohist_all_rss_feeds',
   76: 				{ &filterfeedname($rawname) => $newname },
   77: 				$udom,$uname);
   78: }
   79: 
   80: sub advertisefeeds {
   81:     my ($uname,$udom,$edit)=@_;
   82:     my $feeds='';
   83:     my %feednames=&Apache::lonnet::dump('nohist_all_rss_feeds',$udom,$uname);
   84:     my $mode='public';
   85:     if ($edit) {
   86: 	$mode='adm';
   87:     }
   88:     foreach my $feed (sort(keys(%feednames))) {
   89: 	if ($feed!~/^error\:/) {
   90: 	    my $feedurl='http://'.$ENV{'HTTP_HOST'}.'/public/'.$udom.'/'.$uname.'/'.$feed.'.rss';
   91: 	    my $htmlurl='http://'.$ENV{'HTTP_HOST'}.'/'.$mode.'/'.$udom.'/'.$uname.'/'.$feed.'_rss.html';
   92: 	    $feeds.='<li>'.$feednames{$feed}.
   93: 		'<br />'.($edit?&mt('Edit'):'HTML').': <a href="'.$htmlurl.'"><tt>'.$htmlurl.'</tt></a>'.
   94: 		($edit?'':'<br />RSS: <a href="'.$feedurl.'"><tt>'.$feedurl.'</tt></a>').'</li>';
   95: 	}
   96:     }
   97:     if ($feeds) {
   98: 	return '<h4>'.&mt('Available RSS Feeds and Blogs').'</h4><ul>'.$feeds.'</ul>';
   99:     } else {
  100:         return '';
  101:     }
  102: }
  103: 
  104: sub rss_link {
  105:     my ($url) = @_;
  106:     return qq|<link rel="alternate" type="application/rss+xml" title="Course Announcements" href="$url" />|;
  107: }
  108: 
  109: sub addentry {
  110:     $feedcounter++;
  111:     my $id=time.'00000'.$$.'00000'.$feedcounter;
  112:     return &editentry($id,@_);
  113: }
  114: 
  115: sub editentry {
  116:     my ($id,$uname,$udom,$filename,$title,$description,$url,$status,$encurl,$enclength,$enctype)=@_;
  117:     if ($status eq 'deleted') {
  118: 	return &changestatus($id,$uname,$udom,$filename,$status);
  119:     }
  120:     my $feedname=&feedname($filename);
  121:     &Apache::lonnet::put('nohist_all_rss_feeds',
  122: 			 { &filterfeedname($filename) => &displayfeedname($filename,$uname,$udom) },
  123: 			 $udom,$uname);
  124:     return &Apache::lonnet::put($feedname,{
  125: 	$id.'_title' => $title,
  126: 	$id.'_description' => $description,
  127: 	$id.'_link' => $url,
  128: 	$id.'_enclosureurl' => $encurl,
  129: 	$id.'_enclosurelength' => $enclength,
  130: 	$id.'_enclosuretype' => $enctype,
  131: 	$id.'_status' => $status},$udom,$uname);
  132: }
  133: 
  134: sub changestatus {
  135:     my ($id,$uname,$udom,$filename,$status)=@_;
  136:     my $feedname=&feedname($filename);
  137:     if ($status eq 'deleted') {
  138: 	return &Apache::lonnet::del($feedname,[$id.'_title',
  139: 					       $id.'_description',
  140: 					       $id.'_link',
  141: 					       $id.'_enclosureurl',
  142: 					       $id.'_enclosurelength',
  143: 					       $id.'_enclosuretype',
  144: 					       $id.'_status'],$udom,$uname);
  145:     } else {
  146: 	return &Apache::lonnet::put($feedname,{$id.'_status' => $status},$udom,$uname);
  147:     }
  148: }
  149: 
  150: sub changed_js {
  151:     return <<ENDSCRIPT;
  152: <script type="text/javascript">
  153:     function changed(tform,id) {
  154:         tform.elements[id+"_modified"].checked=true;
  155:     }
  156: </script>
  157: ENDSCRIPT
  158: }
  159: 
  160: sub handler {
  161:     my ($r) = @_;
  162: 
  163:     my $edit=0;
  164:     my $html=0;
  165:     my (undef,$mode,$udom,$uname,$filename)=split(/\//,$r->uri);
  166:     if (($mode eq 'adm') && ($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
  167: 	$edit=1;
  168: 	$html=1;
  169:     }
  170:     if ($filename=~/\.html$/) {
  171: 	$html=1;
  172:     }
  173:     if ($html) {
  174: 	&Apache::loncommon::content_type($r,'text/html');
  175:     } else {
  176: # Workaround Mozilla/Firefox
  177: #	&Apache::loncommon::content_type($r,'application/rss+xml');
  178: 	&Apache::loncommon::content_type($r,'text/xml');
  179:     }
  180:     $r->send_http_header;
  181:     return OK if $r->header_only;
  182: 
  183:     my $filterfeedname=&filterfeedname($filename);
  184:     my $feedname=&feedname($filename);
  185:     my $displayfeedname=&displayfeedname($filename,$uname,$udom);
  186:     if ($html) {
  187: 	$r->print(&Apache::loncommon::start_page($displayfeedname,undef,
  188: 						 {'domain'         => $udom,
  189: 						  'force_register' =>
  190: 						      $env{'form.register'}}).
  191: 		  &changed_js());
  192:     } else {
  193: 	$r->print("<rss version='2.0' xmlns:dc='http://purl.org/dc/elements/1.1'>\n<channel>".
  194: 		  "\n<link>http://".$ENV{'HTTP_HOST'}.'/public/'.$udom.'/'.$uname.'/'.
  195: 		  $filterfeedname.'_rss.html</link>'.
  196: 		  "\n<description>".
  197: 		  &mt('An RSS Feed provided by the LON-CAPA Learning Content Management System').
  198: 		  '</description>');
  199:     }
  200: # Do we have stuff to store?
  201:     if ($edit) {
  202:         my %newsfeed=&Apache::lonnet::dump($feedname,$udom,$uname);
  203: 	foreach my $entry (sort(keys(%newsfeed)),$env{'form.newid'}.'_status') {
  204: 	    if ($entry=~/^(\d+)\_status$/) {
  205: 		my $id=$1;
  206: 		if ($env{'form.'.$id.'_modified'}) {
  207: 		    &editentry($id,$uname,$udom,$feedname,
  208: 			       $env{'form.'.$id.'_title'},
  209: 			       $env{'form.'.$id.'_description'},
  210: 			       $env{'form.'.$id.'_url'},
  211: 			       $env{'form.'.$id.'_status'});
  212: 		}
  213: 	    }
  214: 	}
  215:     }
  216:     $feedcounter++;
  217:     my $newid=time.'00000'.$$.'00000'.$feedcounter;
  218: # Is this user for real?
  219:     my $homeserver=&Apache::lonnet::homeserver($uname,$udom);
  220:     if ($html) {
  221: 	$r->print(&advertisefeeds($uname,$udom,$edit));
  222:     } 
  223:     if ($homeserver eq 'no_host') {
  224: 	$r->print(($html?'<h3>':'<title>').&mt('No feed available').($html?'</h3>':'</title>'));
  225:     } else {
  226: # Course or user?
  227: 	my $name='';
  228: 	if ($uname=~/^\d/) {
  229: 	    my %cenv=&Apache::lonnet::dump('environment',$udom,$uname);
  230: 	    $name=$cenv{'description'};
  231: 	} else {
  232: 	    $name=&Apache::loncommon::nickname($uname,$udom);
  233: 	}
  234:         $r->print("\n".
  235: 		  ($html?'<h3>':'<title>').
  236: 		  &mt('LON-CAPA Feed "[_1]" for [_2]',$displayfeedname,$name).
  237: 		  ($html?'</h3>'.($edit?'<form method="post"><br />'.
  238: 				  &mt('Name of blog/journal').
  239: 				  ': <input type="text" size="50" name="newblogname" value="'.
  240: 				  $displayfeedname.'" />':'').'<ul>':'</title>'));
  241: # Render private items?
  242:         my $viewpubliconly=1;
  243:         if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
  244: 	    $viewpubliconly=0;
  245: 	}
  246: # Get feed items
  247:         my %newsfeed=&Apache::lonnet::dump($feedname,$udom,$uname);
  248: 	foreach my $entry (sort(keys(%newsfeed)),$newid.'_status') {
  249: 	    if ($entry=~/^(\d+)\_status$/) {
  250: 		my $id=$1;
  251: 		if ($edit) {
  252: 		    my %lt=&Apache::lonlocal::texthash('public' => 'public',
  253: 						       'private' => 'private',
  254: 						       'hidden' => 'hidden',
  255: 						       'delete' => 'delete',
  256: 						       'store' => 'Store changes');
  257: 		    my %status=();
  258:                     unless ($newsfeed{$id.'_status'}) { $newsfeed{$id.'_status'}='public'; }
  259: 		    $status{$newsfeed{$id.'_status'}}='checked="checked"';
  260: 		    $r->print(<<ENDEDIT);
  261: <li>
  262: <label><input name='$id\_modified' type='checkbox' value="modified" /> $lt{'store'}</label>
  263: &nbsp;&nbsp;
  264: <label><input name='$id\_status' type="radio" value="public" $status{'public'} onClick="changed(this.form,'$id');" /> $lt{'public'}</label>
  265: &nbsp;&nbsp;
  266: <label><input name='$id\_status' type="radio" value="private" $status{'private'} onClick="changed(this.form,'$id');" /> $lt{'private'}</label>
  267: &nbsp;&nbsp;
  268: <label><input name='$id\_status' type="radio" value="hidden" $status{'hidden'} onClick="changed(this.form,'$id');" /> $lt{'hidden'}</label>
  269: &nbsp;&nbsp;
  270: <label><input name='$id\_status' type="radio" value="deleted" onClick="changed(this.form,'$id');" /> $lt{'delete'}</label>
  271: <br />
  272: <input name='$id\_title' type='text' size='80' value='$newsfeed{$id.'_title'}' onChange="changed(this.form,'$id');" /><br />
  273: <textarea name='$id\_description' rows="6" cols="80" onChange="changed(this.form,'$id');">$newsfeed{$id.'_description'}</textarea><br />
  274: <input name='$id\_link' type='text' size='80' value='$newsfeed{$id.'_link'}' onChange="changed(this.form,'$id');" />
  275: <hr /></li>
  276: ENDEDIT
  277: 		} else {
  278: 		    if (($newsfeed{$id.'_status'} ne 'public') && ($viewpubliconly)) { next; }
  279: 		    if ($newsfeed{$id.'_status'} eq 'hidden') { next; }
  280: 		    $r->print("\n".($html?"\n<li><b>":"<item>\n<title>").$newsfeed{$id.'_title'}.
  281: 			      ($html?"</b><br />\n":"</title>\n<description>").
  282: 			      $newsfeed{$id.'_description'}.
  283: 			      ($html?"<br />\n<a href='":"</description>\n<link>").
  284: 			      "http://".$ENV{'HTTP_HOST'}.
  285: 			      $newsfeed{$id.'_link'}.
  286: 			      ($html?("'>".&mt('Read more')."</a><br />\n"):"</link>\n"));
  287: 		    if ($newsfeed{$id.'_enclosureurl'}) {
  288: 			$r->print(($html?"<a href='":"\n<enclosure url='").
  289: 				  $newsfeed{$id.'_enclosureurl'}."' length='".$newsfeed{$id.'_enclosurelength'}.
  290: 				  "' type='".$newsfeed{$id.'_enclosuretype'}.($html?"'>".&mt('Enclosure')."</a>":"' />"));
  291: 		    }
  292: 		    if ($html) {
  293: 			$r->print("\n<hr /></li>\n");
  294: 		    } else {
  295: 			$r->print("\n<guid isPermaLink='false'>".$id.$filterfeedname.'_'.$udom.'_'.$uname."</guid></item>\n");
  296: 		    }
  297: 		}
  298: 	    }
  299: 	}
  300:     }
  301:     $r->print("\n".($html?'</ul>'.($edit?'<input type="hidden" name="newid" value="'.$newid.'"/><input type="submit" value="'.&mt('Store Marked Changes').'" /></form>':'').&Apache::loncommon::end_page():'</channel></rss>'."\n"));
  302:     return OK;
  303: } 
  304: 1;
  305: __END__

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