array(
'title' => t('Change inactive user settings'),
),
);
}
/**
* Implements hook_menu().
*/
function inactive_user_menu() {
$items['admin/people/inactive-user'] = array(
'title' => 'Inactive users',
'description' => 'Set rules and contact templates for inactive users.',
'page callback' => 'drupal_get_form',
'page arguments' => array('inactive_user_custom_settings'),
'access arguments' => array('change inactive user settings'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_user_cancel().
*/
function inactive_user_user_cancel($edit, $account, $method) {
db_delete('inactive_users')
->condition('uid', $account->uid)
->execute();
}
/**
* Custom settings page: menu callback
*
* (we're using a custom callback to enable a nicer menu title,
* without underscore)
*/
function inactive_user_custom_settings() {
$period = array(0 => 'disabled') + drupal_map_assoc(array(604800, 1209600, 1814400, 2419200, 2592000, 7776000, 15552000, 23328000, 31536000, 47088000, 63072000), '_format_interval');
$warn_period = array(0 => 'disabled') + drupal_map_assoc(array(86400, 172800, 259200, 604800, 1209600, 1814400, 2592000), '_format_interval');
$mail_variables = ' %username, %useremail, %lastaccess, %period, %sitename, %siteurl';
//For Users how have a taxonomie to build groups
$parentGroupID = 8; //8 ist die Parent Id von CUR
$groups = array(0 => 'disabled') + drupal_map_assoc(get_group_list($parentGroupID));
// set administrator e-mail
$form['inactive_user_admin_email_fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Administrator e-mail'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['inactive_user_admin_email_fieldset']['inactive_user_admin_email'] = array(
'#type' => 'textfield',
'#title' => t('E-mail addresses'),
'#default_value' => _inactive_user_admin_mail(),
'#description' => t('Supply a comma-separated list of e-mail addresses that will receive administrator alerts. Spaces between addresses are allowed.'),
'#maxlength' => 256,
'#required' => TRUE,
);
//Selct group
$form['inactive_user_group_fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('User groups'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['inactive_user_group_fieldset']['inactive_user_groups'] = array(
'#type' => 'select',
'#title' => t('user groups'),
'#multiple' => TRUE,
'#default_value' => variable_get('inactive_user_groups', 0),
'#options' => $groups,
'#description' => t('The other functions are applied only to users from the suitable groups. WARNING: Parent Groups don\'t select childs. If you select groups, please unselect disable.
WARNING: Disabled is the same as all groups!'),
);
// inactive user notification
$form['inactive_user_notification'] = array(
'#type' => 'fieldset',
'#title' => t('Inactive user notification'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['inactive_user_notification']['inactive_user_notify_admin'] = array(
'#type' => 'select',
'#title' => t('Notify administrator when a user hasn\'t logged in for more than'),
'#default_value' => variable_get('inactive_user_notify_admin', 0),
'#options' => $period,
'#description' => t('Generate an email to notify the site administrator that a user account hasn\'t been used for longer than the specified amount of time. Requires crontab.'),
);
$form['inactive_user_notification']['inactive_user_notify'] = array(
'#type' => 'select',
'#title' => t('Notify users when they haven\'t logged in for more than'),
'#default_value' => variable_get('inactive_user_notify', 0),
'#options' => $period,
'#description' => t('Generate an email to notify users when they haven\'t used their account for longer than the specified amount of time. Requires crontab.'),
);
$form['inactive_user_notification']['inactive_user_notify_text'] = array(
'#type' => 'textarea',
'#title' => t('Body of user notification e-mail'),
'#default_value' => variable_get('inactive_user_notify_text', _inactive_user_mail_text('notify_text')),
'#cols' => 70,
'#rows' => 10,
'#description' => t('Customize the body of the notification e-mail sent to the user.') . ' ' . t('Available variables are:') . $mail_variables,
'#required' => TRUE,
);
return system_settings_form($form);
}
//list for form
function get_group_list($vid){
$grouplist = array();
//var_dump(taxonomy_get_tree($vid));
foreach (taxonomy_get_tree($vid) as $group) {
$grouplist[$group->tid] = $group->name;
}
return $grouplist;
}
/* Change index of grouplist array to numbers*/
function get_selectedgroups(){
//var_dump(variable_get('inactive_user_groups', 0));
$grouplist = array();
foreach(variable_get('inactive_user_groups', 0) as $key => $group){
$grouplist[] = $group;
}
return $grouplist;
}
//list of selected groups as a string e.g. 'WIN10A','WIN10B',...
function get_selectedgroups_asString(){
//var_dump(variable_get('inactive_user_groups', 0));
$groups = get_selectedgroups();
if(in_array('disabled', $groups, true) OR in_array("0", $groups, true)){
return false;
}
$groupstring = '\''.$groups[0].'\'';
for( $i = 1; $i < count ($groups); $i++){
$groupstring .= ',\''.$groups[$i].'\'';
}
return $groupstring;
}
function inactive_user_custom_settings_validate($form, &$form_state) {
$valid_email = $form_state['values']['inactive_user_admin_email'];
$mails = explode(',', $valid_email);
$count = 0;
foreach ($mails as $mail) {
if ($mail && !valid_email_address(trim($mail))) {
$invalid[] = $mail;
$count++;
}
}
if ($count == 1) {
form_set_error('inactive_user_admin_email', t('%mail is not a valid e-mail address', array('%mail' => $invalid[0])));
}
elseif ($count > 1) {
form_set_error('inactive_user_admin_email', t('The following e-mail addresses are invalid: %mail', array('%mail' => implode(', ', $invalid))));
}
}
/**
* Implements hook_cron().
*/
function inactive_user_cron() {
$grouplist = get_selectedgroups_asString();
if($grouplist == false){
watchdog('cron', 'Check inactive users in all groups.', $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL);
}
else{
watchdog('cron', 'Check inactive users in the following groups: '.$grouplist.'', $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL);
}
//if ((REQUEST_TIME - variable_get('inactive_user_timestamp', '0')) >= 86100) { // Only check once every almost-day, so we slide around the clock and don't overload the server.
variable_set('inactive_user_timestamp', REQUEST_TIME);
$user_list = '';
// reset notifications if recent user activity
$result = db_query('SELECT uid FROM {inactive_users} WHERE uid <> :uid', array(':uid' => 1));
foreach ($result as $uid) {
$u = db_fetch_object(db_query('SELECT access, name FROM {users} WHERE uid = :uid', array(':uid' => $uid)));
if ($u->access > REQUEST_TIME - 604800) {
// user activity in last week, remove from inactivity table
db_query('DELETE FROM {inactive_users} WHERE uid = :uid', array(':uid' => $uid));
db_delete('inactive_users')
->condition('uid', $uid)
->execute();
watchdog('user', 'recent user activity: %user removed from inactivity list', array('%user' => $u->name), WATCHDOG_NOTICE, l(t('edit user'), "user/$uid/edit", array('query' => array('destination' => 'admin/user/user'))));
}
}
// notify administrator of inactive user accounts
if ($notify_time = variable_get('inactive_user_notify_admin', 0)) {
if(is_bool($grouplist) && $grouplist == false){
$result = db_query('SELECT uid, name, mail, access, created FROM {users} WHERE ((access <> 0 AND login <> 0 AND access < (:request_time - :notify_time)) OR (login = 0 AND created < (:request_time - :notify_time))) AND uid <> 1', array(':request_time' => REQUEST_TIME, ':notify_time' => $notify_time));
}
else{
$result = db_query('SELECT {users}.uid, {users}.name, {users}.mail, {users}.access, {users}.created FROM {users} LEFT JOIN {field_revision_field_taxgroup} ON {users}.uid = {field_revision_field_taxgroup}.entity_id LEFT JOIN {taxonomy_term_data} ON {field_revision_field_taxgroup}.field_taxgroup_tid = {taxonomy_term_data}.tid WHERE (({users}.access <> 0 AND {users}.login <> 0 AND {users}.access < (:request_time - :notify_time)) OR ({users}.login = 0 AND {users}.created < (:request_time - :notify_time))) AND {users}.uid <> 1 AND taxonomy_term_data.Name IN (:grouplist)', array(':request_time' => REQUEST_TIME, ':notify_time' => $notify_time, ':grouplist' => $grouplist)); //grouplist is a list as a string
}
var_dump($result);
//while ($user = db_fetch_object($result)) {
foreach ($result as $user) {
echo "found user";
print_r($user);
//If user not notify yet and have a uid etc.
if ($user->uid && !db_fetch_object(db_query('SELECT uid FROM {inactive_users} WHERE uid = :uid AND notified_admin = 1', array(':uid' => $user->uid))) && ($user->created < (REQUEST_TIME - $notify_time))) {
db_query('UPDATE {inactive_users} SET notified_admin = 1 WHERE uid = :uid', $user->uid);
if (!db_affected_rows()) {
// must create a new row
@db_query('INSERT INTO {inactive_users} (uid, notified_admin) VALUES (:uid, 1)', array(':uid' => $user->uid));
}
$user_list .= "$user->name ($user->mail) last active on " . format_date($user->access, 'large') . ".\n";
watchdog('user', 'Admin notified of inactivity user: %user', array('%user' => $user->name), WATCHDOG_INFO, l(t('edit user'), "user/$user->uid/edit", array('query' => array('destination' => 'admin/user/user'))));
}
}
if (isset($user_list)) {
_inactive_user_mail(t('[@sitename] Inactive users', array('@sitename' => variable_get('site_name', 'drupal'))), _inactive_user_mail_text('notify_admin_text'), $notify_time, NULL, $user_list);
unset($user_list);
}
}
// notify users that their account has been inactive
if ($notify_time = variable_get('inactive_user_notify', 0)) {
if(is_bool($grouplist) && $grouplist == false){
$result = db_query('SELECT * FROM {users} WHERE ((access <> 0 AND login <> 0 AND access < (:request_time - :notify_time)) OR (login = 0 AND created < (:request_time - :notify_time))) AND status <> 0 AND uid <> 1', array(':request_time' => REQUEST_TIME, ':notify_time' => $notify_time));
}
else{
$result = db_query('SELECT * FROM {users} LEFT JOIN {field_revision_field_taxgroup} ON {users}.uid = {field_revision_field_taxgroup}.entity_id LEFT JOIN {taxonomy_term_data} ON {field_revision_field_taxgroup}.field_taxgroup_tid = {taxonomy_term_data}.tid WHERE (({users}.access <> 0 AND {users}.login <> 0 AND {users}.access < (:request_time - :notify_time)) OR ({users}.login = 0 AND {users}.created < (:request_time - :notify_time))) AND status <> 0 AND {users}.uid <> 1 AND taxonomy_term_data.Name IN (:grouplist)', array(':request_time' => REQUEST_TIME, ':notify_time' => $notify_time, ':grouplist' => $grouplist));
}
echo '
';
var_dump($result);
//while ($user = db_fetch_object($result)) {
foreach ($result as $user) {
echo "found user";
print_r($user);
if ($user->uid && !db_fetch_object(db_query('SELECT uid FROM {inactive_users} WHERE notified_user = 1 AND uid = :uid', array(':uid' => $user->uid))) && ($user->created < (REQUEST_TIME - $notify_time))) {
db_query('UPDATE {inactive_users} SET notified_user = 1 WHERE uid = :uid', array(':uid' => $user->uid));
if (!db_affected_rows()) {
@db_query('INSERT INTO {inactive_users} (uid, notified_user) VALUES (:uid, 1)', array(':uid' => $user->uid));
}
_inactive_user_mail(t('[@sitename] Account inactivity', array('@sitename' => variable_get('site_name', 'drupal'))), variable_get('inactive_user_notify_text', _inactive_user_mail_text('notify_text')), $notify_time, $user, NULL);
watchdog('user', 'user %user notified of inactivity', array('%user' => $user->name), WATCHDOG_INFO, l(t('edit user'), "user/$user->uid/edit", array('query' => array('destination' => 'admin/user/user'))));
}
}
}
}
/**
* Slighty modified from format_interval() in common.inc to include months.
*/
function _format_interval($timestamp, $granularity = 2) {
$units = array(
'1 year|@count years' => 31536000,
'1 month|@count months' => 2592000,
'1 week|@count weeks' => 604800,
'1 day|@count days' => 86400,
'1 hour|@count hours' => 3600,
'1 min|@count min' => 60,
'1 sec|@count sec' => 1,
);
$output = '';
foreach ($units as $key => $value) {
$key = explode('|', $key);
if ($timestamp >= $value) {
$output .= ($output ? ' ' : '') . format_plural(floor($timestamp / $value), $key[0], $key[1]);
$timestamp %= $value;
$granularity--;
}
if ($granularity == 0) {
break;
}
}
return ($output) ? $output : t('0 sec');
}
/**
* Get administrator e-mail address(es)
*/
function _inactive_user_admin_mail() {
$uid = 1;
$admin_mail = db_query('SELECT mail FROM {users} WHERE uid = :uid', array(':uid' => $uid));
return variable_get('inactive_user_admin_email', variable_get('site_mail', $admin_mail));
}
/**
* Implements hook_mail().
*/
function inactive_user_mail($key, &$message, $params) {
$message['subject'] = $params['subject'];
$message['body'][] = $params['message'];
}
/**
* Wrapper for user_mail.
*/
function _inactive_user_mail($subject, $message, $period, $user = NULL, $user_list = NULL) {
global $base_url;
if ($user_list) {
$to = _inactive_user_admin_mail();
$variables = array(
'%period' => _format_interval($period),
'%sitename' => variable_get('site_name', 'drupal'),
'%siteurl' => $base_url,
"%userlist" => $user_list,
);
}
elseif (isset($user->uid)) {
$to = $user->mail;
$variables = array(
'%username' => $user->name,
'%useremail' => $user->mail,
'%lastaccess' => empty($user->access) ? t('never') : format_date($user->access, 'custom', 'M d, Y'),
'%period' => _format_interval($period),
'%sitename' => variable_get('site_name', 'drupal'),
'%siteurl' => $base_url,
);
}
if (isset($to)) {
$from = variable_get('site_mail', ini_get('sendmail_from'));
$headers = array(
'Reply-to' => $from,
'Return-path' => "<$from>",
'Errors-to' => $from,
);
$recipients = explode(',', $to);
foreach ($recipients as $recipient) {
$recipient = trim($recipient);
$params = array(
'subject' => $subject,
'message' => strtr($message, $variables),
'headers' => $headers,
);
$user = array_shift(user_load_multiple(array(), array('mail' => $recipient)));
$language = isset($user->uid) ? user_preferred_language($user) : language_default();
drupal_mail('inactive_user', 'inactive_user_notice', $recipient, $language, $params, $from, TRUE);
}
}
}
/**
* Some default e-mail notification strings.
*/
function _inactive_user_mail_text($message) {
switch ($message) {
case 'notify_text':
return t("Hello %username,\n\n We haven't seen you at %sitename since %lastaccess, and we miss you! Please come back and visit us soon at %siteurl.\n\nSincerely,\n %sitename team");
break;
case 'notify_admin_text':
return t("Hello,\n\n This automatic notification is to inform you that the following users haven't been seen on %sitename for more than %period:\n\n%userlist");
break;
case 'block_warn_text':
return t("Hello %username,\n\n We haven't seen you at %sitename since %lastaccess, and we miss you! This automatic message is to warn you that your account will be disabled in %period unless you come back and visit us before that time.\n\n Please visit us at %siteurl.\n\nSincerely,\n %sitename team");
break;
case 'block_notify_text':
return t("Hello %username,\n\n This automatic message is to notify you that your account on %sitename has been automatically disabled due to no activity for more than %period.\n\n Please visit us at %siteurl to have your account re-enabled.\n\nSincerely,\n %sitename team");
break;
case 'block_notify_admin_text':
return t("Hello,\n\n This automatic notification is to inform you that the following users have been automatically blocked due to inactivity on %sitename for more than %period:\n\n%userlist");
break;
case 'delete_warn_text':
return t("Hello %username,\n\n We haven't seen you at %sitename since %lastaccess, and we miss you! This automatic message is to warn you that your account will be completely removed in %period unless you come back and visit us before that time.\n\n Please visit us at %siteurl.\n\nSincerely,\n %sitename team");
break;
case 'delete_notify_text':
return t("Hello %username,\n\n This automatic message is to notify you that your account on %sitename has been automatically removed due to no activity for more than %period.\n\n Please visit us at %siteurl if you would like to create a new account.\n\nSincerely,\n %sitename team");
break;
case 'delete_notify_admin_text':
return t("Hello,\n\n This automatic notification is to inform you that the following users have been automatically deleted due to inactivity on %sitename for more than %period:\n\n%userlist");
break;
}
}
/**
* Returns 1 if the user has ever created a node or a comment.
*
* The settings of inactive_user.module allow to protect such
* users from deletion.
*/
function _inactive_user_with_content($uid) {
return (db_fetch_object(db_query('SELECT uid FROM {node} WHERE uid = :uid', array(':uid' => $uid))) || db_fetch_object(db_query('SELECT uid FROM {comments} WHERE uid = :uid', array(':uid' => $uid))));
}