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

Contents of /contributions/modules/account_reminder/account_reminder.module

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


Revision 1.13 - (show annotations) (download) (as text)
Tue Jun 30 07:16:15 2009 UTC (4 months, 4 weeks ago) by jaydub
Branch: MAIN
CVS Tags: HEAD
Changes since 1.12: +53 -35 lines
File MIME type: text/x-php
initial port of Account Reminder to Drupal 7. It works...
1 <?php
2 // $Id: account_reminder.module,v 1.12 2009/06/27 20:57:23 jaydub Exp $
3
4 /**
5 * Module by Computerminds http://www.computerminds.co.uk Jan 2007
6 *
7 * Upgraded to Drupal 5 with contributions from maartenvg
8 * PostgreSQL support added by jaydub
9 * Upgraded to Drupal 6 by jaydub
10 * Upgraded to Drupal 7 by jaydub
11 *
12 * This module is to remind users who have signed up a user account,
13 * but have never signed in. It's driven by the cron job and can be
14 * configured under "/admin/settings/account_reminder" .
15 *
16 */
17
18 /**
19 * Implement hook_help().
20 */
21 function account_reminder_help($path, $arg) {
22 switch ($path) {
23 case 'admin/help#account_reminder':
24 $output = '<p>' . t('The account reminder module is designed to allow a site to communicate with users who have registered for an account at the site but have never logged in. If after a configurable number of days after a user registers the user has not yet logged in, this module will send them a reminder email. After a configurable number of days after the first reminder email if the user still has not logged in, a subsequent email reminder is sent. A cap to the number of reminder emails is also a configurable setting.') . '</p>';
25 return $output;
26 }
27 }
28
29 /**
30 * Implement hook_perm().
31 */
32 function account_reminder_perm() {
33 return array(
34 'administer account reminder' => array(
35 'title' => t('Administer Account Reminder'),
36 'description' => t('Configure settings for Account Reminder'),
37 ),
38 );
39 }
40
41 /**
42 * Implement hook_menu().
43 */
44 function account_reminder_menu() {
45 $items = array();
46
47 $items['admin/settings/account_reminder'] = array(
48 'title' => 'Account Reminder',
49 'description' => 'Settings of Account Reminder, sets reminder frequency and mail details',
50 'page callback' => 'drupal_get_form',
51 'page arguments' => array('account_reminder_settings'),
52 'access callback' => 'user_access',
53 'access arguments' => array('administer account reminder'),
54 );
55 $items['account-reminder'] = array(
56 'title' => 'Unsubscribe',
57 'page callback' => 'account_reminder_unsubscribe',
58 'access callback' => TRUE,
59 'type' => MENU_CALLBACK
60 );
61
62 return $items;
63 }
64
65 /**
66 * For the admin/settings/account_reminder page
67 */
68 function account_reminder_settings() {
69 $form = array();
70
71 $form['frequency'] = array(
72 '#type' => 'fieldset',
73 '#title' => t('Reminder frequency'),
74 );
75 $form['frequency']['account_reminder_initial'] = array(
76 '#type' => 'textfield',
77 '#title' => t('Days till initial reminder'),
78 '#description' => t('The time span (in days) between the user registering and the initial reminder being sent.'),
79 '#default_value' => variable_get('account_reminder_initial', 10),
80 '#size' => 10,
81 );
82 $form['frequency']['account_reminder_frequency'] = array(
83 '#type' => 'textfield',
84 '#title' => t('Days between reminders'),
85 '#description' => t('The time (in days) between subsequent reminders.'),
86 '#default_value' => variable_get('account_reminder_frequency', 14),
87 '#size' => 10,
88 );
89 $form['frequency']['account_reminder_total'] = array(
90 '#type' => 'textfield',
91 '#title' => t('Total number of reminders'),
92 '#description' => t("Total number of reminders to send to a user if they have not logged in. If they do not login after the reminder has been sent out this many times, no further mails will be sent out."),
93 '#default_value' => variable_get('account_reminder_total', 3),
94 '#size' => 10,
95 );
96 $form['frequency']['account_reminder_cronlimit'] = array(
97 '#type' => 'textfield',
98 '#title' => t('Number of reminders sent per cron run'),
99 '#description' => t('The maximum number of account reminder emails sent per cron run. Enter 0 to send all account reminder emails.'),
100 '#default_value' => variable_get('account_reminder_cronlimit', 100),
101 '#size' => 10,
102 );
103
104 $form['mail'] = array(
105 '#type' => 'fieldset',
106 '#title' => t('Reminder email details'),
107 );
108 $form['mail']['account_reminder_bcc'] = array(
109 '#type' => 'textfield',
110 '#title' => t('Bcc email address'),
111 '#description' => t('If you wish to receive a copy of all account reminder emails, enter an email address to Bcc.'),
112 '#default_value' => variable_get('account_reminder_bcc', ''),
113 '#size' => 40,
114 );
115 $form['mail']['account_reminder_subject'] = array(
116 '#type' => 'textfield',
117 '#title' => t('Email subject'),
118 '#default_value' => _account_reminder_mail_text('account_reminder_subject'),
119 '#description' => t('The subject of the account reminder email. Valid replacement variables are !site and !username'),
120 );
121 $form['mail']['account_reminder_msg'] = array(
122 '#type' => 'textarea',
123 '#title' => t('Email body'),
124 '#default_value' => _account_reminder_mail_text('account_reminder_msg'),
125 '#description' => t('Customize the body of the account reminder email. Valid variables are !site, !username, !login_uri, !login_url (the one time login link), !unsubscribe_url (A URL allowing users to opt-out of recieving the reminder emails) and !password.'),
126 );
127
128 return system_settings_form($form);
129 }
130
131 /**
132 * module settings form validation
133 */
134 function account_reminder_settings_validate($form, &$form_state) {
135 if (!is_numeric($form_state['values']['account_reminder_initial'])) {
136 form_set_error('account_reminder_initial', t('You must enter a number for "Days till initial reminder".'));
137 }
138 if (!is_numeric($form_state['values']['account_reminder_frequency'])) {
139 form_set_error('account_reminder_frequency', t('You must enter a number for "Days between reminders".'));
140 }
141 if (!is_numeric($form_state['values']['account_reminder_total'])) {
142 form_set_error('account_reminder_total', t('You must enter a number for "Total number of reminders".'));
143 }
144 if (!is_numeric($form_state['values']['account_reminder_cronlimit'])) {
145 form_set_error('account_reminder_cronlimit', t('You must enter a number for "Number of reminders sent per cron run".'));
146 }
147 if (!empty($form_state['values']['account_reminder_bcc']) && !valid_email_address($form_state['values']['account_reminder_bcc'])) {
148 form_set_error('account_reminder_bcc', t('You must enter a valid email address.'));
149 }
150 }
151
152 /**
153 * Implement hook_user_cancel().
154 *
155 * Delete the user from the account reminder table if the user is cancelled
156 */
157 function account_reminder_user_cancel(&$edit, &$account, $method) {
158 switch ($method) {
159 case 'user_cancel_reassign':
160 case 'user_cancel_delete':
161 db_delete('account_reminder')
162 ->condition('uid', $account->uid)
163 ->execute();
164 break;
165 }
166 }
167
168 /**
169 * Implement hook_cron().
170 */
171 function account_reminder_cron() {
172 // Remove users who have signed up from the reminder table...
173 $subquery = db_select('users', 'u');
174 $subquery->fields('u', array('uid'));
175 $subquery->condition('login', 0, '<>')
176 ->condition('uid', 0, '<>')
177 ->condition('status', 1);
178
179 db_delete('account_reminder')
180 ->condition('uid', $subquery, 'IN')
181 ->execute();
182
183 $count = 0;
184 $first_wait = variable_get('account_reminder_initial', 10);
185 $period = variable_get('account_reminder_frequency', 14);
186 $total_messages = variable_get('account_reminder_total', 3);
187 $cronlimit = variable_get('account_reminder_cronlimit', 100);
188
189 // Add users to the account_reminder table who are not already
190 $result = db_query('SELECT u.*, COALESCE(ar.msg_cnt, 0) AS msg_cnt, COALESCE(ar.last_reminder, :time) AS last_reminder FROM {users} u LEFT JOIN {account_reminder} ar ON u.uid = ar.uid WHERE u.login = 0 AND u.uid <> 0 AND u.status = 1', array(':time' => REQUEST_TIME));
191 while (($cronlimit == 0 || $count < $cronlimit) && $row = $result->fetchObject()) {
192 if ( ($row->msg_cnt == 0 && (($row->created + (86400 * $first_wait)) < REQUEST_TIME))
193 || (((int)((REQUEST_TIME - $row->last_reminder) / 86400)) >= $period && ($row->msg_cnt >= 0 && ($row->msg_cnt < $total_messages))) ) {
194 account_reminder_send_email($row);
195 db_merge('account_reminder')
196 ->key(array('uid' => $row->uid))
197 ->fields(array(
198 'last_reminder' => REQUEST_TIME,
199 'msg_cnt' => $row->msg_cnt + 1,
200 ))
201 ->execute();
202 $count++;
203 }
204 }
205 }
206
207 /**
208 * Implement hook_mail().
209 */
210 function account_reminder_mail($key, &$message, $params) {
211 $language = $message['language'];
212 $variables = user_mail_tokens($params['user'], $language);
213 $variables += array(
214 '!password' => $params['context']['pass'],
215 '!unsubscribe_url' => $params['context']['unsubscribe_url'],
216 );
217
218 $subject = strtr($params['context']['subject'], $variables);
219 $body = strtr($params['context']['body'], $variables);
220 $message['subject'] .= str_replace(array("\r", "\n"), '', $subject);
221 $message['body'][] = $body;
222 if (isset($params['headers']['Bcc'])) {
223 $message['headers']['Bcc'] = $params['headers']['Bcc'];
224 }
225 }
226
227 /**
228 * Produce the URL for users to "just say no"
229 */
230 function account_reminder_unsubscribe_url($user) {
231 return url('account-reminder/' . $user->uid . '/' . md5($user->uid . $user->pass), array('absolute' => TRUE));
232 }
233
234 /**
235 * This user does not want us spamming them any more!
236 */
237 function account_reminder_unsubscribe($uid, $hash) {
238 $account = user_load($uid);
239 if ($hash == md5($uid . $account->pass)) {
240 db_update('account_reminder')
241 ->fields(array('msg_cnt' => -1))
242 ->condition('uid', $uid)
243 ->execute();
244 drupal_set_message(t('Account reminder emails successfully cancelled'));
245 drupal_goto();
246 }
247 else{
248 drupal_set_message(t("invalid account reminder email unsubscription URL"));
249 drupal_goto();
250 }
251 }
252
253 /**
254 * Sends the email out.
255 */
256 function account_reminder_send_email($user) {
257 $context = $headers = array();
258
259 // Assign the user a new password because we do not know the old one!!!
260 $pass = user_password();
261 user_save($user, array('pass' => $pass));
262 $user->pass = md5($pass);
263 $context['pass'] = $pass;
264
265 $context['unsubscribe_url'] = account_reminder_unsubscribe_url($user);
266 $context['subject'] = _account_reminder_mail_text('account_reminder_subject');
267 $context['body'] = _account_reminder_mail_text('account_reminder_msg');
268
269 $bcc = variable_get('account_reminder_bcc', '');
270 if ($bcc) {
271 $headers['Bcc'] = $bcc;
272 }
273
274 $params = array('user' => $user, 'context' => $context, 'headers' => $headers);
275 $message = drupal_mail('account_reminder', 'account_reminder_email', $user->mail, user_preferred_language($user), $params);
276 if ($message['result']) {
277 watchdog("account_reminder", 'Reminder email sent to !user at !email.', array('!user' => $user->name, '!email' => $user->mail));
278 }
279 }
280
281 /**
282 * Returns an email subject or body string.
283 *
284 * Used by account_reminder_send_mail() and the settings forms to retrieve strings.
285 */
286 function _account_reminder_mail_text($key, $language = NULL, $variables = array()) {
287 $options = isset($language) ? array('langcode' => $language->language) : array();
288 if ($default_text = variable_get($key, FALSE)) {
289 return strtr($default_text, $variables);
290 }
291 else {
292 switch ($key) {
293 case 'account_reminder_subject':
294 return t('Account details for !username at !site', $variables, $options);
295 break;
296 case 'account_reminder_msg':
297 return t('!username,
298
299 This is a reminder from !site. You have registered for a user account but have not yet validated your email address. To fully activate your account at !login_uri login using the following username and password:
300
301 username: !username
302 password: !password
303
304 You may also log in by clicking on this link or copying and pasting it in your browser:
305
306 !login_url
307
308 This is a one-time login, so it can be used only once.
309
310
311 -- !site team
312
313 You can stop receiving these reminder emails by either activating your account, or using the unsubscribe link below:
314
315 !unsubscribe_url', $variables, $options);
316 break;
317 }
318 }
319 }

  ViewVC Help
Powered by ViewVC 1.1.2