ad_weight_probability.module in Advertisement 5.2
Same filename and directory in other branches
A plug in for the ad.module, allowing an admin to set the probability that a given advertisement will be displayed.
Copyright (c) 2008. Jeremy Andrews <jeremy@kerneltrap.org>.
File
weight/probability/ad_weight_probability.moduleView source
<?php
/**
* @file
* A plug in for the ad.module, allowing an admin to set the probability that
* a given advertisement will be displayed.
*
* Copyright (c) 2008.
* Jeremy Andrews <jeremy@kerneltrap.org>.
*/
define('AD_PROBABILITY_DEFAULT', 100);
/**
* Implementation of hook_form_alter().
* Generate a form for assigning a weight to an advertisement.
*/
function ad_weight_probability_form_alter($form_id, &$form) {
if (isset($form['type']) && $form_id == 'ad_node_form') {
$node = $form['#node'];
$form['weighting'] = array(
'#type' => 'fieldset',
'#access' => user_access('configure ad probability'),
'#title' => t('Weight'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['weighting']['probability'] = array(
'#type' => 'select',
'#access' => user_access('configure ad probability'),
'#title' => t('Probability'),
'#options' => _ad_weight_probability_weights(),
'#default_value' => $node->probability,
'#description' => t('The greater the probability, the more frequently this advertisement will be displayed. An advertisement with a probablity of 2 will be displayed twice as frequently as an advertisement with a probability of 1.'),
);
$form['weighting']['#weight'] = -1;
}
}
/**
* Implementation of hook_nodeapi().
*/
function ad_weight_probability_nodeapi($node, $op, $arg = 0) {
switch ($op) {
case 'load':
return _ad_weight_probability_node_load($node);
case 'insert':
case 'update':
if (user_access('configure ad probability')) {
return _ad_weight_probability_node_save($node, $op);
}
case 'delete':
return _ad_weight_probability_node_delete($node);
}
}
/**
* Implementation of hook_perm().
*/
function ad_weight_probability_perm() {
return array(
t('configure ad probability'),
);
}
/**
* Implementation of hook_ad_build_cache().
*/
function ad_weight_probability_ad_build_cache() {
$cache = array();
$active = db_query("SELECT a.aid, p.probability FROM {ads} a LEFT JOIN {ad_weight_probability} p ON a.aid = p.aid WHERE adstatus = 'active'");
while ($ad = db_fetch_object($active)) {
$probability = $ad->probability ? $ad->probability : AD_PROBABILITY_DEFAULT;
$ads[$ad->aid] = $probability;
}
$cache['weight']['probability'] = $ads;
$cache['weight']['hook_weight'] = array(
'weight' => 10,
'file' => drupal_get_path('module', 'ad_weight_probability') . '/ad_weight_probability.inc',
'function' => 'ad_weight_probability_cache_filter',
);
return $cache;
}
/**
* Helper function, load the probability from the database.
*/
function _ad_weight_probability_node_load($node) {
$probability = (int) db_result(db_query('SELECT probability FROM {ad_weight_probability} WHERE aid = %d', $node->nid));
$output['probability'] = $probability ? $probability : AD_PROBABILITY_DEFAULT;
return $output;
}
/**
* Helper function, save the probability to the database.
*/
function _ad_weight_probability_node_save($node) {
if (is_object($node) && $node->nid) {
db_query('UPDATE {ad_weight_probability} SET probability = %d WHERE aid = %d', $node->probability, $node->nid);
if (!db_affected_rows()) {
db_query('INSERT INTO {ad_weight_probability} (aid, probability) VALUES(%d, %d)', $node->nid, $node->probability);
}
ad_rebuild_cache();
}
}
/**
* Helper function, delete the probability from the database.
*/
function _ad_weight_probability_node_delete($node) {
db_query('DELETE FROM {ad_weight_probability} WHERE aid = %d', $node->nid);
}
/**
*
*/
function _ad_weight_probability_weights() {
return array(
25 => t('1/4'),
33 => t('1/3'),
50 => t('1/2'),
100 => t('1'),
200 => t('2'),
300 => t('3'),
400 => t('4'),
);
}
Functions
Name![]() |
Description |
---|---|
ad_weight_probability_ad_build_cache | Implementation of hook_ad_build_cache(). |
ad_weight_probability_form_alter | Implementation of hook_form_alter(). Generate a form for assigning a weight to an advertisement. |
ad_weight_probability_nodeapi | Implementation of hook_nodeapi(). |
ad_weight_probability_perm | Implementation of hook_perm(). |
_ad_weight_probability_node_delete | Helper function, delete the probability from the database. |
_ad_weight_probability_node_load | Helper function, load the probability from the database. |
_ad_weight_probability_node_save | Helper function, save the probability to the database. |
_ad_weight_probability_weights |
Constants
Name![]() |
Description |
---|---|
AD_PROBABILITY_DEFAULT | @file A plug in for the ad.module, allowing an admin to set the probability that a given advertisement will be displayed. |