FINAL suepr merge step : added all modules to this super repos

This commit is contained in:
Bachir Soussi Chiadmi
2015-04-19 16:46:59 +02:00
7585 changed files with 1723356 additions and 18 deletions

View File

@@ -0,0 +1,339 @@
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Lesser General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) year name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.
<signature of Ty Coon>, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License.

View File

@@ -0,0 +1,95 @@
CONTENTS OF THIS FILE
---------------------
- Introduction
- Installation
- Configuration
- Accessing statistics from a theme
INTRODUCTION
------------
Current Maintainer: Liam McDermott.
Contact via: http://www.intermedia-online.com/#contact
The primary goal of User stats is to provide commonly requested user statistics
for themers. These are:
- days registered;
- join date;
- days since last login;
- days since last post;
- post count;
- login count;
- user online/offline;
- IP address;
The module has also been improved to integrate with the Rules (http://drupal.org/project/rules)
and Views (http://drupal.org/project/views) modules. Finally, it also provides
IP address to assist with catching spammers on community sites.
INSTALLATION
------------
User stats is installed in the same way as most other Drupal modules.
Here are the instructions for anyone unfamiliar with the process:
1. Copy this user_stats/ directory to your sites/SITENAME/modules directory.
2. Back in your Web browser, navigate to Administer -> Site building -> Modules
then enable the User Stats module.
CONFIGURATION
-------------
The configuration page for User Stats can be found by browsing to
Administer -> Site configuration -> User Stats settings
If IP address tracking is enabled (and Views is installed),
'IP address tracking' will appear under the Administer -> Reports section.
Integration with Rules and Views happens automatically, see these modules'
configuration pages for functionality provided by User Stats.
ACCESSING STATISTICS FROM A THEME
---------------------------------
Some knowledge of theme authoring is needed for this task. For more information
on theme authoring, see: http://drupal.org/theme-guide/6 Alternatively the
Advanced Forum (http://drupal.org/project/advanced_forum) module assists with
this and provides lots of helpful documentation.
User Stats are accessed from a theme using the user_stats_get_stats() function.
For example:
<p>Post count: <?php print user_stats_get_stats('post_count', $node->uid); ?></p>
Placed in node-forum.tpl.php would print the user's post count. The first argument
is a string indicating which statistic should be retrieved, the second is an
integer user id who's statistics should be retrieved. Following is a list of the
statistics it is possible to retrieve:
- ip_address -- IP Address of the user that posted the node;
- join_date -- time stamp representing when the user joined;
- login_count -- number of times the user has logged-in;
- login_days -- number of days since the user was last logged-in to the site;
- post_count -- number of nodes and comments created by the user (the type of
posts to count is configurable in the User stats settings page);
- post_days -- number of days since the user last posted;
- reg_days -- number of days since the user registered on the site;
- online -- whether the user is online (basically whether they have done
anything in the last 15 minutes);
For more information, and examples, see: http://www.apaddedcell.com/an-introduction-user-stats-module-drupal

View File

@@ -0,0 +1,196 @@
<?php
/**
* @file
* Commonly requested user statistics for themers administration interface.
*/
/**
* Implements hook_settings().
*/
function user_stats_admin_settings($form, &$form_state) {
$form['post_count_options'] = array(
'#type' => 'fieldset',
'#title' => t('Post count options'),
'#collapsible' => TRUE,
'#collapsed' => (variable_get('user_stats_count_posts', TRUE) || variable_get('user_stats_count_comments', TRUE) ? FALSE : TRUE),
);
$form['post_count_options']['user_stats_count_posts'] = array(
'#type' => 'checkbox',
'#title' => t('Count posts'),
'#description' => t('If checked user post counts will be calculated.'),
'#default_value' => variable_get('user_stats_count_posts', TRUE),
);
$form['post_count_options']['user_stats_count_comments'] = array(
'#type' => 'checkbox',
'#title' => t('Count comments'),
'#description' => t('If checked user comments counts will be included in the total user post count.'),
'#default_value' => variable_get('user_stats_count_comments', TRUE),
);
foreach (node_type_get_types() as $types) {
$options[$types->type] = $types->name;
}
$form['post_count_options']['user_stats_included_content_types'] = array(
'#type' => 'select',
'#title' => t('Content types to include in post count'),
'#description' => t('Select the content types to include in the user post count (hold ctrl or shift to select multiple types). Both nodes and comments will be included in the post count. If you do not select any content types, then all types will be counted.'),
'#options' => $options,
'#default_value' => variable_get('user_stats_included_content_types', array()),
'#multiple' => TRUE,
'#size' => 10,
);
$form['post_count_options']['user_stats_user_per_cron'] = array(
'#type' => 'select',
'#title' => t('Number of users to update per cron run'),
'#options' => array(
'10' => '10',
'25' => '25',
'50' => '50',
'100' => '100',
'200' => '200',
),
'#default_value' => variable_get('user_stats_user_per_cron', array('25')),
);
$form['post_count_options']['post_count_reset'] = array(
'#type' => 'fieldset',
'#title' => t('Post count reset'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['post_count_options']['post_count_reset']['user_stats_reset_post_count'] = array(
'#type' => 'submit',
'#value' => t('Reset all post counts'),
);
$form['login_count_options'] = array(
'#type' => 'fieldset',
'#title' => t('Login count options'),
'#collapsible' => TRUE,
'#collapsed' => (variable_get('user_stats_count_logins', TRUE) ? FALSE : TRUE),
);
$form['login_count_options']['user_stats_count_logins'] = array(
'#type' => 'checkbox',
'#title' => t('Count logins'),
'#description' => t('If checked user login counts will be calculated.'),
'#default_value' => variable_get('user_stats_count_logins', TRUE),
);
$form['login_count_options']['login_count_reset'] = array(
'#type' => 'fieldset',
'#title' => t('Login count reset'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['login_count_options']['login_count_reset']['user_stats_reset_login_count'] = array(
'#type' => 'submit',
'#value' => t('Reset all login counts'),
);
$form['ip_tracking_options'] = array(
'#type' => 'fieldset',
'#title' => t('IP address tracking'),
'#collapsible' => TRUE,
'#collapsed' => (variable_get('user_stats_track_ips', TRUE) ? FALSE : TRUE),
);
$form['ip_tracking_options']['user_stats_track_ips'] = array(
'#type' => 'checkbox',
'#title' => t('Track IP addresses'),
'#description' => t('If checked the IP addresses of users will be logged. This may be a privacy issue for some people, but is very useful for discovering spammers on community sites.'),
'#default_value' => variable_get('user_stats_track_ips', TRUE),
);
$period = drupal_map_assoc(array(604800, 4838400, 15724800, 31536000, 157680000, 315360000), 'format_interval');
$form['ip_tracking_options']['user_stats_flush_ips_timer'] = array(
'#type' => 'select',
'#title' => t('Discard IP logs older than'),
'#description' => t('Older IP log entries will be automatically discarded. (Requires a correctly configured <a href="@cron">cron maintenance task</a>.)', array('@cron' => url('admin/reports/status'))),
'#options' => $period,
'#default_value' => variable_get('user_stats_flush_ips_timer', 31536000),
);
$form['online_user_options'] = array(
'#type' => 'fieldset',
'#title' => t('Online users'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
// Following blatantly nicked from /modules/user/user.module and should be updated if that changes.
$period = drupal_map_assoc(array(30, 60, 120, 180, 300, 600, 900, 1800, 2700, 3600, 5400, 7200, 10800, 21600, 43200, 86400), 'format_interval');
$form['online_user_options']['user_block_seconds_online'] = array(
'#type' => 'select',
'#title' => t('User activity'),
'#default_value' => variable_get('user_block_seconds_online', 900),
'#options' => $period,
'#description' => t('A user is considered online for this long after they have last viewed a page.'),
);
$form['#submit'][] = 'user_stats_settings_change';
return system_settings_form($form);
}
/**
* Settings change callback.
* TODO: make this check a setting has been changed before resetting counts.
*/
function user_stats_settings_change($form, &$form_state) {
$op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
// Give user feedback that post counts have been reset as requested.
if ($op == t('Reset all post counts')) {
drupal_goto('admin/config/people/user_stats/reset_post_count');
}
else if ($op == t('Reset all login counts')) {
drupal_goto('admin/config/people/user_stats/reset_login_count');
}
// If the user has decided not to track IPs, we delete all the logged entries.
if (!$form_state['values']['user_stats_track_ips'] &&
variable_get('user_stats_track_ips', TRUE) &&
db_query("SELECT COUNT(*) FROM {user_stats_ips}")->fetchField()) {
db_query("TRUNCATE TABLE {user_stats_ips}");
}
// We also need to rebuild the menus, as some Views we provide in the
// Reports section rely on IP address tracking.
if ($form_state['values']['user_stats_track_ips'] != variable_get('user_stats_track_ips', TRUE)) {
// We have to save the form value for user_stats_track_ips before the menus
// are rebuilt.
variable_set('user_stats_track_ips', $form_state['values']['user_stats_track_ips']);
menu_rebuild();
}
// Rebuild post count stats when settings change.
variable_set('user_stats_rebuild_stats', TRUE);
user_stats_reset_counts('post_count');
}
/**
* Reset post count handler.
*/
function user_stats_reset_post_count() {
variable_set('user_stats_rebuild_stats', TRUE);
user_stats_reset_counts('post_count');
drupal_set_message(t('Post counts have been reset.'));
drupal_goto('admin/config/people/user_stats');
}
/**
* Reset login count handler.
*/
function user_stats_reset_login_count() {
user_stats_reset_counts('login_count');
drupal_set_message(t('User login counts have been reset.'));
drupal_goto('admin/config/people/user_stats');
}

View File

@@ -0,0 +1,31 @@
<?php
/**
* Aggregates list of all custom user statistics
*/
function hook_default_user_stats() {
return array(
'custom_count_1' => 'custom_count_1_callback',
'custom_count_2' => 'custom_count_2_callback',
);
}
function custom_count_1_callback($uid = NULL) {
global $user;
if ($uid == NULL) {
$uid = $user->uid;
}
return 1;
}
function custom_count_2_callback($uid = NULL) {
global $user;
if ($uid == NULL) {
$uid = $user->uid;
}
return 2;
}

View File

@@ -0,0 +1,61 @@
<?php
/**
* @file
* Provides a preprocess function for the author pane used by
* Advanced Forum and Advanced Profile Kit.
*/
/**
* Implements hook_preprocess_author_pane().
*/
function user_stats_preprocess_author_pane(&$variables) {
if (function_exists('author_pane_api') && author_pane_api() == "2") {
// Check if this preprocess needs to be run given who's calling it.
if (!author_pane_run_preprocess('user_stats', $variables['caller'])) {
return;
}
}
$account_id = $variables['account']->uid;
if ($account_id != 0) {
static $cached_stats = array();
if (isset($cached_stats[$account_id])) {
// Pull the values from the cache
if ($cached_stats[$account_id]['posts'] !== FALSE) {
// We don't want the variable to be created if the user doesn't have
// access to see them to avoid printing the label.
$variables['user_stats_posts'] = $cached_stats[$account_id]['posts'];
}
if ($cached_stats[$account_id]['ip'] !== FALSE) {
$variables['user_stats_ip'] = $cached_stats[$account_id]['ip'];
}
}
else {
// No cached values for this user. Fetch them.
$posts = user_stats_get_stats('post_count', $account_id);
if ($posts !== FALSE) {
$variables['user_stats_posts'] = $posts;
}
$ip = user_stats_get_stats('ip_address', $account_id);
if ($ip !== FALSE) {
$variables['user_stats_ip'] = $ip;
}
// Write to the cache variable. We do this even if it's FALSE because
// we check for that before using it.
$cached_stats[$account_id]['posts'] = $posts;
$cached_stats[$account_id]['ip'] = $ip;
}
}
}
/**
* Implements hook_author_pane_allow_preprocess_disable().
*/
function user_stats_author_pane_allow_preprocess_disable() {
return array('user_stats' => 'User Stats');
}

View File

@@ -0,0 +1,32 @@
name = User Stats
description = "Provides post count and other user statistics. Integrates well with Token and Advanced Forum (if installed)."
core = 7.x
configure = admin/config/people/user_stats
project = "user_stats"
files[] = user_stats.admin.inc
files[] = user_stats.api.php
files[] = user_stats.author-pane.inc
files[] = user_stats.install
files[] = user_stats.module
files[] = user_stats.rules.inc
files[] = views/user_stats.views.inc
files[] = views/user_stats.views_default.inc
files[] = views/views_handler_field_ip_user_count.inc
files[] = views/views_handler_field_is_online.inc
files[] = views/views_handler_field_user_ip_count.inc
files[] = views/views_handler_field_user_picture_bare.inc
files[] = views/views_handler_filter_is_online.inc
files[] = views/views_handler_sort_ip_user_count.inc
files[] = views/views_handler_sort_is_online.inc
files[] = views/views_handler_sort_user_ip_count.inc
; Information added by drupal.org packaging script on 2013-09-11
version = "7.x-1.x-dev"
core = "7.x"
project = "user_stats"
datestamp = "1378898964"

View File

@@ -0,0 +1,104 @@
<?php
/**
* @file
* Install and update hooks for the User Stats module.
*/
/**
* Implements hook_schema().
*/
function user_stats_schema() {
$schema['user_stats_values'] = array(
'description' => 'User Stats data.',
'fields' => array(
'name' => array(
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'default' => '',
'description' => 'The name of the statistic.',
),
'uid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'The {users}.uid of the statistic user.',
),
'value' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'The value of the statistic.',
),
),
'primary key' => array('name', 'uid'),
);
$schema['user_stats_ips'] = array(
'description' => 'IP address storage, links timestamps and uids to IP',
'fields' => array(
'iid' => array(
'type' => 'serial',
'not null' => TRUE,
'description' => 'Primary key: IP address unique ID.',
),
'uid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'The {users}.uid of the user.',
),
'ip_address' => array(
'type' => 'varchar',
'length' => 40,
'not null' => TRUE,
'default' => '',
'description' => "The user's IP address.",
),
'first_seen_timestamp' => array(
'description' => 'The Unix timestamp when the IP address was first used by this user.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array('iid'),
'indexes' => array(
'uid' => array('uid'),
'first_seen_timestamp' => array('first_seen_timestamp'),
),
);
return $schema;
}
/**
* Implements hook_uninstall().
*/
function user_stats_uninstall() {
variable_del('user_stats_rebuild_stats');
variable_del('user_stats_last_cron_check');
variable_del('user_stats_included_content_types');
variable_del('user_stats_reset_login_count');
variable_del('user_stats_reset_post_count');
variable_del('user_stats_user_per_cron');
variable_del('user_stats_count_posts');
variable_del('user_stats_count_comments');
variable_del('user_stats_count_logins');
}
/**
* Expands the width of the field table to 40.
*/
function user_stats_update_7102(&$sandbox) {
$ip_address = array(
'type' => 'varchar',
'length' => 40,
'not null' => TRUE,
'default' => '',
'description' => "The user's IP address.",
);
db_change_field('user_stats_ips', 'ip_address', 'ip_address', $ip_address);
}

View File

@@ -0,0 +1,957 @@
<?php
/**
* @file
* User Stats provides commonly requested user statistics for themers.
* These are:
* - days registered;
* - join date;
* - days since last login;
* - days since last post;
* - post count;
* - login count;
* - user online/offline;
* - IP address;
*
* Note for hackers: function parameters should go in the order
* $op/$type, $uid, $data (where applicable).
*/
/**
* Implements hook_permission().
*/
function user_stats_permission() {
return array(
'administer user stats' => array(
'title' => t('administer user stats'),
'description' => t('TODO Add a description for \'administer user stats\''),
),
'View statistics' => array(
'title' => t('View statistics'),
'description' => t('TODO Add a description for \'View statistics\''),
),
'View IP addresses' => array(
'title' => t('View IP addresses'),
'description' => t('TODO Add a description for \'View IP addresses\''),
),
);
}
/**
* Implements hook_menu().
*/
function user_stats_menu() {
$items = array();
// Admin settings
$items['admin/config/people/user_stats'] = array(
'title' => 'User Stats settings',
'description' => 'Configuration of user stats module options.',
'page callback' => 'drupal_get_form',
'page arguments' => array('user_stats_admin_settings'),
'access arguments' => array('administer user stats'),
'file' => 'user_stats.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/config/people/user_stats/reset_post_count'] = array(
'title' => 'reset user post stats',
'page callback' => 'user_stats_reset_post_count',
'access arguments' => array('administer user stats'),
'file' => 'user_stats.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/config/people/reset_login_count'] = array(
'title' => 'reset user login stats',
'page callback' => 'user_stats_reset_login_count',
'access arguments' => array('administer user stats'),
'file' => 'user_stats.admin.inc',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Returns user stats.
*
* @param $type
* The statistic to return. Possible values are:
* - "ip_address"
* - "join_date"
* - "login_count"
* - "login_days"
* - "post_count"
* - "post_days"
* - "reg_days"
* - "online"
* - "profile"
* @param $uid
* The user id who's stats should be retrieved.
*
* @return
* The statistic requested. Every statistic except join_date, online and IP address is a numeric.
* Join date is a string, whilst online is a boolean and IP Address a string.
* Note: if $type = "post_days" and the user hasn't posted any content (of the
* counted types) then 'n/a' is returned.
*/
function user_stats_get_stats($type, $uid) {
// Sometimes $uid can be NULL (comment previews for example).
if (!is_numeric($uid)) {
return;
}
// IP address is really a bit of feature creep.
// At some point in the future, this could be split off into its own module.
if ($type == 'ip_address') {
if (!user_access('View IP addresses')) {
return FALSE;
}
// Check cache.
if (user_stats_cache_get($type, $uid) === FALSE) {
$query = db_query("SELECT ip_address
FROM {user_stats_ips} WHERE uid = :uid
ORDER BY first_seen_timestamp LIMIT 1", array(':uid' => $uid));
user_stats_cache_set($type, $uid, $query->fetchField());
}
return user_stats_cache_get($type, $uid);
}
// Everything else is under the 'View statistics' permission.
if (!user_access('View statistics')) {
return FALSE;
}
// Check cache first.
if (user_stats_cache_get($type, $uid) !== FALSE) {
return user_stats_cache_get($type, $uid);
}
switch ($type) {
case 'join_date':
$data = db_query("SELECT created FROM {users} WHERE uid = :uid", array(':uid' => $uid))->fetchField();
break;
case 'login_count':
if (!variable_get('user_stats_count_logins', TRUE)) {
$data = 'n/a';
}
else if (user_stats_isset($type, $uid)) {
$data = db_query("SELECT value FROM {user_stats_values} WHERE name = :name AND uid = :uid", array(':name' => 'login_count', ':uid' => $uid))->fetchField();
}
else {
return 0;
}
break;
case 'login_days':
$user_access = db_query("SELECT access FROM {users} WHERE uid = :uid", array(':uid' => $uid))->fetchField();
$data = floor((REQUEST_TIME - $user_access) / 86400);
break;
case 'login_date':
$data = db_query("SELECT access FROM {users} WHERE uid = :uid", array(':uid' => $uid))->fetchField();
break;
case 'post_count':
if (!variable_get('user_stats_count_posts', TRUE) && !variable_get('user_stats_count_comments', TRUE)) {
$data = 'n/a';
}
else if (!user_stats_isset('post_count', $uid)) {
user_stats_post_count_update('reset', $uid);
}
$query = db_query("SELECT value FROM {user_stats_values}
WHERE name = :name AND uid = :uid", array(':name' => 'post_count', ':uid' => $uid));
$posts = $query->fetchField();
//@TODO Figure out why adding comments here wasn't in the D6 version
if(variable_get('user_stats_count_comments', TRUE)){
}
$data = $posts;
break;
case 'post_days':
$last_post = _user_stats_last_post($uid);
if ($last_post !== FALSE) {
$data = floor((REQUEST_TIME - $last_post) / 86400);
}
else {
$data = 'n/a';
}
break;
case 'reg_days':
$user_created = db_query("SELECT created FROM {users} WHERE uid = :uid", array(':uid' => $uid))->fetchField();
$data = floor((REQUEST_TIME - $user_created) / 86400);
break;
case 'online':
$user_access = db_query("SELECT timestamp FROM {sessions} WHERE uid = :uid", array(':uid' => $uid))->fetchField();
$data = ((REQUEST_TIME - $user_access) < variable_get('user_block_seconds_online', 900) ? TRUE : FALSE);
break;
default:
// Check for custom statistics
$custom_stats = array();
$module_list = module_implements('default_user_stats');
foreach ($module_list as $module) {
$custom_stats = array_merge($custom_stats, module_invoke($module, 'default_user_stats'));
}
if (array_key_exists($type, $custom_stats)) {
module_load_include('module', 'qna');
$data = call_user_func($custom_stats[$type], $uid);
break;
}
else {
// Raise an error if the statistic doesn't exist.
$err_message = 'Statistic "' . check_plain($type) . '" does not exist.';
trigger_error($err_message, E_USER_WARNING);
return;
}
}
user_stats_cache_set($type, $uid, $data);
return user_stats_cache_get($type, $uid);
}
/**
* Return data from the non-persistent User Stats cache. Single values
* are returned according to type of statistic and unique user id.
*
* @param $type
* The type of statistic to retrieve, this corresponds to the statistic
* types used by user_stats_get_stats().
* @param $uid
* Unique ID of the user who's statistic is being retrieved.
*
* @return
* A single value, representing the statistic $type where the unique user id
* is $uid. Or FALSE if there is no value in the cache for this combination
* of $type and $uid.
*
* @see user_stats_get_stats().
* @see user_stats_cache_set().
*/
function user_stats_cache_get($type, $uid) {
$user_stats_cache = user_stats_cache_set();
if (isset($user_stats_cache[$uid][$type])) {
return $user_stats_cache[$uid][$type];
}
else {
return FALSE;
}
}
/**
* Store a value in the non-persistent User Stats cache.
*
* If the function is called with no arguments, the entire cache is returned
* without being cleared.
*
* The User Stats cache is a static array, which is why we call it
* non-persistent. The array structure is:
* $user_stats_cache[$uid][$type] = $value.
*
* @param $type
* The type of statistic being stored, this corresponds to the statistic
* types used by user_stats_get_stats(), and one extra used to reset the
* cache: 'reset'.
* @param $uid
* Unique ID of the user who's statistic is being stored. If the type
* is set to 'reset', this user id will have the cache values associated with
* it reset. Alternatively, if $type is set to 'reset' and this is -1, the
* entire cache will be reset.
*
* @return
* Array of the entire cache, or NULL if the cache has been reset.
*
* @see user_stats_get_stats().
* @see user_stats_cache_get().
*/
function user_stats_cache_set($type = NULL, $uid = 0, $data = NULL) {
static $user_stats_cache = array();
// Flush entire cache.
if ($uid == -1 && $type == 'reset') {
unset($user_stats_cache);
return;
}
else if ($uid > -1 && $type == 'reset') {
unset($user_stats_cache[$uid]);
return;
}
// Set cache data. Check against NULL since a zero (in $data at least)
// is valid.
if ($type !== NULL && $data !== NULL) {
$user_stats_cache[$uid][$type] = $data;
}
return $user_stats_cache;
}
/**
* Drupal hook implementations.
*/
/**
* Implements hook_node_insert().
*/
function user_stats_node_insert($node) {
$post_count_content_types = variable_get('user_stats_included_content_types', array());
if ((empty($post_count_content_types) ||
in_array($node->type, $post_count_content_types)) &&
variable_get('user_stats_count_posts', TRUE)) {
if ($node->status) {
user_stats_post_count_update('increment', $node->uid);
}
}
// Do IP Address update.
global $user;
// User IP addresses are only interesting if they are posting the content.
if ($node->uid == $user->uid) {
user_stats_ip_address_update($user->uid, ip_address());
}
}
/**
* Implements hook_node_update().
*/
function user_stats_node_update($node) {
$post_count_content_types = variable_get('user_stats_included_content_types', array());
if ((empty($post_count_content_types) ||
in_array($node->type, $post_count_content_types)) &&
variable_get('user_stats_count_posts', TRUE)) {
// Can't think of any other way of doing this than resetting the user.
user_stats_post_count_update('reset', $node->uid);
}
// User IP addresses are only interesting if they are posting the content.
global $user;
if ($node->uid == $user->uid) {
user_stats_ip_address_update($user->uid, ip_address());
}
}
/**
* Implements hook_node_delete().
*/
function user_stats_node_delete($node) {
$post_count_content_types = variable_get('user_stats_included_content_types', array());
if ((empty($post_count_content_types) ||
in_array($node->type, $post_count_content_types)) &&
variable_get('user_stats_count_posts', TRUE)) {
// Node must be published as unpublished nodes would have already been
// removed from user's post count.
if ($node->status) {
user_stats_post_count_update('decrement', $node->uid);
}
}
}
/**
* Implements hook_comment_insert().
*/
function user_stats_comment_insert($comment) {
if (variable_get('user_stats_count_comments', TRUE)) {
if ($comment->uid == NULL) {
$comment->uid = 0;
}
$post_count_content_types = variable_get('user_stats_included_content_types', array());
$node = node_load($comment->nid);
if (empty($post_count_content_types) || in_array($node->type, $post_count_content_types)) {
if ($comment->status == COMMENT_PUBLISHED) {
user_stats_post_count_update('increment', $comment->uid);
}
}
}
global $user;
// User IP addresses are only interesting if they are posting the content.
if (TRUE && $comment->uid == $user->uid) {
// User IP addresses are only interesting if they are posting the content.
user_stats_ip_address_update($user->uid, ip_address());
}
}
/**
* Implements hook_comment_update().
*/
function user_stats_comment_update($comment) {
if (variable_get('user_stats_count_comments', TRUE)) {
if ($comment->uid == NULL) {
$comment->uid = 0;
}
$post_count_content_types = variable_get('user_stats_included_content_types', array());
$node = node_load($comment->nid);
if (empty($post_count_content_types) || in_array($node->type, $post_count_content_types)) {
if ($comment->status == COMMENT_PUBLISHED) {
user_stats_post_count_update('reset', $comment->uid);
}
}
}
}
/**
* Implements hook_comment_delete().
*/
function user_stats_comment_delete($comment) {
if (variable_get('user_stats_count_comments', TRUE)) {
if ($comment->uid == NULL) {
$comment->uid = 0;
}
$post_count_content_types = variable_get('user_stats_included_content_types', array());
$node = node_load($comment->nid);
if (empty($post_count_content_types) || in_array($node->type, $post_count_content_types)) {
if ($comment->status == COMMENT_PUBLISHED) {
user_stats_post_count_update('decrement', $comment->uid);
}
}
}
}
/**
* Implements hook_cron().
*
* We slowly work through all users without a post count
* updating them.
*/
function user_stats_cron() {
if (variable_get('user_stats_rebuild_stats', TRUE) &&
(variable_get('user_stats_count_posts', TRUE) ||
variable_get('user_stats_count_comments', TRUE))) {
$sql = "SELECT uid FROM {users} WHERE uid NOT IN
(SELECT uid FROM {user_stats_values} WHERE name = 'post_count')";
// Update 25 users per cron run.
$result = db_query_range($sql, 0, variable_get('user_stats_user_per_cron', '25'));
$users_updated = FALSE;
foreach ($result as $update_user) {
user_stats_post_count_update('reset', $update_user->uid);
$users_updated = TRUE;
}
// If all users have been updated we'll avoid running this expensive
// query again by setting the following flag.
if (!$users_updated) {
variable_set('user_stats_rebuild_stats', FALSE);
}
}
// Fire rules day_older event.
// This may seem grossly inefficient, but testing showed that, even firing
// the event for ~100 users, takes less than a second to run when there are
// no rules using this event. With a rule (that adds a role if the user has
// been a member for over 1,000 days) cron took an extra ~40 seconds to run.
// Basically, this has no potential to harm a site's performance, unless a
// rule is configured.
// Having said this: if there's a better way, please raise a bug report!
if (module_exists('rules')) {
$sql = "SELECT uid FROM {users} u ";
// ((last cron - created) - (time() - created)) > one day
$sql .= "WHERE (FLOOR((:request_time-created)/(60*60*24))-FLOOR((:cron_last-created)/(60*60*24)))>0 AND uid>0";
$result = db_query($sql, array(':request_time' => REQUEST_TIME, ':cron_last' => variable_get('cron_last', REQUEST_TIME)));
$reset_user_count = 0;
foreach ($result as $update_user) {
rules_invoke_event('user_stats_day_older', $update_user->uid);
}
}
if (variable_get('user_stats_track_ips', TRUE)) {
// Delete items from the IP log that are past expiry.
db_delete('user_stats_ips')
->condition('first_seen_timestamp', REQUEST_TIME - variable_get('user_stats_flush_ips_timer', 31536000), '<')
->execute();
}
}
/**
* Implements hook_user_login().
*/
function user_stats_user_login(&$edit, $account) {
if (variable_get('user_stats_count_logins', TRUE)) {
user_stats_login_count_update('increment', $account->uid);
}
user_stats_ip_address_update($account->uid, ip_address());
}
/**
* Implements hook_user_logout().
*/
function user_stats_user_logout($account) {
user_stats_ip_address_update($account->uid, ip_address());
}
/**
* Helper function to get the last post created by the user.
*
* @param $account
* User object.
*
* @return
* Unix timestamp: date of the last post (node or comment).
*/
function _user_stats_last_post($uid) {
$sql = "SELECT MAX(created) FROM {node} WHERE status=:status AND uid=:uid";
$all_content_types = node_type_get_types();
$post_count_content_types = variable_get('user_stats_included_content_types', array());
$where = "";
// If some, but not all, content types have been selected in the admin
// interface add a WHERE clause to select only them.
if (!empty($post_count_content_types) && array_keys($all_content_types) != array_keys($post_count_content_types)) {
$content_types = "'" . implode("','", $post_count_content_types) . "'";
$where = ' AND type IN (' . $content_types . ')';
}
$sql .= $where;
// TODO Please convert this statement to the D7 database API syntax.
$max_node = db_query($sql, array(':status' => COMMENT_PUBLISHED, ':uid' => $uid))->fetchField();
$sql = "SELECT MAX(c.changed) FROM {comment} c ";
$where = " WHERE c.status=:status AND c.uid=:uid ";
$join = "";
if (!empty($post_count_content_types) && array_keys($all_content_types) != array_keys($post_count_content_types)) {
$join = " INNER JOIN {node} n ON c.nid=n.nid ";
$where .= 'AND n.type IN (' . $content_types . ')';
}
$sql .= $join . $where;
// TODO Please convert this statement to the D7 database API syntax.
$max_comments = db_query($sql, array(':status' => COMMENT_PUBLISHED, ':uid' => $uid))->fetchField();
if (is_null($max_node) && is_null($max_comments)) {
return FALSE;
}
else if ($max_node > $max_comments) {
return $max_node;
}
else if ($max_node <= $max_comments) {
return $max_comments;
}
}
/**
* Implements hook_views_api().
*
* Other Views hooks in user_stats.views.inc.
*/
function user_stats_views_api() {
return array(
'api' => '2.0',
'path' => drupal_get_path('module', 'user_stats') . '/views',
);
}
/**
* Actions/Rules hooks and implementing functions.
*
* (we don't do Triggers as the API doesn't seem complete -- having to use
* _trigger_get_hook_aids() for example). Patches welcome for this, as long
* as they do not use private member functions!
*
* Most Rules hooks are in user_stats.rules.inc.
*/
/**
* Implements hook_action_info().
*/
function user_stats_action_info() {
return array(
'user_stats_post_count_reset_action' => array(
'label' => t('Reset user post count'),
'type' => 'user',
'configurable' => FALSE,
'triggers' => array(
'nodeapi_delete',
'nodeapi_insert',
'nodeapi_update',
'nodeapi_view',
'comment_view',
'comment_insert',
'comment_update',
'comment_delete',
'user_login',
'user_logout',
),
),
'user_stats_login_count_reset_action' => array(
'label' => t('Reset user login count'),
'type' => 'user',
'configurable' => FALSE,
'triggers' => array(
'nodeapi_delete',
'nodeapi_insert',
'nodeapi_update',
'nodeapi_view',
'comment_view',
'comment_insert',
'comment_update',
'comment_delete',
'user_login',
'user_logout',
),
),
);
}
/**
* Implementation of a Drupal action.
*
* Resets a user's post count.
*/
function user_stats_post_count_reset_action(&$object, $context = array()) {
if (isset($object->uid)) {
$uid = $object->uid;
}
elseif (isset($context['uid'])) {
$uid = $context['uid'];
}
else {
global $user;
$uid = $user->uid;
}
user_stats_post_count_update('reset', $uid);
}
/**
* Implementation of a Drupal action.
* Resets a user's login count.
*/
function user_stats_login_count_reset_action(&$object, $context = array()) {
if (isset($object->uid)) {
$uid = $object->uid;
}
elseif (isset($context['uid'])) {
$uid = $context['uid'];
}
else {
global $user;
$uid = $user->uid;
}
user_stats_login_count_update('reset', $uid);
}
/**
* Implements hook_user_stats().
*
* Invoke the Rules module.
*/
function user_stats_user_stats($type, $op, $uid, $value) {
/*
if (module_exists('rules')) {
rules_invoke_event('user_stats_' . $type . '_' . $op, $uid, $value);
}
*/
}
/**
* Token hook implementations
*/
/**
* Implements hook_token_values().
*/
function user_stats_token_values($type, $object = NULL) {
switch ($type) {
case 'user':
case 'all':
if (isset($object)) {
// Think this is sometimes an array (please raise this as an issue if wrong).
$object = (object) $object;
$uid = $object->uid;
}
else {
global $user;
$uid = $user->uid;
}
// Check_plain added as per Greggles suggestion: http://drupal.org/node/166305#comment-665874
$values['reg-days'] = check_plain(user_stats_get_stats('reg_days', $uid));
$values['login-days'] = check_plain(user_stats_get_stats('login_days', $uid));
$values['post-days'] = check_plain(user_stats_get_stats('post_days', $uid));
$values['post-count'] = check_plain(user_stats_get_stats('post_count', $uid));
$values['ip-address'] = check_plain(user_stats_get_stats('ip_address', $uid));
$values['login-count'] = check_plain(user_stats_get_stats('login_count', $uid));
return $values;
}
}
/**
* Implements hook_token_list().
*/
function user_stats_token_list($type = 'all') {
if ($type == 'user' || $type == 'all') {
$tokens['user']['reg-days'] = t('Number of days since the user registered');
$tokens['user']['login-days'] = t('Number of days since the user logged in');
$tokens['user']['post-days'] = t('Number of days since the user posted');
$tokens['user']['post-count'] = t("User's post count");
$tokens['user']['ip-address'] = t("User's IP address");
$tokens['user']['login-count'] = t("User's login count");
return $tokens;
}
}
/**
* Checks whether a statistic is set for a given user.
*
* @param $uid
* User ID of the user who's statistics should be checked.
* @param $statistic
* What statistic to check.
*/
function user_stats_isset($statistic, $uid) {
$result = db_query("SELECT COUNT(*)
FROM {user_stats_values}
WHERE uid = :uid AND name = :name",
array(':uid' => $uid, ':name' => $statistic))->fetchField();
if ($result > 0) {
return TRUE;
}
return FALSE;
}
/**
* Manage the login count of a given user.
*
* @param $uid
* Unique id of the user who's record should be updated.
* @param $op
* Whether the user login count should be incremented, decremented, or reset.
* Possible values are:
* - 'increment'
* - 'decrement'
* - 'reset'
*/
function user_stats_login_count_update($op, $uid) {
if (!is_numeric($uid)) {
return;
}
switch ($op) {
case 'increment':
if (user_stats_isset('login_count', $uid)) {
// Update existing value.
db_update('user_stats_values')
->expression('value', 'value + :value', array(':value' => 1))
->condition('name', 'login_count')
->condition('uid', $uid)
->execute();
}
else {
// If there isn't a value insert it.
$id = db_insert('user_stats_values')
->fields(array(
'name' => 'login_count',
'uid' => $uid,
'value' => 1,
))
->execute();
}
break;
case 'decrement':
if (user_stats_isset('login_count', $uid)) {
// Update existing value.
$count = (user_stats_cache_get('login_count', $uid) - 1);
db_update('user_stats_values')
->expression('value', 'value + :value', array(':value' => 1))
->condition('name', 'login_count')
->condition('uid', $uid)
->execute();
}
else {
// If there isn't a value insert it.
$id = db_insert('user_stats_values')
->fields(array(
'name' => 'login_count',
'uid' => $uid,
'value' => 0,
))
->execute();
}
break;
case 'reset':
db_delete('user_stats_values')
->condition('name', 'login_count')
->condition('uid', $uid)
->execute();
break;
}
// Flush token cache.
//if (module_exists('token')) {
//token_get_values('user', NULL, TRUE);
//}
// Flush internal cache.
user_stats_cache_set('reset', $uid);
// Allow modules to react to a statistic change.
module_invoke_all('user_stats', 'login_count', $op, $uid, user_stats_get_stats('login_count', $uid));
}
/**
* Manage the post count of a given user.
*
* @param $uid
* Unique id of the user who's record should be updated.
* @param $op
* Whether the user post count should be incremented, decremented, or reset.
* The default is to increment. Possible values are:
* 'increment'
* 'decrement'
* 'reset'
*/
function user_stats_post_count_update($op, $uid) {
if (!is_numeric($uid)) {
return;
}
switch ($op) {
case 'increment':
if (user_stats_isset('post_count', $uid)) {
//@TODO: Previous query tried to update and add in one query,
// that wasn't working.
$count = (user_stats_get_stats('post_count', $uid) + 1);
db_update('user_stats_values')
->fields(array(
'value' => $count,
))
->condition('name', 'post_count')
->condition('uid', $uid)
->execute();
// Flush internal cache.
user_stats_cache_set('reset', $uid);
}
else {
user_stats_post_count_update('reset', $uid);
}
break;
case 'decrement':
if (user_stats_isset('post_count', $uid)) {
//@TODO: Same issue as 'increment'. Previous query tried to update
// and add in one query... that wasn't working
$count = (user_stats_get_stats('post_count', $uid) - 1);
db_update('user_stats_values')
->fields(array(
'value' => $count,
))
->condition('name', 'post_count')
->condition('uid', $uid)
->execute();
// Flush internal cache.
user_stats_cache_set('reset', $uid);
}
else {
user_stats_post_count_update('reset', $uid);
}
break;
case 'reset':
$total_count = 0;
if (variable_get('user_stats_count_posts', TRUE)) {
$sql = "SELECT COUNT(*) FROM {node} WHERE uid = :uid AND status = 1";
$post_count_content_types = variable_get('user_stats_included_content_types', array());
if (!empty($post_count_content_types)) {
$content_types = "'" . implode("','", $post_count_content_types) . "'";
$where = ' AND type IN (' . $content_types . ')';
$sql .= $where;
}
$node_count = db_query($sql, array(':uid' => $uid))->fetchField();
$total_count += $node_count;
}
if (variable_get('user_stats_count_comments', TRUE)) {
// COMMENT_PUBLISHED is now 1 in D7, and COMMENT_UNPUBLISHED is 0
$sql = "SELECT COUNT(*) FROM {comment} c
INNER JOIN {node} n ON c.nid = n.nid
WHERE c.uid = :uid AND c.status = 1 AND n.status = 1";
if (!empty($post_count_content_types)) {
$where = ' AND n.type IN (' . $content_types . ')';
$sql .= $where;
}
$comments_count = db_query($sql, array(':uid' => $uid))->fetchField();
$total_count += $comments_count;
}
db_delete('user_stats_values')
->condition('name', 'post_count')
->condition('uid', $uid)
->execute();
$id = db_insert('user_stats_values')
->fields(array(
'name' => 'post_count',
'uid' => $uid,
'value' => $total_count,
))
->execute();
// Prime the cache, this will be used by module_invoke_all() below.
user_stats_cache_set('post_count', $uid, $total_count);
break;
}
// Flush token cache
//if (module_exists('token')) {
//token_get_values('user', NULL, TRUE);
//}
// Allow modules to react to a statistic change.
module_invoke_all('user_stats', 'post_count', $op, $uid, user_stats_get_stats('post_count', $uid));
}
/**
* Update the IP address of a given user.
*
* The IP address is not updated if it is the same as the last recorded IP,
* however, if the user has IP address A, then switches to IP address B
* and back to A again, A will be recorded twice. This is to keep an accurate
* log of IP addresses used by users.
*
* @param $uid
* User ID of user who's IP is being updated.
* @param $ip_address
* IP address to assign to user.
*/
function user_stats_ip_address_update($uid, $ip_address) {
if (!is_numeric($uid)) {
return;
}
// Don't bother recording IPs of anonymous users, and don't record any
// addresses if the config form tells us not to.
if ($uid == 0 || !variable_get('user_stats_track_ips', TRUE)) {
return;
}
$query = db_query_range("SELECT ip_address
FROM {user_stats_ips}
WHERE uid = :uid
ORDER BY first_seen_timestamp DESC",
0, 1, array(':uid' => $uid));
if ($ip_address != $query->fetchField()) {
// Reset internal cache.
user_stats_cache_set('reset', $uid);
$id = db_insert('user_stats_ips')
->fields(array(
'uid' => $uid,
'ip_address' => $ip_address,
'first_seen_timestamp' => REQUEST_TIME,
))
->execute();
// Allow modules to react to an IP address change.
module_invoke_all('user_stats', 'ip_address', 'insert', $uid, $ip_address);
}
}
/**
* Resets statistics. Full stop.
*
* @param $statistic
* The name of the statistic to be reset.
* Corresponds with {user_stats_values}.name.
*/
function user_stats_reset_counts($statistic) {
db_delete('user_stats_values')
->condition('name', $statistic)
->execute();
}
/**
* Implements hook_user_insert to record ip of new user on registration.
*/
function user_stats_user_insert(&$edit, $account, $category) {
global $user;
if ($user->uid == 0) {
$uid = $account->uid;
$ip = ip_address();
user_stats_ip_address_update($uid, $ip);
}
}

View File

@@ -0,0 +1,109 @@
<?php
/**
* @file
* Functions for integrating the Rules module with User Stats.
*/
/**
* Implements hook_rules_event_info().
* @ingroup rules
*/
function user_stats_rules_event_info() {
$defaults = array(
'group' => t('User Stats'),
'module' => 'user_stats',
);
return array(
'user_stats_login_count_increment' => $defaults + array(
'label' => t('User login count increased'),
'variables' => user_stats_rules_events_variables(),
),
'user_stats_login_count_decrement' => $defaults + array(
'label' => t('User login count decreased'),
'variables' => user_stats_rules_events_variables(),
),
'user_stats_login_count_reset' => $defaults + array(
'label' => t('User login count reset'),
'variables' => user_stats_rules_events_variables(),
),
'user_stats_post_count_increment' => $defaults + array(
'label' => t('User post count increased'),
'variables' => user_stats_rules_events_variables(),
),
'user_stats_post_count_decrement' => $defaults + array(
'label' => t('User post count decreased'),
'variables' => user_stats_rules_events_variables(),
),
'user_stats_post_count_reset' => $defaults + array(
'label' => t('User post count reset'),
'variables' => user_stats_rules_events_variables(),
),
'user_stats_ip_address_insert' => $defaults + array(
'label' => t('User has a new IP address'),
'variables' => user_stats_rules_events_variables(),
),
'user_stats_day_older' => $defaults + array(
'label' => t('User is a day older'),
'variables' => user_stats_rules_events_variables_day_older(),
),
);
}
/**
* Defines variables for user_stats_rules_event_info().
*/
function user_stats_rules_events_variables() {
return array(
'uid' => array(
'type' => 'number',
'hidden' => TRUE,
),
'statistic_value' => array(
'type' => 'number',
'label' => t('Value of the statistic'),
),
'user' => array(
'type' => 'user',
'label' => t("User who's statistics have changed"),
'handler' => 'user_stats_events_argument_user',
) //+ rules_events_global_user_argument(),
// ^ Do we need to set the acting user?
);
}
/**
* Defines variables for user_stats_rules_event_info().
*
* The variables for a day_older event are slightly different to other items.
*/
function user_stats_rules_events_variables_day_older() {
// Get the default variables.
$variables = user_stats_rules_events_variables();
$variables['statistic_value']['handler'] = 'user_stats_events_argument_day_older';
return $variables;
}
/**
* Handler to load user object on event.
*
* @param $uid
* Unique user ID used to load the user object.
* @param $value
* Value of the statistic, not relevant to loading the user object,
* but passed through by Rules engine.
*
* @return
* Loaded user object.
*/
function user_stats_events_argument_user($uid, $value) {
return user_load($uid);
}
/**
* Handler to load number of days user has been registered on event.
*/
function user_stats_events_argument_day_older($uid, $value) {
return user_stats_get_stats('reg_days', $uid);
}

View File

@@ -0,0 +1,217 @@
<?php
/**
* @file
* Hooks for integrating Views and User Stats.
*/
/**
* Implements hook_views_data().
*/
function user_stats_views_data() {
// User Statistics table.
$data['user_stats_values']['table']['group'] = t('User Stats');
$data['user_stats_values']['table']['join'] = array(
'users' => array(
'left_field' => 'uid',
'field' => 'uid',
),
);
// Name field.
$data['user_stats_values']['name'] = array(
'title' => t('Statistic name'),
'help' => t('The unique statistic name'),
'field' => array(
'handler' => 'views_handler_field',
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
'argument' => array(
'views_handler_argument_string',
),
);
// Value field.
$data['user_stats_values']['value'] = array(
'title' => t('Value'),
'help' => t('Statistic value'),
'field' => array(
'handler' => 'views_handler_field_numeric',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_numeric',
),
);
// IP address table definition.
$data['user_stats_ips']['table']['group'] = t('IP address');
$data['user_stats_ips']['table']['join'] = array(
'users' => array(
'left_field' => 'uid',
'field' => 'uid',
),
);
$data['user_stats_ips']['table']['base'] = array(
'field' => 'iid',
'title' => t('IP addresses'),
'help' => t('Users and their IP addresses'),
);
// IID field.
$data['user_stats_ips']['iid'] = array(
'title' => t('IID'),
'help' => t('The unique ID of the IP address record'),
'field' => array(
'handler' => 'views_handler_numeric',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort',
),
'filter' => array(
'handler' => 'views_handler_filter_numeric',
),
'argument' => array(
'handler' => 'views_handler_argument_numeric',
),
);
// IP address field.
$data['user_stats_ips']['ip_address'] = array(
'title' => t('IP'),
'help' => t('IP address'),
'field' => array(
'handler' => 'views_handler_field',
),
'filter' => array(
'handler' => 'views_handler_filter_string',
),
'argument' => array(
'handler' => 'views_handler_argument_string',
),
);
// First seen timestamp field.
$data['user_stats_ips']['first_seen_timestamp'] = array(
'title' => t('First seen'),
'help' => t('First seen timestamp'),
'field' => array(
'handler' => 'views_handler_field_date',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort_date',
),
'filter' => array(
'handler' => 'views_handler_filter_date',
),
'argument' => array(
'handler' => 'views_handler_argument_date',
),
);
// Number of users associated with a given IP address (virtual field).
$data['user_stats_ips']['ip_user_count'] = array(
'title' => t('IP users count'),
'help' => t('Number of users used by an IP address'),
'field' => array(
'handler' => 'views_handler_field_ip_user_count',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort_ip_user_count',
),
);
// Number of ip addresses associated with a given user (virtual field).
$data['user_stats_ips']['user_ip_count'] = array(
'title' => t('User IP count'),
'help' => t('Number of IPs used by a user'),
'field' => array(
'handler' => 'views_handler_field_user_ip_count',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort_user_ip_count',
),
);
// Is user online boolean (virtual field).
$data['users']['is_online'] = array(
'title' => t('Is online'),
'help' => t('Shows whether the user is online'),
'field' => array(
'handler' => 'views_handler_field_is_online',
'click sortable' => TRUE,
),
'sort' => array(
'handler' => 'views_handler_sort_is_online',
),
'filter' => array(
'handler' => 'views_handler_filter_is_online',
'label' => t('Online'),
),
);
// Un-themed version of the user's picture.
$data['users']['picture_bare'] = array(
'title' => t('Bare user picture'),
'help' => t('Normally user pictures are show with theming around them, which is crufty and may have CSS attached not appropriate for a view. This skips the theming cruft and just prints the bare img tag'),
'real field' => 'picture',
'field' => array(
'handler' => 'views_handler_field_user_picture_bare',
'click sortable' => TRUE,
),
'filter' => array(
'handler' => 'views_handler_filter_boolean_operator_string',
'label' => t('Has Avatar'),
'type' => 'yes-no',
),
);
return $data;
}
/**
* Implements hook_views_handlers().
*/
function user_stats_views_handlers() {
return array(
'info' => array(
'path' => drupal_get_path('module', 'user_stats') . '/views',
),
'handlers' => array(
'views_handler_field_ip_user_count' => array(
'parent' => 'views_handler_field_numeric',
),
'views_handler_sort_ip_user_count' => array(
'parent' => 'views_handler_sort',
),
'views_handler_field_user_ip_count' => array(
'parent' => 'views_handler_field_numeric',
),
'views_handler_sort_user_ip_count' => array(
'parent' => 'views_handler_sort',
),
'views_handler_field_is_online' => array(
'parent' => 'views_handler_field_boolean',
),
'views_handler_sort_is_online' => array(
'parent' => 'views_handler_sort',
),
'views_handler_filter_is_online' => array(
'parent' => 'views_handler_filter_boolean_operator',
),
'views_handler_field_user_picture_bare' => array(
'parent' => 'views_handler_field_user_picture',
),
),
);
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,20 @@
<?php
/**
* @file
* User Stats user count by IP address.
*/
/**
* Users by IP address count handler.
*/
class views_handler_field_ip_user_count extends views_handler_field_numeric {
function query() {
$this->table_alias = $this->ensure_my_table();
// We need another subquery here to extract DISTINCT values
$sql = "SELECT COUNT(usi.uid)
FROM (SELECT DISTINCT(uid), ip_address FROM {user_stats_ips}) usi
WHERE usi.ip_address = " . check_plain($this->table_alias) . ".ip_address";
$this->field_alias = $this->query->add_field(NULL, "(" . $sql . ")", $this->table_alias . '_' . $this->field);
}
}

View File

@@ -0,0 +1,56 @@
<?php
/**
* @file
* User Stats is user online handler.
*/
/**
* Is user online handler.
*/
class views_handler_field_is_online extends views_handler_field_boolean {
function query() {
$this->ensure_my_table();
// Currently Views has no support for/information on the {sessions} table.
$join = new views_join;
$join->construct('sessions', $this->table_alias, 'uid', 'uid', array());
$session = $this->query->ensure_table('sessions', NULL, $join);
// We use an IF for MySQL/PostgreSQL compatibility. Otherwise PostgreSQL
// would return 'f' and 't'.
$sql_if_part = "IF((" . REQUEST_TIME . " - $session.timestamp) < " . variable_get('user_block_seconds_online', 900) . ", 1, 0)";
// We liberally steal from views_handler_sort_formula and
// views_handler_filter_search here.
$this->field_alias = $this->query->add_field(NULL, $sql_if_part, $this->table_alias . '_' . $this->field, array('function' => 'max'));
}
function option_definition() {
$options = parent::option_definition();
$options['type'] = array('default' => 'online-offline');
return $options;
}
/**
* Add the online-offline type to options form.
*/
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
$form['type']['#options']['online-offline'] = t('Online/Offline');
}
function render($values) {
$value = $values->{$this->field_alias};
if (!empty($this->options['not'])) {
$value = !$value;
}
if ($this->options['type'] == 'online-offline') {
return $value ? '<span class="user-stat-online">' . t('Online') . '</span>' : '<span class="user-stat-offline">' . t('Offline') . '</span>';
}
else {
return parent::render($values);
}
}
}

View File

@@ -0,0 +1,20 @@
<?php
/**
* @file
* User Stats IP address count by user.
*/
/**
* IP addresses by user count handler.
*/
class views_handler_field_user_ip_count extends views_handler_field_numeric {
function query() {
$this->table_alias = $this->ensure_my_table();
// We need another subquery here to extract DISTINCT values
$sql = "SELECT COUNT(usi.ip_address)
FROM (SELECT DISTINCT(uid), ip_address FROM {user_stats_ips}) usi
WHERE usi.uid = " . check_plain($this->table_alias) . ".uid";
$this->field_alias = $this->query->add_field(NULL, "(" . $sql . ")", $this->table_alias . '_' . $this->field);
}
}

View File

@@ -0,0 +1,53 @@
<?php
/**
* @file
* User Stats non-themed user picture.
*/
/**
* Bare user picture handler.
*/
class views_handler_field_user_picture_bare extends views_handler_field_user_picture {
function render($values) {
if (module_exists('image')) {
$output = '';
// When an image style is not defined, use the image style from the account settings.
$style_name = $this->options['image_style'];
if (empty($style_name)) {
$style_name = variable_get('user_picture_style', '');
}
// Load the picture file and get the uri.
if ($user_picture_fid = $this->get_value($values)) {
$user_picture = file_load($user_picture_fid);
$user_picture_filepath = $user_picture->uri;
} else {
$user_picture_filepath = variable_get('user_picture_default', '');
}
// Return empty string when either style_name or picture are unavailable.
if (empty($style_name) || empty($user_picture_filepath)) {
return $output;
}
// Use the user name for alt attribute.
$user_name = $values->{$this->aliases['name']} ? $values->{$this->aliases['name']} : variable_get('anonymous', t('Anonymous'));
$alt = t("@user's picture", array('@user' => $user_name));
// Output the picture with image_style.
if (file_valid_uri($user_picture_filepath)) {
$output = theme('image_style', array('style_name' => $style_name,'path' => $user_picture_filepath, 'alt' => $alt));
}
// Wrap the picture in a link to the user picture.
if ($this->options['link_photo_to_profile'] && user_access('access user profiles')) {
$uid = $this->get_value($values, 'uid');
$output = l($output, "user/$uid", array('html' => TRUE));
}
return $output;
}
}
}

View File

@@ -0,0 +1,35 @@
<?php
/**
* @file
* User Stats is user online sort handler.
*/
/**
* Is user online sort handler.
*/
class views_handler_filter_is_online extends views_handler_filter_boolean_operator {
function query() {
$this->ensure_my_table();
$join = new views_join;
$join->construct('sessions', $this->table_alias, 'uid', 'uid', array());
$session = $this->query->ensure_table('sessions', NULL, $join);
// We have to make sure this field is in the query, and Views knows to
// create GROUP BY's.
$sql_if_part = "IF((" . REQUEST_TIME . " - $session.timestamp) < " . variable_get('user_block_seconds_online', 900) . ", 1, 0)";
$sql = $sql_if_part . " = :value";
$this->query->add_where_expression($this->options['group'], $sql, array(':value' => $this->value));
}
/**
* Override default True/False options.
*/
function get_value_options() {
$this->value_options = array(
1 => t('Online'),
0 => t('Offline'),
);
}
}

View File

@@ -0,0 +1,20 @@
<?php
/**
* @file
* User Stats user count by IP address.
*/
/**
* Users by IP address count handler.
*/
class views_handler_sort_ip_user_count extends views_handler_sort {
function query() {
$this->table_alias = $this->ensure_my_table();
// We need another subquery here to extract DISTINCT values
$sql = "SELECT COUNT(usi.uid)
FROM (SELECT DISTINCT(uid), ip_address FROM {user_stats_ips}) usi
WHERE usi.ip_address = " . check_plain($this->table_alias) . ".ip_address";
$this->field_alias = $this->query->add_orderby(NULL, "(" . $sql . ")", $this->options['order'], $this->table_alias . '_' . $this->field);
}
}

View File

@@ -0,0 +1,24 @@
<?php
/**
* @file
* User Stats is user online sort handler.
*/
/**
* Is user online sort handler.
*/
class views_handler_sort_is_online extends views_handler_sort {
function query() {
$this->ensure_my_table();
// Currently Views has no support for/information on the {sessions} table.
$join = new views_join;
$join->construct('sessions', $this->table_alias, 'uid', 'uid', array());
$session = $this->query->ensure_table('sessions', NULL, $join);
// We use an IF for MySQL/PostgreSQL compatibility. Otherwise PostgreSQL
// would return 'f' and 't'.
$sql_if_part = "IF((" . REQUEST_TIME . " - $session.timestamp) < " . variable_get('user_block_seconds_online', 900) . ", 1, 0)";
$this->query->add_orderby(NULL, $sql_if_part, $this->options['order'], $this->table_alias . '_' . $this->field, array('function' => 'max'));
}
}

View File

@@ -0,0 +1,20 @@
<?php
/**
* @file
* User Stats IP address count by user.
*/
/**
* IP addresses by user count handler.
*/
class views_handler_sort_user_ip_count extends views_handler_sort {
function query() {
$this->ensure_my_table();
// We need another subquery here to extract DISTINCT values
$sql = "SELECT COUNT(usi.ip_address)
FROM (SELECT DISTINCT(uid), ip_address FROM {user_stats_ips}) usi
WHERE usi.uid = " . check_plain($this->table_alias) . ".uid";
$this->field_alias = $this->query->add_orderby(NULL, "(" . $sql . ")", $this->options['order'], $this->table_alias . '_' . $this->field);
}
}