/** * The login of a user is a scored game event. This method processes this. * @param CEvent $event */ public function handleScoreLogin($event) { if (Yii::app()->gamificationObserver->enabled) { $scoreType = 'LoginUser'; $category = GamificationRules::SCORE_CATEGORY_LOGIN_USER; $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreType, Yii::app()->user->userModel); $gameScore->addValue(); self::resolveSaveGameScoreAndAddPointsByCategory($gameScore, $category); if (Yii::app()->timeZoneHelper->isCurrentUsersTimeZoneConfirmed()) { $hour = date('G'); if ($hour >= 22 || $hour < 4) { $scoreType = 'NightOwl'; $category = GamificationRules::SCORE_CATEGORY_LOGIN_USER; $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreType, Yii::app()->user->userModel); $gameScore->addValue(); self::resolveSaveGameScoreAndAddPointsByCategory($gameScore, $category); } elseif ($hour >= 4 && $hour < 8) { $scoreType = 'EarlyBird'; $category = GamificationRules::SCORE_CATEGORY_LOGIN_USER; $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreType, Yii::app()->user->userModel); $gameScore->addValue(); self::resolveSaveGameScoreAndAddPointsByCategory($gameScore, $category); } } } }
/** * @depends testResolveToGetByTypeAndPerson */ public function testGetAllByPersonIndexedByType() { Yii::app()->user->userModel = User::getByUsername('steven'); $gameScore = new GameScore(); $gameScore->person = Yii::app()->user->userModel; $gameScore->type = 'SomeTypeX'; $gameScore->value = 10; $this->assertTrue($gameScore->save()); $gameScores = GameScore::getAllByPersonIndexedByType(Yii::app()->user->userModel); $this->assertEquals(2, count($gameScores)); $this->assertTrue(isset($gameScores['SomeType'])); $this->assertTrue(isset($gameScores['SomeTypeX'])); }
/** * (non-PHPdoc) * @see GamificationRules::scoreOnSaveModel() */ public function scoreOnSaveModel(CEvent $event) { parent::scoreOnSaveModel($event); $scoreType = static::SCORE_TYPE_WIN_AGREEMENT; $category = static::SCORE_CATEGORY_WIN_AGREEMENT; $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreType, Yii::app()->user->userModel); $gameScore->addValue(); $saved = $gameScore->save(); if (!$saved) { throw new FailedToSaveModelException(); } GamePointUtil::addPointsByPointData(Yii::app()->user->userModel, static::getPointTypeAndValueDataByCategory($category)); }
/** * (non-PHPdoc) * @see GamificationRules::scoreOnSaveModel() */ public function scoreOnSaveModel(CEvent $event) { parent::scoreOnSaveModel($event); if (array_key_exists('value', $event->sender->stage->originalAttributeValues) && $event->sender->stage->value == Opportunity::getStageClosedWonValue()) { $scoreType = static::SCORE_TYPE_WIN_OPPORTUNITY; $category = static::SCORE_CATEGORY_WIN_OPPORTUNITY; $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreType, Yii::app()->user->userModel); $gameScore->addValue(); $saved = $gameScore->save(); if (!$saved) { throw new FailedToSaveModelException(); } GamePointUtil::addPointsByPointData(Yii::app()->user->userModel, static::getPointTypeAndValueDataByCategory($category)); } }
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'); } }
/** * @param CEvent $event */ public function scoreCompletedOnTime(CEvent $event) { $model = $event->sender; assert('$model instanceof Item'); if (!$model->getIsNewModel() && array_key_exists('completed', $model->originalAttributeValues) && $model->completed == true && $model->dueDateTime != null) { $completedTimestamp = DateTimeUtil::convertDbFormatDateTimeToTimestamp($model->completedDateTime); $dueTimestamp = DateTimeUtil::convertDbFormatDateTimeToTimestamp($model->dueDateTime); if ($completedTimestamp <= $dueTimestamp) { $scoreType = static::SCORE_TYPE_COMPLETED_TASK_ON_TIME; $category = static::SCORE_CATEGORY_TIME_SENSITIVE_ACTION; $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreType, Yii::app()->user->userModel); $gameScore->addValue(); $saved = $gameScore->save(); if (!$saved) { throw new FailedToSaveModelException(); } GamePointUtil::addPointsByPointData(Yii::app()->user->userModel, static::getPointTypeAndValueDataByCategory($category)); } } }
protected function scoreOnSaveWhereStateIsLead(CEvent $event) { $model = $event->sender; assert('$model instanceof Item'); if ($model->getIsNewModel()) { $scoreType = static::SCORE_TYPE_CREATE_LEAD; $category = static::SCORE_CATEGORY_CREATE_LEAD; $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreType, Yii::app()->user->userModel); } else { $scoreType = static::SCORE_TYPE_UPDATE_LEAD; $category = static::SCORE_CATEGORY_UPDATE_LEAD; $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreType, Yii::app()->user->userModel); } $gameScore->addValue(); $saved = $gameScore->save(); if (!$saved) { throw new FailedToSaveModelException(); } GamePointUtil::addPointsByPointData(Yii::app()->user->userModel, static::getPointTypeAndValueDataByCategory($category)); }
/** * Override to handle 'draft' email template creation * @param CEvent $event * @throws FailedToSaveModelException */ public function scoreOnSaveModel(CEvent $event) { $model = $event->sender; assert('$model instanceof Item'); if (Yii::app()->gameHelper->isScoringModelsOnSaveMuted()) { return; } if ($model->getIsNewModel()) { if (!$model->isDraft) { $scoreType = static::resolveCreateScoreTypeByModel($model); $category = static::SCORE_CATEGORY_CREATE_MODEL; $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreType, Yii::app()->user->userModel); } else { return; } } elseif (!$this->scoreOnUpdate) { return; } else { if (array_key_exists('isDraft', $event->sender->originalAttributeValues) && $event->sender->isDraft) { //Considered a new 'email template' $scoreType = static::resolveCreateScoreTypeByModel($model); $category = static::SCORE_CATEGORY_CREATE_MODEL; $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreType, Yii::app()->user->userModel); } else { $scoreType = static::resolveUpdateScoreTypeByModel($model); $category = static::SCORE_CATEGORY_UPDATE_MODEL; $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreType, Yii::app()->user->userModel); } } $gameScore->addValue(); $saved = $gameScore->save(); if (!$saved) { throw new FailedToSaveModelException(); } GamePointUtil::addPointsByPointData(Yii::app()->user->userModel, static::getPointTypeAndValueDataByCategory($category)); }
/** * 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(); } } } } } }
/** * (non-PHPdoc) * @see GamificationRules::scoreOnSaveModel() */ public function scoreOnSaveModel(CEvent $event) { parent::scoreOnSaveModel($event); //Is the Mission being taken by a user, when previously available. if (array_key_exists('status', $event->sender->originalAttributeValues) && $event->sender->originalAttributeValues['status'] == Mission::STATUS_AVAILABLE && $event->sender->status == Mission::STATUS_TAKEN) { $scoreType = static::SCORE_TYPE_TAKE_MISSION; $category = static::SCORE_CATEGORY_TAKE_MISSION; $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreType, $event->sender->takenByUser); $gameScore->addValue(); $saved = $gameScore->save(); if (!$saved) { throw new FailedToSaveModelException(); } GamePointUtil::addPointsByPointData(Yii::app()->user->userModel, static::getPointTypeAndValueDataByCategory($category)); } elseif (array_key_exists('status', $event->sender->originalAttributeValues) && $event->sender->originalAttributeValues['status'] == Mission::STATUS_TAKEN && $event->sender->status == Mission::STATUS_COMPLETED) { $scoreType = static::SCORE_TYPE_COMPLETE_MISSION; $category = static::SCORE_CATEGORY_COMPLETE_MISSION; $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreType, $event->sender->takenByUser); $gameScore->addValue(); $saved = $gameScore->save(); if (!$saved) { throw new FailedToSaveModelException(); } GamePointUtil::addPointsByPointData(Yii::app()->user->userModel, static::getPointTypeAndValueDataByCategory($category)); } elseif (array_key_exists('status', $event->sender->originalAttributeValues) && $event->sender->originalAttributeValues['status'] == Mission::STATUS_COMPLETED && $event->sender->status == Mission::STATUS_ACCEPTED) { $scoreType = static::SCORE_TYPE_ACCEPTED_MISSION; $category = static::SCORE_CATEGORY_ACCEPTED_MISSION; $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreType, $event->sender->takenByUser); $gameScore->addValue(); $saved = $gameScore->save(); if (!$saved) { throw new FailedToSaveModelException(); } GamePointUtil::addPointsByPointData(Yii::app()->user->userModel, static::getPointTypeAndValueDataByCategory($category)); } }
public function testScoreOnSaveModelForMissionMovingThroughStatusChanges() { $super = User::getByUsername('super'); // asserting simpleUser score before taken/completed/accepted of mission $simpleUser = UserTestHelper::createBasicUser('simpleUser'); $gamescore = GameScore::getAllByPersonIndexedByType($simpleUser); $this->assertEquals(0, count($gamescore)); $scoreTypeMissionTaken = MissionGamificationRules::SCORE_TYPE_TAKE_MISSION; $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreTypeMissionTaken, $simpleUser); $this->assertEquals(0, count($gamescore)); $scoreTypeMissionComplete = MissionGamificationRules::SCORE_TYPE_COMPLETE_MISSION; $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreTypeMissionComplete, $simpleUser); $this->assertEquals(0, count($gamescore)); $scoreTypeMissionAccepted = MissionGamificationRules::SCORE_TYPE_ACCEPTED_MISSION; $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreTypeMissionAccepted, $simpleUser); $this->assertEquals(0, count($gamescore)); $missions = Mission::getAll(); $this->assertEquals(0, count($missions)); $mission = new Mission(); $mission->owner = $super; $mission->takenByUser = $simpleUser; $mission->description = 'Test description'; $mission->reward = 'Test reward'; $mission->status = Mission::STATUS_AVAILABLE; $mission->save(); //Confirm mission saved. $missions = Mission::getAll(); $this->assertEquals(1, count($missions)); $gamescore = GameScore::getAllByPersonIndexedByType($super); $this->assertEquals(1, count($gamescore)); //Changing Status to Taken $mission = $missions[0]; $mission->status = Mission::STATUS_TAKEN; $this->assertTrue($mission->save()); $missions = Mission::getAll(); $this->assertEquals(1, count($missions)); $scoreTypeMissionTaken = MissionGamificationRules::SCORE_TYPE_TAKE_MISSION; $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreTypeMissionTaken, $simpleUser); $this->assertEquals(1, count($gamescore)); $gamescoreOfUser = GameScore::getAllByPersonIndexedByType($simpleUser); $this->assertEquals(1, count($gamescoreOfUser)); //Changing Status to Completed $mission = $missions[0]; $mission->status = Mission::STATUS_COMPLETED; $mission->save(); $missions = Mission::getAll(); $this->assertEquals(1, count($missions)); $scoreTypeMissionComplete = MissionGamificationRules::SCORE_TYPE_COMPLETE_MISSION; $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreTypeMissionComplete, $simpleUser); $this->assertEquals(1, count($gamescore)); $gamescoreOfUser = GameScore::getAllByPersonIndexedByType($simpleUser); $this->assertEquals(2, count($gamescoreOfUser)); //Changing Status to Accepted $mission = $missions[0]; $mission->status = Mission::STATUS_ACCEPTED; $mission->save(); $missions = Mission::getAll(); $this->assertEquals(1, count($missions)); $scoreTypeMissionAccepted = MissionGamificationRules::SCORE_TYPE_ACCEPTED_MISSION; $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreTypeMissionAccepted, $simpleUser); $this->assertEquals(1, count($gamescore)); $gamescoreOfUser = GameScore::getAllByPersonIndexedByType($simpleUser); $this->assertEquals(3, count($gamescoreOfUser)); }
/** * @depends testTriggerMassEditEvent */ public function testTriggerImportEvent() { $super = User::getByUsername('super'); Yii::app()->user->userModel = $super; $models = GameScore::getAllByPersonIndexedByType(Yii::app()->user->userModel); $this->assertEquals(2, count($models)); Yii::app()->gameHelper->triggerImportEvent('Account'); $models = GameScore::getAllByPersonIndexedByType(Yii::app()->user->userModel); $this->assertEquals(3, count($models)); $this->assertEquals('ImportAccount', $models['ImportAccount']->type); }
/** * @param string $modelClassName */ public static function scoreOnImportModels($modelClassName) { assert('is_string($modelClassName)'); $scoreType = 'Import' . $modelClassName; $category = static::SCORE_CATEGORY_IMPORT; $gameScore = GameScore::resolveToGetByTypeAndPerson($scoreType, Yii::app()->user->userModel); $gameScore->addValue(); $saved = $gameScore->save(); if (!$saved) { throw new FailedToSaveModelException(); } GamePointUtil::addPointsByPointData(Yii::app()->user->userModel, static::getPointTypeAndValueDataByCategory($category)); }
/** * @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>"; }
/** * @depends testGetPointTypeAndValueDataByCategory */ public function testScoreOnSaveModelIndirectly() { $super = User::getByUsername('super'); Yii::app()->user->userModel = $super; $models = GameScore::getAllByPersonIndexedByType(Yii::app()->user->userModel); $this->assertEquals(3, count($models)); $account = new Account(); $account->owner = $super; $account->name = 'someName'; $this->assertTrue($account->save()); $models = GameScore::getAllByPersonIndexedByType(Yii::app()->user->userModel); $this->assertEquals(4, count($models)); $this->assertEquals('CreateAccount', $models['CreateAccount']->type); }