--- loncom/interface/lonrss.pm 2005/11/20 19:37:44 1.6 +++ loncom/interface/lonrss.pm 2006/06/02 10:02:37 1.24 @@ -1,7 +1,7 @@ # The LearningOnline Network # RSS Feeder # -# $Id: lonrss.pm,v 1.6 2005/11/20 19:37:44 www Exp $ +# $Id: lonrss.pm,v 1.24 2006/06/02 10:02:37 www Exp $ # # Copyright Michigan State University Board of Trustees # @@ -29,6 +29,7 @@ package Apache::lonrss; use strict; +use LONCAPA; use Apache::Constants qw(:common); use Apache::loncommon; use Apache::lonnet; @@ -36,10 +37,13 @@ use Apache::lontexconvert; use Apache::lonlocal; use Apache::lonhtmlcommon; + sub filterfeedname { my $filename=shift; $filename=~s/(\_rss\.html|\.rss)$//; $filename=~s/\W//g; + $filename=~s/\_rssfeed$//; + $filename=~s/^nohist\_//; return $filename; } @@ -51,8 +55,8 @@ sub displayfeedname { my ($rawname,$uname,$udom)=@_; my $filterfilename=&filterfeedname($rawname); # do we have a stored name? - my %stored=&Apache::lonnet::get('nohist_all_rss_feeds',[$filterfilename],$udom,$uname); - if ($stored{$filterfilename}) { return $stored{$filterfilename}; } + my %stored=&Apache::lonnet::get('nohist_all_rss_feeds',[$filterfilename,'feed_display_option_'.$filterfilename],$udom,$uname); + if ($stored{$filterfilename}) { return ($stored{$filterfilename},$stored{'feed_display_option_'.$filterfilename}); } # no, construct a name my $name=$filterfilename; if ($name=~/^CourseBlog/) { @@ -63,16 +67,23 @@ sub displayfeedname { } else { $name=~s/\_/ /g; } - return $name; + return ($name,$stored{'feed_display_option_'.$filterfilename}); } -sub renamefeed { +sub namefeed { my ($rawname,$uname,$udom,$newname)=@_; return &Apache::lonnet::put('nohist_all_rss_feeds', { &filterfeedname($rawname) => $newname }, $udom,$uname); } +sub changefeeddisplay { + my ($rawname,$uname,$udom,$newstatus)=@_; + return &Apache::lonnet::put('nohist_all_rss_feeds', + { 'feed_display_option_'.&filterfeedname($rawname) => $newstatus }, + $udom,$uname); +} + sub advertisefeeds { my ($uname,$udom,$edit)=@_; my $feeds=''; @@ -82,12 +93,18 @@ sub advertisefeeds { $mode='adm'; } foreach my $feed (sort(keys(%feednames))) { - if ($feed!~/^error\:/) { - my $feedurl='feed://'.$ENV{'HTTP_HOST'}.'/public/'.$udom.'/'.$uname.'/'.$feed.'.rss'; + if (($feed!~/^error\:/) && ($feed!~/^feed\_display\_option\_/)) { + my $feedurl='http://'.$ENV{'HTTP_HOST'}.'/public/'.$udom.'/'.$uname.'/'.$feed.'.rss'; my $htmlurl='http://'.$ENV{'HTTP_HOST'}.'/'.$mode.'/'.$udom.'/'.$uname.'/'.$feed.'_rss.html'; - $feeds.='
  • '.$feednames{$feed}. - '
    '.($edit?&mt('Edit'):'HTML').': '.$htmlurl.''. - ($edit?'':'
    RSS: '.$feedurl.'').'
  • '; + if ($feednames{'feed_display_option_'.$feed} eq 'hidden') { + if ($edit) { + $feeds.='
  • '.$feednames{$feed}.'
    '.&mt('Hidden').': '.$htmlurl.'
  • '; + } + } else { + $feeds.='
  • '.$feednames{$feed}. + '
    '.($edit?&mt('Edit'):'HTML').': '.$htmlurl.''. + '
    RSS: '.$feedurl.'
  • '; + } } } if ($feeds) { @@ -97,27 +114,39 @@ sub advertisefeeds { } } +sub rss_link { + my ($url) = @_; + return qq||; +} + { my $feedcounter; - sub addentry { + sub get_new_feed_id { $feedcounter++; - my $id=time.'00000'.$$.'00000'.$feedcounter; - return &editentry($id,@_); + return time().'00000'.$$.'00000'.$feedcounter; } } +sub addentry { + my $id=&get_new_feed_id(); + return &editentry($id,@_); +} + sub editentry { - my ($id,$uname,$udom,$filename,$title,$description,$url,$status,$encurl,$enclength,$enctype)=@_; + my ($id,$uname,$udom,$filename,$title,$description,$url,$status,$encurl,$enctype)=@_; + if ($status eq 'deleted') { + return &changestatus($id,$uname,$udom,$filename,$status); + } my $feedname=&feedname($filename); &Apache::lonnet::put('nohist_all_rss_feeds', - { &filterfeedname($filename) => &displayfeedname($filename,$uname,$udom) }, + { &filterfeedname($filename) => + (&displayfeedname($filename,$uname,$udom))[0] }, $udom,$uname); return &Apache::lonnet::put($feedname,{ $id.'_title' => $title, $id.'_description' => $description, $id.'_link' => $url, $id.'_enclosureurl' => $encurl, - $id.'_enclosurelength' => $enclength, $id.'_enclosuretype' => $enctype, $id.'_status' => $status},$udom,$uname); } @@ -130,7 +159,6 @@ sub changestatus { $id.'_description', $id.'_link', $id.'_enclosureurl', - $id.'_enclosurelength', $id.'_enclosuretype', $id.'_status'],$udom,$uname); } else { @@ -138,8 +166,52 @@ sub changestatus { } } +sub changed_js { + return < + function changed(tform,id) { + tform.elements[id+"_modified"].checked=true; + } + +ENDSCRIPT +} + +sub determine_enclosure_types { + my ($url)=@_; + my ($ending)=($url=~/\.(\w+)$/); + return &Apache::loncommon::filemimetype($ending); +} + +sub course_blog_link { + my ($id,$title,$description,$url,$encurl,$enctype)=@_; + if ($env{'request.course.id'}) { + return &add_blog_entry_link($id, + $env{'course.'.$env{'request.course.id'}.'.num'}, + $env{'course.'.$env{'request.course.id'}.'.domain'}, + 'Course_Announcements', + $title,$description,$url,'public',$encurl,$enctype, + &mt('Add to Course Announcements')); + } else { + return ''; + } +} + +sub add_blog_entry_link { + my ($id,$uname,$udom,$filename,$title,$description,$url,$status,$encurl,$enctype,$linktext)=@_; + return "".$linktext.''; + +} + sub handler { - my $r = shift; + my ($r) = @_; my $edit=0; my $html=0; @@ -148,6 +220,10 @@ sub handler { $edit=1; $html=1; } + if (($mode eq 'adm') && (&Apache::lonnet::allowed('mdc',$env{'request.course.id'}))) { + $edit=1; + $html=1; + } if ($filename=~/\.html$/) { $html=1; } @@ -163,18 +239,14 @@ sub handler { my $filterfeedname=&filterfeedname($filename); my $feedname=&feedname($filename); - my $displayfeedname=&displayfeedname($filename,$uname,$udom); + my ($displayfeedname,$displayoption)=&displayfeedname($filename,$uname,$udom); if ($html) { - $r->print(&Apache::lonxml::xmlbegin().&Apache::loncommon::bodytag - ($displayfeedname,'','','',$udom, - $env{'form.register'}).< - function changed(tform,id) { - tform.elements[id+"_modified"].checked=true; - } - -ENDSCRIPT - } else { + $r->print(&Apache::loncommon::start_page(($displayfeedname?$displayfeedname:&mt("Available RSS Feeds and Blogs")),undef, + {'domain' => $udom, + 'force_register' => + $env{'form.register'}}). + &changed_js()); + } else { # render RSS $r->print("\n". "\nhttp://".$ENV{'HTTP_HOST'}.'/public/'.$udom.'/'.$uname.'/'. $filterfeedname.'_rss.html'. @@ -182,14 +254,40 @@ ENDSCRIPT &mt('An RSS Feed provided by the LON-CAPA Learning Content Management System'). ''); } +# This will be the entry id for new additions to the blog + my $newid = &get_new_feed_id(); # Is this user for real? my $homeserver=&Apache::lonnet::homeserver($uname,$udom); if ($html) { +# Any new feeds or renaming of feeds? + if ($edit) { +# Hide a feed? + if ($env{'form.hidethisblog'}) { + &changefeeddisplay($feedname,$uname,$udom,'hidden'); + ($displayfeedname,$displayoption)=&displayfeedname($filename,$uname,$udom); + } +# Advertise a feed? + if ($env{'form.advertisethisblog'}) { + &changefeeddisplay($feedname,$uname,$udom,'public'); + ($displayfeedname,$displayoption)=&displayfeedname($filename,$uname,$udom); + } +# New feed? + if ($env{'form.namenewblog'}=~/\w/) { + &namefeed($env{'form.namenewblog'},$uname,$udom,$env{'form.namenewblog'}); + } +# Old feed that is being renamed? + if (($displayfeedname) && ($env{'form.newblogname'}=~/\w/)) { + if ($env{'form.newblogname'} ne $displayfeedname) { + &namefeed($feedname,$uname,$udom,$env{'form.newblogname'}); + ($displayfeedname,$displayoption)=&displayfeedname($filename,$uname,$udom); + } + } + } $r->print(&advertisefeeds($uname,$udom,$edit)); } if ($homeserver eq 'no_host') { $r->print(($html?'

    ':'').&mt('No feed available').($html?'</h3>':'')); - } else { + } else { # is indeed a user # Course or user? my $name=''; if ($uname=~/^\d/) { @@ -198,34 +296,90 @@ ENDSCRIPT } else { $name=&Apache::loncommon::nickname($uname,$udom); } - $r->print("\n". - ($html?'

    ':''). - &mt('LON-CAPA Feed "[_1]" for [_2]',$displayfeedname,$name). - ($html?'</h3>'.($edit?'<form method="post"><br />'. - &mt('Name of blog/journal'). - ': <input type="text" size="50" name="newblogname" value="'. - $displayfeedname.'" />':'').'<ul>':'')); -# Render private items? - my $viewpubliconly=1; - if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) { - $viewpubliconly=0; +# Add a new feed + if (($html) && ($edit)) { + $r->print('
    '); + $r->print(&mt('Name for New Feed').": "); + $r->print(''); + $r->print('
    '); } + if ($displayfeedname) { # this is an existing feed +# Anything to store? + if ($edit) { +# check if this was called with a query string + &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['queryid']); + if ($env{'form.queryid'}) { +# yes, collect the remainder + &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'}, + ['title', + 'description', + 'url', + 'status', + 'enclosureurl', + 'enclosuretype']); +# my ($id,$uname,$udom,$filename,$title,$description,$url,$status,$encurl,$enctype)=@_; + + &editentry($env{'form.queryid'}, + $uname,$udom,$filename, + $env{'form.title'}, + $env{'form.description'}, + $env{'form.url'}, + $env{'form.status'}, + $env{'form.encurl'}, + $env{'form.enctype'} + ); + } + my %newsfeed=&Apache::lonnet::dump($feedname,$udom,$uname); + foreach my $entry (sort(keys(%newsfeed)),$env{'form.newid'}.'_status') { + if ($entry=~/^(\d+)\_status$/) { + my $id=$1; + if ($env{'form.'.$id.'_modified'}) { + &editentry($id,$uname,$udom,$feedname, + $env{'form.'.$id.'_title'}, + $env{'form.'.$id.'_description'}, + $env{'form.'.$id.'_url'}, + $env{'form.'.$id.'_status'}, + $env{'form.'.$id.'_enclosureurl'}, + $env{'form.'.$id.'_enclosuretype'}, + ); + } + } + } + } #done storing + + $r->print("\n". + ($html?'

    ':''). + &mt('LON-CAPA Feed "[_1]" for [_2]',$displayfeedname,$name). + ($displayoption eq 'hidden'?' ('.&mt('Hidden').')':''). + ($html?'</h3>'.($edit?'<form method="post"><br />'. + &mt('Name of this Feed'). + ': <input type="text" size="50" name="newblogname" value="'. + $displayfeedname.'" />':'').'<ul>':'')); +# Render private items? + my $viewpubliconly=1; + if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) { + $viewpubliconly=0; + } # Get feed items - my %newsfeed=&Apache::lonnet::dump($feedname,$udom,$uname); - foreach my $entry (sort(keys(%newsfeed))) { - if ($entry=~/^(\d+)\_status$/) { - my $id=$1; - if ($edit) { - my %lt=&Apache::lonlocal::texthash('public' => 'public', - 'private' => 'private', - 'hidden' => 'hidden', - 'delete' => 'delete', - 'store' => 'Store changes'); - my %status=(); - $status{$newsfeed{$id.'_status'}}='checked="checked"'; - $r->print(< 'public', + 'private' => 'private', + 'hidden' => 'hidden', + 'delete' => 'delete', + 'store' => 'Store changes', + 'title' => 'Title', + 'link' => 'Link', + 'description' => 'Description'); + my %status=(); + unless ($newsfeed{$id.'_status'}) { $newsfeed{$id.'_status'}='public'; } + $status{$newsfeed{$id.'_status'}}='checked="checked"'; + $r->print(< - +       @@ -233,39 +387,57 @@ ENDSCRIPT       - +
    -
    +$lt{'title'}: +
    +$lt{'description'}:

    - +$lt{'link'}: +
    ENDEDIT - } else { - if (($newsfeed{$id.'_status'} ne 'public') && ($viewpubliconly)) { next; } - if ($newsfeed{$id.'_status'} eq 'hidden') { next; } - $r->print("\n".($html?"\n
  • ":"\n").$newsfeed{$id.'_title'}. - ($html?"</b><br />\n":"\n"). - $newsfeed{$id.'_description'}. - ($html?"
    \n".&mt('Read more')."
    \n"):"\n")); - if ($newsfeed{$id.'_enclosureurl'}) { - $r->print(($html?"".&mt('Enclosure')."":"' />")); - } - if ($html) { - $r->print("\n
  • \n"); - } else { - $r->print("\n".$id.$filterfeedname.'_'.$udom.'_'.$uname."\n"); - } + } else { # not in edit mode, just displaying + if (($newsfeed{$id.'_status'} ne 'public') && ($viewpubliconly)) { next; } + if ($newsfeed{$id.'_status'} eq 'hidden') { next; } + $r->print("\n".($html?"\n
  • ":"\n").$newsfeed{$id.'_title'}. + ($html?"</b><br />\n":"\n"). + $newsfeed{$id.'_description'}. + ($html?"
    \n".&mt('Read more')."
    \n"):"\n")); +# Enclosure? Get stats + if ($newsfeed{$id.'_enclosureurl'}) { + my @stat=&Apache::lonnet::stat_file($newsfeed{$id.'_enclosureurl'}); + if ($stat[7]) { +# Has non-zero length (and exists) + my $enclosuretype=$newsfeed{$id.'_enclosetype'}; + $r->print(($html?"".&mt('Enclosure')."":"' />")); + } + } + if ($html) { # is HTML + $r->print("\n
  • \n"); + } else { # is RSS + $r->print("\n".$id.$filterfeedname.'_'.$udom.'_'.$uname."\n"); + } + } # end of "in edit mode" + } # end of rendering a real entry + } # end of loop through all keys + if ($html) { + $r->print(''); + if ($edit) { + $r->print(''. + ($displayoption eq 'hidden'?'': + '')); } } - } - } - $r->print("\n".($html?''.($edit?'':'').'':''."\n")); + } # was a real display feedname + $r->print(($html?''.&Apache::loncommon::end_page():''."\n")); + } # a real user return OK; -} +} # end handler 1; __END__