Drupal: Donate userpoints to node author

This CodLet will dispay a userpoint form on node pages which allow the current user to donate some userpoints to the author of the node. This CodeLet will award the points to admin user by default.

CodeLet: 

/**
* @file
*
* Donate userpoints on a node page
*
* @author DrupalD
*
*/

/**
* usertip_get_usertip
*
* @param unknown_type $node
* @author DrupalD
*/
function usertip_get_usertip($sn__uid = NULL) {
global $user;
$form = array();
$sn__points = usertip_user_get_pionts();
$form['usertip_container'] = array(
'' => 'fieldset',
'' => t('Give Tip'),
'' => TRUE,
'' => FALSE,
'' => 2,
);

if ($user->uid != 1 && user_access('give points to another user') && $sn__points != 0) {
$sn__donation_tid = variable_get('userpoints_category_default_tid');
$r__result = db_query("SELECT name FROM {taxonomy_term_data} WHERE tid = :tid", array(':tid' => $sn__donation_tid));
$os__category = $r__result->fetchObject();
$form['usertip_container']['usertip_remaining'] = array(
'' => t('Your point balance is !points for !category category', array('!points' => $sn__points, '!category' => $os__category->name)),
'' => -10
);

$form['usertip_container']['usertip_form'] = array(
'' => drupal_render(drupal_get_form('userpoints_donation_giveform', 1, 0, NULL)),
'' => -9,
);

$form['usertip_container']['usertip_doners'] = array(
'' => _get_usertip_donors(),
'' => -8,
);
}
else {
$form['usertip_container']['tip_warning'] = array(
'' => t('Either you do not have privilege to tip or your point balance is low.')
);
}

return drupal_render($form);
}

/**
* Implementation of hook_form_alter
*
* @param unknown_type $form
* @param unknown_type $form_state
* @param unknown_type $form_id
* @author DrupalD
*/
function usertip_form_alter(&$form, $form_state, $form_id) {
drupal_add_library('system', 'drupal.ajax');
drupal_add_library('system', 'jquery.form');
include_once('includes/ajax.inc');

if ($form_id == 'userpoints_donation_giveform') {
$form_state['build_info']['base_form_id'] = str_replace('-', '_', 'userpoints-donation-giveform');
$form['to'][''] = 'hidden';
$form['amount'][''] = 'select';
$form['amount'][''] = 1;
$form['amount'][''] = "";
$form['amount'][''] = "";
$form['amount'][''] = array(
0 => t('Select amount'),
5 => 5,
10 => 10,
25 => 25,
50 => 50,
100 => 100,
);

$form['submit'][''] = array('class' => array('use-ajax-submit'));
$form['submit'][''] = t('tip');
$form['submit'][''] = "";
$form['submit'][''] = "";
$form['reason'][''] = 'hidden';
$form['reason'][''] = t('Video');
unset($form['tid']);
}
}

/**
* Implementation of hook_block_info
*
* @author DrupalD
*/
function usertip_block_info() {
$block['usertip'] = array(
'info' => t('Give tip'),
'cache' => DRUPAL_NO_CACHE,
);
return $block;
}

/**
* Implementation of hook_block_view
*
* @param unknown_type $delta
* @author DrupalD
*/
function usertip_block_view($delta = '') {
switch ($delta) {
case 'usertip':
$block['subject'] = t('Give tip');
$block['content'] = usertip_get_usertip();
break;
}
return $block;
}

/**
* _get_usertip_donors
*
* Return faces of usertip donors
*
* @author DrupalD
*/
function _get_usertip_donors() {
global $user;
$output = "";
$r__result = db_select('userpoints_txn', 'utn');
$r__result->leftjoin('users', 'u', 'utn.uid = u.uid');
$r__result->fields('utn', array('points'))
->fields('u', array('picture', 'name', 'uid'))
->condition('utn.operation', 'userpoints_donation_given', '=')
->condition('utn.entity_type', 'user', '=')
->condition('utn.entity_id', 1, '=')
->orderby ('utn.time_stamp', 'DESC')
->range(0, 5);
$am__user = $r__result->execute();
while ($am__user_record = $am__user->fetchAssoc()) {
if ($am__user_record['picture']) {
$r__image = db_select('file_managed', 'fm');
$r__image->leftjoin('users', 'u', 'fm.fid = u.picture');
$r__image->fields('fm', array('uri'))
->condition('fm.fid', $am__user_record['picture'], '=');
$as__image = $r__image->execute()->fetchAssoc();
$ss__user = ($am__user_record['name'] == $user->name) ? t('You') : $am__user_record['name'];
$ss__alt_title = t('@name has dondated @points tip', array('@name' => $ss__user, '@points' => abs($am__user_record['points'])));
$am__variable = array(
'path' => $as__image['uri'],
'width' => '16px',
'height' => '16px',
'alt' => $ss__alt_title,
'title' => $ss__alt_title,
);
$ss__image = theme('image', $am__variable);
$output .= "". l($ss__image, "user/". $am__user_record['uid'], array('html' => TRUE)) ."";
}
}
return $output ."";
}

/**
* usertip_user_get_pionts
*
* @author DrupalD
*/
function usertip_user_get_pionts() {
global $user;
$sn__donation_tid = variable_get('userpoints_category_default_tid');
$r__result = db_select('userpoints', 'up');
$r__result->fields('up', array('points'))
->condition('up.uid', $user->uid, '=');
$am__points = $r__result->execute();
$om__balance = $am__points->fetchAll();
if (isset($om__balance[0]->points)) {
return $om__balance[0]->points;
}
else {
return 0;
}
}

Drupal: Donate userpoints to node author | Drupal Developer

By DrupalD on Thu, 25/04/2013 - 10:32am

Add comment

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