1

I am trying to build a website where people vote on individual nodes of a content type using the rate module. Is it possible to reset these votes say every 7 days or every 30 days, so that another round of voting can take place. How would I do this within drupal. I do not want to have to go through phpmyadmin to clear the voting api cache. I would prefer if this is done from within the website I'm building.

Is this possible?

1 Answer 1

0

There's many ways to accomplish this. The voting data for the Voting API module is stored in the votingapi_vote & votingapi_cache tables. Essentially, you need a way to target the vote data you wish to eliminate in these tables (e.g. via a DB query) and then build some process on which to regularly execute it (e.g. a cron run).

A crude example would be a custom module that evokes hook_cron() to check the last query execution and fire it if it hits its time threshold:

/**
 * Implements hook_cron().
 */
function MYMODULE_cron() {

  // Clear the vote every week.
  $vote_clear_timestamp = variable_get('MYMODULE_vote_clear_timestamp', 0);
  if ($vote_clear_timestamp <= strtotime('-1 week')) {

    // A more targeted process would use db_delete() to target criteria
    db_query("TRUNCATE {votingapi_vote}");
    db_query("TRUNCATE {votingapi_cache}");
    variable_set('MYMODULE_vote_clear_timestamp', time());
  }
}
3
  • I'm not very good with writing custom modules, please could you guide me Jul 12, 2016 at 6:54
  • Drupal Answers isn't a tutorial site. Google for tutorials on creating a module in D7 or take a look at the examples module. The code is pretty straight forward for D7; you need a mymodule.info & mymodule.module file with the cron function in the mymodule.module
    – Shawn Conn
    Jul 12, 2016 at 17:46
  • Ok, I think I understand what you're saying now. The question now is how to limit it per content type Jul 13, 2016 at 6:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.