You are here

function UserpointsTransaction::getMessage in User Points 7.2

A message that can be displayed to the current user.

If set, the message defined by UserpointsTransaction::setMessage() is used. Otherwise, a message is displayed that takes into account the points amount (negative or positive), the category, the status and if the transaction is for the currently logged in user or not.

Return value

A message string that describes this transaction to the currently logged in user. Can be empty if not automated message could have been generated.

See also

UserpointsTransaction::setMessage().

File

./userpoints.transaction.inc, line 893
Contains the UserpointsTransaction and related classes.

Class

UserpointsTransaction
A Userpoints transaction.

Code

function getMessage() {
  global $user;

  // If set, use the overriden message.
  if (!empty($this->message)) {
    return $this->message;
  }

  // Prepare arguments. They are the same for all string combinations.
  $categories = userpoints_get_categories();
  $total_points = userpoints_get_current_points($this
    ->getUid(), $this
    ->getTid());
  $arguments = array_merge(userpoints_translation(), array(
    '!username' => theme('username', array(
      'account' => $this
        ->getUser(),
    )),
    '%total' => theme('userpoints_points', array(
      'points' => $total_points,
    )),
    '%category' => $this
      ->getCategory(),
  ));
  $view_own_points = user_access('view own userpoints') || user_access('view userpoints') || user_access('administer userpoints');
  $view_all_points = user_access('view userpoints') || user_access('administer userpoints');
  $points = theme('userpoints_points', array(
    'points' => $this
      ->getPoints(),
  ));
  $absolute_points = theme('userpoints_points', array(
    'points' => $this
      ->getPoints(),
  ));
  $message = NULL;
  if ($this
    ->isDeclined()) {

    // Points have been declined.
    if ($this
      ->getUid() == $user->uid && $view_own_points) {
      $message = format_plural($points, 'You did not receive approval for @count !point in the %category category.', 'You did not receive approval for @count !points in the %category category.', $arguments);
    }
    elseif ($view_all_points) {
      $message = format_plural($points, '!username did not receive approval for @count !point in the %category category.', '!username did not receive approval for @count !points in the %category category.', $arguments);
    }
  }
  elseif ($this
    ->getPoints() < 0) {
    if ($this
      ->isPending()) {
      if ($this
        ->getUid() == $user->uid && $view_own_points) {

        // Directly address the user if he is loosing points.
        $message = format_plural($absolute_points, 'You just had a !point deducted, pending administrator approval.', 'You just had @count !points deducted, pending administrator approval.', $arguments);
      }
      elseif ($view_all_points) {

        // Only display message about other users if user has permission to view userpoints.
        $message = format_plural($absolute_points, '!username just had a !point deducted, pending administrator approval.', '!username just had @count !points deducted, pending administrator approval.', $arguments);
      }
    }
    else {
      if ($this
        ->getUid() == $user->uid && $view_own_points) {
        $message = format_plural($absolute_points, 'You just had a !point deducted and now have %total !points in the %category category.', 'You just had @count !points deducted and now have %total !points in the %category category.', $arguments);
      }
      elseif ($view_all_points) {
        $message = format_plural($absolute_points, '!username just had a !point deducted and now has %total !points in the %category category.', '!username just had @count !points deducted and now has %total !points in the %category category.', $arguments);
      }
    }
  }
  else {
    if ($this
      ->isPending()) {
      if ($this
        ->getUid() == $user->uid && $view_own_points) {

        // Directly address the user if he is loosing points.
        $message = format_plural($absolute_points, 'You just earned a !point, pending administrator approval.', 'You just earned @count !points, pending administrator approval.', $arguments);
      }
      elseif ($view_all_points) {

        // Only display message about other users if user has permission to view userpoints.
        $message = format_plural($absolute_points, '!username just earned a !point, pending administrator approval.', '!username just earned @count !points, pending administrator approval.', $arguments);
      }
    }
    else {
      if ($this
        ->getUid() == $user->uid && $view_own_points) {
        $message = format_plural($absolute_points, 'You just earned a !point and now have %total !points in the %category category.', 'You just earned @count !points and now have %total !points in the %category category.', $arguments);
      }
      elseif ($view_all_points) {
        $message = format_plural($absolute_points, '!username just earned a !point and now has %total !points in the %category category.', '!username just earned @count !points and now has %total !points in the %category category.', $arguments);
      }
    }
  }
  return $message;
}