home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (rules_scheduler.module)

This example Drupal source code file (rules_scheduler.module) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

array, evaluation, execute, file, function, items, log, math, php, plugin, replacements, return, task, tid

The rules_scheduler.module Drupal example source code

<?php
// $Id: rules_scheduler.module,v 1.1.4.7 2011/01/10 19:40:49 fago Exp $

/**
 * @file
 * Rules scheduler module.
 */

define('RULES_SCHEDULER_PATH', 'admin/config/workflow/rules/schedule');

/**
 * Implements hook_cron().
 */
function rules_scheduler_cron() {
  // Evaluate at maxium 30 tasks.
  //TODO: make this configurable.
  $result = db_select('rules_scheduler', 'r', array('fetch' => PDO::FETCH_ASSOC))
    ->fields('r')
    ->condition('date', time(), '<=')
    ->range(0, 30)
    ->execute();

  foreach ($result as $task) {
    if ($component = rules_get_cache('comp_' . $task['config'])) {
      $replacements = array('%label' => $component->label(), '%plugin' => $component->plugin());
      rules_log('Scheduled evaluation of %plugin %label.', $replacements, RulesLog::INFO, TRUE);
      $state = unserialize($task['state']);
      $state->restoreBlocks();
      // Finally evaluate the component with the given state.
      $component->evaluate($state);
      rules_log('Finished evaluation of %plugin %label.', $replacements, RulesLog::INFO, FALSE);
      $state->cleanUp();
    }
    db_delete('rules_scheduler')
      ->condition('tid', $task['tid'])
      ->execute();
  }
  // Log the rules log to the system log.
  if ($log = RulesLog::logger()->render()) {
    watchdog('rules scheduler', 'Scheduled component evaluation: !log', array('!log' => $log), WATCHDOG_NOTICE);
  }
}

/**
 * Implements hook_menu().
 */
function rules_scheduler_menu() {
  $items = array();
  $items[RULES_SCHEDULER_PATH] = array(
    'title' => 'Schedule',
    'type' => MENU_LOCAL_TASK,
    'page callback' => 'rules_scheduler_schedule_page',
    'access arguments' => array('administer rules'),
    'file' => 'rules_scheduler.admin.inc',
  );
  $items[RULES_SCHEDULER_PATH .'/%rules_scheduler_task/delete'] = array(
    'title' => 'Delete a scheduled task',
    'type' => MENU_CALLBACK,
    'page callback' => 'drupal_get_form',
    'page arguments' => array('rules_scheduler_delete_task', 5),
    'access arguments' => array('administer rules'),
    'file' => 'rules_scheduler.admin.inc',
  );
  return $items;
}

/**
 * Load a task by a given task ID.
 */
function rules_scheduler_task_load($tid) {
  $result = db_select('rules_scheduler', 'r')
    ->fields('r')
    ->condition('tid', (int) $tid)
    ->execute();
  return $result->fetchAssoc();
}

/**
 * Delete a task by a given task ID.
 */
function rules_scheduler_task_delete($tid) {
  db_delete('rules_scheduler')
    ->condition('tid', $tid)
    ->execute();
}

/**
 * Schedule a task to be executed later on.
 *
 * @param $task
 *   An array representing the task with the following keys:
 *   - config: The machine readable name of the to be scheduled component.
 *   - date: Timestamp when the component should be executed.
 *   - state: An rules evaluation state to use for scheduling.
 *   - identifier: User provided string to identify the task per scheduled
 *   configuration.
 */
function rules_scheduler_schedule_task($task) {
  if (!empty($task['identifier'])) {
    // If there is a task with the same identifier and component, we replace it.
    db_delete('rules_scheduler')
      ->condition('config', $task['config'])
      ->condition('identifier', $task['identifier'])
      ->execute();
  }
  drupal_write_record('rules_scheduler', $task);
}

/**
 * Implements hook_rules_config_delete().
 */
function rules_scheduler_rules_config_delete($rules_config) {
  // Delete all tasks scheduled for this config.
  db_delete('rules_scheduler')
    ->condition('config', $rules_config->name)
    ->execute();
}

/**
 * Implements hook_views_api().
 */
function rules_scheduler_views_api() {
  return array(
    'api' => '3.0-alpha1',
    'path' => drupal_get_path('module', 'rules_scheduler') .'/includes',
  );
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal rules_scheduler.module source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.