comparison weight/percent/ad_weight_percent.module @ 0:d8a3998dac8e ad

ajout module ad
author pierre
date Fri, 20 Feb 2009 14:04:09 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d8a3998dac8e
1 <?php
2 // $Id: ad_weight_percent.module,v 1.1.2.4.2.2 2009/02/16 17:06:50 jeremy Exp $
3
4 /**
5 * @file
6 * A plug in for the ad.module, providing a percentage based weighting mechanism
7 * for the random selection of ads.
8 *
9 * Copyright (c) 2007-2009.
10 * Jeremy Andrews <jeremy@tag1consulting.com>.
11 */
12
13 /**
14 * Drupal hook_menu().
15 */
16 function ad_weight_percent_menu() {
17 $items = array();
18
19 /* TODO
20 Non menu code that was placed in hook_menu under the '!$may_cache' block
21 so that it could be run during initialization, should now be moved to hook_init.
22 Previously we called hook_init twice, once early in the bootstrap process, second
23 just after the bootstrap has finished. The first instance is now called boot
24 instead of init.
25
26 In Drupal 6, there are now two hooks that can be used by modules to execute code
27 at the beginning of a page request. hook_boot() replaces hook_boot() in Drupal 5
28 and runs on each page request, even for cached pages. hook_boot() now only runs
29 for non-cached pages and thus can be used for code that was previously placed in
30 hook_menu() with $may_cache = FALSE:
31
32 Dynamic menu items under a '!$may_cache' block can often be simplified
33 to remove references to arg(n) and use of '%<function-name>' to check
34 conditions. See http://drupal.org/node/103114.
35
36 The title and description arguments should not have strings wrapped in t(),
37 because translation of these happen in a later stage in the menu system.
38 */
39 if ($may_cache) {
40 $items['admin/content/ad/groups/percent'] = array(
41 'title' => 'Weight Percent',
42 'page callback' => 'drupal_get_form',
43 'page arguments' => array('ad_weight_percent_settings'),
44 'type' => MENU_LOCAL_TASK,
45 'weight' => 5,
46 );
47 }
48
49 return $items;
50 }
51
52 /**
53 * Configure per-group percentage settings.
54 */
55 function ad_weight_percent_settings() {
56 $form = array();
57
58 $groups = module_invoke('ad', 'groups_list', TRUE);
59 foreach ($groups as $tid => $group) {
60 $form["group-$tid"] = array(
61 '#type' => 'fieldset',
62 '#title' => $group->name,
63 '#collapsible' => TRUE,
64 '#collapsed' => variable_get("enable-$tid", 0) ? FALSE : TRUE,
65 );
66 $form["group-$tid"]["description-$tid"] = array(
67 '#type' => 'markup',
68 '#prefix' => '<div>',
69 '#suffix' => '</div>',
70 '#value' => theme_placeholder("$group->description"),
71 );
72 $form["group-$tid"]["enable-$tid"] = array(
73 '#type' => 'checkbox',
74 '#default_value' => variable_get("enable-$tid", 0),
75 '#title' => t('Enabled'),
76 '#description' => t('If enabled, each ad in this group will be weighted per the percentages defined below.'),
77 );
78
79 $result = db_query('SELECT nid FROM {term_node} WHERE tid = %d', $group->tid);
80 while ($nid = db_fetch_object($result)) {
81 $ad = node_load($nid->nid);
82 $percent = db_fetch_object(db_query('SELECT * FROM {ad_weight_percent} WHERE tid = %d AND aid = %d', $tid, $nid->nid));
83 $form["group-$tid"]["ad-$tid"]["$tid-$nid->nid"] = array(
84 '#type' => 'fieldset',
85 '#title' => $ad->title,
86 '#collapsible' => TRUE,
87 );
88 $form["group-$tid"]["ad-$tid"]["$tid-$nid->nid"]["ad-$tid-$nid->nid"] = array(
89 '#type' => 'markup',
90 '#prefix' => '<div>',
91 '#suffix' => '</div>',
92 '#value' => "$ad->ad<br />$ad->url",
93 );
94 $form["group-$tid"]["ad-$tid"]["$tid-$nid->nid"]["percent-$tid-$nid->nid"] = array(
95 '#type' => 'textfield',
96 '#title' => t('Display percent'),
97 '#default_value' => $percent->weight,
98 '#size' => 2,
99 '#maxlength' => 3,
100 '#description' => t("Enter a percentage from 0 to 100. The total percentages of all ads in this group must add up to 100. For example, if you have two ads, and want one to be displayed 70% of the time and the other 30% of the time enter '70' in one and '30' in the other."),
101 );
102 }
103
104 }
105
106 $form['submit'] = array(
107 '#type' => 'submit',
108 '#value' => t('Submit'),
109 );
110
111 return $form;
112 }
113
114 /**
115 * Be sure that all enabled groups add up to a total of 100%.
116 */
117 function ad_weight_percent_settings_validate($form, &$form_state) {
118 $groups = module_invoke('ad', 'groups_list', TRUE);
119 foreach ($groups as $tid => $group) {
120 if ($form_state['values']["enable-$tid"]) {
121 $result = db_query('SELECT nid FROM {term_node} WHERE tid = %d', $group->tid);
122 $total = 0;
123 // Add up total percentages for all nids in group, confirm equals 100%.
124 $first = 0;
125 while ($nid = db_fetch_object($result)) {
126 if (!$first) $first = $nid->nid;
127 $total = $total + (int)$form_state['values']["percent-$tid-$nid->nid"];
128 }
129 // Confirmed that total equals 100%.
130 if ($total != 100) {
131 form_set_error("percent-$tid-$first", t('The total percentage for all ads in the %group group combined must equal 100%. It currently equals %percent.', array('%group' => $group->name, '%percent' => "$total%")));
132 }
133 }
134 }
135 }
136
137 /**
138 * Save the weight percent settings in the database.
139 */
140 function ad_weight_percent_settings_submit($form, &$form_state) {
141 $groups = module_invoke('ad', 'groups_list', TRUE);
142 foreach ($groups as $tid => $group) {
143 variable_set("enable-$tid", (int)$form_state['values']["enable-$tid"]);
144 db_query('DELETE FROM {ad_weight_percent} WHERE tid = %d', $tid);
145 $result = db_query('SELECT nid FROM {term_node} WHERE tid = %d', $group->tid);
146 while ($nid = db_fetch_object($result)) {
147 db_query('INSERT INTO {ad_weight_percent} (tid, aid, weight) VALUES(%d, %d, %d)', $tid, $nid->nid, (int)$form_state['values']["percent-$tid-$nid->nid"]);
148 }
149 }
150 }
151
152 /**
153 * Returns the greatest common divisor of an array of integers.
154 */
155 function ad_weight_percent_gcd($integers) {
156 $gcd = array_shift($integers);
157
158 while (!empty($integers)) {
159 $gcd = _ad_weight_percent_gcd($gcd, array_shift($integers));
160 }
161 return $gcd;
162 }
163
164 /**
165 * Helper function to calculate the greatest common divisor using the Euclidean
166 * algorithm (http://en.wikipedia.org/wiki/Euclidean_algorithm).
167 */
168 function _ad_weight_percent_gcd($a, $b) {
169 if ($b == 0) {
170 return $a;
171 }
172 else {
173 return _ad_weight_percent_gcd($b, $a % $b);
174 }
175 }
176
177 /**
178 * Ad module's adcacheapi _hook().
179 */
180 /*
181 function ad_cache_file_adcacheapi($op, &$node) {
182 switch ($op) {
183 case 'display_variables':
184 $files = variable_get('ad_files', 3);
185 $path = file_create_path();
186 return "&amp;f=$files&amp;p=$path";
187 case 'method':
188 return array('file' => t('File'));
189 case 'description':
190 return t('File based caching will usually offer better performance, however, some find it difficult to enable and it may not offer valid statistics if you are using multiple load balanced web servers.');
191 case 'settings':
192 $form = array();
193 $form['cache']['file'] = array(
194 '#type' => 'fieldset',
195 '#title' => t('File cache settings'),
196 '#collapsible' => TRUE,
197 '#collapsed' => (variable_get('ad_cache', 'none') == 'file') ? FALSE : TRUE,
198 );
199 $form['cache']['file']['ad_files'] = array(
200 '#type' => 'select',
201 '#title' => t('Number of cache files'),
202 '#default_value' => variable_get('ad_files', 3),
203 '#options' => drupal_map_assoc(array(1, 3, 5, 10, 15)),
204 '#description' => t('Please select the number of cache files the ad module should use. Select a smaller value for better accuracy when performaing automatic actions on advertisements at specified thresholds. Select a larger value for better performance. This configuration option is only relevant if the file cache is enabled.')
205 );
206 $period = drupal_map_assoc(array(15,30,60,600,1800,3600,21600,43200,86400), 'format_interval');
207 $form['cache']['file']['ad_cache_file_lifetime'] = array(
208 '#type' => 'select',
209 '#title' => t('Cache lifetime'),
210 '#default_value' => variable_get('ad_cache_file_lifetime', 60),
211 '#options' => $period,
212 '#description' => t('Specify how long information should be cached before ad statistics are updated in the database. Increasing the cache lifetime can improve overall performance. This configuration options is only relevant if the file cache is enabled.'),
213 );
214 return $form;
215 case 'settings_submit':
216 variable_set('ad_cache_file_lifetime', $node['ad_cache_file_lifetime']);
217 if ($node['ad_cache'] != 'file') {
218 ad_cache_file_build(0, variable_get('ad_files', 3));
219 }
220 else {
221 ad_cache_file_build($node['ad_files'], variable_get('ad_files', 3));
222 }
223 variable_set('ad_files', $node['ad_files']);
224 break;
225
226 case 'insert':
227 case 'update':
228 case 'delete':
229 if (variable_get('ad_cache', 'none') == 'file') {
230 ad_cache_file_build();
231 }
232 break;
233 }
234 }
235 */
236