I know about the schedule module, but that requires extra information to be entered on every node. Event nodes already have dates. It would seem a very common requirement to have events disappear once they are past. Does anyone know of a way to accomplish this?

Comments

syawillim’s picture

I'm pretty sure that http://drupal.org/project/scheduler will do what you want.

www.aussiecarpool.com
share the cost of commuting

www.justapickle.com
blogging community for the socially conscious

www.slickfish.com.au
professional, affordable web site design, production and maintenance for small business

venkat-rk’s picture

Unpublish from where? From the page? They disappear automatically from the Upcoming events block once the time is past.

andrewfn’s picture

Several of my sites have a lot of events. The way I structure the sites is to tag all the events with a taxonomy name and then all events of that type can be viewed from associated the taxonomy page, which I link to a menu item. Everything works very well, except that old events have to be manually unpublished or they still keep appearing.

andrewfn’s picture

I have solved the problem by writing the following ultra-simple module called eventhide. If an event is more than a day old, then it's 'changed' date is set to 1999-12-31. I have replaced the standard taxonomy view using the views module to exclude nodes that have changed dates before 2000-01-01 and to put the items in ascending order. Stickies will still show at the top. The advantage of this approach is that if an event gets modified so that it is in the future again (e.g. by recycling old events), then the 'changed' date will automatically be updated and the event will be unhidden.
The code shows an alternative line of code which will unpublish events, but then of course they will disappear from the calendar.

I would value any comments on the code:

// $Id: eventhide.module,v 1.0.0.0 2006/08/06 17:30:14 Andrewfn Exp $

/* If an event is more than a day old, then it's 'changed' date is set to 1999-12-31.
 * To use this, replaced the standard taxonomy view using the views module to exclude nodes that have changed dates before 2000-01-01
 * and to put the items in ascending order. Stickies should still show at the top.
 * The advantage of this approach is that if an event gets modified so that it is in the future again (e.g. by recycling old events),
 * then the 'changed' date will automatically be updated and the event will be unhidden.
 * The code shows an alternative line of code which will unpublish events, but then of course they will disappear from the calendar.
 */

/**
 * Implementation of hook_help().
 */
function eventhide_help($section) {
  if ($section == 'admin/modules#description') {
    return t('A module to hide events that are out of date by setting [changed] to before 2000 so they can be filtered out by the views module.');
  }
}


/**
 * Implementation of hook_perm().
 */
function eventhide_perm() {
  return array('mark event nodes that are out of date');
}

/**
 * Implementation of hook_cron().
 */
function eventhide_cron() {
  $clear_cache = FALSE;

  //if the event_end time is more than a day old, set the change date to 1999-12-31 so that the query module can hide it
  $nodes = db_query('SELECT * FROM {node} n LEFT JOIN {event} e ON e.nid = n.nid WHERE event_end < %d -86400 AND changed > 946600000', time());
  //alternative line of code to unpublish the node (to use, comment previous line and uncomment following line)
  //$nodes = db_query('SELECT * FROM {node} n LEFT JOIN {event} e ON e.nid = n.nid WHERE event_end < %d -86400 AND n.status = 1', time());
  
  while ($node = db_fetch_object($nodes)) {
    //if this node is to be hidden, we can update the node
    db_query('UPDATE {node} SET changed=946600000, promote=0 WHERE nid = %d', $node->nid);
    //alternative line of code to unpublish the node (to use, comment previous line and uncomment following line)
    //db_query('UPDATE {node} SET status=0 WHERE nid = %d', $node->nid);
    
    watchdog('content', t('%type: scheduled hiding of %title.', array('%type' => theme('placeholder', t($node->type)), '%title' => theme('placeholder', $node->title))), WATCHDOG_NOTICE, l(t('view'), 'node/'. $node->nid));
    $clear_cache = TRUE;
  }
  
  if ($clear_cache) {
    // clear the cache so an anonymous poster can see the node being published or unpublished
    cache_clear_all();
  }
}

slingeraap’s picture

Well, I tried this, but now all my dates are out of my calender...

Only odd saterdays and odd sundays show up now, and dates with events.

I removed your module and everything is still blanc in IE6. In Firefox, all dates are being displayed again normally. Don't know if this is an IE problem, cache problem, or problem with your code.

andrewfn’s picture

