HowTo: Integrate Privatemsg with UserPoints Module
There are two way to integrate Privatemsg with the UserPoints module. First, it is possible to use the privatemsg_rules module (see #327938: Rules Integration) which allows to configure some basic definitions (For example, when sending a private message).
The second possibility is to create custom hook functions which allow for much mor flexiblitly and make it possible to set a description.
Example 1: Give a user 5 points when sending a new message and 6 when sending a reply.
function yourmodule_privatemsg_message_insert($message) {
// If thread_id and mid is the same, this is a new thread.
if ($message['thread_id'] == $message['mid']) {
$params = array(
'uid' => $message['author']->uid,
'points' => 5,
'description' => 'You have sent a new private message to a user of the community.',
);
}
else {
$params = array(
'uid' => $message['author']->uid,
'points' => 6,
'description' => 'You have replied to a private message.',
);
}
userpoints_userpointsapi($params);
}
Example 2: Give a user 3 points when viewing a message the first time:
function yourmodule_privatemsg_message_view($message) {
global $user;
// Only give points when the message is new for the user and has not been read before.
if ($message['is_new']) {
$params = array(
'uid' => $user->uid,
'points' => 3,
'description' => 'You have read a private message.',
);
userpoints_userpointsapi($params);
}
}
Documentation about the used hooks (and others) is available at http://blog.worldempire.ch/api/group/message_hooks/1
Another example of incorporating user points and privatemsg is to limit privatemsg module to only allow people with greater than 0 points to send a message. You will need to follow the guide at http://drupal.org/node/416986 to create a simple module. You only need the .module files which is
<?php
/**
* Implements hook_privatemsg_message_validate().
*/
function yourmodulename_privatemsg_message_validate($message, $form = FALSE) {
$points = userpoints_get_current_points($message->author->uid); // Specify the category id in the second argument if it's not the default.
if ($points <= 0) {
// Change this message to whatever you want. But keep it english and then translate it.
$error = t("You are not allowed to send private messages when you don't have any points.");
if ($form) {
form_set_error('recipient', $error);
}
else {
return array('error' => array($error));
}
}
}
?>
The .info file
; $Id$
name = yourmodulename
description = Custom functions for this site.
core = 6.x
This will create a custom module. Once enabled it will automatically start disallowing users to send messages with 0 or less points. You could also set this 0 to a higher number such as 10 to not allow users to send messages until they get 10 points.
If you wish to add a permission so it can be configured for various roles to use then here is the .module code...
<?php
function yourmodulename_perm() {
// Use whatever name you want.
return array('bypass privatemsg points requirement');
}
/**
* Implements hook_privatemsg_message_validate().
*/
function yourmodulename_privatemsg_message_validate($message, $form = FALSE) {
if (user_access('bypass privatemsg points requirement', $message->author)) {
return;
}
$points = userpoints_get_current_points($message->author->uid); // Specify the category id in the second argument if it's not the default.
if ($points <= 0) {
// Change this message to whatever you want. But keep it english and then translate it.
$error = t("You must have Points to send messages. ");
if ($form) {
form_set_error('recipient', $error);
}
else {
return array('error' => array($error));
}
}
}
?>
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion