Warning message

Newsletter category Drupal Developer newsletter format has not been set to HTML. There will be no statistics recorded for this newsletter.

Download file and track of downloads by authenticated users.

This CodeLet let you allow a download file and track of that file.

CodeLet: 

.install file

/**
* @file
*
* Create scheema for mymodule
*
* @author Saru1683
*/

/**
* Implemenation of hook_schema
*
* @author Saru1683
*/
function mymodule_schema() {
$schema['wc_count_trial_version_download'] = array(
'fields' => array(
'download_id' => array(
'type' => 'serial',
'lenght' => 11,
),

'uid' => array(
'type' => 'int',
'lenght' => 11,
),

'date' => array(
'type' => 'int',
'lenght' => 11,
),
),
'primary key' => array('download_id'),
);

return $schema;
}
?>
.module file
/**
* Implements hook_menu().
*/
function mymodule_menu() {

$items['download-trial-version'] = array(
'title' => t('Download'),
'type' => MENU_CALLBACK,
'page callback' => 'download_and_track_trial_version',
'access callback' => true,
);

$items['admin/download-track'] = array(
'title' => t('Downlod track'),
'type' => MENU_CALLBACK,
'page callback' => 'wccustom_track_trial_version_download',
'access arguments' => array('administer site configuration'),
);
return $items;
}

function wccustom_track_trial_version_download(){

drupal_set_title(t('Trial version download track'));

$query = db_select('wc_count_trial_version_download', 'd');
$query->fields('d', array('uid', 'date'));
$result = $query->execute();

$header = array(t('Sr No'), t('User name'), t('E-mail'), t('Date'));
$rows = array();
$i = 1;

while($record = $result->fetchAssoc()) {
$arr__user = _get_user_by_uid($record['uid']);
$rows[$i]['srno'] = $i;
$rows[$i]['username'] = $arr__user['name'];
$rows[$i]['mail'] = $arr__user['mail'];
$rows[$i]['date'] = date('d M, Y', $record['date']);
$i++;
}

return theme('table', array('header' => $header, 'rows' => $rows));
}

function _get_user_by_uid($sn__uid = 0) {

if($sn__uid == 0) {
return 'Anonymous';
}
$query = db_select('users', 'u');
$query->fields('u', array('name', 'mail'))
->condition('u.uid', $sn__uid, '=');
$result = $query->execute();

$result = $result->fetchAssoc();
$return = array();
$return['name'] = !empty($result['name']) ? $result['name'] : 'Anonymous';
$return['mail'] = !empty($result['mail']) ? $result['mail'] : '-';
return $return;
}

/**
* Implementation of list download
* @author Saru1683
*/
function download_and_track_trial_version() {
global $user;
if(!$user->uid) {
drupal_access_denied();
exit;
}

$options = array('uid' => $user->uid, 'date' => REQUEST_TIME);
$query = db_insert('wc_count_trial_version_download')->fields($options)->execute();
drupal_goto('http://www.mysite.com/sites/default/files/downloadable_files/download.jpg');
exit;
}

By saru1683 on Fri, 13/09/2013 - 1:00pm

Add comment

Log in or register to post comments
QR code for this page URL This page URL
Feedback