public function makeAll(&$demoDataHelper) { assert('$demoDataHelper instanceof DemoDataHelper'); assert('$demoDataHelper->isSetRange("User")'); foreach (User::getAll() as $user) { $gameScore = GameScore::resolveToGetByTypeAndPerson('LoginUser', $user); $gameScore->value = 10; $saved = $gameScore->save(); assert('$saved'); $gamePoint = GamePoint::resolveToGetByTypeAndPerson(GamePoint::TYPE_USER_ADOPTION, $user); $gamePoint->value = mt_rand(100, 300); $saved = $gamePoint->save(); assert('$saved'); $gameScore = GameScore::resolveToGetByTypeAndPerson('CreateAccount', $user); $gameScore->value = 10; $saved = $gameScore->save(); assert('$saved'); $gamePoint = GamePoint::resolveToGetByTypeAndPerson(GamePoint::TYPE_NEW_BUSINESS, $user); $gamePoint->value = 100; $saved = $gamePoint->save(); assert('$saved'); //Badges $gameBadge = new GameBadge(); $gameBadge->type = 'LoginUser'; $gameBadge->grade = 2; $gameBadge->person = $user; $saved = $gameBadge->save(); assert('$saved'); $gameBadge = new GameBadge(); $gameBadge->type = 'CreateAccount'; $gameBadge->grade = 3; $gameBadge->person = $user; $saved = $gameBadge->save(); assert('$saved'); //Levels $gameLevel = GameLevel::resolveByTypeAndPerson(GameLevel::TYPE_GENERAL, $user); $gameLevel->value = 1; $saved = $gameLevel->save(); assert('$saved'); $gameLevel = GameLevel::resolveByTypeAndPerson(GameLevel::TYPE_NEW_BUSINESS, $user); $gameLevel->value = 1; $saved = $gameLevel->save(); assert('$saved'); } }
/** * Called at the end of the page request. Processes any new badges or badge grade changes for the current user. */ public function resolveNewBadges() { if (!$this->enabled) { return; } $userBadgesByType = GameBadge::getAllByPersonIndexedByType(Yii::app()->user->userModel); $userPointsByType = GamePoint::getAllByPersonIndexedByType(Yii::app()->user->userModel); $userScoresByType = GameScore::getAllByPersonIndexedByType(Yii::app()->user->userModel); $badgeRulesClassNames = GameBadgeRules::getBadgeRulesData(); foreach ($badgeRulesClassNames as $badgeRulesClassName) { $newBadge = false; $gradeChange = false; $badgeGrade = $badgeRulesClassName::badgeGradeUserShouldHaveByPointsAndScores($userPointsByType, $userScoresByType); if ($badgeGrade > 0) { if (isset($userBadgesByType[$badgeRulesClassName::getType()])) { $gameBadge = $userBadgesByType[$badgeRulesClassName::getType()]; if ($badgeGrade > $gameBadge->grade) { $gameBadge->grade = $badgeGrade; $saved = $gameBadge->save(); if (!$saved) { throw new NotSupportedException(); } $gradeChange = true; } } else { $gameBadge = new GameBadge(); $gameBadge->type = $badgeRulesClassName::getType(); $gameBadge->person = Yii::app()->user->userModel; $gameBadge->grade = 1; $saved = $gameBadge->save(); if (!$saved) { throw new NotSupportedException(); } $newBadge = true; } if ($gradeChange || $newBadge) { if ($gradeChange) { $gradeChangeOrNewBadge = 'GradeChange'; } else { $gradeChangeOrNewBadge = 'NewBadge'; } GameBadge::processBonusPoints($gameBadge, Yii::app()->user->userModel, $gradeChangeOrNewBadge); if ($this->modalNotificationsEnabled) { $gameNotification = new GameNotification(); $gameNotification->user = Yii::app()->user->userModel; if ($newBadge) { $gameNotification->setNewBadgeByType($gameBadge->type); } elseif ($gradeChange) { $gameNotification->setBadgeGradeChangeByTypeAndNewGrade($gameBadge->type, $gameBadge->grade); } $saved = $gameNotification->save(); if (!$saved) { throw new FailedToSaveModelException(); } } } } } }
/** * @depends testDoesUserExceedPointsByLevelType * @expectedException NotSupportedException */ public function testDoesUserExceedPointsByInvalidLevelType() { Yii::app()->user->userModel = User::getByUsername('steven'); $result = GamePoint::doesUserExceedPointsByLevelType(Yii::app()->user->userModel, 5, 'SomethingInvalid'); }
/** * @param User $user * @param $levelType * @return array */ public static function getStatisticsDataForAGivenLevelType(User $user, $levelType) { assert('is_string($levelType) && $levelType != null'); $rulesClassName = $levelType . 'GameLevelRules'; $currentGameLevel = GameLevel::resolveByTypeAndPerson($levelType, $user); $currentPointsData = GamePoint::getSummationPointsDataByLevelTypeAndUser($user, $levelType); if ($currentPointsData != null) { $currentPoints = $currentPointsData['sum']; } else { $currentPoints = 0; } //If the user has not reached level one, the model has not been saved yet if ($currentGameLevel->id < 0) { $nextLevel = 1; $trueCurrentGameLevel = 0; $className = $levelType . 'GameLevelRules'; $nextLevelPointValue = $className::getMinimumPointsForLevel(1); $currentLevelMinimumPointValue = 0; } else { $nextLevelPointValue = GameLevelUtil::getNextLevelPointValueByTypeAndCurrentLevel($levelType, $currentGameLevel); $nextLevel = GameLevelUtil::getNextLevelByTypeAndCurrentLevel($levelType, $currentGameLevel); $currentLevelMinimumPointValue = $rulesClassName::getMinimumPointsForLevel(intval($currentGameLevel->value)); $trueCurrentGameLevel = $currentGameLevel->value; } if ($nextLevel !== false) { $pointsCollectedTowardsNextLevel = $currentPoints - $currentLevelMinimumPointValue; if ($pointsCollectedTowardsNextLevel == 0) { $nextLevelPercentageComplete = 0; } else { $nextLevelPercentageComplete = $pointsCollectedTowardsNextLevel / ($nextLevelPointValue - $currentLevelMinimumPointValue) * 100; } } else { $nextLevelPercentageComplete = null; } $rankingData = array('level' => (int) $trueCurrentGameLevel, 'points' => (int) $currentPoints, 'nextLevelPercentageComplete' => round($nextLevelPercentageComplete), 'levelTypeLabel' => $rulesClassName::getDisplayLabel()); return $rankingData; }
/** * Tests the global configuration to enable/disable modalNotifications */ public function testGamificationModalNotificationsGlobalConfiguration() { $super = User::getByUsername('super'); Yii::app()->user->userModel = $super; $scot = UserTestHelper::createBasicUser('Scot'); Yii::app()->user->userModel = $scot; $this->assertEquals(0, count(GameNotification::getAllByUser($scot))); //test user at general level 0 where they do have enough points to move up (Game notification created) $gamePoint = new GamePoint(); $gamePoint->type = GamePoint::TYPE_USER_ADOPTION; $gamePoint->person = $scot; $gamePoint->value = 300; $this->assertTrue($gamePoint->save()); Yii::app()->gameHelper->resolveLevelChange(); $this->assertEquals(1, count(GameNotification::getAllByUser($scot))); $gamePoint = GamePoint::resolveToGetByTypeAndPerson(GamePoint::TYPE_USER_ADOPTION, $scot); $this->assertEquals(300, $gamePoint->value); $gameLevel = GameLevel::resolveByTypeAndPerson(GameLevel::TYPE_GENERAL, $scot); $this->assertTrue($gameLevel->id > 0); $this->assertEquals(1, $gameLevel->value); //test user at general level 1 where they do have enough points to move up (No game notification created) ZurmoConfigurationUtil::setByModuleName('ZurmoModule', 'gamificationModalNotificationsEnabled', false); $gamePoint = new GamePoint(); $gamePoint->type = GamePoint::TYPE_USER_ADOPTION; $gamePoint->person = $scot; $gamePoint->value = 300; $this->assertTrue($gamePoint->save()); Yii::app()->gameHelper->resolveLevelChange(); $this->assertEquals(1, count(GameNotification::getAllByUser($scot))); $this->assertEquals(300, $gamePoint->value); $this->assertTrue($gameLevel->id > 0); $this->assertEquals(2, $gameLevel->value); //test user at general level 2 where they do have enough points to move up (Game notification created) ZurmoConfigurationUtil::setByModuleName('ZurmoModule', 'gamificationModalNotificationsEnabled', true); $gamePoint = new GamePoint(); $gamePoint->type = GamePoint::TYPE_USER_ADOPTION; $gamePoint->person = $scot; $gamePoint->value = 500; $this->assertTrue($gamePoint->save()); Yii::app()->gameHelper->resolveLevelChange(); $this->assertEquals(2, count(GameNotification::getAllByUser($scot))); $this->assertEquals(500, $gamePoint->value); $this->assertTrue($gameLevel->id > 0); $this->assertEquals(3, $gameLevel->value); }
/** * @depends testResolveByTypeAndPerson */ public function testProcessBonusPointsOnLevelChange() { Yii::app()->user->userModel = User::getByUsername('steven'); $gamePoint = GamePoint::resolveToGetByTypeAndPerson(GamePoint::TYPE_USER_ADOPTION, Yii::app()->user->userModel); $this->assertEquals(GamePoint::TYPE_USER_ADOPTION, $gamePoint->type); $this->assertEquals(0, $gamePoint->value); //Testing a level that does not give bonus points. $gameLevel = new GameLevel(); $gameLevel->person = Yii::app()->user->userModel; $gameLevel->type = GameLevel::TYPE_SALES; $gameLevel->value = 1; $this->assertTrue($gameLevel->save()); GameLevel::processBonusPointsOnLevelChange($gameLevel, Yii::app()->user->userModel); //Test that bonus points were actually received. $gamePoint = GamePoint::resolveToGetByTypeAndPerson(GamePoint::TYPE_USER_ADOPTION, Yii::app()->user->userModel); $this->assertEquals(GamePoint::TYPE_USER_ADOPTION, $gamePoint->type); $this->assertEquals(100, $gamePoint->value); //Now get the GameLevel again, and make sure it works for level 2 $gameLevel = GameLevel::resolveByTypeAndPerson(GameLevel::TYPE_SALES, Yii::app()->user->userModel); $gameLevel->person = Yii::app()->user->userModel; $gameLevel->type = GameLevel::TYPE_SALES; $gameLevel->value = 2; $this->assertTrue($gameLevel->save()); GameLevel::processBonusPointsOnLevelChange($gameLevel, Yii::app()->user->userModel); //Test that bonus points were actually received. $gamePoint = GamePoint::resolveToGetByTypeAndPerson(GamePoint::TYPE_USER_ADOPTION, Yii::app()->user->userModel); $this->assertEquals(GamePoint::TYPE_USER_ADOPTION, $gamePoint->type); $this->assertEquals(210, $gamePoint->value); }
/** * Given a user and a gameBadge, process the bonus points, if applicable for the badge. This will also * process grade change points for the given badge. * @param GameBadge $gameBadge * @param User $user * @param $gradeChangeOrNewBadge * @throws NotSupportedException */ public static function processBonusPoints(GameBadge $gameBadge, User $user, $gradeChangeOrNewBadge) { assert('$gameBadge->id > 0'); assert('$gradeChangeOrNewBadge == "GradeChange" || $gradeChangeOrNewBadge == "NewBadge"'); $gameBadgeRulesClassName = $gameBadge->type . 'GameBadgeRules'; $gamePoint = null; if ($gradeChangeOrNewBadge == 'NewBadge' && $gameBadgeRulesClassName::hasBonusPointsOnCreation()) { $type = $gameBadgeRulesClassName::getNewBonusPointType(); $gamePoint = GamePoint::resolveToGetByTypeAndPerson($type, $user); $value = $gameBadgeRulesClassName::getNewBonusPointValue(); } elseif ($gradeChangeOrNewBadge == 'GradeChange' && $gameBadgeRulesClassName::hasBonusPointsOnGradeChange()) { $type = $gameBadgeRulesClassName::getGradeBonusPointType(); $gamePoint = GamePoint::resolveToGetByTypeAndPerson($type, $user); $value = $gameBadgeRulesClassName::getGradeBonusPointValue($gameBadge->grade); } if ($gamePoint != null && $value > 0) { $gamePoint->addValue($value, false); $saved = $gamePoint->save(); GamePointTransaction::addTransactionResolvedForOptimization($gamePoint, $value); if (!$saved) { throw new NotSupportedException(); } } }
/** * Given a user and a gameLevel, process the bonus points, if applicable for the badge. This will also * process grade change points for the given badge. * @param GameBadge $gameBadge * @param User $user */ public static function processBonusPointsOnLevelChange(GameLevel $gameLevel, User $user) { assert('$gameLevel->id > 0'); $gameLevelRulesClassName = $gameLevel->type . 'GameLevelRules'; $gamePoint = null; if ($gameLevelRulesClassName::hasBonusPointsOnLevelChange()) { $type = $gameLevelRulesClassName::getLevelBonusPointType(); $gamePoint = GamePoint::resolveToGetByTypeAndPerson($type, $user); $value = $gameLevelRulesClassName::getLevelBonusPointValue($gameLevel->value); } if ($gamePoint != null && $value > 0) { $gamePoint->addValue($value, false); $saved = $gamePoint->save(); GamePointTransaction::addTransactionResolvedForOptimization($gamePoint, $value); if (!$saved) { throw new NotSupportedException(); } } }
/** * @depends testGetAllByPersonIndexedByType */ public function testProcessBonusPoints() { Yii::app()->user->userModel = User::getByUsername('steven'); $gamePoint = GamePoint::resolveToGetByTypeAndPerson(GamePoint::TYPE_USER_ADOPTION, Yii::app()->user->userModel); $this->assertEquals(GamePoint::TYPE_USER_ADOPTION, $gamePoint->type); $this->assertEquals(0, $gamePoint->value); //Testing a badge that does not give bonus points. $gameBadge = new GameBadge(); $gameBadge->person = Yii::app()->user->userModel; $gameBadge->type = 'CreateLead'; $gameBadge->grade = 1; $this->assertTrue($gameBadge->save()); GameBadge::processBonusPoints($gameBadge, Yii::app()->user->userModel, 'NewBadge'); //Test that bonus points were actually received. $gamePoint = GamePoint::resolveToGetByTypeAndPerson(GamePoint::TYPE_USER_ADOPTION, Yii::app()->user->userModel); $this->assertEquals(GamePoint::TYPE_USER_ADOPTION, $gamePoint->type); $this->assertEquals(50, $gamePoint->value); }
/** * @see GamificationUtil::logAndNotifyOnDuplicateGameModel($logContent for an explanation of why you would * use this method. */ public function actionRepairGamification() { $duplicateModelsData = array(); //Check GameCoin for duplication person models $gameCoinDuplicateData = GamificationUtil::findGameTableRowsThatAreDuplicatedByPersonKey(GameCoin::getTableName()); $duplicateModelsData['GameCoin'] = $gameCoinDuplicateData; //Check GameCollection, GameLevel, GamePoint, and GameScore for duplicate type/person models $gameCollectionDuplicateData = GamificationUtil::findGameTableRowsThatAreDuplicatedByTypePersonKey(GameCollection::getTableName()); $duplicateModelsData['GameCollection'] = $gameCollectionDuplicateData; $gameLevelDuplicateData = GamificationUtil::findGameTableRowsThatAreDuplicatedByTypePersonKey(GameLevel::getTableName()); $duplicateModelsData['GameLevel'] = $gameLevelDuplicateData; $gamePointDuplicateData = GamificationUtil::findGameTableRowsThatAreDuplicatedByTypePersonKey(GamePoint::getTableName()); $duplicateModelsData['GamePoint'] = $gamePointDuplicateData; $gameScoreDuplicateData = GamificationUtil::findGameTableRowsThatAreDuplicatedByTypePersonKey(GameScore::getTableName()); $duplicateModelsData['GameScore'] = $gameScoreDuplicateData; foreach ($duplicateModelsData as $modelClassName => $duplicatesData) { if (empty($duplicatesData)) { echo 'No duplicates found for ' . $modelClassName . "<BR>"; } else { echo 'Duplicates discovered for ' . $modelClassName . "<BR>"; foreach ($duplicatesData as $typePersonKeyDuplicateData) { $searchAttributeData = array(); if ($modelClassName == 'GameCoin') { $searchAttributeData['clauses'] = array(1 => array('attributeName' => 'person', 'relatedAttributeName' => 'id', 'operatorType' => 'equals', 'value' => $typePersonKeyDuplicateData['person_item_id'])); $searchAttributeData['structure'] = '1'; } else { $searchAttributeData['clauses'] = array(1 => array('attributeName' => 'type', 'operatorType' => 'equals', 'value' => $typePersonKeyDuplicateData['type']), 2 => array('attributeName' => 'person', 'relatedAttributeName' => 'id', 'operatorType' => 'equals', 'value' => $typePersonKeyDuplicateData['person_item_id'])); $searchAttributeData['structure'] = '1 and 2'; } $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter($modelClassName); $where = RedBeanModelDataProvider::makeWhere($modelClassName, $searchAttributeData, $joinTablesAdapter); $models = $modelClassName::getSubset($joinTablesAdapter, null, null, $where, null); if ($modelClassName == 'GameCoin') { echo $modelClassName . ' --- Quantity of duplicates: ' . count($models) . ' --- for person_item_id: ' . $typePersonKeyDuplicateData['person_item_id'] . "<BR>"; } else { echo $modelClassName . ' --- Quantity of duplicates: ' . count($models) . ' --- for person_item_id: ' . $typePersonKeyDuplicateData['person_item_id'] . ' with type: ' . $typePersonKeyDuplicateData['type'] . "<BR>"; } $messageContent = null; GamificationUtil::removeDuplicatesByModels($models, $messageContent); echo $messageContent; } } } echo "<BR>" . 'Repair complete.' . "<BR>"; }