user_badges.actions.inc in User Badges 7.3
Same filename and directory in other branches
Actions for User Badges module.
File
user_badges.actions.incView source
<?php
/**
* @file
* Actions for User Badges module.
*/
/**
* Implements hook_action_info().
*/
function user_badges_action_info() {
return array(
'user_badges_add_badge_action' => array(
'label' => t('Add badge'),
'type' => 'user',
'configurable' => TRUE,
'triggers' => array(
'any' => TRUE,
),
),
'user_badges_remove_badge_action' => array(
'label' => t('Remove badge'),
'type' => 'user',
'configurable' => TRUE,
'triggers' => array(
'any' => TRUE,
),
),
);
}
/**
* Implementsa Drupal action.
* Adds a badge to the current user.
*/
function user_badges_add_badge_action(&$account, $context = array()) {
if (isset($account->uid)) {
$uid = $account->uid;
$user_name = $account->name;
}
elseif (isset($context['uid'])) {
$uid = $context['uid'];
$user_name = $context['uid']->name;
}
else {
global $user;
$uid = $user->uid;
$user_name = $user->name;
}
$success = TRUE;
$badges = user_badges_get_badges('all');
foreach ($context['badges'] as $bid) {
$success = (bool) user_badges_user_add_badge($uid, $bid, 'user');
if ($success) {
watchdog('action', 'Added user badge %badge to user %name.', array(
'%name' => check_plain($user_name),
'%badge' => check_plain($badges[$bid]->name),
));
}
else {
watchdog('action', 'Unable to add user badge %badge to user %name.', array(
'%name' => check_plain($user_name),
'%badge' => check_plain($badges[$bid]->name),
), WATCHDOG_WARNING);
}
}
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function user_badges_add_badge_action_form($context) {
$form['badges'] = array(
'#tree' => TRUE,
);
$badges = user_badges_get_badges('all');
$context['badges'] = isset($context['badges']) ? $context['badges'] : array();
foreach ($badges as $badge) {
$form['badges'][$badge->bid] = array(
'#type' => 'checkbox',
'#title' => theme('user_badge', array(
'badge' => $badge,
)),
'#return_value' => 1,
'#default_value' => isset($context['badges'][$badge->bid]) ? $context['badges'][$badge->bid] : FALSE,
'#description' => check_plain($badge->name),
);
}
return $form;
}
/**
* Submit handler for user_badges_add_badge_action.
* @see http://drupal.org/node/1354
*/
function user_badges_add_badge_action_submit($form, $form_state) {
$badges = array();
foreach ($form_state['values']['badges'] as $badge => $set) {
if ($set) {
$badges[$badge] = $badge;
}
}
return array(
'badges' => $badges,
);
}
/**
* Implementsa Drupal action.
* Removes a badge to the current user.
*/
function user_badges_remove_badge_action(&$account, $context = array()) {
if (isset($account->uid)) {
$uid = $account->uid;
$user_name = $account->name;
}
elseif (isset($context['uid'])) {
$uid = $context['uid'];
$user_name = $context['uid']->name;
}
else {
global $user;
$uid = $user->uid;
$user_name = $user->name;
}
$success = TRUE;
$badges = user_badges_get_badges('all');
foreach ($context['badges'] as $bid) {
$success = (bool) user_badges_user_remove_badge($uid, $bid, 'user');
if ($success) {
watchdog('action', 'Removed user badge %badge to user %name.', array(
'%name' => check_plain($user_name),
'%badge' => check_plain($badges[$bid]->name),
));
}
else {
watchdog('action', 'Unable to remove user badge %badge to user %name.', array(
'%name' => check_plain($user_name),
'%badge' => check_plain($badges[$bid]->name),
), WATCHDOG_WARNING);
}
}
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function user_badges_remove_badge_action_form($context) {
$form['badges'] = array(
'#tree' => TRUE,
);
$badges = user_badges_get_badges('all');
$context['badges'] = is_array($context['badges']) ? $context['badges'] : array();
foreach ($badges as $badge) {
$form['badges'][$badge->bid] = array(
'#type' => 'checkbox',
'#title' => theme('user_badge', array(
'badge' => $badge,
)),
'#return_value' => 1,
'#default_value' => $context['badges'][$badge->bid],
'#description' => check_plain($badge->name),
);
}
return $form;
}
/**
* @todo Please document this function.
* @see http://drupal.org/node/1354
*/
function user_badges_remove_badge_action_submit($form, $form_state) {
$badges = array();
foreach ($form_state['values']['badges'] as $badge => $set) {
if ($set) {
$badges[$badge] = $badge;
}
}
return array(
'badges' => $badges,
);
}
Functions
Name | Description |
---|---|
user_badges_action_info | Implements hook_action_info(). |
user_badges_add_badge_action | Implementsa Drupal action. Adds a badge to the current user. |
user_badges_add_badge_action_form | @todo Please document this function. |
user_badges_add_badge_action_submit | Submit handler for user_badges_add_badge_action. |
user_badges_remove_badge_action | Implementsa Drupal action. Removes a badge to the current user. |
user_badges_remove_badge_action_form | @todo Please document this function. |
user_badges_remove_badge_action_submit | @todo Please document this function. |