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
<?php
/**
* @file
*
* Donate userpoints on a node page
*
* @author DrupalD <DrupalD@twitter>
*
*/
/**
* usertip_get_usertip
*
* @param unknown_type $node
* @author DrupalD <DrupalD@twitter>
*/
function usertip_get_usertip($sn__uid = NULL) {
global $user;
$form = array();
$sn__points = usertip_user_get_pionts();
$form['usertip_container'] = array(
'#type' => 'fieldset',
'#title' => t('Give Tip'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#weight' => 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(
'#markup' => t('Your point balance is !points for !category category', array('!points' => $sn__points, '!category' => $os__category->name)),
'#weight' => -10
);
$form['usertip_container']['usertip_form'] = array(
'#markup' => drupal_render(drupal_get_form('userpoints_donation_giveform', 1, 0, NULL)),
'#weight' => -9,
);
$form['usertip_container']['usertip_doners'] = array(
'#markup' => _get_usertip_donors(),
'#wieght' => -8,
);
}
else {
$form['usertip_container']['tip_warning'] = array(
'#markup' => 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 <DrupalD@twitter>
*/
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']['#type'] = 'hidden';
$form['amount']['#type'] = 'select';
$form['amount']['#size'] = 1;
$form['amount']['#prefix'] = "<div style='clear:both;float:left;margin-right:10px'>";
$form['amount']['#suffix'] = "</div>";
$form['amount']['#options'] = array(
0 => t('Select amount'),
5 => 5,
10 => 10,
25 => 25,
50 => 50,
100 => 100,
);
$form['submit']['#attributes'] = array('class' => array('use-ajax-submit'));
$form['submit']['#value'] = t('tip');
$form['submit']['#prefix'] = "<div style='dispaly:inline; width:200px; margin-top:12px;'>";
$form['submit']['#suffix'] = "</div>";
$form['reason']['#type'] = 'hidden';
$form['reason']['#default_value'] = t('Video');
unset($form['tid']);
}
}
/**
* Implementation of hook_block_info
*
* @author DrupalD <DrupalD@twitter>
*/
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 <DrupalD@twitter>
*/
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 <DrupalD@twitter>
*/
function _get_usertip_donors() {
global $user;
$output = "<div class='donor'>";
$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 .= "<div class='donor-user'>". l($ss__image, "user/". $am__user_record['uid'], array('html' => TRUE)) ."</div>";
}
}
return $output ."</div>";
}
/**
* usertip_user_get_pionts
*
* @author DrupalD <DrupalD@twitter>
*/
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;
}
}
?>