Updated 03-15-2006 for 4images v1.7.2Keep Track Of What Each User Has DownloadedFor each registered user, this mod maintains a list of the images they download and the date/time of the their last download.
Summary Description NOTE: This is NOT a download stats or counter modIt has been said that necessity is the mother of invention. On my web site, I only allow downloads for registered users and I wanted to keep track of who was downloading what.
This mod lets you track what images have been downloaded by each registered user. It also enhances the "Edit Users" function in the admin control panel to let you query all users who downloaded anything before, after, or within a date range you enter by entering dates in both the before and after fields. You can also sort the returned list of users by the last download date in either ascending or descending order.
Once that query list is generated, clicking the "Edit" link next to a specific user will display the following in the "Edit User" HTML form:
1. A text area that holds the id numbers of the images they downloaded.
2. A search link to display those images in a new window.
3. The date and time of the user's last download.
The search link is also displayed in the member_profile.html template if you are logged in as a 4images administrator.
The database table 4images_users is modified to store the image id numbers for all downloads made by each user. It is also modified to store the date and time of the last download. This code was heavily copied from the lightbox logic so it only took me 4 or 5 hours to research, code, test, document the installation procedure and write an installer to modify the database. Your time to install this mod is approximately 15 minutes, you lucky bastard!
Summary of files affected[change] admin/users.php
[change] includes/functions.php
[change] includes/sessions.php
[change] includes/db_field_definitions.php
[change] lang/english/main.php
[change] lang/english/admin.php
[change] download.php
[change] member.php
[change] search.php
[change] templates/default/member_profile.html
[new] 4i_install_tud.php
admin/users.php:Locate:
4images v1.7 - 1.7.1 show_input_row($lang['field_lastaction'].$lang['date_desc'], "user_lastaction", $user_row['user_lastaction'], $textinput_size);
4images v1.7.2 show_date_input_row($lang['field_lastaction'].$lang['date_desc'], "user_lastaction", $user_row['user_lastaction'], $textinput_size);
Insert after:
$user_row['download_lastaction'] = date("Y-m-d H:i", $user_row['download_lastaction']); // Mod: Track User Downloads
$additional_user_fields['downloaded_image_ids'] = array("<a href=\"".ROOT_PATH."search.php?user_downloads=".$user_row['user_id']."\" target=\"_blank\">".$lang['downloaded_image_ids']."</a>", "textarea", 0); // Mod: Track User Downloads
Locate:
4images v1.7 - 1.7.1 show_input_row($lang['field_lastaction_before'].$lang['date_format'], "lastactionbefore", "", $textinput_size);
4images v1.7.2 show_date_input_row($lang['field_lastaction_before'].$lang['date_format'], "lastactionbefore", "", $textinput_size);
Insert after:
$user_row['download_lastaction'] = date("Y-m-d H:i", $user_row['download_lastaction']); // Mod: Track User Downloads
show_input_row($lang['field_download_lastaction_after'].$lang['date_format'], "download_lastaction_after", "", $textinput_size); // Mod: Track User Downloads
show_input_row($lang['field_download_lastaction_before'].$lang['date_format'], "download_lastaction_before", "", $textinput_size); // Mod: Track User Downloads
Locate:
<option value="<?php echo get_user_table_field("", "user_lastaction"); ?>"><?php echo $lang['field_lastaction']; ?></option>
Insert after:
<option value="<?php echo get_user_table_field("", "download_lastaction"); ?>"><?php echo $lang['field_download_lastaction']; ?></option>
Locate:
$lastactionbefore = trim($HTTP_POST_VARS['lastactionbefore']);
if ($lastactionbefore != "") {
$condition .= " AND ".get_user_table_field("", "user_lastaction")." < UNIX_TIMESTAMP('$lastactionbefore')";
}
Insert after:
$download_lastaction_after = trim($HTTP_POST_VARS['download_lastaction_after']); // Mod: Track User Downloads
if ($download_lastaction_after != "") { // Mod: Track User Downloads
$condition .= " AND ".get_user_table_field("", "download_lastaction")." > UNIX_TIMESTAMP('$download_lastaction_after')"; // Mod: Track User Downloads
} // Mod: Track User Downloads
$download_lastaction_before = trim($HTTP_POST_VARS['download_lastaction_before']); // Mod: Track User Downloads
if ($download_lastaction_before != "") { // Mod: Track User Downloads
$condition .= " AND ".get_user_table_field("", "download_lastaction")." < UNIX_TIMESTAMP('$download_lastaction_before')"; // Mod: Track User Downloads
$condition .= " AND ".get_user_table_field("", "download_lastaction")." > 0"; // Mod: Track User Downloads
} // Mod: Track User Downloads
Locate:
show_hidden_input("lastactionafter", $lastactionafter);
show_hidden_input("lastactionbefore", $lastactionbefore);
Insert after:
show_hidden_input("download_lastaction_after", $download_lastaction_after); // Mod: Track User Downloads
show_hidden_input("download_lastaction_before", $download_lastaction_before); // Mod: Track User Downloads
Open includes/functions.php, at the very bottom and just before the closing:?>
Insert before:
// Mod: Track User Downloads BLOCK INSERT BEGIN
function add_to_downloaded_image_ids($id) {
global $user_info, $site_db;
$id = intval($id);
if (!$id) {
return false;
}
$downloaded_ids = $user_info['downloaded_image_ids'];
$downloaded_array = explode(" ", $downloaded_ids);
if (!in_array($id, $downloaded_array)) {
$downloaded_ids .= " ".$id;
}
$user_info['downloaded_image_ids'] = trim($downloaded_ids);
$user_info['download_lastaction'] = time();
$sql = "UPDATE ".USERS_TABLE."
SET download_lastaction = ".$user_info['download_lastaction'].", downloaded_image_ids = '".$user_info['downloaded_image_ids']."'
WHERE user_id = ".$user_info['user_id'];
return ($site_db->query($sql)) ? 1 : 0;
}
// Mod: Track User Downloads BLOCK INSERT END
Open includes/sessions.php, locate at the top:$user_table_fields = array(
"user_id" => "user_id",
"user_level" => "user_level",
"user_name" => "user_name",
"user_password" => "user_password",
"user_email" => "user_email",
"user_showemail" => "user_showemail",
"user_allowemails" => "user_allowemails",
"user_invisible" => "user_invisible",
"user_joindate" => "user_joindate",
"user_activationkey" => "user_activationkey",
"user_lastaction" => "user_lastaction",
"user_location" => "user_location",
"user_lastvisit" => "user_lastvisit",
"user_comments" => "user_comments",
"user_homepage" => "user_homepage",
"user_icq" => "user_icq"
);
Add a comma at the end of the user_icq line and add a new line after:
$user_table_fields = array(
"user_id" => "user_id",
"user_level" => "user_level",
"user_name" => "user_name",
"user_password" => "user_password",
"user_email" => "user_email",
"user_showemail" => "user_showemail",
"user_allowemails" => "user_allowemails",
"user_invisible" => "user_invisible",
"user_joindate" => "user_joindate",
"user_activationkey" => "user_activationkey",
"user_lastaction" => "user_lastaction",
"user_location" => "user_location",
"user_lastvisit" => "user_lastvisit",
"user_comments" => "user_comments",
"user_homepage" => "user_homepage",
"user_icq" => "user_icq", // Mod: Track User Downloads (comma was added at end of line!)
"download_lastaction" => "download_lastaction" // Mod: Track User Downloads
);
Open includes/db_field_definitions.php, locate:// Example for additional user fields
//$additional_user_fields['user_adress'] = array($lang['user_adress'], "text", 1);
Insert after:
$additional_user_fields['downloaded_image_ids'] = array($lang['downloaded_image_ids'], "textarea", 0); // Mod: Track User Downloads
$additional_user_fields['download_lastaction'] = array($lang['download_lastaction'], "text", 0); // Mod: Track User Downloads
Open lang/english/main.php, at the very bottom and just before the closing:?>
Insert before:
$lang['downloaded_image_ids'] = "Downloaded Image IDs"; // Mod: Track User Downloads
$lang['download_lastaction'] = "Date Of Last Download"; // Mod: Track User Downloads
$lang['downloaded_images'] = "Downloaded Images"; // Mod: Track User Downloads
Open lang/english/admin.phpLocate:
$lang['field_lastaction'] = "Last activity";
Insert after:
$lang['field_download_lastaction'] = "Last Download"; // Mod: Track User Downloads
Locate:
$lang['field_lastaction_before'] = "Last activity before";
$lang['field_lastaction_after'] = "Last activity after";
Insert after:
$lang['field_download_lastaction_before'] = "Last download before"; // Mod: Track User Downloads
$lang['field_download_lastaction_after'] = "Last download after"; // Mod: Track User Downloads
Open download.php, locate: if ($user_info['user_level'] != ADMIN) {
$sql = "UPDATE ".IMAGES_TABLE."
SET image_downloads = image_downloads + 1
WHERE image_id = $image_id";
$site_db->query($sql);
}
Insert after:
if ($user_info['user_level'] == USER) // Mod: Track User Downloads
add_to_downloaded_image_ids($image_id); // Mod: Track User Downloads
OPTIONAL: If you want to track downloads from users AND administrators, change if ($user_info['user_level'] == USER) // Mod: Track User Downloads
To if ($user_info['user_level'] >= USER) // Mod: Track User (and administrator) Downloads
Open member.php:Locate:
"lang_email" => $lang['email'],
"lang_homepage" => $lang['homepage'],
"lang_icq" => $lang['icq']
));
REPLACE with:
"lang_email" => $lang['email'],
"lang_homepage" => $lang['homepage'],
"lang_icq" => $lang['icq'],
"lang_download_lastaction" => $lang['download_lastaction'], // Mod: Track User Downloads
"lang_user_downloads" => $lang['downloaded_images'], // Mod: Track User Downloads
"url_user_downloads" => ($user_info['user_level'] == ADMIN) ? $site_sess->url(ROOT_PATH."search.php?user_downloads=".$user_row['user_id']) : "" // Mod: Track User Downloads
));
Open search.php:Locate:
$search_id = array();
Insert after:
// Mod: Track User Downloads BLOCK INSERT BEGIN
if (isset($HTTP_POST_VARS['user_downloads']) || isset($HTTP_GET_VARS['user_downloads'])) {
$user_downloads = (isset($HTTP_POST_VARS['user_downloads'])) ? intval($HTTP_POST_VARS['user_downloads']) : intval($HTTP_GET_VARS['user_downloads']);
if ($user_downloads != "" && $user_info['user_level'] == ADMIN) {
$show_result = 1;
$sql = "SELECT downloaded_image_ids
FROM ".USERS_TABLE."
WHERE ".get_user_table_field("", "user_id")." = $user_downloads";
$user_downloads_info = $site_db->query_firstrow($sql);
$user_downloads_ids = str_replace(" ", ",", trim($user_downloads_info['downloaded_image_ids']));
$search_id['image_ids'] = $user_downloads_ids;
}
}
// Mod: Track User Downloads BLOCK INSERT END
Open templates/default/member_profile.html:Locate:
<tr>
<td class="row2"><b>{lang_icq}</b></td>
<td class="row2">{if user_icq}<a href="http://wwp.icq.com/scripts/search.dll?to={user_icq}">{user_icq}</a> (<b>{user_icq_status}</b>){endif user_icq}</td>
</tr>
Insert after:
{if url_user_downloads}
<tr>
<td class="row1"><b>{lang_user_downloads}</b></td>
<td class="row1"><a href="{url_user_downloads}">{lang_user_downloads}</a></td>
</tr>
{endif url_user_downloads}
4i_install_tud.phpBecause the database must be modified, and it can be difficult to do correctly, I have written and tested an installer. Download the 4i_install_tud.zip file attached to this post. Extract the 4i_install_tud.php file and upload it to your web site. In a web browser, go to
http://www.example.com/4images/4i_install_tud.php and follow the on-screen instructions. Once your database is successfully updated, you should delete 4i_install_tud.php
Revisions:
* Enhanced with V@no's add-on that provides a search link in both the admin Control Panel "Edit Users" and the member_profile.html template. Separately we both thought of adding the same feature but V@no beat me to it. Thank you.
* Added "Last Download" to the "Order By" options list for admin CP "Edit Users".
* Attached the installer file to this post, just download it !