Overview

Packages

  • None
  • Yaga

Classes

  • ActedModel
  • ActionController
  • ActionModel
  • AwardCombo
  • BadgeAwardModel
  • BadgeController
  • BadgeModel
  • BadgesController
  • BadgesModule
  • BestController
  • BestFilterModule
  • CakeDayPost
  • CommentCount
  • CommentMarathon
  • DiscussionBodyLength
  • DiscussionCategory
  • DiscussionCount
  • DiscussionPageCount
  • HasMentioned
  • HolidayVisit
  • LeaderBoardModule
  • LengthOfService
  • ManualAward
  • NecroPost
  • NewbieComment
  • PhotoExists
  • PostCount
  • PostReactions
  • QnAAnserCount
  • RankController
  • RankModel
  • ReactController
  • ReactionCount
  • ReactionModel
  • ReflexComment
  • RulesController
  • SocialConnection
  • Yaga
  • YagaController
  • YagaHooks

Interfaces

  • YagaRule
  • Overview
  • Package
  • Class
  • Tree
  • Todo
  • Download
  1: <?php if (!defined('APPLICATION')) exit();
  2: /* Copyright 2013 Zachary Doll */
  3: 
  4: /**
  5:  * Describes ranks and their associated requirements/rewards
  6:  *
  7:  * Events:
  8:  *
  9:  * @package Yaga
 10:  * @since 1.0
 11:  */
 12: 
 13: class RankModel extends Gdn_Model {
 14: 
 15:   /**
 16:    * Used as a cache
 17:    * @var DataSet
 18:    */
 19:   private static $_Ranks = NULL;
 20:   
 21:   /**
 22:    * Used as a cache
 23:    * @var DataSet
 24:    */
 25:   private static $_Perks = array();
 26: 
 27:   /**
 28:    * Defines the related database table name.
 29:    */
 30:   public function __construct() {
 31:     parent::__construct('Rank');
 32:   }
 33: 
 34:   /**
 35:    * Returns a list of all ranks
 36:    *
 37:    * @return DataSet
 38:    */
 39:   public function Get() {
 40:     if(empty(self::$_Ranks)) {
 41:       self::$_Ranks = $this->SQL
 42:               ->Select()
 43:               ->From('Rank')
 44:               ->OrderBy('Sort')
 45:               ->Get()
 46:               ->Result();
 47:     }
 48:     return self::$_Ranks;
 49:   }
 50: 
 51:   /**
 52:    * Gets the number of ranks currently specified in the database.
 53:    * 
 54:    * @return int
 55:    */
 56:   public function GetCount() {
 57:     return count($this->Get());
 58:   }
 59: 
 60:   /**
 61:    * Returns data for a specific rank
 62:    *
 63:    * @param int $RankID
 64:    * @return DataSet
 65:    */
 66:   public function GetByID($RankID) {
 67:     $Ranks = $this->Get();
 68:     
 69:     foreach($Ranks as $Rank) {
 70:       if($Rank->RankID == $RankID) {
 71:         return $Rank;
 72:       }
 73:     }
 74:     
 75:     return NULL;
 76:   }
 77: 
 78:   /**
 79:    * Returns the highest rank a user can currently achieve
 80:    *
 81:    * @param object $User
 82:    * @return mixed NULL if no qualifying ranks are found, Rank object otherwise
 83:    */
 84:   public function GetHighestQualifyingRank($User) {
 85:     $Points = $User->Points;
 86:     $Posts = $User->CountDiscussions + $User->CountComments;
 87:     $StartDate = strtotime($User->DateInserted);
 88:     
 89:     $Ranks = $this->Get();
 90:     
 91:     $HighestRank = NULL;
 92:     foreach($Ranks as $Rank) {
 93:       // skip disabled ranks
 94:       if(!$Rank->Enabled) {
 95:         continue;
 96:       }
 97:       
 98:       $TargetDate = time() - $Rank->AgeReq;
 99:       if($Points >= $Rank->PointReq && $Posts >= $Rank->PostReq && $StartDate <= $TargetDate) {
100:         $HighestRank = $Rank;
101:       }
102:       else {
103:         // Don't continue if we do not qualify
104:         break;
105:       }
106:     }
107: 
108:     return $HighestRank;
109:   }
110:   
111:   /**
112:    * Get a list of perks associated with the specified Rank ID
113:    * 
114:    * @param int $RankID
115:    * @return array
116:    */
117:   public function GetPerks($RankID) {
118:     if(!array_key_exists($RankID, self::$_Perks)) {
119:       $Ranks = $this->Get();
120:       foreach($Ranks as $Rank) {
121:         self::$_Perks[$Rank->RankID] = unserialize($Rank->Perks);
122:         
123:         if(self::$_Perks[$Rank->RankID] === FALSE) {
124:           self::$_Perks[$Rank->RankID] = array();
125:         }
126:       }
127:     }
128:     
129:     return self::$_Perks[$RankID];
130:   }
131:   
132:   /**
133:    * Returns all role IDs the specified rank confers as a perk
134:    * 
135:    * @param int $RankID
136:    * @return array
137:    */
138:   public function GetPerkRoleIDs($RankID) {
139:     $Perks = $this->GetPerks($RankID);
140:       
141:     $RoleIDs = array();
142:     foreach($Perks as $Perk => $Value) {
143:       if(substr($Perk, 0, 4) === 'Role') {
144:         $RoleIDs[] = $Value;
145:       }
146:     }
147: 
148:     return $RoleIDs;
149:   }
150: 
151:   /**
152:    * Enable or disable a rank
153:    *
154:    * @param int $RankID
155:    * @param bool $Enable
156:    */
157:   public function Enable($RankID, $Enable) {
158:     $Enable = (!$Enable) ? FALSE : TRUE;
159:     $this->SQL
160:             ->Update('Rank')
161:             ->Set('Enabled', $Enable)
162:             ->Where('RankID', $RankID)
163:             ->Put();
164:   }
165: 
166:   /**
167:    * Remove a rank and associated awards
168:    *
169:    * @param int $RankID
170:    * @return boolean
171:    */
172:   public function Delete($RankID) {
173:     $Rank = $this->GetByID($RankID);
174:     if(!$Rank) {
175:       $this->SQL->Delete('Rank', array('RankID' => $RankID));
176:       return TRUE;
177:     }
178:     return FALSE;
179:   }
180: 
181:   /**
182:    * Set a user's rank and record some activity if it was a promotion
183:    *
184:    * @param int $RankID
185:    * @param int $UserID This is the user that should get the award
186:    * @param bool $Activity Whether or not to insert an activity record.
187:    */
188:   public function Set($RankID, $UserID, $Activity = FALSE) {
189:     $Rank = $this->GetByID($RankID);
190:     if($Activity) {
191:       // Throw up a promotion activity
192:       $ActivityModel = new ActivityModel();
193: 
194:       $Activity = array(
195:           'ActivityType' => 'RankPromotion',
196:           'ActivityUserID' => $UserID,
197:           'RegardingUserID' => $UserID,
198:           'Photo' => C('Yaga.Ranks.Photo'),
199:           'RecordType' => 'Rank',
200:           'RecordID' => $Rank->RankID,
201:           'HeadlineFormat' => T('Yaga.HeadlineFormat.Promoted'),
202:           'Data' => array(
203:               'Name' => $Rank->Name
204:           ),
205:           'Story' => $Rank->Description
206:       );
207: 
208:       // Create a public record
209:       $ActivityModel->Queue($Activity, FALSE); // TODO: enable the grouped notifications after issue #1776 is resolved , array('GroupBy' => 'Story'));
210: 
211:       // Notify the user of the award
212:       $Activity['NotifyUserID'] = $UserID;
213:       $ActivityModel->Queue($Activity, 'RankPromotion', array('Force' => TRUE));
214: 
215:       $ActivityModel->SaveQueue();
216:     }
217:     
218:     // Update the rank id
219:     $UserModel = Gdn::UserModel();
220:     $OldRankID = $UserModel->GetID($UserID)->RankID;
221:     
222:     $UserModel->SetField($UserID, 'RankID', $Rank->RankID);
223: 
224:     // Update the roles if necessary
225:     $this->_UpdateUserRoles($UserID, $OldRankID, $Rank->RankID);
226:     
227:     $this->EventArguments['Rank'] = $Rank;
228:     $this->EventArguments['UserID'] = $UserID;
229:     $this->FireEvent('AfterRankChange');
230:   }
231:   
232:   /**
233:    * Updates the sort field for each rank in the sort array
234:    * 
235:    * @param array $SortArray
236:    * @return boolean
237:    */
238:   public function SaveSort($SortArray) {
239:     foreach($SortArray as $Index => $Rank) {
240:       // skip the header row
241:       if($Index == 0) {
242:         continue;
243:       }
244:       
245:       // remove the 'RankID_' prefix
246:       $RankID = substr($Rank, 7);
247:       $this->SetField($RankID, 'Sort', $Index);
248:     }
249:     return TRUE;
250:   }
251:   
252:   /**
253:    * Updates a user roles by removing role perks from the old rank and adding the
254:    * roles from the new rank
255:    * @param int $UserID
256:    * @param int $OldRankID
257:    * @param int $NewRankID
258:    */
259:   private function _UpdateUserRoles($UserID, $OldRankID, $NewRankID) {
260:     $UserModel = Gdn::UserModel();
261:     
262:     // Get the user's current roles
263:     $CurrentRoleData = $UserModel->GetRoles($UserID);
264:     $CurrentRoleIDs = ConsolidateArrayValuesByKey($CurrentRoleData->Result(), 'RoleID');
265: 
266:     // Get the associated role perks
267:     $OldPerkRoles = $this->GetPerkRoleIDs($OldRankID);
268:     $NewPerkRoles = $this->GetPerkRoleIDs($NewRankID);
269: 
270:     // Remove any role perks the old rank had
271:     $TempRoleIDs = array_diff($CurrentRoleIDs, $OldPerkRoles);
272: 
273:     // Add our selected roles
274:     $NewRoleIDs = array_unique(array_merge($TempRoleIDs, $NewPerkRoles));
275: 
276:     // Set the combined roles
277:     if($NewRoleIDs != $CurrentRoleIDs) {
278:       $UserModel->SaveRoles($UserID, $NewRoleIDs, FALSE);
279:     }
280: 
281:   }
282: }
283: 
Yaga API documentation generated by ApiGen 2.8.0