/** * Award a badge to a user and record some activity * * @param int $BadgeID * @param int $UserID This is the user that should get the award * @param int $InsertUserID This is the user that gave the award * @param string $Reason This is the reason the giver gave with the award */ public function Award($BadgeID, $UserID, $InsertUserID = NULL, $Reason = '') { $Badge = Yaga::BadgeModel()->GetByID($BadgeID); if (!empty($Badge)) { if (!$this->Exists($UserID, $BadgeID)) { $this->SQL->Insert('BadgeAward', array('BadgeID' => $BadgeID, 'UserID' => $UserID, 'InsertUserID' => $InsertUserID, 'Reason' => $Reason, 'DateInserted' => date(DATE_ISO8601))); // Record the points for this badge UserModel::GivePoints($UserID, $Badge->AwardValue, 'Badge'); // Increment the user's badge count $this->SQL->Update('User')->Set('CountBadges', 'CountBadges + 1', FALSE)->Where('UserID', $UserID)->Put(); if (is_null($InsertUserID)) { $InsertUserID = Gdn::Session()->UserID; } // Record some activity $ActivityModel = new ActivityModel(); $Activity = array('ActivityType' => 'BadgeAward', 'ActivityUserID' => $UserID, 'RegardingUserID' => $InsertUserID, 'Photo' => $Badge->Photo, 'RecordType' => 'Badge', 'RecordID' => $BadgeID, 'Route' => '/badges/detail/' . $Badge->BadgeID . '/' . Gdn_Format::Url($Badge->Name), 'HeadlineFormat' => T('Yaga.Badge.EarnedHeadlineFormat'), 'Data' => array('Name' => $Badge->Name), 'Story' => $Badge->Description); // Create a public record $ActivityModel->Queue($Activity, FALSE); // TODO: enable the grouped notifications after issue #1776 is resolved , array('GroupBy' => 'Route')); // Notify the user of the award $Activity['NotifyUserID'] = $UserID; $ActivityModel->Queue($Activity, 'BadgeAward', array('Force' => TRUE)); // Actually save the activity $ActivityModel->SaveQueue(); $this->EventArguments['UserID'] = $UserID; $this->FireEvent('AfterBadgeAward'); } } }
/** * Add another user to the conversation. * * @since 2.0.0 * @access public * * @param int $ConversationID Unique ID of conversation effected. * @param int $UserID Unique ID of current user. */ public function AddUserToConversation($ConversationID, $UserID) { if (!is_array($UserID)) { $UserID = array($UserID); } // First define the current users in the conversation $OldContributorData = $this->GetRecipients($ConversationID); $OldContributorData = Gdn_DataSet::Index($OldContributorData, 'UserID'); $AddedUserIDs = array(); // Get some information about this conversation $ConversationData = $this->SQL->Select('LastMessageID')->Select('CountMessages')->From('Conversation')->Where('ConversationID', $ConversationID)->Get()->FirstRow(); // Add the user(s) if they are not already in the conversation foreach ($UserID as $NewUserID) { if (!array_key_exists($NewUserID, $OldContributorData)) { $AddedUserIDs[] = $NewUserID; $this->SQL->Insert('UserConversation', array('UserID' => $NewUserID, 'ConversationID' => $ConversationID, 'LastMessageID' => $ConversationData->LastMessageID, 'CountReadMessages' => 0, 'DateConversationUpdated' => $ConversationData['DateUpdated'])); } elseif ($OldContributorData[$NewUserID]->Deleted) { $AddedUserIDs[] = $NewUserID; $this->SQL->Put('UserConversation', array('Deleted' => 0), array('ConversationID' => $ConversationID, 'UserID' => $NewUserID)); } } if (count($AddedUserIDs) > 0) { $Session = Gdn::Session(); // Update the Contributors field on the conversation $Contributors = array_unique(array_merge($AddedUserIDs, array_keys($OldContributorData))); sort($Contributors); $this->SQL->Update('Conversation')->Set('Contributors', Gdn_Format::Serialize($Contributors))->Where('ConversationID', $ConversationID)->Put(); $ActivityModel = new ActivityModel(); foreach ($AddedUserIDs as $AddedUserID) { $ActivityModel->Queue(array('ActivityType' => 'AddedToConversation', 'NotifyUserID' => $AddedUserID, 'HeadlineFormat' => T('You were added to a conversation.', '{InsertUserID,user} added {NotifyUserID,you} to a <a href="{Url,htmlencode}">conversation</a>.')), 'ConversationMessage'); } $ActivityModel->SaveQueue(); $this->UpdateUserUnreadCount($AddedUserIDs); } }
/** * Unban a user. * * @since 2.1 * * @param int $UserID * @param array $Options */ public function unBan($UserID, $Options = array()) { $User = $this->getID($UserID, DATASET_TYPE_ARRAY); if (!$User) { throw notFoundException(); } $Banned = $User['Banned']; if (!BanModel::isBanned($Banned, BanModel::BAN_MANUAL)) { throw new Gdn_UserException(t("The user isn't banned.", "The user isn't banned or is banned by some other function.")); } // Unban the user. $NewBanned = BanModel::setBanned($Banned, false, BanModel::BAN_MANUAL); $this->setField($UserID, 'Banned', $NewBanned); // Restore the user's content. if (val('RestoreContent', $Options)) { $BanLogID = $this->getAttribute($UserID, 'BanLogID'); if ($BanLogID) { $LogModel = new LogModel(); try { $LogModel->restore($BanLogID); } catch (Exception $Ex) { if ($Ex->getCode() != 404) { throw $Ex; } } $this->saveAttribute($UserID, 'BanLogID', null); } } // Add an activity for the unbanning. if (val('AddActivity', $Options, true)) { $ActivityModel = new ActivityModel(); $Story = val('Story', $Options, null); // Notify the moderators of the unban. $Activity = array('ActivityType' => 'Ban', 'NotifyUserID' => ActivityModel::NOTIFY_MODS, 'ActivityUserID' => $UserID, 'RegardingUserID' => Gdn::session()->UserID, 'HeadlineFormat' => t('HeadlineFormat.Unban', '{RegardingUserID,You} unbanned {ActivityUserID,you}.'), 'Story' => $Story, 'Data' => array('Unban' => true)); $ActivityModel->Queue($Activity); // Notify the user of the unban. $Activity['NotifyUserID'] = $UserID; $Activity['Emailed'] = ActivityModel::SENT_PENDING; $Activity['HeadlineFormat'] = t('HeadlineFormat.Unban.Notification', "You've been unbanned."); $ActivityModel->Queue($Activity, false, array('Force' => true)); $ActivityModel->SaveQueue(); } }
/** * Unban a user. * @since 2.1 * @param int $UserID * @param array $Options */ public function UnBan($UserID, $Options = array()) { $User = $this->GetID($UserID, DATASET_TYPE_ARRAY); if (!$User) { throw NotFoundException(); } if (!$User['Banned']) { throw new Gdn_UserException(T("The user isn't banned.")); } // Unban the user. $this->SetField($UserID, 'Banned', FALSE); // Restore the user's content. if (GetValue('RestoreContent', $Options)) { $BanLogID = $this->GetAttribute($UserID, 'BanLogID'); if ($BanLogID) { $LogModel = new LogModel(); try { $LogModel->Restore($BanLogID); } catch (Exception $Ex) { if ($Ex->getCode() != 404) { throw $Ex; } } $this->SaveAttribute($UserID, 'BanLogID', NULL); } } // Add an activity for the unbanning. if (GetValue('AddActivity', $Options, TRUE)) { $ActivityModel = new ActivityModel(); $Story = GetValue('Story', $Options, NULL); // Notify the moderators of the unban. $Activity = array('ActivityType' => 'Ban', 'NotifyUserID' => ActivityModel::NOTIFY_MODS, 'ActivityUserID' => $UserID, 'RegardingUserID' => Gdn::Session()->UserID, 'HeadlineFormat' => T('HeadlineFormat.Unban', '{RegardingUserID,You} unbanned {ActivityUserID,you}.'), 'Story' => $Story); $ActivityModel->Queue($Activity); // Notify the user of the unban. $Activity['NotifyUserID'] = $UserID; $Activity['Emailed'] = ActivityModel::SENT_PENDING; $Activity['HeadlineFormat'] = T('HeadlineFormat.Unban.Notification', "You've been unbanned."); $ActivityModel->Queue($Activity, FALSE, array('Force' => TRUE)); $ActivityModel->SaveQueue(); } }
/** * Set a user's rank and record some activity if it was a promotion * * @param int $RankID * @param int $UserID This is the user that should get the award * @param bool $Activity Whether or not to insert an activity record. */ public function Set($RankID, $UserID, $Activity = FALSE) { $Rank = $this->GetByID($RankID); if ($Activity) { // Throw up a promotion activity $ActivityModel = new ActivityModel(); $Activity = array('ActivityType' => 'RankPromotion', 'ActivityUserID' => $UserID, 'RegardingUserID' => $UserID, 'Photo' => C('Yaga.Ranks.Photo'), 'RecordType' => 'Rank', 'RecordID' => $Rank->RankID, 'HeadlineFormat' => T('Yaga.Rank.PromotedHeadlineFormat'), 'Data' => array('Name' => $Rank->Name), 'Story' => $Rank->Description); // Create a public record $ActivityModel->Queue($Activity, FALSE); // TODO: enable the grouped notifications after issue #1776 is resolved , array('GroupBy' => 'Story')); // Notify the user of the award $Activity['NotifyUserID'] = $UserID; $ActivityModel->Queue($Activity, 'RankPromotion', array('Force' => TRUE)); $ActivityModel->SaveQueue(); } // Update the rank id $UserModel = Gdn::UserModel(); $OldRankID = $UserModel->GetID($UserID)->RankID; $UserModel->SetField($UserID, 'RankID', $Rank->RankID); // Update the roles if necessary $this->_UpdateUserRoles($UserID, $OldRankID, $Rank->RankID); $this->EventArguments['Rank'] = $Rank; $this->EventArguments['UserID'] = $UserID; $this->FireEvent('AfterRankChange'); }