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'); } }
/** * Process any points that have been added to @see $pointTypesAndValuesByUserIdToAdd throughout the page * request. */ public function processDeferredPoints() { if (!$this->enabled) { return; } foreach (self::$pointTypesAndValuesByUserIdToAdd as $userId => $typeAndValues) { if ($typeAndValues != null) { foreach ($typeAndValues as $type => $value) { $gamePoint = GamePoint::resolveToGetByTypeAndPerson($type, User::getById($userId)); $gamePoint->addValue($value); $saved = $gamePoint->save(); if (!$saved) { throw new NotSupportedException(); } } } } $this->resetDeferredPointTypesAndValuesByUserIdToAdd(); }
/** * @depends testCreateGamePointSettingValueDirectly */ public function testResolveToGetByTypeAndPerson() { Yii::app()->user->userModel = User::getByUsername('steven'); $gamePoint = GamePoint::resolveToGetByTypeAndPerson('SomeType', Yii::app()->user->userModel); $this->assertEquals('SomeType', $gamePoint->type); $this->assertEquals(60, $gamePoint->value); $this->assertEquals(Yii::app()->user->userModel, $gamePoint->person); $gamePoint = GamePoint::resolveToGetByTypeAndPerson('SomeType2', Yii::app()->user->userModel); $this->assertTrue($gamePoint->id < 0); }
/** * 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); }