/[drupal]/contributions/modules/pmetrics/pmetrics.module
ViewVC logotype

Contents of /contributions/modules/pmetrics/pmetrics.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download) (as text)
Mon Jul 2 20:10:13 2007 UTC (2 years, 4 months ago) by yaph
Branch: MAIN
CVS Tags: DRUPAL-5--1-0-DEV, HEAD
File MIME type: text/x-php
Initial commit of the pmetrics module for Drupal
1 <?php
2 /**
3 * $Id$
4 *
5 * @file
6 * Author: Ramiro Gómez - http://www.ramiro.org
7 * This module integrates the perfromancing Metrics tracking code for Drupal
8 * sites and provides functions for accessing the pMetrics Web services API.
9 */
10
11 // Constants
12 define(MAX_TITLE_LENGTH, 30);
13 /**
14 * Implementation of hook_perm().
15 */
16 function pmetrics_perm() {
17 return array('administer pmetrics');
18 }
19
20 /**
21 * Implementation of hook_menu().
22 */
23 function pmetrics_menu($may_cache) {
24 $items = array();
25 $items[] = array(
26 'path' => 'admin/settings/pmetrics',
27 'title' => t('pmetrics'),
28 'description' => t('Set your site id and key for accessing data collected
29 by pMetrics via its Web services API.'),
30 'callback' => 'drupal_get_form',
31 'callback arguments' => array('pmetrics_admin_settings'),
32 'access' => user_access('administer pmetrics'),
33 'type' => MENU_NORMAL_ITEM
34 );
35 return $items;
36 }
37
38 /**
39 * admin settings for the pmetrics module
40 */
41 function pmetrics_admin_settings() {
42 $form = array();
43 $form['pmetrics'] = array(
44 '#type' => 'fieldset',
45 '#title' => 'pmetrics ' . t('settings'),
46 '#collapsible' => TRUE,
47 '#collapsed' => FALSE
48 );
49
50 $form['pmetrics']['pmetrics_site_id'] = array(
51 '#type' => 'textfield',
52 '#title' => t('Site ID'),
53 '#default_value' => variable_get('pmetrics_site_id', ''),
54 '#size' => 10,
55 '#maxlength' => 10,
56 '#required' => TRUE,
57 '#description' => t('Enter your pmetrics Site ID.')
58 );
59
60 $form['pmetrics']['pmetrics_site_key'] = array(
61 '#type' => 'textfield',
62 '#title' => t('Site Key'),
63 '#default_value' => variable_get('pmetrics_site_key', ''),
64 '#size' => 20,
65 '#maxlength' => 20,
66 '#required' => False,
67 '#description' => t('Enter your pmetrics Site Key. If you want to make pMetrics
68 statistics data accessible from your site you must enter your Site Key.')
69 );
70
71 $form['pmetrics']['pmetrics_api_url'] = array(
72 '#type' => 'textfield',
73 '#title' => t('Base API URL'),
74 '#default_value' => variable_get('pmetrics_api_url',
75 'http://pmetrics.performancing.com/stats/api'),
76 '#size' => 60,
77 '#maxlength' => 128,
78 '#required' => TRUE,
79 '#description' => t('The URL of the pMetrics API server. Only change the value
80 if you know what you are doing.')
81 );
82
83 $period = drupal_map_assoc(array(3600, 21600, 43200, 86400), 'format_interval');
84 $form['pmetrics']['pmetrics_cache_lifetime'] = array(
85 '#type' => 'select',
86 '#title' => t('Minimum cache lifetime'),
87 '#default_value' => variable_get('pmetrics_cache_lifetime', 43200),
88 '#options' => $period,
89 '#required' => TRUE,
90 '#description' => t('The data accessible via the pMetrics API is updated every 30 minutes.
91 To not overuse this service the minium cache lifetime is set to 12 hours by default.')
92 );
93
94 return system_settings_form($form);
95 }
96
97 /*
98 * pmetrics settings validation
99 */
100 function pmetrics_admin_settings_validate($form_id, $form_values) {
101 $site_id = trim($form_values['pmetrics_site_id']);
102 $cache_lifetime = trim($form_values['pmetrics_cache_lifetime']);
103 if ($site_id == '' || !is_numeric($site_id)) {
104 form_set_error('pmetrics_site_id', t('You must enter a correct Site ID.'));
105 }
106 if ($cache_lifetime == '' || !is_numeric($cache_lifetime)) {
107 form_set_error('pmetrics_site_id', t('A minimum cache life time must be set.'));
108 }
109 }
110
111 /**
112 * implementation of hook_block
113 */
114 function pmetrics_block($op = 'list', $delta = O, $edit = array()) {
115 if ($op == 'view') {
116 // pMetrics Tracking Code
117 if ($delta == 0) {
118 return array(
119 'subject' => '',
120 'content' => pmetrics_tracking_code()
121 );
122 }
123 }
124 elseif ($op == 'list') {
125 $blocks[0]['info'] = t('pMetrics Tracking Code');
126 return $blocks;
127 }
128 elseif ($op == 'configure' && $delta == 0) {
129 $form['code_type'] = array(
130 '#type' => 'select',
131 '#title' => t('JavaScript or Image'),
132 '#default_value' => variable_get('pmetrics_tracking_code_type', 1),
133 '#options' => array( t('Image only'), t('JavaScript') ),
134 '#required' => TRUE,
135 '#description' => t('Use JavaScript code providing more detailed statistics or an image.')
136 );
137 return $form;
138 }
139 elseif ($op == 'save' && $delta == 0) {
140 variable_set('pmetrics_tracking_code_type', $edit['code_type']);
141 return;
142 }
143 }
144
145 /*
146 * returns the pMetrics tracking code to be included in a block
147 */
148 function pmetrics_tracking_code() {
149 $site_id = variable_get('pmetrics_site_id', '');
150 if ($site_id) {
151 $pmetrics_code_type = variable_get('pmetrics_tracking_code_type', 1);
152 $pmetrics_code = '<p><img alt="Performancing Metrics"
153 src="http://pmetrics.performancing.com/'.
154 $site_id .'ns.gif" /></p>';
155
156 if ($pmetrics_code_type) {
157 $pmetrics_code = '<script src="http://pmetrics.performancing.com/'. $site_id .
158 '.js" type="text/javascript"></script><noscript>'. $pmetrics_code .'</noscript>';
159 }
160 return $pmetrics_code;
161 }
162 }
163
164 /**
165 * The Web Services request is done here.
166 *
167 * @param $request_params
168 * an array of parameters to pass to pmetrics as part of the query.
169 * the element name and value are passed as the parameter name and value
170 * respectively
171 * @result
172 * a string containing the query result as a php object.
173 */
174 function pmetrics_request($request_params) {
175 $url = pmetrics_request_url($request_params);
176
177 // check whether the request is cached
178 $phpobj = cache_get($url, 'pmetrics_cache');
179 if (is_object($phpobj)) {
180 if($phpobj->expire < time()) {
181 cache_clear_all($url, 'pmetrics_cache');
182 } else {
183 return $phpobj->data;
184 }
185 }
186 // not cached or cache expired
187 else {
188 unset($phpobj);
189 /*
190 $ch = curl_init($url);
191 curl_setopt($ch, CURLOPT_HEADER, 0);
192 ob_start();
193 curl_exec($ch);
194 $pmetrics_php = ob_get_clean();
195 curl_close($ch);
196 */
197
198 $handle = @fopen($url, "r");
199 if ($handle) {
200 while (!feof($handle)) {
201 $phpobj .= fgets($handle, 4096);
202 }
203 fclose($handle);
204 }
205
206 cache_set($url, 'pmetrics_cache',
207 $phpobj, time() + variable_get('pmetrics_cache_lifetime', 43200));
208 return $phpobj;
209 }
210 }
211
212 /**
213 * @param $request_params
214 * an array of parameters to pass to pmetrics as part of the query.
215 * the element name and value are passed as the parameter name and value
216 * respectively
217 * @result
218 * a string containing the query URL.
219 */
220 function pmetrics_request_url($request_params) {
221 $api_url = variable_get('pmetrics_api_url', 'http://pmetrics.performancing.com/stats/api');
222 $request_params['output'] = 'php';
223 $request_params['site_id'] = variable_get('pmetrics_site_id', '');
224 $request_params['sitekey'] = variable_get('pmetrics_site_key', '');
225
226 // build query string
227 $params = array();
228 foreach($request_params as $key => $val) {
229 $params[] = $key .'='. $val;
230 }
231 $query = implode('&', $params);
232
233 return url($api_url, $query, NULL, TRUE);
234 }
235 ?>

  ViewVC Help
Powered by ViewVC 1.1.2