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:  * Handles badge awards
  6:  *
  7:  * Events:
  8:  *
  9:  * @package Yaga
 10:  * @since 1.0
 11:  */
 12: class BadgeAwardModel extends Gdn_Model {
 13: 
 14:   /**
 15:    * Defines the related database table name.
 16:    */
 17:   public function __construct() {
 18:     parent::__construct('BadgeAward');
 19:   }
 20: 
 21:   /**
 22:    * Gets the number of badges that have been awarded with a specific ID
 23:    * 
 24:    * @param int $BadgeID
 25:    * @return int
 26:    */
 27:   public function GetCount($BadgeID = FALSE) {
 28:     if($BadgeID) {
 29:       $Wheres = array('BadgeID' => $BadgeID);
 30:     }
 31:     else {
 32:       $Wheres = array();
 33:     }
 34:     return $this->SQL->GetCount('BadgeAward', $Wheres);
 35:   }
 36: 
 37:   /**
 38:    * Gets recently awarded badges with a specific ID
 39:    * 
 40:    * @param int $BadgeID
 41:    * @param int $Limit
 42:    * @return dataset
 43:    */
 44:   public function GetRecent($BadgeID, $Limit = 15) {
 45:     return $this->SQL
 46:                     ->Select('ba.UserID, ba.DateInserted, u.Name, u.Photo, u.Gender, u.Email')
 47:                     ->From('BadgeAward ba')
 48:                     ->Join('User u', 'ba.UserID = u.UserID')
 49:                     ->Where('BadgeID', $BadgeID)
 50:                     ->OrderBy('DateInserted', 'Desc')
 51:                     ->Limit($Limit)
 52:                     ->Get()
 53:                     ->Result();
 54:   }
 55: 
 56:   /**
 57:    * Award a badge to a user and record some activity
 58:    *
 59:    * @param int $BadgeID
 60:    * @param int $UserID This is the user that should get the award
 61:    * @param int $InsertUserID This is the user that gave the award
 62:    * @param string $Reason This is the reason the giver gave with the award
 63:    */
 64:   public function Award($BadgeID, $UserID, $InsertUserID = NULL, $Reason = '') {
 65:     $Badge = Yaga::BadgeModel()->GetByID($BadgeID);
 66:     if(!empty($Badge)) {
 67:       if(!$this->Exists($UserID, $BadgeID)) {
 68:         $this->SQL->Insert('BadgeAward', array(
 69:             'BadgeID' => $BadgeID,
 70:             'UserID' => $UserID,
 71:             'InsertUserID' => $InsertUserID,
 72:             'Reason' => $Reason,
 73:             'DateInserted' => date(DATE_ISO8601)
 74:         ));
 75: 
 76:         // Record the points for this badge
 77:         UserModel::GivePoints($UserID, $Badge->AwardValue, 'Badge');
 78: 
 79:         // Increment the user's badge count
 80:         $this->SQL->Update('User')
 81:                 ->Set('CountBadges', 'CountBadges + 1', FALSE)
 82:                 ->Where('UserID', $UserID)
 83:                 ->Put();
 84: 
 85:         if(is_null($InsertUserID)) {
 86:           $InsertUserID = Gdn::Session()->UserID;
 87:         }
 88: 
 89:         // Record some activity
 90:         $ActivityModel = new ActivityModel();
 91: 
 92:         $Activity = array(
 93:             'ActivityType' => 'BadgeAward',
 94:             'ActivityUserID' => $UserID,
 95:             'RegardingUserID' => $InsertUserID,
 96:             'Photo' => $Badge->Photo,
 97:             'RecordType' => 'Badge',
 98:             'RecordID' => $BadgeID,
 99:             'Route' => '/badges/detail/' . $Badge->BadgeID . '/' . Gdn_Format::Url($Badge->Name),
100:             'HeadlineFormat' => T('Yaga.HeadlineFormat.BadgeEarned'),
101:             'Data' => array(
102:                 'Name' => $Badge->Name
103:             ),
104:             'Story' => $Badge->Description
105:         );
106: 
107:         // Create a public record
108:         $ActivityModel->Queue($Activity, FALSE); // TODO: enable the grouped notifications after issue #1776 is resolved , array('GroupBy' => 'Route'));
109:         // Notify the user of the award
110:         $Activity['NotifyUserID'] = $UserID;
111:         $ActivityModel->Queue($Activity, 'BadgeAward', array('Force' => TRUE));
112: 
113:         // Actually save the activity
114:         $ActivityModel->SaveQueue();
115: 
116:         $this->EventArguments['UserID'] = $UserID;
117:         $this->FireEvent('AfterBadgeAward');
118:       }
119:     }
120:   }
121: 
122:   /**
123:    * Returns how many badges the user has of this particular id. It should only
124:    * ever be 1 or zero.
125:    *
126:    * @param int $UserID
127:    * @param int $BadgeID
128:    * @return int
129:    */
130:   public function Exists($UserID, $BadgeID) {
131:     return $this->SQL
132:                     ->Select()
133:                     ->From('BadgeAward')
134:                     ->Where('BadgeID', $BadgeID)
135:                     ->Where('UserID', $UserID)
136:                     ->Get()
137:                     ->FirstRow();
138:   }
139: 
140:   /**
141:    * Returns the badges a user has already received
142:    *
143:    * @param int $UserID
144:    * @param string $DataType
145:    * @return mixed
146:    */
147:   public function GetByUser($UserID, $DataType = DATASET_TYPE_ARRAY) {
148:     return $this->SQL
149:                     ->Select()
150:                     ->From('Badge b')
151:                     ->Join('BadgeAward ba', 'ba.BadgeID = b.BadgeID', 'left')
152:                     ->Where('ba.UserID', $UserID)
153:                     ->Get()
154:                     ->Result($DataType);
155:   }
156: 
157:   /**
158:    * Returns the list of unobtained but enabled badges for a specific user
159:    *
160:    * @param int $UserID
161:    * @return DataSet
162:    */
163:   public function GetUnobtained($UserID) {
164:     $Px = $this->Database->DatabasePrefix;
165:     $Sql = 'select b.BadgeID, b.Enabled, b.RuleClass, b.RuleCriteria, '
166:             . 'ba.UserID '
167:             . "from {$Px}Badge as b "
168:             . "left join {$Px}BadgeAward as ba ON b.BadgeID = ba.BadgeID and ba.UserID = :UserID ";
169: 
170:     return $this->Database->Query($Sql, array(':UserID' => $UserID))->Result();
171:   }
172: 
173:   /**
174:    * Used by the DBA controller to update the denormalized badge count on the
175:    * user table via dba/counts
176:    * @param string $Column
177:    * @param int $UserID
178:    * @return boolean
179:    * @throws Gdn_UserException
180:    */
181:   public function Counts($Column, $UserID = NULL) {
182:     if($UserID) {
183:       $Where = array('UserID' => $UserID);
184:     }
185:     else {
186:       $Where = NULL;
187:     }
188:     
189:     $Result = array('Complete' => TRUE);
190:     switch($Column) {
191:       case 'CountBadges':
192:         Gdn::Database()->Query(DBAModel::GetCountSQL('count', 'User', 'BadgeAward', 'CountBadges', 'BadgeAwardID', 'UserID', 'UserID', $Where));
193:         break;
194:       default:
195:         throw new Gdn_UserException("Unknown column $Column");
196:     }
197:     return $Result;
198:   }
199: 
200: }
201: 
Yaga API documentation generated by ApiGen 2.8.0