The only thing my code will modify is the "changed" date on each node. It runs a check and makes changes every time cron is run. If you remove the module, it won't put the old values back again since the change is made in the database.
Unless you set up a view that makes use of this new changed date, then you will see absolutely no difference on your site. The fact that dates are being displayed in Firefox means that your pages are being generated correctly by Drupal. I strongly suggest that you turn off caching while developing the site otherwise you *will* get strange effects. Also it is a good idea to close and restart IE to force it to refresh.

Suzy’s picture

this seems to working well for me,

I have chosen to leave the nodes published for the sake of non-broken (and recycling ;)) URL's and I have also set up an admin "view" to show which nodes I can then recycle more easily ~ I left the comment count showing so I don't recycle events which perhaps had comments

I don't know if this is the best way but I like it, thank you for your ideas and code!

MarcDo’s picture

I added this method to event module, to remove the stickies (on top of page) and promote (on frontpage) of the event:

/**
 * Implementation of hook_cron().
 * @ingroup event_core
 */
function event_cron() {
   
    $curtime = time();
    //calculate the closing time for the event
    //when closing is preferred.  query the database for all events which have a end time less than this,
    $closing_time = $curtime ;
    $result = db_query("SELECT nid FROM {event} WHERE event_end < %d", $closing_time);

    //loop through the results, calling and remove the sticky for the event
    while ($event = db_fetch_object($result)) {
      $node = node_load($event->nid);
	  if ($node->sticky == 1) {
	    db_query("UPDATE {node} SET sticky = 0 WHERE nid = %d", $event->nid);	  
      	watchdog('event', t('Sticky removed for %event by cron.', array('%event' => $node->title)));
	  }
	  if ($node->promote == 1) {
	    db_query("UPDATE {node} SET promote = 0 WHERE nid = %d", $event->nid);	  
      	watchdog('event', t('Promote removed for %event by cron.', array('%event' => $node->title)));
	  }
	}
}

That was enough for me. I didn't want to delete the old events.

Might help some.

Marc

vjuhasz’s picture

Yes, this worked for me, too.

I changed the code a bit to unpublish expired events, by changing the 'sticky' variable to 'status'.

 if ($node->status == 1) {
    db_query("UPDATE {node} SET status = 0 WHERE nid = %d", $event->nid);   
      watchdog('event', t('Expired %event unpublished by cron.', array('%event' => $node->title)));
  }
lias’s picture

Thank you for this code, worked great with Drupal 5.x

rimma’s picture

I will try these codes they are exactly what I want.

Richard Ma
www.meetcampus.com

yolene’s picture

sorry i'm a beginner here ... i don't know how to do that : i simply open the event.module file in notepad, paste in the end the code you wrote, save and upload to the site ?
I did that, but then what ? Where to administer if we want them sticky or not, published or not ?
Thanks a lot in advance for your help with this.

shansmart’s picture

where i have to paste that coding?? In Calendar or Date module?? please tell briefly

Robertas’s picture

This code is great - I just had to fix two lines for drupal 6:

    $curtime = date("Y-m-d h:i");
    //calculate the closing time for the event
    $result = db_query("SELECT nid FROM {event} WHERE event_start < \"%s\"", $curtime);

For some reason original lines did not work.

BigMike’s picture

andrewfn,

I realize this thread is rather old, but I have a question if you are still around to help.

You said,

using the views module to exclude nodes that have changed dates before 2000-01-01

I am new to Drupal and am still trying to figure out how the filtering and rules works. I have Events module and the Views module installed on a 5.12 installation. I am using a View for events that are linked to my calendar, and a Block through the same View. This Block I display on the right sidebar of my site where Teasers of upcoming events are displayed.

Of course if I do not add any new content to this Block, expired events remain on the right side bar for all to see..

So I am at the Edit page for this View I have, and I do not know how to set it to exclude nodes that have changed dates before 2000-01-01. I figured out how to put them in ascending order, but I need help with the exclusion based on date method!

Can someone please shed some light my way! Thanks!
Mike

saitanay’s picture

BigMike’s picture

saitanay,

Thank you for your reply, however this will unpublish nodes which I do not need. I ended up just removing the block from my front page.

I found out that in D6 there is a module to add different types of publishing options that could then be used with a Views filter, so I think I'll go this route as soon as I make the jump (yeah, I'm still using D5...)

Thanks,
Mike